Skip to content

Commit

Permalink
first try to add a demo to main page
Browse files Browse the repository at this point in the history
  • Loading branch information
hmbanan666 committed Jun 9, 2024
1 parent c4b836f commit d5df937
Show file tree
Hide file tree
Showing 199 changed files with 9,241 additions and 23 deletions.
Binary file removed apps/client/public/tools/axe1_64_2.png
Binary file not shown.
Binary file added src/lib/assets/game/audio/chop-1.wav
Binary file not shown.
Binary file added src/lib/assets/game/audio/fire-1.wav
Binary file not shown.
Binary file added src/lib/assets/game/audio/forest-1.mp3
Binary file not shown.
Binary file added src/lib/assets/game/audio/hand-punch-1.wav
Binary file not shown.
Binary file not shown.
Binary file added src/lib/assets/game/audio/mine-1.wav
Binary file not shown.
Binary file added src/lib/assets/game/audio/wagon-1.wav
Binary file not shown.
Binary file added src/lib/assets/game/audio/yeah-1.wav
Binary file not shown.
Binary file added src/lib/assets/game/images/animation/campfire/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/lib/assets/game/images/animation/campfire/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/lib/assets/game/images/animation/campfire/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/lib/assets/game/images/animation/campfire/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/lib/assets/game/images/animation/campfire/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/lib/assets/game/images/background/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/lib/assets/game/images/background/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/lib/assets/game/images/background/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/lib/assets/game/images/background/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/lib/assets/game/images/background/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/lib/assets/game/images/buildings/camp-fire-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/lib/assets/game/images/buildings/camp-fire-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/lib/assets/game/images/buildings/store.png
Binary file added src/lib/assets/game/images/buildings/warehouse-1.png
Binary file added src/lib/assets/game/images/icons/coin-1.png
Binary file added src/lib/assets/game/images/icons/fuel-1.png
Binary file added src/lib/assets/game/images/icons/items/coin.png
Binary file added src/lib/assets/game/images/icons/items/stone.png
Binary file added src/lib/assets/game/images/icons/items/wood.png
Binary file added src/lib/assets/game/images/icons/speed-1.png
Binary file added src/lib/assets/game/images/objects/flag-1.png
Binary file added src/lib/assets/game/images/objects/flag-2.png
Binary file added src/lib/assets/game/images/objects/stone-1.png
Binary file added src/lib/assets/game/images/tools/axe1_64.png
Binary file added src/lib/assets/game/images/tools/pickaxe1_64.png
Binary file added src/lib/assets/game/images/wagon/chest-1.png
Binary file added src/lib/assets/game/images/wagon/clouds/1.png
Binary file added src/lib/assets/game/images/wagon/clouds/2.png
Binary file added src/lib/assets/game/images/wagon/clouds/3.png
Binary file added src/lib/assets/game/images/wagon/clouds/4.png
Binary file added src/lib/assets/game/images/wagon/engine-1.png
Binary file added src/lib/assets/game/images/wagon/fuel-1.png
Binary file added src/lib/assets/game/images/wagon/fuel-2.png
Binary file added src/lib/assets/game/images/wagon/fuel-container.png
Binary file added src/lib/assets/game/images/wagon/wagon-1.png
Binary file added src/lib/assets/game/images/wagon/wagon-2.png
Binary file added src/lib/assets/game/images/wagon/wheel-1.png
Binary file added src/lib/assets/game/images/water/1.png
Binary file added src/lib/assets/game/images/water/2.png
Binary file added src/lib/assets/game/images/water/3.png
Binary file added src/lib/assets/game/images/water/4.png
14 changes: 14 additions & 0 deletions src/lib/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function getDatePlusMinutes(minutes: number) {
const milliseconds = minutes * 60 * 1000
return new Date(new Date().getTime() + milliseconds)
}

export function getDateMinusMinutes(minutes: number) {
const milliseconds = minutes * 60 * 1000
return new Date(new Date().getTime() - milliseconds)
}

export function getDatePlusSeconds(seconds: number) {
const milliseconds = seconds * 1000
return new Date(new Date().getTime() + milliseconds)
}
22 changes: 22 additions & 0 deletions src/lib/game/actions/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { IGameAction, IGameActionResponse, } from "$lib/game/types"
import type { Player } from "../objects/units"

interface IActionOptions {
command: IGameAction["command"]
commandDescription: IGameAction["commandDescription"]
}

export class Action implements IGameAction {
public command: string
public commandDescription: string

public live!: (
player: Player,
params: string[],
) => Promise<IGameActionResponse>

constructor({ command, commandDescription }: IActionOptions) {
this.command = command
this.commandDescription = commandDescription
}
}
43 changes: 43 additions & 0 deletions src/lib/game/actions/donateWoodToVillageAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Village } from "../chunks"
import type { Warehouse } from "../objects/buildings/warehouse"
import type { Player } from "../objects/units"
import type { GameScene } from "../scenes/gameScene"
import { Action } from "./action"
import { ANSWER } from "$lib/game/services/actionService";

interface IDonateWoodToVillageActionOptions {
scene: GameScene
}

export class DonateWoodToVillageAction extends Action {
private scene: GameScene

constructor({ scene }: IDonateWoodToVillageActionOptions) {
super({ command: "donate", commandDescription: "!donate [quantity]" })

this.scene = scene
this.live = this.initLive
}

async initLive(player: Player, params: string[]) {
const amount = this.scene.actionService.getAmountFromChatCommand(params[0])
if (!amount) {
return ANSWER.WRONG_AMOUNT_ERROR
}

let warehouse: Warehouse | undefined
if (this.scene.chunkNow instanceof Village) {
warehouse = this.scene.chunkNow.getWarehouse()
}

const isSuccess = await player.inventory.reduceOrDestroyItem("WOOD", amount)
if (!isSuccess) {
return ANSWER.NOT_ENOUGH_WOOD_ERROR
}

await warehouse?.inventory.addOrCreateItem("WOOD", amount)
await player.addReputation(amount)

return ANSWER.DONATE_WOOD_OK
}
}
50 changes: 50 additions & 0 deletions src/lib/game/actions/plantTreeAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Village } from "../chunks"
import type { Player } from "../objects/units"
import type { GameScene } from "../scenes"
import { PlantNewTreeScript } from "../scripts/plantNewTreeScript"
import { Action } from "./action"
import { ANSWER } from "$lib/game/services/actionService";

interface IPlantTreeActionOptions {
scene: GameScene
}

export class PlantTreeAction extends Action {
private scene: GameScene

constructor({ scene }: IPlantTreeActionOptions) {
super({ command: "plant", commandDescription: "!plant" })

this.scene = scene
this.live = this.initLive
}

async initLive(player: Player) {
if (player.script && !player.script.isInterruptible) {
return ANSWER.BUSY_ERROR
}

if (this.scene.chunkNow instanceof Village) {
const target = this.scene.chunkNow.checkIfNeedToPlantTree()
if (!target) {
return ANSWER.NO_SPACE_AVAILABLE_ERROR
}

const plantNewTreeFunc = () => {
if (this.scene.chunkNow instanceof Village) {
this.scene.chunkNow.plantNewTree(target)
}
}

player.script = new PlantNewTreeScript({
object: player,
target,
plantNewTreeFunc,
})

return ANSWER.OK
}

return ANSWER.ERROR
}
}
34 changes: 34 additions & 0 deletions src/lib/game/actions/voteAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { Poll } from "../common"
import type { Player } from "../objects/units"
import { Action } from "./action"
import { ANSWER } from "$lib/game/services/actionService";

interface IVoteActionOptions {
poll: Poll
}

export class VoteAction extends Action {
private poll: Poll
private readonly id: string

constructor({ poll }: IVoteActionOptions) {
super({ command: "go", commandDescription: "!go" })

this.id = poll.generatePollId()

this.command = `go ${this.id}`
this.commandDescription = `!go ${this.id}`

this.poll = poll
this.live = this.initLive
}

async initLive(player: Player) {
const isSuccess = this.poll.vote(player)
if (!isSuccess) {
return ANSWER.ALREADY_VOTED_ERROR
}

return ANSWER.VOTED_OK
}
}
71 changes: 71 additions & 0 deletions src/lib/game/chunks/forest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { type IGameChunkTheme, type IGameForestChunk, } from "$lib/game/types"
import { Stone, Tree } from "../objects"
import type { GameScene } from "../scenes/gameScene.ts"
import { GameChunk } from "./gameChunk"
import { getRandomInRange } from "$lib/random";

interface IForestOptions {
center: IGameForestChunk["center"]
width: number
height: number
theme: IGameChunkTheme
scene: GameScene
}

export class Forest extends GameChunk implements IGameForestChunk {
constructor({ width, height, center, theme, scene }: IForestOptions) {
super({
title: "Grand Wood",
type: "FOREST",
width,
height,
center,
theme,
scene,
})

const treesToPrepare = Math.round(
(this.area.area.endX - this.area.area.startX) / 10,
)
this.initTrees(treesToPrepare)
this.initStones(3)
}

live() {
super.live()

for (const obj of this.objects) {
void obj.live()
}
}

initTrees(count: number) {
for (let i = 0; i < count; i++) {
const point = this.getRandomPoint()
const size = getRandomInRange(75, 90)
const tree = new Tree({
scene: this.scene,
x: point.x,
y: point.y,
size,
resource: 1,
health: 20,
variant: this.area.theme,
})
this.objects.push(tree)
}
}

initStones(count: number) {
for (let i = 0; i < count; i++) {
const point = this.getRandomPoint()
const stone = new Stone({
scene: this.scene,
x: point.x,
y: point.y,
resource: 1,
})
this.objects.push(stone)
}
}
}
120 changes: 120 additions & 0 deletions src/lib/game/chunks/gameChunk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { createId } from "@paralleldrive/cuid2"
import { type IGameChunk, type IGameChunkTheme, } from "$lib/game/types"
import { Area, type GameObject, Tree } from "../objects"
import type { GameScene } from "../scenes/gameScene.ts"
import { getRandomInRange } from "$lib/random";

interface IGameChunkOptions {
center: IGameChunk["center"]
title: IGameChunk["title"]
type: IGameChunk["type"]
theme: IGameChunkTheme
width: number
height: number
scene: GameScene
}

export class GameChunk implements IGameChunk {
public id: string
public title: string
public type: IGameChunk["type"]
public center!: IGameChunk["center"]
public area!: Area

public scene: GameScene
public objects: GameObject[] = []

constructor({
title,
type,
theme,
width,
height,
center,
scene,
}: IGameChunkOptions) {
this.id = createId()
this.center = center
this.title = title
this.type = type

this.scene = scene

this.initArea({ width, height, theme })
}

public live() {
}

private initArea({
width,
height,
theme,
}: {
width: number
height: number
theme: IGameChunkTheme
}) {
const halfWidth = Math.round(width / 2)
const halfHeight = Math.round(height / 2)

const area = {
startX: this.center.x - halfWidth,
endX: this.center.x + halfWidth,
startY: this.center.y - halfHeight,
endY: this.center.y + halfHeight,
}

this.area = new Area({ scene: this.scene, theme, area })
}

public getRandomPoint() {
return {
x: getRandomInRange(this.area.area.startX, this.area.area.endX),
y: getRandomInRange(this.area.area.startY, this.area.area.endY),
}
}

public getRandomOutPoint() {
const height = this.area.area.endY - this.area.area.startY
const offsetFromTop = Math.round(height / 4)

return {
x: this.area.area.endX,
y: getRandomInRange(
this.area.area.startY + offsetFromTop,
this.area.area.endY,
),
}
}

public checkIfPointIsInArea(point: { x: number; y: number }): boolean {
if (point.x >= this.area.area.startX && point.x <= this.area.area.endX) {
if (point.y >= this.area.area.startY && point.y <= this.area.area.endY) {
return true
}
}

return false
}

removeObject(object: GameObject) {
const index = this.objects.indexOf(object)
this.objects.splice(index, 1)
}

getAvailableTree(): Tree | undefined {
const trees = this.objects.filter(
(obj) =>
obj instanceof Tree &&
obj.state !== "DESTROYED" &&
!obj.isReserved &&
obj.isReadyToChop,
)
if (!trees || !trees.length) {
return undefined
}

return trees[Math.floor(Math.random() * trees.length)] as Tree
}
}
4 changes: 4 additions & 0 deletions src/lib/game/chunks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { Forest } from "./forest"
export { GameChunk } from "./gameChunk"
export { Village } from "./village"
export { LakeChunk } from "./lake"
Loading

0 comments on commit d5df937

Please sign in to comment.