From cca09cf9581bf2451afdf33a423343a03665d3ee Mon Sep 17 00:00:00 2001 From: Nick Kosarev Date: Sun, 16 Jun 2024 17:40:07 +0200 Subject: [PATCH] circling fixed --- .../{app-docker.yml => build-and-push.yml} | 2 +- .../{app-deploy.yml => deploy-prod.yml} | 2 +- .../workflows/{test.yml => test-and-lint.yml} | 19 +-- package.json | 2 +- .../game/actions/{action.ts => baseAction.ts} | 10 +- .../game/actions/donateWoodToVillageAction.ts | 14 +-- src/lib/game/actions/plantTreeAction.ts | 14 +-- src/lib/game/actions/voteAction.ts | 22 ++-- src/lib/game/{game.ts => baseGame.ts} | 68 ++-------- src/lib/game/chunks/forest.ts | 3 +- src/lib/game/chunks/gameChunk.ts | 14 ++- src/lib/game/chunks/index.ts | 4 - src/lib/game/chunks/lake.ts | 7 +- src/lib/game/chunks/village.ts | 11 +- src/lib/game/common/index.ts | 6 - src/lib/game/common/poll.ts | 3 +- src/lib/game/common/route.ts | 33 +++-- .../components/wagonEngineCloudsContainer.ts | 62 ++++----- src/lib/game/index.ts | 4 - src/lib/game/objects/area.ts | 25 ++-- .../objects/{gameObject.ts => baseObject.ts} | 105 ++++++++-------- .../{building.ts => baseBuilding.ts} | 26 ++-- src/lib/game/objects/buildings/campfire.ts | 9 +- .../objects/buildings/constructionArea.ts | 13 +- src/lib/game/objects/buildings/store.ts | 13 +- src/lib/game/objects/buildings/wagonStop.ts | 13 +- src/lib/game/objects/buildings/warehouse.ts | 17 ++- .../game/objects/{ => creatures}/rabbit.ts | 23 ++-- src/lib/game/objects/{ => creatures}/wolf.ts | 32 +++-- .../game/objects/{flag.ts => flagObject.ts} | 69 +++++----- src/lib/game/objects/index.ts | 9 -- src/lib/game/objects/lake.ts | 23 ++-- .../game/objects/{stone.ts => stoneObject.ts} | 32 +++-- .../game/objects/{tree.ts => treeObject.ts} | 76 ++++++----- src/lib/game/objects/units/courier.ts | 12 +- src/lib/game/objects/units/farmer.ts | 8 +- src/lib/game/objects/units/index.ts | 7 -- src/lib/game/objects/units/mechanic.ts | 10 +- src/lib/game/objects/units/player.ts | 13 +- src/lib/game/objects/units/raider.ts | 12 +- src/lib/game/objects/units/trader.ts | 8 +- .../objects/units/{unit.ts => unitObject.ts} | 26 ++-- src/lib/game/objects/wagon.ts | 117 +++++++++-------- src/lib/game/objects/water.ts | 9 +- .../game/quests/{quest.ts => baseQuest.ts} | 12 +- src/lib/game/quests/noTradingPostQuest.ts | 8 +- .../game/quests/treesAreRunningOutQuest.ts | 4 +- .../scenes/{gameScene.ts => baseScene.ts} | 67 +++++----- src/lib/game/scenes/movingScene.ts | 20 ++- .../{ => scenes}/services/actionService.ts | 37 +++--- .../{ => scenes}/services/eventService.ts | 35 +++--- .../game/{ => scenes}/services/pollService.ts | 16 ++- .../{ => scenes}/services/questService.ts | 21 ++-- .../{ => scenes}/services/routeService.ts | 18 +-- .../{ => scenes}/services/tradeService.ts | 21 ++-- .../{ => scenes}/services/wagonService.ts | 24 ++-- src/lib/game/scripts/buildScript.ts | 5 +- src/lib/game/scripts/chopTreeScript.ts | 5 +- src/lib/game/scripts/mineStoneScript.ts | 5 +- .../moveOffScreenAndSelfDestroyScript.ts | 5 +- src/lib/game/scripts/moveToTargetScript.ts | 5 +- .../scripts/moveToTradePostAndTradeScript.ts | 5 +- .../scripts/placeItemInWarehouseScript.ts | 5 +- src/lib/game/scripts/plantNewTreeScript.ts | 5 +- src/lib/game/scripts/script.ts | 5 +- src/lib/game/types.ts | 119 +++++++++++------- src/lib/game/utils/index.ts | 3 - src/lib/game/utils/webSocketManager.ts | 2 +- src/routes/(game)/play/+page.svelte | 4 +- .../(game)/play/GamePollProgress.svelte | 4 +- .../(game)/play/GameQuestTaskProgress.svelte | 6 +- src/routes/(game)/play/GameRouteInfo.svelte | 2 +- src/routes/(game)/play/GameRouteItem.svelte | 2 +- src/routes/(website)/+page.svelte | 4 +- 74 files changed, 697 insertions(+), 784 deletions(-) rename .github/workflows/{app-docker.yml => build-and-push.yml} (97%) rename .github/workflows/{app-deploy.yml => deploy-prod.yml} (97%) rename .github/workflows/{test.yml => test-and-lint.yml} (51%) rename src/lib/game/actions/{action.ts => baseAction.ts} (55%) rename src/lib/game/{game.ts => baseGame.ts} (76%) delete mode 100644 src/lib/game/chunks/index.ts delete mode 100644 src/lib/game/common/index.ts delete mode 100644 src/lib/game/index.ts rename src/lib/game/objects/{gameObject.ts => baseObject.ts} (54%) rename src/lib/game/objects/buildings/{building.ts => baseBuilding.ts} (56%) rename src/lib/game/objects/{ => creatures}/rabbit.ts (68%) rename src/lib/game/objects/{ => creatures}/wolf.ts (51%) rename src/lib/game/objects/{flag.ts => flagObject.ts} (55%) delete mode 100644 src/lib/game/objects/index.ts rename src/lib/game/objects/{stone.ts => stoneObject.ts} (68%) rename src/lib/game/objects/{tree.ts => treeObject.ts} (70%) delete mode 100644 src/lib/game/objects/units/index.ts rename src/lib/game/objects/units/{unit.ts => unitObject.ts} (90%) rename src/lib/game/quests/{quest.ts => baseQuest.ts} (80%) rename src/lib/game/scenes/{gameScene.ts => baseScene.ts} (91%) rename src/lib/game/{ => scenes}/services/actionService.ts (95%) rename src/lib/game/{ => scenes}/services/eventService.ts (89%) rename src/lib/game/{ => scenes}/services/pollService.ts (80%) rename src/lib/game/{ => scenes}/services/questService.ts (91%) rename src/lib/game/{ => scenes}/services/routeService.ts (94%) rename src/lib/game/{ => scenes}/services/tradeService.ts (92%) rename src/lib/game/{ => scenes}/services/wagonService.ts (86%) delete mode 100644 src/lib/game/utils/index.ts diff --git a/.github/workflows/app-docker.yml b/.github/workflows/build-and-push.yml similarity index 97% rename from .github/workflows/app-docker.yml rename to .github/workflows/build-and-push.yml index 7ac8aecc..f8d4f028 100644 --- a/.github/workflows/app-docker.yml +++ b/.github/workflows/build-and-push.yml @@ -1,4 +1,4 @@ -name: App – Build and Push Docker Image +name: Build and Push Docker Image on: push: diff --git a/.github/workflows/app-deploy.yml b/.github/workflows/deploy-prod.yml similarity index 97% rename from .github/workflows/app-deploy.yml rename to .github/workflows/deploy-prod.yml index c2391b14..e6f56f23 100644 --- a/.github/workflows/app-deploy.yml +++ b/.github/workflows/deploy-prod.yml @@ -1,4 +1,4 @@ -name: App – Deploy Docker Image on server +name: Deploy Docker Image on Prod on: workflow_dispatch: diff --git a/.github/workflows/test.yml b/.github/workflows/test-and-lint.yml similarity index 51% rename from .github/workflows/test.yml rename to .github/workflows/test-and-lint.yml index 55394cca..2b593918 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test-and-lint.yml @@ -1,9 +1,7 @@ -name: Test +name: Run Tests and Lint on: - push: - branches: - - main + push permissions: contents: write @@ -18,8 +16,11 @@ jobs: - run: bun install - - name: Lint - run: | - bun run lint - bun run lint:deps - bun run check + - name: ESLint + run: bun run lint + + - name: Find circular dependencies + run: bun run lint:deps + + - name: Check Svelte files + run: bun run check diff --git a/package.json b/package.json index fbe8633f..24482dba 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "check:watch": "svelte-kit sync && svelte-check --tsconfig tsconfig.json --watch", "lint": "eslint .", "lint:fix": "eslint . --fix", - "lint:deps": "madge -c --extensions ts ./" + "lint:deps": "madge -c --extensions ts ./src" }, "dependencies": { "@paralleldrive/cuid2": "2.2.2", diff --git a/src/lib/game/actions/action.ts b/src/lib/game/actions/baseAction.ts similarity index 55% rename from src/lib/game/actions/action.ts rename to src/lib/game/actions/baseAction.ts index 1ba393a2..78368322 100644 --- a/src/lib/game/actions/action.ts +++ b/src/lib/game/actions/baseAction.ts @@ -1,20 +1,14 @@ -import type { Player } from '../objects/units' -import type { IGameAction, IGameActionResponse } from '$lib/game/types' +import type { IGameAction } from '$lib/game/types' interface IActionOptions { command: IGameAction['command'] commandDescription: IGameAction['commandDescription'] } -export class Action implements IGameAction { +export class BaseAction implements IGameAction { public command: string public commandDescription: string - public live!: ( - player: Player, - params: string[], - ) => Promise - constructor({ command, commandDescription }: IActionOptions) { this.command = command this.commandDescription = commandDescription diff --git a/src/lib/game/actions/donateWoodToVillageAction.ts b/src/lib/game/actions/donateWoodToVillageAction.ts index 4bc3d186..605e4ecc 100644 --- a/src/lib/game/actions/donateWoodToVillageAction.ts +++ b/src/lib/game/actions/donateWoodToVillageAction.ts @@ -1,25 +1,23 @@ 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' +import { BaseAction } from './baseAction' +import { ANSWER } from '$lib/game/scenes/services/actionService' +import type { GameScene } from '$lib/game/types' interface IDonateWoodToVillageActionOptions { scene: GameScene } -export class DonateWoodToVillageAction extends Action { - private scene: GameScene +export class DonateWoodToVillageAction extends BaseAction { + scene: GameScene constructor({ scene }: IDonateWoodToVillageActionOptions) { super({ command: 'donate', commandDescription: '!donate [quantity]' }) this.scene = scene - this.live = this.initLive } - async initLive(player: Player, params: string[]) { + async live(player, params) { const amount = this.scene.actionService.getAmountFromChatCommand(params[0]) if (!amount) { return ANSWER.WRONG_AMOUNT_ERROR diff --git a/src/lib/game/actions/plantTreeAction.ts b/src/lib/game/actions/plantTreeAction.ts index f6683104..20dbf0a9 100644 --- a/src/lib/game/actions/plantTreeAction.ts +++ b/src/lib/game/actions/plantTreeAction.ts @@ -1,25 +1,23 @@ import { Village } from '../chunks' -import type { Player } from '../objects/units' -import type { GameScene } from '../scenes/gameScene' import { PlantNewTreeScript } from '../scripts/plantNewTreeScript' -import { Action } from './action' -import { ANSWER } from '$lib/game/services/actionService' +import { BaseAction } from './baseAction' +import { ANSWER } from '$lib/game/scenes/services/actionService' +import type { GameScene } from '$lib/game/types' interface IPlantTreeActionOptions { scene: GameScene } -export class PlantTreeAction extends Action { - private scene: GameScene +export class PlantTreeAction extends BaseAction { + scene: GameScene constructor({ scene }: IPlantTreeActionOptions) { super({ command: 'plant', commandDescription: '!plant' }) this.scene = scene - this.live = this.initLive } - async initLive(player: Player) { + async live(player) { if (player.script && !player.script.isInterruptible) { return ANSWER.BUSY_ERROR } diff --git a/src/lib/game/actions/voteAction.ts b/src/lib/game/actions/voteAction.ts index c0c9e15b..331773d0 100644 --- a/src/lib/game/actions/voteAction.ts +++ b/src/lib/game/actions/voteAction.ts @@ -1,29 +1,27 @@ import type { Poll } from '../common' -import type { Player } from '../objects/units' -import { Action } from './action' -import { ANSWER } from '$lib/game/services/actionService' +import { BaseAction } from './baseAction' +import { ANSWER } from '$lib/game/scenes/services/actionService' interface IVoteActionOptions { poll: Poll } -export class VoteAction extends Action { - private poll: Poll - private readonly id: string +export class VoteAction extends BaseAction { + #poll: Poll + readonly #id: string constructor({ poll }: IVoteActionOptions) { super({ command: 'go', commandDescription: '!go' }) - this.id = poll.generatePollId() + this.#id = poll.generatePollId() - this.command = `go ${this.id}` - this.commandDescription = `!go ${this.id}` + this.command = `go ${this.#id}` + this.commandDescription = `!go ${this.#id}` - this.poll = poll - this.live = this.initLive + this.#poll = poll } - async initLive(player: Player) { + async live(player) { const isSuccess = this.poll.vote(player) if (!isSuccess) { return ANSWER.ALREADY_VOTED_ERROR diff --git a/src/lib/game/game.ts b/src/lib/game/baseGame.ts similarity index 76% rename from src/lib/game/game.ts rename to src/lib/game/baseGame.ts index ebad532e..efa350bb 100644 --- a/src/lib/game/game.ts +++ b/src/lib/game/baseGame.ts @@ -1,19 +1,16 @@ import { Application, Container } from 'pixi.js' -import type { GameObject, Wagon } from './objects' -import { Player, Trader } from './objects/units' -import type { GameScene } from './scenes/gameScene' -import { MovingScene } from './scenes/movingScene' -import { AssetsManager, AudioManager } from './utils' -import { BackgroundGenerator } from './utils/generators/background' import type { - GameSceneType, - IGameObject, - IGameObjectPlayer, - IGameObjectTrader, - WebSocketMessage, + Game, + GameObject, + GameScene, + GameSceneType, WebSocketMessage, } from '$lib/game/types' +import type { Wagon } from '$lib/game/objects/wagon' +import { AudioManager } from '$lib/game/utils/audioManager' +import { BackgroundGenerator } from '$lib/game/utils/generators/background' +import { AssetsManager } from '$lib/game/utils/assetsManager' -export class Game extends Container { +export class BaseGame extends Container implements Game { public children: GameObject[] = [] public app!: Application public audio!: AudioManager @@ -176,30 +173,6 @@ export class Game extends Container { } } - initPlayer(object: IGameObjectPlayer) { - const player = new Player({ game: this, object }) - this.addChild(player) - } - - updatePlayer(object: IGameObjectPlayer) { - const player = this.findObject(object.id) - if (player instanceof Player) { - player.update(object) - } - } - - initTrader(object: IGameObjectTrader) { - const unit = new Trader({ game: this, object }) - this.addChild(unit) - } - - updateTrader(object: IGameObjectTrader) { - const unit = this.findObject(object.id) - if (unit instanceof Trader) { - unit.update(object) - } - } - public checkIfThisFlagIsTarget(id: string) { for (const obj of this.children) { if (obj.target?.id === id) { @@ -234,31 +207,12 @@ export class Game extends Container { } } - handleMessageObject(object: Partial) { + handleMessageObject(object: Partial) { if (!object.id) { return } - const obj = this.findObject(object.id) - if (!obj) { - if (object.entity === 'PLAYER') { - this.initPlayer(object as IGameObjectPlayer) - return - } - if (object.entity === 'TRADER') { - this.initTrader(object as IGameObjectTrader) - return - } - return - } - - if (object.entity === 'PLAYER') { - this.updatePlayer(object as IGameObjectPlayer) - return - } - if (object.entity === 'TRADER') { - this.updateTrader(object as IGameObjectTrader) - } + this.findObject(object.id) } handleMessageEvent(event: WebSocketMessage['event']) { diff --git a/src/lib/game/chunks/forest.ts b/src/lib/game/chunks/forest.ts index cebe108d..747df201 100644 --- a/src/lib/game/chunks/forest.ts +++ b/src/lib/game/chunks/forest.ts @@ -1,8 +1,7 @@ import { Stone, Tree } from '../objects' -import type { GameScene } from '../scenes/gameScene.ts' import { GameChunk } from './gameChunk' import { getRandomInRange } from '$lib/random' -import type { IGameChunkTheme, IGameForestChunk } from '$lib/game/types' +import type { GameScene, IGameChunkTheme, IGameForestChunk } from '$lib/game/types' interface IForestOptions { center: IGameForestChunk['center'] diff --git a/src/lib/game/chunks/gameChunk.ts b/src/lib/game/chunks/gameChunk.ts index 3b45b673..dd2853d7 100644 --- a/src/lib/game/chunks/gameChunk.ts +++ b/src/lib/game/chunks/gameChunk.ts @@ -1,8 +1,12 @@ import { createId } from '@paralleldrive/cuid2' -import { Area, type GameObject, Tree } from '../objects' -import type { GameScene } from '../scenes/gameScene.ts' +import { Area, Tree } from '../objects' import { getRandomInRange } from '$lib/random' -import type { IGameChunk, IGameChunkTheme } from '$lib/game/types' +import type { + GameObject, + GameScene, + IGameChunk, + IGameChunkTheme, +} from '$lib/game/types' interface IGameChunkOptions { center: IGameChunk['center'] @@ -40,12 +44,12 @@ export class GameChunk implements IGameChunk { this.scene = scene - this.initArea({ width, height, theme }) + this.#initArea({ width, height, theme }) } public live() {} - private initArea({ + #initArea({ width, height, theme, diff --git a/src/lib/game/chunks/index.ts b/src/lib/game/chunks/index.ts deleted file mode 100644 index 7c89cad5..00000000 --- a/src/lib/game/chunks/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -export { Forest } from './forest' -export { GameChunk } from './gameChunk' -export { Village } from './village' -export { LakeChunk } from './lake' diff --git a/src/lib/game/chunks/lake.ts b/src/lib/game/chunks/lake.ts index cc64fbd2..421b6060 100644 --- a/src/lib/game/chunks/lake.ts +++ b/src/lib/game/chunks/lake.ts @@ -1,8 +1,11 @@ import { Lake, Stone, Tree } from '../objects' -import type { GameScene } from '../scenes/gameScene.ts' import { GameChunk } from './gameChunk' import { getRandomInRange } from '$lib/random' -import type { IGameChunkTheme, IGameLakeChunk } from '$lib/game/types' +import type { + GameScene, + IGameChunkTheme, + IGameLakeChunk, +} from '$lib/game/types' interface ILakeOptions { scene: GameScene diff --git a/src/lib/game/chunks/village.ts b/src/lib/game/chunks/village.ts index 88043f52..0bdc9d6a 100644 --- a/src/lib/game/chunks/village.ts +++ b/src/lib/game/chunks/village.ts @@ -5,7 +5,6 @@ import { Store } from '../objects/buildings/store' import { WagonStop } from '../objects/buildings/wagonStop' import { Warehouse } from '../objects/buildings/warehouse' import { Courier, Farmer } from '../objects/units' -import type { GameScene } from '../scenes/gameScene' import { BuildScript } from '../scripts/buildScript' import { ChopTreeScript } from '../scripts/chopTreeScript' import { MoveToTargetScript } from '../scripts/moveToTargetScript' @@ -14,9 +13,9 @@ import { PlantNewTreeScript } from '../scripts/plantNewTreeScript' import { GameChunk } from './gameChunk' import { getRandomInRange } from '$lib/random' import type { - IGameChunkTheme, - IGameObjectFlag, - IGameVillageChunk, + GameObjectFlag, + GameScene, + IGameChunkTheme, IGameVillageChunk, } from '$lib/game/types' interface IVillageOptions { @@ -179,7 +178,7 @@ export class Village extends GameChunk implements IGameVillageChunk { } } - initFlag(type: IGameObjectFlag['type']) { + initFlag(type: GameObjectFlag['type']) { const randomPoint = this.getRandomPoint() this.objects.push( new Flag({ @@ -191,7 +190,7 @@ export class Village extends GameChunk implements IGameVillageChunk { ) } - initFlags(type: IGameObjectFlag['type'], count: number) { + initFlags(type: GameObjectFlag['type'], count: number) { for (let i = 0; i < count; i++) { this.initFlag(type) } diff --git a/src/lib/game/common/index.ts b/src/lib/game/common/index.ts deleted file mode 100644 index aacc870c..00000000 --- a/src/lib/game/common/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -export { Inventory } from './inventory' -export { Skill } from './skill' -export { Event } from './event' -export { Group } from './group' -export { Route } from './route' -export { Poll } from './poll' diff --git a/src/lib/game/common/poll.ts b/src/lib/game/common/poll.ts index 8cc260c6..897cabf3 100644 --- a/src/lib/game/common/poll.ts +++ b/src/lib/game/common/poll.ts @@ -1,8 +1,7 @@ import { createId } from '@paralleldrive/cuid2' import { VoteAction } from '../actions/voteAction' -import type { GameScene } from '../scenes/gameScene' import { getRandomInRange } from '$lib/random' -import type { IGameObjectPlayer, IGamePoll } from '$lib/game/types' +import type { GameScene, IGameObjectPlayer, IGamePoll } from '$lib/game/types' interface IPollOptions { scene: GameScene diff --git a/src/lib/game/common/route.ts b/src/lib/game/common/route.ts index 4b152a71..1f024799 100644 --- a/src/lib/game/common/route.ts +++ b/src/lib/game/common/route.ts @@ -1,6 +1,5 @@ import { Flag } from '../objects' -import type { GameScene } from '../scenes/gameScene' -import type { IGameChunk, IGameRoute } from '$lib/game/types' +import type { GameScene, IGameChunk, IGameRoute } from '$lib/game/types' interface IRoutePoint { x: number @@ -41,11 +40,11 @@ export class Route implements IGameRoute { }) } - public setEndPoint({ x, y }: IRoutePoint) { + setEndPoint({ x, y }: IRoutePoint) { this.endPoint = { x, y } } - public addFlag({ x, y }: IRoutePoint) { + #addFlag({ x, y }: IRoutePoint) { const movementFlag = new Flag({ scene: this.scene, type: 'WAGON_MOVEMENT', @@ -55,7 +54,7 @@ export class Route implements IGameRoute { const prevFlag = this.flags[this.flags.length - 1] if (prevFlag) { - this.initArea(prevFlag, movementFlag) + this.#initArea(prevFlag, movementFlag) } this.flags.push(movementFlag) @@ -64,25 +63,25 @@ export class Route implements IGameRoute { public addGlobalFlag(end: IRoutePoint) { const prevGlobalFlag = this.flags[this.flags.length - 1] if (!prevGlobalFlag) { - return this.addFlag(end) + return this.#addFlag(end) } - this.generatePath({ x: prevGlobalFlag.x, y: prevGlobalFlag.y }, end) - this.addFlag({ x: end.x, y: end.y }) + this.#generatePath({ x: prevGlobalFlag.x, y: prevGlobalFlag.y }, end) + this.#addFlag({ x: end.x, y: end.y }) } - public getNextFlag() { + getNextFlag() { return this.flags[0] } - public removeFlag(flag: Flag) { + removeFlag(flag: Flag) { const index = this.flags.findIndex((f) => f.id === flag.id) if (index >= 0) { this.flags.splice(index, 1) } } - public initArea(flag1: Flag, flag2: Flag) { + #initArea(flag1: Flag, flag2: Flag) { const offset = 150 const halfOffset = offset / 2 @@ -102,7 +101,7 @@ export class Route implements IGameRoute { this.areas.push(area) } - private isInArea(area: IRouteArea, point: IRoutePoint) { + #isInArea(area: IRouteArea, point: IRoutePoint) { return ( area.startX < point.x && point.x < area.endX @@ -111,9 +110,9 @@ export class Route implements IGameRoute { ) } - public checkIfPointIsOnWagonPath(point: IRoutePoint) { + checkIfPointIsOnWagonPath(point: IRoutePoint) { for (const area of this.areas) { - if (this.isInArea(area, point)) { + if (this.#isInArea(area, point)) { return true } } @@ -121,7 +120,7 @@ export class Route implements IGameRoute { return false } - generatePath(start: IRoutePoint, end: IRoutePoint) { + #generatePath(start: IRoutePoint, end: IRoutePoint) { const pathDistance = Route.getDistanceBetween2Points(start, end) console.log('path', pathDistance) @@ -137,11 +136,11 @@ export class Route implements IGameRoute { for (let i = 0; i < pointsCount; i++) { nowX += stepX nowY += stepY - this.addFlag({ x: nowX, y: nowY }) + this.#addFlag({ x: nowX, y: nowY }) } } - public static getDistanceBetween2Points( + static getDistanceBetween2Points( point1: { x: number y: number diff --git a/src/lib/game/components/wagonEngineCloudsContainer.ts b/src/lib/game/components/wagonEngineCloudsContainer.ts index a8cbcd3f..d338b4be 100644 --- a/src/lib/game/components/wagonEngineCloudsContainer.ts +++ b/src/lib/game/components/wagonEngineCloudsContainer.ts @@ -8,8 +8,8 @@ interface IWagonEngineCloudsContainerOptions { } export class WagonEngineCloudsContainer extends GraphicsContainer { - private offset = 0 - private wagon: Wagon + #offset = 0 + #wagon: Wagon constructor({ wagon }: IWagonEngineCloudsContainerOptions) { super({ type: 'WAGON_ENGINE_CLOUD', direction: 'LEFT' }) @@ -17,11 +17,35 @@ export class WagonEngineCloudsContainer extends GraphicsContainer { this.x = -106 this.y = -118 - this.wagon = wagon + this.#wagon = wagon } - createRandom() { - const sprite = Sprite.from(this.getRandomSpriteIndex()) + animate(speed: number) { + this.#offset -= speed + 1 + + const cloudsActive = speed * 8 + 1 + const canCreateCloud + = this.children.length < cloudsActive && this.#offset <= 0 + if (canCreateCloud) { + this.#createRandom() + this.#offset = speed * getRandomInRange(170, 190) + 3 + } + + for (const container of this.children) { + container.visible = true + + container.x -= (speed / 3 + 2.5) / this.#wagon.scene.game.tick + container.y -= 3 / this.#wagon.scene.game.tick + container.alpha -= 0.5 / this.#wagon.scene.game.tick + + if (container.alpha <= 0) { + this.#remove(container) + } + } + } + + #createRandom() { + const sprite = Sprite.from(this.#getRandomSpriteIndex()) sprite.anchor.set(0.5, 1) sprite.scale = 0.75 sprite.visible = false @@ -29,7 +53,7 @@ export class WagonEngineCloudsContainer extends GraphicsContainer { this.addChild(sprite) } - getRandomSpriteIndex() { + #getRandomSpriteIndex() { const random = getRandomInRange(1, 1000) if (random <= 500) { return 'wagonEngineCloud1' @@ -43,31 +67,7 @@ export class WagonEngineCloudsContainer extends GraphicsContainer { return 'wagonEngineCloud4' } - remove(container: Container) { + #remove(container: Container) { return this.removeChild(container) } - - animate(speed: number) { - this.offset -= speed + 1 - - const cloudsActive = speed * 8 + 1 - const canCreateCloud - = this.children.length < cloudsActive && this.offset <= 0 - if (canCreateCloud) { - this.createRandom() - this.offset = speed * getRandomInRange(170, 190) + 3 - } - - for (const container of this.children) { - container.visible = true - - container.x -= (speed / 3 + 2.5) / this.wagon.scene.game.tick - container.y -= 3 / this.wagon.scene.game.tick - container.alpha -= 0.5 / this.wagon.scene.game.tick - - if (container.alpha <= 0) { - this.remove(container) - } - } - } } diff --git a/src/lib/game/index.ts b/src/lib/game/index.ts deleted file mode 100644 index cde3bfa9..00000000 --- a/src/lib/game/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -// const game = new Game() -// void game.play() -// -// export { game } diff --git a/src/lib/game/objects/area.ts b/src/lib/game/objects/area.ts index 707eeec8..0a78b050 100644 --- a/src/lib/game/objects/area.ts +++ b/src/lib/game/objects/area.ts @@ -1,6 +1,5 @@ -import type { GameScene } from '../scenes/gameScene' -import { GameObject } from './gameObject' -import type { IGameObjectArea } from '$lib/game/types' +import { BaseObject } from './baseObject' +import type { GameScene, IGameObjectArea } from '$lib/game/types' interface IAreaOptions { scene: GameScene @@ -8,7 +7,7 @@ interface IAreaOptions { area: IGameObjectArea['area'] } -export class Area extends GameObject implements IGameObjectArea { +export class Area extends BaseObject implements IGameObjectArea { public theme: IGameObjectArea['theme'] public area: IGameObjectArea['area'] @@ -16,15 +15,21 @@ export class Area extends GameObject implements IGameObjectArea { const x = area.startX const y = area.startY - super({ scene, x, y }) + super({ scene, x, y, type: 'AREA' }) this.theme = theme this.area = area - this.initGraphics() + this.#initGraphics() } - private initGraphics() { + animate() { + super.animate() + + this.zIndex = -1 + } + + #initGraphics() { this.scene.game.bg.changePaletteByTheme(this.theme) const bg = this.scene.game.bg.getGeneratedBackgroundTilingSprite() @@ -33,10 +38,4 @@ export class Area extends GameObject implements IGameObjectArea { this.addChild(bg) } - - animate() { - super.animate() - - this.zIndex = -1 - } } diff --git a/src/lib/game/objects/gameObject.ts b/src/lib/game/objects/baseObject.ts similarity index 54% rename from src/lib/game/objects/gameObject.ts rename to src/lib/game/objects/baseObject.ts index 68ca1db5..88f5f04c 100644 --- a/src/lib/game/objects/gameObject.ts +++ b/src/lib/game/objects/baseObject.ts @@ -1,31 +1,31 @@ import { createId } from '@paralleldrive/cuid2' import { Container } from 'pixi.js' -import type { GameScene } from '../scenes/gameScene.ts' -import type { IGameObject, IGameScript } from '$lib/game/types' +import type { GameObject, GameScene, IGameScript } from '$lib/game/types' -interface IGameObjectOptions { +interface GameObjectOptions { scene: GameScene + type: GameObject['type'] id?: string x?: number y?: number } -export class GameObject extends Container implements IGameObject { - public id: string - public state!: IGameObject['state'] - public direction: IGameObject['direction'] - public entity: IGameObject['entity'] - public target: IGameObject['target'] - public health!: IGameObject['health'] - public speedPerSecond!: IGameObject['speedPerSecond'] - public size!: IGameObject['size'] - - public scene: GameScene - public script: IGameScript | undefined - public minDistance = 1 - public isOnWagonPath = false - - constructor({ scene, x, y, id }: IGameObjectOptions) { +export class BaseObject extends Container implements GameObject { + id: GameObject['id'] + state: GameObject['state'] + direction: GameObject['direction'] + type: GameObject['type'] + target: GameObject['target'] + health!: GameObject['health'] + speedPerSecond!: GameObject['speedPerSecond'] + size!: GameObject['size'] + + scene: GameScene + script: IGameScript + minDistance = 1 + isOnWagonPath = false + + constructor({ scene, x, y, id, type }: GameObjectOptions) { super() this.scene = scene @@ -33,8 +33,9 @@ export class GameObject extends Container implements IGameObject { this.id = id ?? createId() this.x = x ?? 0 this.y = y ?? 0 - this.entity = 'TREE' + this.type = type this.direction = 'RIGHT' + this.state = 'IDLE' this.scene.game.addChild(this) } @@ -43,33 +44,47 @@ export class GameObject extends Container implements IGameObject { this.zIndex = Math.round(this.y) } - live(): void {} - - public move() { - const isOnTarget = this.checkIfIsOnTarget() + move(): boolean { + const isOnTarget = this.#checkIfIsOnTarget() if (isOnTarget) { - this.stop() + this.#stop() return false } if (!this.target || !this.target.x || !this.target.y) { - this.stop() + this.#stop() return false } - const distanceToX = this.getDistanceToTargetX() - const distanceToY = this.getDistanceToTargetY() + const distanceToX = this.#getDistanceToTargetX() + const distanceToY = this.#getDistanceToTargetY() // Fix diagonal speed const speed = this.speedPerSecond / this.scene.game.tick const finalSpeed = distanceToX > 0 && distanceToY > 0 ? speed * 0.75 : speed - this.moveX(finalSpeed > distanceToX ? distanceToX : finalSpeed) - this.moveY(finalSpeed > distanceToY ? distanceToY : finalSpeed) + this.#moveX(finalSpeed > distanceToX ? distanceToX : finalSpeed) + this.#moveY(finalSpeed > distanceToY ? distanceToY : finalSpeed) return true } - moveX(speed: number) { + setTarget(target: GameObject) { + this.target = target + this.state = 'MOVING' + } + + removeTarget() { + this.target = undefined + } + + destroy() { + super.destroy() + this.size = 0 + this.health = 0 + this.state = 'DESTROYED' + } + + #moveX(speed: number) { if (!this.target?.x || this.target.x === this.x) { return } @@ -84,7 +99,7 @@ export class GameObject extends Container implements IGameObject { } } - moveY(speed: number) { + #moveY(speed: number) { if (!this.target?.y || this.target.y === this.y) { return } @@ -97,44 +112,28 @@ export class GameObject extends Container implements IGameObject { } } - stop() { + #stop() { this.state = 'IDLE' } - public destroy() { - super.destroy() - this.size = 0 - this.health = 0 - this.state = 'DESTROYED' - } - - checkIfIsOnTarget() { + #checkIfIsOnTarget() { return ( - this.getDistanceToTargetX() + this.getDistanceToTargetY() + this.#getDistanceToTargetX() + this.#getDistanceToTargetY() <= this.minDistance ) } - getDistanceToTargetX() { + #getDistanceToTargetX() { if (!this.target?.x) { return 0 } return Math.abs(this.target.x - this.x) } - getDistanceToTargetY() { + #getDistanceToTargetY() { if (!this.target?.y) { return 0 } return Math.abs(this.target.y - this.y) } - - public setTarget(target: IGameObject) { - this.target = target - this.state = 'MOVING' - } - - public removeTarget() { - this.target = undefined - } } diff --git a/src/lib/game/objects/buildings/building.ts b/src/lib/game/objects/buildings/baseBuilding.ts similarity index 56% rename from src/lib/game/objects/buildings/building.ts rename to src/lib/game/objects/buildings/baseBuilding.ts index bdf87255..227ad8d6 100644 --- a/src/lib/game/objects/buildings/building.ts +++ b/src/lib/game/objects/buildings/baseBuilding.ts @@ -1,26 +1,30 @@ import { createId } from '@paralleldrive/cuid2' import { Inventory } from '../../common' -import type { GameScene } from '../../scenes/gameScene' -import { GameObject } from '../gameObject' -import type { IGameObjectBuilding, ItemType } from '$lib/game/types' +import { BaseObject } from '../baseObject' +import type { + GameObjectBuildingType, + GameScene, + IGameObjectBuilding, + ItemType, +} from '$lib/game/types' interface IBuildingOptions { scene: GameScene x: number y: number + type: GameObjectBuildingType } -export class Building extends GameObject implements IGameObjectBuilding { +export class BaseBuilding extends BaseObject implements IGameObjectBuilding { public inventory!: Inventory - constructor({ scene, x, y }: IBuildingOptions) { - super({ scene, x, y }) + constructor({ scene, x, y, type }: IBuildingOptions) { + super({ scene, x, y, type }) - this.state = 'IDLE' - this.initInventory() + this.#initInventory() } - public animate() { + animate() { super.animate() this.zIndex = Math.round(this.y - 5) @@ -30,7 +34,7 @@ export class Building extends GameObject implements IGameObjectBuilding { } } - private initInventory() { + #initInventory() { this.inventory = new Inventory({ objectId: this.id, id: createId(), @@ -38,7 +42,7 @@ export class Building extends GameObject implements IGameObjectBuilding { }) } - public getItemByType(type: ItemType) { + getItemByType(type: ItemType) { if (!this.inventory?.items) { return } diff --git a/src/lib/game/objects/buildings/campfire.ts b/src/lib/game/objects/buildings/campfire.ts index 0493acc9..a3f1a538 100644 --- a/src/lib/game/objects/buildings/campfire.ts +++ b/src/lib/game/objects/buildings/campfire.ts @@ -1,9 +1,8 @@ import { type AnimatedSprite, Sprite } from 'pixi.js' import { FireParticlesContainer } from '../../components/fireParticlesContainer' -import type { GameScene } from '../../scenes/gameScene.ts' import { AssetsManager } from '../../utils' -import { Building } from './building' -import type { IGameBuildingCampfire } from '$lib/game/types' +import { BaseBuilding } from './baseBuilding' +import type { GameScene, IGameBuildingCampfire } from '$lib/game/types' interface ICampfireOptions { scene: GameScene @@ -11,12 +10,12 @@ interface ICampfireOptions { y: number } -export class Campfire extends Building implements IGameBuildingCampfire { +export class Campfire extends BaseBuilding implements IGameBuildingCampfire { private fireAnimation!: AnimatedSprite private fireParticles!: FireParticlesContainer constructor({ scene, x, y }: ICampfireOptions) { - super({ scene, x, y }) + super({ scene, x, y, type: 'CAMPFIRE' }) this.initGraphics() } diff --git a/src/lib/game/objects/buildings/constructionArea.ts b/src/lib/game/objects/buildings/constructionArea.ts index 42a76e11..44fc49de 100644 --- a/src/lib/game/objects/buildings/constructionArea.ts +++ b/src/lib/game/objects/buildings/constructionArea.ts @@ -1,7 +1,6 @@ import { Sprite } from 'pixi.js' -import type { GameScene } from '../../scenes/gameScene' -import { Building } from './building' -import type { IGameBuildingConstructionArea } from '$lib/game/types' +import { BaseBuilding } from './baseBuilding' +import type { GameScene, IGameBuildingConstructionArea } from '$lib/game/types' interface IConstructionAreaOptions { scene: GameScene @@ -10,15 +9,15 @@ interface IConstructionAreaOptions { } export class ConstructionArea - extends Building + extends BaseBuilding implements IGameBuildingConstructionArea { constructor({ scene, x, y }: IConstructionAreaOptions) { - super({ scene, x, y }) + super({ scene, x, y, type: 'CONSTRUCTION_AREA' }) - this.initGraphics() + this.#initGraphics() } - private initGraphics() { + #initGraphics() { const sprite = Sprite.from('constructionArea1') if (sprite) { sprite.anchor.set(0.5, 0.92) diff --git a/src/lib/game/objects/buildings/store.ts b/src/lib/game/objects/buildings/store.ts index 8244cd95..87b47807 100644 --- a/src/lib/game/objects/buildings/store.ts +++ b/src/lib/game/objects/buildings/store.ts @@ -1,7 +1,6 @@ import { Sprite } from 'pixi.js' -import type { GameScene } from '../../scenes/gameScene.ts' -import { Building } from './building' -import type { IGameBuildingStore } from '$lib/game/types' +import { BaseBuilding } from './baseBuilding' +import type { GameScene, IGameBuildingStore } from '$lib/game/types' interface IStoreOptions { scene: GameScene @@ -9,14 +8,14 @@ interface IStoreOptions { y: number } -export class Store extends Building implements IGameBuildingStore { +export class Store extends BaseBuilding implements IGameBuildingStore { constructor({ scene, x, y }: IStoreOptions) { - super({ scene, x, y }) + super({ scene, x, y, type: 'STORE' }) - this.initGraphics() + this.#initGraphics() } - private initGraphics() { + #initGraphics() { const sprite = Sprite.from('store1') if (sprite) { sprite.anchor.set(0.5, 1) diff --git a/src/lib/game/objects/buildings/wagonStop.ts b/src/lib/game/objects/buildings/wagonStop.ts index de3337e9..a6e7fa92 100644 --- a/src/lib/game/objects/buildings/wagonStop.ts +++ b/src/lib/game/objects/buildings/wagonStop.ts @@ -1,7 +1,6 @@ import { Sprite } from 'pixi.js' -import type { GameScene } from '../../scenes/gameScene.ts' -import { Building } from './building' -import type { IGameBuildingWagonStop } from '$lib/game/types' +import { BaseBuilding } from './baseBuilding' +import type { GameScene, IGameBuildingWagonStop } from '$lib/game/types' interface IWagonStopOptions { scene: GameScene @@ -9,14 +8,14 @@ interface IWagonStopOptions { y: number } -export class WagonStop extends Building implements IGameBuildingWagonStop { +export class WagonStop extends BaseBuilding implements IGameBuildingWagonStop { constructor({ scene, x, y }: IWagonStopOptions) { - super({ scene, x, y }) + super({ scene, x, y, type: 'WAGON_STOP' }) - this.initGraphics() + this.#initGraphics() } - private initGraphics() { + #initGraphics() { const sprite = Sprite.from('wagonStop1') if (sprite) { sprite.anchor.set(0.5, 0.92) diff --git a/src/lib/game/objects/buildings/warehouse.ts b/src/lib/game/objects/buildings/warehouse.ts index 88d1c83a..cfcaaaef 100644 --- a/src/lib/game/objects/buildings/warehouse.ts +++ b/src/lib/game/objects/buildings/warehouse.ts @@ -1,8 +1,7 @@ import { Sprite } from 'pixi.js' import { BuildingInterface } from '../../components/buildingInterface' -import type { GameScene } from '../../scenes/gameScene.ts' -import { Building } from './building' -import type { IGameBuildingWarehouse } from '$lib/game/types' +import { BaseBuilding } from './baseBuilding' +import type { GameScene, IGameBuildingWarehouse } from '$lib/game/types' interface IWarehouseOptions { scene: GameScene @@ -10,17 +9,17 @@ interface IWarehouseOptions { y: number } -export class Warehouse extends Building implements IGameBuildingWarehouse { +export class Warehouse extends BaseBuilding implements IGameBuildingWarehouse { public interface!: BuildingInterface constructor({ scene, x, y }: IWarehouseOptions) { - super({ scene, x, y }) + super({ scene, x, y, type: 'WAREHOUSE' }) - this.initGraphics() - // this.initInterface() + this.#initGraphics() + // this.#initInterface() } - private initGraphics() { + #initGraphics() { const sprite = Sprite.from('warehouse1') if (sprite) { sprite.anchor.set(0.5, 1) @@ -28,7 +27,7 @@ export class Warehouse extends Building implements IGameBuildingWarehouse { } } - private initInterface() { + #initInterface() { this.interface = new BuildingInterface(this) this.addChild(this.interface) } diff --git a/src/lib/game/objects/rabbit.ts b/src/lib/game/objects/creatures/rabbit.ts similarity index 68% rename from src/lib/game/objects/rabbit.ts rename to src/lib/game/objects/creatures/rabbit.ts index e2563697..a11706b5 100644 --- a/src/lib/game/objects/rabbit.ts +++ b/src/lib/game/objects/creatures/rabbit.ts @@ -1,24 +1,23 @@ import { Sprite } from 'pixi.js' -import type { Game } from '../game' -import { GameObject } from './gameObject' -import type { IGameObjectRabbit } from '$lib/game/types' +import { BaseObject } from '../baseObject' +import type { GameScene, IGameObjectRabbit } from '$lib/game/types' -interface IRabbitOptions { - game: Game - object: IGameObjectRabbit +interface RabbitOptions { + scene: GameScene + x: number + y: number } -export class Rabbit extends GameObject implements IGameObjectRabbit { +export class Rabbit extends BaseObject implements IGameObjectRabbit { public animationAngle = 0 - constructor({ game, object }: IRabbitOptions) { - super({ game, ...object }) + constructor({ scene, x, y }: RabbitOptions) { + super({ scene, x, y, type: 'RABBIT' }) - this.update(object) - this.init() + this.#init() } - init() { + #init() { const spriteRight = Sprite.from('rabbitRight') const spriteLeft = Sprite.from('rabbitLeft') diff --git a/src/lib/game/objects/wolf.ts b/src/lib/game/objects/creatures/wolf.ts similarity index 51% rename from src/lib/game/objects/wolf.ts rename to src/lib/game/objects/creatures/wolf.ts index 5b2d3eda..ef540753 100644 --- a/src/lib/game/objects/wolf.ts +++ b/src/lib/game/objects/creatures/wolf.ts @@ -1,21 +1,19 @@ import { Sprite } from 'pixi.js' -import type { Game } from '../game' -import { GameObject } from './gameObject' -import type { IGameObjectWolf } from '$lib/game/types' +import { BaseObject } from '../baseObject' +import type { GameScene, IGameObjectWolf } from '$lib/game/types' interface IWolfOptions { - game: Game - object: IGameObjectWolf + scene: GameScene + x: number + y: number } -export class Wolf extends GameObject implements IGameObjectWolf { - public animationAngle = 0 - public animationSlowSpeed = 0.1 +export class Wolf extends BaseObject implements IGameObjectWolf { + #animationSlowSpeed = 0.1 - constructor({ game, object }: IWolfOptions) { - super({ game, ...object }) + constructor({ scene, x, y }: IWolfOptions) { + super({ scene, x, y, type: 'WOLF' }) - this.update(object) this.init() } @@ -45,16 +43,14 @@ export class Wolf extends GameObject implements IGameObjectWolf { } if (this.state === 'MOVING') { - this.angle = this.animationAngle - this.shakeAnimation() + this.#shakeAnimation() } } - shakeAnimation() { - if (Math.abs(this.animationAngle) >= 2) { - this.animationSlowSpeed *= -1 + #shakeAnimation() { + if (Math.abs(this.angle) >= 2) { + this.#animationSlowSpeed *= -1 } - this.animationAngle += this.animationSlowSpeed - this.angle = this.animationAngle + this.angle += this.#animationSlowSpeed } } diff --git a/src/lib/game/objects/flag.ts b/src/lib/game/objects/flagObject.ts similarity index 55% rename from src/lib/game/objects/flag.ts rename to src/lib/game/objects/flagObject.ts index 42877b07..00d33e69 100644 --- a/src/lib/game/objects/flag.ts +++ b/src/lib/game/objects/flagObject.ts @@ -1,65 +1,45 @@ import { Sprite } from 'pixi.js' -import type { GameScene } from '../scenes/gameScene' -import { GameObject } from './gameObject' -import type { IGameObjectFlag } from '$lib/game/types' +import { BaseObject } from './baseObject' +import type { GameObjectFlag, GameScene } from '$lib/game/types' -interface IFlagOptions { +interface FlagOptions { scene: GameScene x: number y: number - type: IGameObjectFlag['type'] + variant: GameObjectFlag['variant'] offsetX?: number offsetY?: number } -export class Flag extends GameObject implements IGameObjectFlag { - public type!: IGameObjectFlag['type'] +export class FlagObject extends BaseObject implements GameObjectFlag { + variant: GameObjectFlag['variant'] public isReserved: boolean public offsetX: number public offsetY: number - constructor({ scene, x, y, type, offsetX, offsetY }: IFlagOptions) { - super({ scene, x, y }) + constructor({ scene, x, y, variant, offsetX, offsetY }: FlagOptions) { + super({ scene, x, y, type: 'FLAG' }) - this.type = type + this.variant = variant this.isReserved = false this.offsetX = offsetX ?? 0 this.offsetY = offsetY ?? 0 this.visible = false - this.initGraphics() + this.#initGraphics() } - public live() { + live() { + super.live() + if (this.target?.state === 'DESTROYED') { this.removeTarget() } } - private initGraphics() { - const sprite = this.getSpriteByType() - if (sprite) { - sprite.anchor.set(0.5, 1) - this.addChild(sprite) - } - } - - getSpriteByType() { - if ( - this.type === 'MOVEMENT' - || this.type === 'WAGON_NEAR_MOVEMENT' - || this.type === 'WAGON_MOVEMENT' - ) { - return Sprite.from('flag1') - } - if (this.type === 'RESOURCE') { - return Sprite.from('flag2') - } - } - - public animate() { + animate() { if (this.scene.game.checkIfThisFlagIsTarget(this.id)) { this.visible = true return @@ -77,4 +57,25 @@ export class Flag extends GameObject implements IGameObjectFlag { this.visible = false } + + #initGraphics() { + const sprite = this.#getSprite() + if (sprite) { + sprite.anchor.set(0.5, 1) + this.addChild(sprite) + } + } + + #getSprite() { + if ( + this.variant === 'MOVEMENT' + || this.variant === 'WAGON_NEAR_MOVEMENT' + || this.variant === 'WAGON_MOVEMENT' + ) { + return Sprite.from('flag1') + } + if (this.variant === 'RESOURCE') { + return Sprite.from('flag2') + } + } } diff --git a/src/lib/game/objects/index.ts b/src/lib/game/objects/index.ts deleted file mode 100644 index e51ba3b8..00000000 --- a/src/lib/game/objects/index.ts +++ /dev/null @@ -1,9 +0,0 @@ -export { GameObject } from './gameObject' -export { Rabbit } from './rabbit' -export { Stone } from './stone' -export { Tree } from './tree' -export { Wolf } from './wolf' -export { Flag } from './flag' -export { Lake } from './lake' -export { Area } from './area' -export { Wagon } from './wagon' diff --git a/src/lib/game/objects/lake.ts b/src/lib/game/objects/lake.ts index 15da07b0..4ffaad0a 100644 --- a/src/lib/game/objects/lake.ts +++ b/src/lib/game/objects/lake.ts @@ -1,8 +1,7 @@ -import type { GameScene } from '../scenes/gameScene' import { AssetsManager } from '../utils' -import { GameObject } from './gameObject' +import { BaseObject } from './baseObject' import { Water } from './water' -import type { IGameObjectLake } from '$lib/game/types' +import type { GameScene, IGameObjectLake } from '$lib/game/types' interface ILakeOptions { scene: GameScene @@ -10,35 +9,35 @@ interface ILakeOptions { y: number } -export class Lake extends GameObject implements IGameObjectLake { +export class Lake extends BaseObject implements IGameObjectLake { public water: Water[] = [] constructor({ scene, x, y }: ILakeOptions) { - super({ scene, x, y }) + super({ scene, x, y, type: 'LAKE' }) - this.generate(13) - this.initGraphics() + this.#generate(13) + this.#initGraphics() } - public animate() { + animate() { super.animate() this.zIndex = 0 } - generate(r: number) { + #generate(r: number) { for (let y = r; y >= -r; --y) { for (let x = -r; x <= r; x++) { const value = x ** 2 + y ** 2 if (value < r ** 2) { - this.draw(x, y) + this.#draw(x, y) } } } } - draw(x: number, y: number) { + #draw(x: number, y: number) { const water = new Water({ scene: this.scene, x: x * 32, y: y * 32 }) this.water.push(water) } @@ -66,7 +65,7 @@ export class Lake extends GameObject implements IGameObjectLake { } } - private initGraphics() { + #initGraphics() { for (const w of this.water) { const sprite = AssetsManager.getRandomSpriteForWater() sprite.anchor.set(0.5, 1) diff --git a/src/lib/game/objects/stone.ts b/src/lib/game/objects/stoneObject.ts similarity index 68% rename from src/lib/game/objects/stone.ts rename to src/lib/game/objects/stoneObject.ts index 02133bab..3fe56479 100644 --- a/src/lib/game/objects/stone.ts +++ b/src/lib/game/objects/stoneObject.ts @@ -1,10 +1,9 @@ import { Sprite } from 'pixi.js' -import type { GameScene } from '../scenes/gameScene' -import { GameObject } from './gameObject' +import { BaseObject } from './baseObject' import { getRandomInRange } from '$lib/random' -import type { IGameObjectStone } from '$lib/game/types' +import type { GameObjectStone, GameScene } from '$lib/game/types' -interface IStoneOptions { +interface StoneOptions { scene: GameScene x: number y: number @@ -13,39 +12,38 @@ interface IStoneOptions { health?: number } -export class Stone extends GameObject implements IGameObjectStone { - public type!: IGameObjectStone['type'] +export class StoneObject extends BaseObject implements GameObjectStone { + public variant!: GameObjectStone['variant'] public resource!: number public isReserved = false public animationAngle = 0 public animationHighSpeed = 0.05 - constructor({ scene, x, y, resource, size }: IStoneOptions) { - super({ scene, x, y }) + constructor({ scene, x, y, resource, size }: StoneOptions) { + super({ scene, x, y, type: 'STONE' }) - this.state = 'IDLE' this.resource = resource ?? getRandomInRange(1, 5) this.size = size ?? 100 - this.initGraphics() + this.#initGraphics() } - private initGraphics() { - const sprite = this.getSpriteByType() + #initGraphics() { + const sprite = this.#getSprite() if (sprite) { sprite.anchor.set(0.5, 1) this.addChild(sprite) } } - getSpriteByType() { - if (this.type === '1') { + #getSprite() { + if (this.variant === '1') { return Sprite.from('stone1') } } - public animate() { + animate() { super.animate() if (this.state === 'DESTROYED') { @@ -54,11 +52,11 @@ export class Stone extends GameObject implements IGameObjectStone { if (this.state === 'MINING') { this.scale = 0.98 - this.shakeAnimation() + this.#shakeAnimation() } } - shakeAnimation() { + #shakeAnimation() { if (Math.abs(this.animationAngle) >= 0.5) { this.animationHighSpeed *= -1 } diff --git a/src/lib/game/objects/tree.ts b/src/lib/game/objects/treeObject.ts similarity index 70% rename from src/lib/game/objects/tree.ts rename to src/lib/game/objects/treeObject.ts index 7485e89b..d645476e 100644 --- a/src/lib/game/objects/tree.ts +++ b/src/lib/game/objects/treeObject.ts @@ -1,10 +1,9 @@ import { Sprite } from 'pixi.js' -import type { GameScene } from '../scenes/gameScene' -import { GameObject } from './gameObject' +import { BaseObject } from './baseObject' import { getRandomInRange } from '$lib/random' -import type { IGameObjectTree } from '$lib/game/types' +import type { GameObjectTree, GameScene } from '$lib/game/types' -interface ITreeOptions { +interface TreeOptions { scene: GameScene x: number y: number @@ -12,13 +11,13 @@ interface ITreeOptions { size?: number health?: number growSpeed?: number - type?: IGameObjectTree['type'] - variant?: IGameObjectTree['variant'] + theme?: GameObjectTree['theme'] + variant?: GameObjectTree['variant'] } -export class Tree extends GameObject implements IGameObjectTree { - public type!: IGameObjectTree['type'] - public variant!: IGameObjectTree['variant'] +export class TreeObject extends BaseObject implements GameObjectTree { + public variant!: GameObjectTree['variant'] + public theme!: GameObjectTree['theme'] public resource!: number public isReadyToChop!: boolean @@ -35,43 +34,42 @@ export class Tree extends GameObject implements IGameObjectTree { resource, size, health, - type, + theme, variant, - }: ITreeOptions) { - super({ scene, x, y }) + }: TreeOptions) { + super({ scene, x, y, type: 'TREE' }) - this.state = 'IDLE' this.resource = resource ?? getRandomInRange(1, 5) this.size = size ?? 100 this.health = health ?? 100 - this.type = type ?? this.getNewType() - this.variant = variant ?? this.getNewVariant() + this.variant = variant ?? this.#getRandomVariant() + this.theme = theme ?? this.#getRandomTheme() - this.initGraphics() + this.#initGraphics() } - public live() { - this.checkHealth() + live() { + this.#checkHealth() switch (this.state) { case 'IDLE': - this.grow() + this.#grow() break case 'CHOPPING': - this.handleChoppingState() + this.#handleChoppingState() break case 'DESTROYED': break } } - public animate() { + animate() { super.animate() this.scale = this.size / 100 if (this.state === 'IDLE') { - this.shakeOnWind() + this.#shakeOnWind() } if (this.state === 'DESTROYED') { @@ -79,7 +77,7 @@ export class Tree extends GameObject implements IGameObjectTree { } if (this.state === 'CHOPPING') { - this.shakeAnimation() + this.#shakeAnimation() } } @@ -89,10 +87,10 @@ export class Tree extends GameObject implements IGameObjectTree { this.health -= 0.08 } - private initGraphics() { + #initGraphics() { this.angle = getRandomInRange(-1, 1) - const sprite = this.getSpriteByType() + const sprite = this.#getSprite() if (sprite) { sprite.anchor.set(0.5, 1) @@ -106,7 +104,7 @@ export class Tree extends GameObject implements IGameObjectTree { } } - private getSpriteByType() { + #getSprite() { if (this.variant === 'GREEN') { return Sprite.from(`tree${this.type}Green`) } @@ -127,7 +125,7 @@ export class Tree extends GameObject implements IGameObjectTree { } } - private shakeAnimation() { + #shakeAnimation() { if (Math.abs(this.angle) < 3) { this.angle += (this.animationSpeedPerSecond * 5) / this.scene.game.tick return @@ -138,7 +136,7 @@ export class Tree extends GameObject implements IGameObjectTree { += ((this.animationSpeedPerSecond * 5) / this.scene.game.tick) * 10 } - private shakeOnWind() { + #shakeOnWind() { if (Math.abs(this.angle) < 1.8) { this.angle += this.animationSpeedPerSecond / this.scene.game.tick return @@ -148,13 +146,13 @@ export class Tree extends GameObject implements IGameObjectTree { this.angle += (this.animationSpeedPerSecond / this.scene.game.tick) * 10 } - private checkHealth() { + #checkHealth() { if (this.health <= 0) { this.destroy() } } - private handleChoppingState() { + #handleChoppingState() { const random = getRandomInRange(1, 20) if (random <= 1) { this.state = 'IDLE' @@ -162,7 +160,7 @@ export class Tree extends GameObject implements IGameObjectTree { } } - private grow() { + #grow() { if (this.size >= this.minSizeToChop && !this.isReadyToChop) { this.isReadyToChop = true } @@ -174,14 +172,14 @@ export class Tree extends GameObject implements IGameObjectTree { this.size += this.growSpeedPerSecond / this.scene.game.tick } - private getNewType(): IGameObjectTree['type'] { - const types: IGameObjectTree['type'][] = ['1', '2', '3', '4', '5'] - const index = getRandomInRange(0, types.length - 1) - return types[index] + #getRandomVariant(): GameObjectTree['variant'] { + const variants: GameObjectTree['variant'][] = ['1', '2', '3', '4', '5'] + const index = getRandomInRange(0, variants.length - 1) + return variants[index] } - private getNewVariant(): IGameObjectTree['variant'] { - const variants: IGameObjectTree['variant'][] = [ + #getRandomTheme(): GameObjectTree['theme'] { + const themes: GameObjectTree['theme'][] = [ 'GREEN', 'BLUE', 'STONE', @@ -189,7 +187,7 @@ export class Tree extends GameObject implements IGameObjectTree { 'TOXIC', 'VIOLET', ] - const index = getRandomInRange(0, variants.length - 1) - return variants[index] + const index = getRandomInRange(0, themes.length - 1) + return themes[index] } } diff --git a/src/lib/game/objects/units/courier.ts b/src/lib/game/objects/units/courier.ts index 42e5d778..8588d9dd 100644 --- a/src/lib/game/objects/units/courier.ts +++ b/src/lib/game/objects/units/courier.ts @@ -1,21 +1,21 @@ import { generateUnitUserName } from '../../common/generators/unitName' import { generateUnitTop } from '../../common/generators/unitTop' -import type { GameScene } from '../../scenes/gameScene' -import { Unit } from './unit' -import type { IGameObjectCourier } from '$lib/game/types' +import { UnitObject } from './unitObject' +import type { GameScene, IGameObjectCourier } from '$lib/game/types' -interface ICourierOptions { +interface CourierOptions { scene: GameScene x: number y: number } -export class Courier extends Unit implements IGameObjectCourier { - constructor({ scene, x, y }: ICourierOptions) { +export class Courier extends UnitObject implements IGameObjectCourier { + constructor({ scene, x, y }: CourierOptions) { super({ scene, x, y, + type: 'VILLAGE_UNIT', }) this.speedPerSecond = 100 diff --git a/src/lib/game/objects/units/farmer.ts b/src/lib/game/objects/units/farmer.ts index 64e4fecd..52d43872 100644 --- a/src/lib/game/objects/units/farmer.ts +++ b/src/lib/game/objects/units/farmer.ts @@ -1,8 +1,7 @@ import { generateUnitUserName } from '../../common/generators/unitName' import { generateUnitTop } from '../../common/generators/unitTop' -import type { GameScene } from '../../scenes/gameScene' -import { Unit } from './unit' -import type { IGameObjectFarmer } from '$lib/game/types' +import { UnitObject } from './unitObject' +import type { GameScene, IGameObjectFarmer } from '$lib/game/types' interface IFarmerOptions { scene: GameScene @@ -10,12 +9,13 @@ interface IFarmerOptions { y: number } -export class Farmer extends Unit implements IGameObjectFarmer { +export class Farmer extends UnitObject implements IGameObjectFarmer { constructor({ scene, x, y }: IFarmerOptions) { super({ scene, x, y, + type: 'VILLAGE_UNIT', }) this.speedPerSecond = 70 diff --git a/src/lib/game/objects/units/index.ts b/src/lib/game/objects/units/index.ts deleted file mode 100644 index 28e7f5ef..00000000 --- a/src/lib/game/objects/units/index.ts +++ /dev/null @@ -1,7 +0,0 @@ -export { Courier } from './courier' -export { Farmer } from './farmer' -export { Mechanic } from './mechanic' -export { Player } from './player' -export { Raider } from './raider' -export { Trader } from './trader' -export { Unit } from './unit' diff --git a/src/lib/game/objects/units/mechanic.ts b/src/lib/game/objects/units/mechanic.ts index 248caff5..6f6a38da 100644 --- a/src/lib/game/objects/units/mechanic.ts +++ b/src/lib/game/objects/units/mechanic.ts @@ -1,6 +1,5 @@ -import type { GameScene } from '../../scenes/gameScene.ts' -import { Unit } from './unit' -import type { IGameObjectMechanic } from '$lib/game/types' +import { UnitObject } from './unitObject' +import type { GameScene, IGameObjectMechanic } from '$lib/game/types' interface IMechanicOptions { scene: GameScene @@ -8,12 +7,13 @@ interface IMechanicOptions { y: number } -export class Mechanic extends Unit implements IGameObjectMechanic { +export class Mechanic extends UnitObject implements IGameObjectMechanic { constructor({ scene, x, y }: IMechanicOptions) { super({ scene, x, y, + type: 'MECHANIC', }) this.userName = 'Mechanic' @@ -25,7 +25,7 @@ export class Mechanic extends Unit implements IGameObjectMechanic { }) } - public animate() { + animate() { super.animate() this.zIndex = Math.round(this.y + 100) diff --git a/src/lib/game/objects/units/player.ts b/src/lib/game/objects/units/player.ts index 147399b3..2de07774 100644 --- a/src/lib/game/objects/units/player.ts +++ b/src/lib/game/objects/units/player.ts @@ -1,17 +1,16 @@ import { Inventory, Skill } from '../../common' -import type { GameScene } from '../../scenes/gameScene.ts' -import { Unit } from './unit' +import { UnitObject } from './unitObject' import { getRandomInRange } from '$lib/random' -import type { IGameObjectPlayer, IGameSkill } from '$lib/game/types' +import type { GameScene, IGameObjectPlayer, IGameSkill } from '$lib/game/types' -interface IPlayerOptions { +interface PlayerOptions { scene: GameScene id?: string x: number y: number } -export class Player extends Unit implements IGameObjectPlayer { +export class Player extends UnitObject implements IGameObjectPlayer { reputation!: number villainPoints!: number refuellerPoints!: number @@ -21,8 +20,8 @@ export class Player extends Unit implements IGameObjectPlayer { public inventoryId?: string - constructor({ scene, id, x, y }: IPlayerOptions) { - super({ scene, id, x, y }) + constructor({ scene, id, x, y }: PlayerOptions) { + super({ scene, id, x, y, type: 'PLAYER' }) this.speedPerSecond = 2 void this.initFromDB() diff --git a/src/lib/game/objects/units/raider.ts b/src/lib/game/objects/units/raider.ts index 27ae2289..89d66397 100644 --- a/src/lib/game/objects/units/raider.ts +++ b/src/lib/game/objects/units/raider.ts @@ -1,19 +1,19 @@ -import type { GameScene } from '../../scenes/gameScene.ts' -import { Unit } from './unit' -import type { IGameObjectRaider } from '$lib/game/types' +import { UnitObject } from './unitObject' +import type { GameScene, IGameObjectRaider } from '$lib/game/types' -interface IRaiderOptions { +interface RaiderOptions { scene: GameScene x: number y: number } -export class Raider extends Unit implements IGameObjectRaider { - constructor({ scene, x, y }: IRaiderOptions) { +export class Raider extends UnitObject implements IGameObjectRaider { + constructor({ scene, x, y }: RaiderOptions) { super({ scene, x, y, + type: 'RAIDER', }) this.speedPerSecond = 1.5 diff --git a/src/lib/game/objects/units/trader.ts b/src/lib/game/objects/units/trader.ts index 3daab208..8a15ee8a 100644 --- a/src/lib/game/objects/units/trader.ts +++ b/src/lib/game/objects/units/trader.ts @@ -1,8 +1,7 @@ import { generateUnitUserName } from '../../common/generators/unitName' import { generateUnitTop } from '../../common/generators/unitTop' -import type { GameScene } from '../../scenes/gameScene' -import { Unit } from './unit' -import type { IGameObjectTrader } from '$lib/game/types' +import { UnitObject } from './unitObject' +import type { GameScene, IGameObjectTrader } from '$lib/game/types' interface ITraderOptions { scene: GameScene @@ -10,12 +9,13 @@ interface ITraderOptions { y: number } -export class Trader extends Unit implements IGameObjectTrader { +export class Trader extends UnitObject implements IGameObjectTrader { constructor({ scene, x, y }: ITraderOptions) { super({ scene, x, y, + type: 'TRADER', }) this.speedPerSecond = 60 diff --git a/src/lib/game/objects/units/unit.ts b/src/lib/game/objects/units/unitObject.ts similarity index 90% rename from src/lib/game/objects/units/unit.ts rename to src/lib/game/objects/units/unitObject.ts index b394479b..a9ec8c71 100644 --- a/src/lib/game/objects/units/unit.ts +++ b/src/lib/game/objects/units/unitObject.ts @@ -7,23 +7,23 @@ import { UnitHairContainer } from '../../components/unitHairContainer' import { UnitHeadContainer } from '../../components/unitHeadContainer' import { UnitInterface } from '../../components/unitInterface' import { UnitTopContainer } from '../../components/unitTopContainer' -import type { GameScene } from '../../scenes/gameScene' import { AssetsManager } from '../../utils' -import { Flag } from '../flag' -import { GameObject } from '../gameObject' -import { Stone } from '../stone' -import { Tree } from '../tree' +import { FlagObject } from '../flagObject' +import { BaseObject } from '../baseObject' +import { StoneObject } from '../stoneObject' +import { TreeObject } from '../treeObject' import { getRandomInRange } from '$lib/random' -import type { IGameObjectUnit } from '$lib/game/types' +import type { GameObject, GameScene, IGameObjectUnit } from '$lib/game/types' interface IUnitOptions { scene: GameScene id?: string x: number y: number + type: GameObject['type'] } -export class Unit extends GameObject implements IGameObjectUnit { +export class UnitObject extends BaseObject implements IGameObjectUnit { public inventory!: Inventory public visual!: IGameObjectUnit['visual'] public userName!: IGameObjectUnit['userName'] @@ -36,8 +36,8 @@ export class Unit extends GameObject implements IGameObjectUnit { private readonly animationMovingLeft!: AnimatedSprite private readonly animationMovingRight!: AnimatedSprite - constructor({ scene, x, y, id }: IUnitOptions) { - super({ scene, x, y, id }) + constructor({ scene, x, y, id, type }: IUnitOptions) { + super({ scene, x, y, id, type }) this.initInventory() this.initVisual() @@ -51,7 +51,7 @@ export class Unit extends GameObject implements IGameObjectUnit { this.initGraphics() } - public live() { + live() { this.handleMessages() if (this.script) { @@ -101,7 +101,7 @@ export class Unit extends GameObject implements IGameObjectUnit { } public chopTree() { - if (this.target instanceof Tree && this.target.state !== 'DESTROYED') { + if (this.target instanceof TreeObject && this.target.state !== 'DESTROYED') { this.direction = 'RIGHT' this.state = 'CHOPPING' this.checkAndBreakTool('AXE') @@ -111,7 +111,7 @@ export class Unit extends GameObject implements IGameObjectUnit { } public mineStone() { - if (this.target instanceof Stone && this.target.state !== 'DESTROYED') { + if (this.target instanceof StoneObject && this.target.state !== 'DESTROYED') { this.direction = 'RIGHT' this.state = 'MINING' this.checkAndBreakTool('PICKAXE') @@ -217,7 +217,7 @@ export class Unit extends GameObject implements IGameObjectUnit { this.showToolInHand() this.handleSoundByState() - if (this.target && this.target instanceof Flag) { + if (this.target && this.target instanceof FlagObject) { this.target.visible = true } } diff --git a/src/lib/game/objects/wagon.ts b/src/lib/game/objects/wagon.ts index e4bb5c80..2fe88732 100644 --- a/src/lib/game/objects/wagon.ts +++ b/src/lib/game/objects/wagon.ts @@ -7,10 +7,9 @@ import { WagonEngineCloudsContainer } from '../components/wagonEngineCloudsConta import { WagonEngineContainer } from '../components/wagonEngineContainer' import { WagonFuelBoxContainer } from '../components/wagonFuelBoxContainer' import { WagonWheelContainer } from '../components/wagonWheelContainer' -import type { GameScene } from '../scenes/gameScene' -import { GameObject } from './gameObject' +import { BaseObject } from './baseObject' import { Mechanic } from './units' -import type { IGameObjectWagon } from '$lib/game/types' +import type { GameScene, IGameObjectWagon } from '$lib/game/types' interface IWagonOptions { scene: GameScene @@ -18,7 +17,7 @@ interface IWagonOptions { y: number } -export class Wagon extends GameObject implements IGameObjectWagon { +export class Wagon extends BaseObject implements IGameObjectWagon { public fuel!: number public visibilityArea!: IGameObjectWagon['visibilityArea'] public cargoType: IGameObjectWagon['cargoType'] @@ -30,27 +29,27 @@ export class Wagon extends GameObject implements IGameObjectWagon { public collisionArea!: IGameObjectWagon['visibilityArea'] constructor({ scene, x, y }: IWagonOptions) { - super({ scene, x, y }) + super({ scene, x, y, type: 'WAGON' }) - this.state = 'IDLE' this.speedPerSecond = 0 this.fuel = 2000 - this.updateVisibilityArea() - this.updateServerDataArea() - this.initMechanic() - this.initGraphics() + this.#updateVisibilityArea() + this.#updateServerDataArea() + + this.#initMechanic() + this.#initGraphics() } - public live() { - this.updateVisibilityArea() - this.updateServerDataArea() - this.updateCollisionArea() - this.updateMechanic() - this.consumeFuel() + live() { + this.#updateVisibilityArea() + this.#updateServerDataArea() + this.#updateCollisionArea() + this.#updateMechanic() + this.#consumeFuel() } - consumeFuel() { + #consumeFuel() { if (this.speedPerSecond <= 0) { return } @@ -70,7 +69,22 @@ export class Wagon extends GameObject implements IGameObjectWagon { this.fuel = 0 } - updateVisibilityArea() { + setCargo() { + this.cargo = new Inventory({ + id: createId(), + saveInDb: false, + objectId: this.id, + }) + void this.cargo.addOrCreateItem('WOOD', 100) + this.cargoType = 'CHEST' + } + + emptyCargo() { + this.cargo = undefined + this.cargoType = undefined + } + + #updateVisibilityArea() { const offsetX = 2560 / 2 const offsetY = 1440 / 2 @@ -82,7 +96,7 @@ export class Wagon extends GameObject implements IGameObjectWagon { } } - updateServerDataArea() { + #updateServerDataArea() { const offsetX = 2560 * 1.5 const offsetY = 1440 @@ -94,7 +108,7 @@ export class Wagon extends GameObject implements IGameObjectWagon { } } - updateCollisionArea() { + #updateCollisionArea() { const offsetX = 250 const offsetY = 180 @@ -133,7 +147,7 @@ export class Wagon extends GameObject implements IGameObjectWagon { ) } - initMechanic() { + #initMechanic() { this.mechanic = new Mechanic({ scene: this.scene, x: this.x, @@ -141,29 +155,33 @@ export class Wagon extends GameObject implements IGameObjectWagon { }) } - updateMechanic() { + #updateMechanic() { this.mechanic.live() this.mechanic.direction = 'LEFT' this.mechanic.x = this.x - 50 this.mechanic.y = this.y - 48 } - public setCargo() { - this.cargo = new Inventory({ - id: createId(), - saveInDb: false, - objectId: this.id, - }) - void this.cargo.addOrCreateItem('WOOD', 100) - this.cargoType = 'CHEST' - } + animate() { + super.animate() - public emptyCargo() { - this.cargo = undefined - this.cargoType = undefined + for (const container of this.children) { + container.visible = true + + this.#drawWheels(container) + this.#drawEngine(container) + this.#drawCargo(container) + this.#drawFuel(container) + + if (container instanceof WagonEngineCloudsContainer) { + container.animate(this.speedPerSecond) + } + } + + this.#handleSoundByState() } - private initGraphics() { + #initGraphics() { const spriteSide = Sprite.from('wagonBase1') spriteSide.anchor.set(0.5, 1) spriteSide.scale = 0.75 @@ -200,26 +218,7 @@ export class Wagon extends GameObject implements IGameObjectWagon { ) } - public animate() { - super.animate() - - for (const container of this.children) { - container.visible = true - - this.drawWheels(container) - this.drawEngine(container) - this.drawCargo(container) - this.drawFuel(container) - - if (container instanceof WagonEngineCloudsContainer) { - container.animate(this.speedPerSecond) - } - } - - this.handleSoundByState() - } - - drawWheels(container: GraphicsContainer) { + #drawWheels(container: GraphicsContainer) { if (container instanceof WagonWheelContainer) { if (container.side === 'LEFT') { container.x = -123 @@ -238,7 +237,7 @@ export class Wagon extends GameObject implements IGameObjectWagon { } } - drawEngine(container: GraphicsContainer) { + #drawEngine(container: GraphicsContainer) { if (container instanceof WagonEngineContainer) { container.x = -102 container.y = -58 @@ -247,7 +246,7 @@ export class Wagon extends GameObject implements IGameObjectWagon { } } - drawCargo(container: GraphicsContainer) { + #drawCargo(container: GraphicsContainer) { if (container instanceof WagonCargoContainer) { if (this.cargoType === 'CHEST') { container.visible = true @@ -262,7 +261,7 @@ export class Wagon extends GameObject implements IGameObjectWagon { } } - drawFuel(container: GraphicsContainer) { + #drawFuel(container: GraphicsContainer) { let initFuel = this.fuel if (container instanceof WagonFuelBoxContainer) { for (const c of container.children) { @@ -278,7 +277,7 @@ export class Wagon extends GameObject implements IGameObjectWagon { } } - handleSoundByState() { + #handleSoundByState() { if (this.state === 'MOVING') { this.scene.game.audio.playSound('WAGON_MOVING') } diff --git a/src/lib/game/objects/water.ts b/src/lib/game/objects/water.ts index 6b4c2dc5..d3cc8643 100644 --- a/src/lib/game/objects/water.ts +++ b/src/lib/game/objects/water.ts @@ -1,6 +1,5 @@ -import type { GameScene } from '../scenes/gameScene' -import { GameObject } from './gameObject' -import type { IGameObjectWater } from '$lib/game/types' +import { BaseObject } from './baseObject' +import type { GameScene, IGameObjectWater } from '$lib/game/types' interface IWaterOptions { scene: GameScene @@ -8,8 +7,8 @@ interface IWaterOptions { y: number } -export class Water extends GameObject implements IGameObjectWater { +export class Water extends BaseObject implements IGameObjectWater { constructor({ scene, x, y }: IWaterOptions) { - super({ scene, x, y }) + super({ scene, x, y, type: 'WATER' }) } } diff --git a/src/lib/game/quests/quest.ts b/src/lib/game/quests/baseQuest.ts similarity index 80% rename from src/lib/game/quests/quest.ts rename to src/lib/game/quests/baseQuest.ts index 81d0917c..0cd8fbed 100644 --- a/src/lib/game/quests/quest.ts +++ b/src/lib/game/quests/baseQuest.ts @@ -7,9 +7,9 @@ interface IQuestOptions { description: IGameQuest['description'] } -export class Quest implements IGameQuest { - public id: string - public type: IGameQuest['type'] +export class BaseQuest implements IGameQuest { + readonly #id: string + type: IGameQuest['type'] public title: IGameQuest['title'] public description: IGameQuest['description'] public tasks: IGameQuest['tasks'] @@ -18,7 +18,7 @@ export class Quest implements IGameQuest { public conditions!: IGameQuest['conditions'] constructor({ type, title, description }: IQuestOptions) { - this.id = createId() + this.#id = createId() this.type = type this.title = title this.description = description @@ -27,4 +27,8 @@ export class Quest implements IGameQuest { this.status = 'ACTIVE' this.conditions = {} } + + get id(): string { + return this.#id + } } diff --git a/src/lib/game/quests/noTradingPostQuest.ts b/src/lib/game/quests/noTradingPostQuest.ts index ff8ffc01..fe867d4c 100644 --- a/src/lib/game/quests/noTradingPostQuest.ts +++ b/src/lib/game/quests/noTradingPostQuest.ts @@ -1,6 +1,6 @@ import { createId } from '@paralleldrive/cuid2' import type { DonateWoodToVillageAction } from '../actions/donateWoodToVillageAction' -import { Quest } from './quest' +import { BaseQuest } from './baseQuest' import type { IGameQuestTaskFunc } from '$lib/game/types' interface INoTradingPostQuestOptions { @@ -10,7 +10,7 @@ interface INoTradingPostQuestOptions { taskAction1: DonateWoodToVillageAction } -export class NoTradingPostQuest extends Quest { +export class NoTradingPostQuest extends BaseQuest { constructor({ creatorId, taskUpdateFunc1, @@ -24,10 +24,10 @@ export class NoTradingPostQuest extends Quest { }) this.creatorId = creatorId - this.initTasks({ taskUpdateFunc1, taskUpdateFunc2, taskAction1 }) + this.#initTasks({ taskUpdateFunc1, taskUpdateFunc2, taskAction1 }) } - initTasks({ + #initTasks({ taskUpdateFunc1, taskUpdateFunc2, taskAction1, diff --git a/src/lib/game/quests/treesAreRunningOutQuest.ts b/src/lib/game/quests/treesAreRunningOutQuest.ts index 527901e4..0c49ba7d 100644 --- a/src/lib/game/quests/treesAreRunningOutQuest.ts +++ b/src/lib/game/quests/treesAreRunningOutQuest.ts @@ -1,6 +1,6 @@ import { createId } from '@paralleldrive/cuid2' import type { PlantTreeAction } from '../actions/plantTreeAction' -import { Quest } from './quest' +import { BaseQuest } from './baseQuest' import type { IGameQuestTaskFunc } from '$lib/game/types' interface ITreesAreRunningOutQuestOptions { @@ -9,7 +9,7 @@ interface ITreesAreRunningOutQuestOptions { taskAction1: PlantTreeAction } -export class TreesAreRunningOutQuest extends Quest { +export class TreesAreRunningOutQuest extends BaseQuest { constructor({ creatorId, taskUpdateFunc1, diff --git a/src/lib/game/scenes/gameScene.ts b/src/lib/game/scenes/baseScene.ts similarity index 91% rename from src/lib/game/scenes/gameScene.ts rename to src/lib/game/scenes/baseScene.ts index ebd33eb7..c2a95f81 100644 --- a/src/lib/game/scenes/gameScene.ts +++ b/src/lib/game/scenes/baseScene.ts @@ -1,73 +1,74 @@ import { createId } from '@paralleldrive/cuid2' import { type GameChunk, Village } from '../chunks' import { Group, Route } from '../common' -import type { Game } from '../game' -import { - Flag, - type GameObject, - type Rabbit, - Stone, - Tree, - type Wolf, -} from '../objects' -import { Player, Raider, Trader } from '../objects/units' import { ChopTreeScript } from '../scripts/chopTreeScript' import { MoveOffScreenAndSelfDestroyScript } from '../scripts/moveOffScreenAndSelfDestroyScript' import { MoveToTargetScript } from '../scripts/moveToTargetScript' -import { ActionService } from '../services/actionService' -import { EventService } from '../services/eventService' -import { TradeService } from '../services/tradeService' -import { WagonService } from '../services/wagonService' +import { WagonService } from './services/wagonService' +import { TradeService } from './services/tradeService' +import { ActionService } from './services/actionService' +import { EventService } from './services/eventService' import { getRandomInRange } from '$lib/random' import type { + Game, + GameObject, + GameScene, GetSceneResponse, - IGameChunk, - IGameChunkTheme, - IGameInventoryItem, + IGameChunk, IGameChunkTheme, IGameInventoryItem, } from '$lib/game/types' import { getDateMinusMinutes } from '$lib/date' +import { RouteService } from '$lib/game/scenes/services/routeService' interface IGameSceneOptions { game: Game } -export class GameScene { - public id: string - public game: Game - public objects: GameObject[] = [] - public group: Group - public chunks: GameChunk[] = [] - public chunkNow: GameChunk | undefined +export class BaseScene implements GameScene { + game: Game + objects: GameObject[] = [] + group: Group + chunks: GameChunk[] = [] + chunkNow: GameChunk | undefined + + actionService: ActionService + eventService: EventService + tradeService: TradeService + wagonService: WagonService + routeService: RouteService - public actionService: ActionService - public eventService: EventService - public tradeService: TradeService - public wagonService: WagonService + readonly #id: string constructor({ game }: IGameSceneOptions) { - this.id = createId() this.game = game - this.group = new Group() + this.#id = createId() + + this.group = new Group() this.actionService = new ActionService({ scene: this }) this.eventService = new EventService({ scene: this }) this.tradeService = new TradeService({ scene: this }) this.wagonService = new WagonService({ scene: this }) + this.routeService = new RouteService({ scene: this }) } - public live() { + live() { this.eventService.update() this.tradeService.update() this.wagonService.update() + this.routeService.update() this.updateObjects() this.updateChunks() this.updateChunkNow() } - public destroy() { + destroy() { this.objects = [] } + get id() { + return this.#id + } + getChunkNow(): IGameChunk | null { if (!this.chunkNow) { return null @@ -101,7 +102,7 @@ export class GameScene { group: this.group.getGroup(), wagon: this.wagonService.wagon, chunk: this.getChunkNow(), - route: this.wagonService.routeService.getRoute(), + route: this.routeService.getRoute(), warehouseItems: this.getWarehouseItems(), } } diff --git a/src/lib/game/scenes/movingScene.ts b/src/lib/game/scenes/movingScene.ts index 6046ffe8..b012e423 100644 --- a/src/lib/game/scenes/movingScene.ts +++ b/src/lib/game/scenes/movingScene.ts @@ -1,30 +1,28 @@ -import type { Game } from '../game' -import { GameScene } from './gameScene' +import { BaseScene } from './baseScene' +import type { Game } from '$lib/game/types' interface IMovingSceneOptions { game: Game } -export class MovingScene extends GameScene { +export class MovingScene extends BaseScene { constructor({ game }: IMovingSceneOptions) { - super({ - game, - }) + super({ game }) void this.init() } - public async init() { - const village = this.initStartingVillage() + async init() { + const village = this.#initStartingVillage() const wagonStartPoint = village.getWagonStopPoint() this.wagonService.initWagon(wagonStartPoint) - await this.initGroupPlayers() + await this.#initGroupPlayers() // void this.live() } - initStartingVillage() { + #initStartingVillage() { const initialOffsetX = 0 const initialOffsetY = 2000 const width = 5000 @@ -49,7 +47,7 @@ export class MovingScene extends GameScene { return village } - async initGroupPlayers() { + async #initGroupPlayers() { if (!this.group) { return } diff --git a/src/lib/game/services/actionService.ts b/src/lib/game/scenes/services/actionService.ts similarity index 95% rename from src/lib/game/services/actionService.ts rename to src/lib/game/scenes/services/actionService.ts index 315f0eaa..901ca021 100644 --- a/src/lib/game/services/actionService.ts +++ b/src/lib/game/scenes/services/actionService.ts @@ -1,18 +1,17 @@ -import type { Action } from '../actions/action' -import { Village } from '../chunks' -import { Group } from '../common' -import { Stone, Tree } from '../objects' -import type { Warehouse } from '../objects/buildings/warehouse' -import type { Player } from '../objects/units' -import type { GameScene } from '../scenes/gameScene' -import { ChopTreeScript } from '../scripts/chopTreeScript' -import { MineStoneScript } from '../scripts/mineStoneScript' -import { PlantNewTreeScript } from '../scripts/plantNewTreeScript' +import type { BaseAction } from '../../actions/baseAction' +import { Village } from '../../chunks' +import { Group } from '../../common' +import { Stone, Tree } from '../../objects' +import type { Warehouse } from '../../objects/buildings/warehouse' +import type { Player } from '../../objects/units' +import { ChopTreeScript } from '../../scripts/chopTreeScript' +import { MineStoneScript } from '../../scripts/mineStoneScript' +import { PlantNewTreeScript } from '../../scripts/plantNewTreeScript' import type { + GameScene, + GameSceneService, GameSceneType, - IGameActionResponse, - IGameSceneAction, - ItemType, + IGameActionResponse, IGameSceneAction, ItemType, } from '$lib/game/types' import { ADMIN_PLAYER_ID, @@ -90,11 +89,11 @@ export const ANSWER = { }, } -export class ActionService { - public possibleCommands!: ICommandWithAction[] - public possibleActions!: IGameSceneAction[] - public activeActions!: IGameSceneAction[] - public scene: GameScene +export class ActionService implements GameSceneService { + possibleCommands!: ICommandWithAction[] + possibleActions!: IGameSceneAction[] + activeActions!: IGameSceneAction[] + scene: GameScene constructor({ scene }: IActionServiceOptions) { this.scene = scene @@ -222,7 +221,7 @@ export class ActionService { } public async handleDynamicAction( - action: Action, + action: BaseAction, playerId: string, params: string[], ): Promise { diff --git a/src/lib/game/services/eventService.ts b/src/lib/game/scenes/services/eventService.ts similarity index 89% rename from src/lib/game/services/eventService.ts rename to src/lib/game/scenes/services/eventService.ts index 28f812ba..5289206c 100644 --- a/src/lib/game/services/eventService.ts +++ b/src/lib/game/scenes/services/eventService.ts @@ -1,34 +1,33 @@ -import type { Action } from '../actions/action' -import { Village } from '../chunks' -import { Event } from '../common' -import type { GameScene } from '../scenes/gameScene' +import type { BaseAction } from '../../actions/baseAction' +import { Village } from '../../chunks' +import { Event } from '../../common' import { PollService } from './pollService' import { QuestService } from './questService' import type { + GameScene, + GameSceneService, GameSceneType, - IGameEvent, - IGamePoll, - IGameQuest, - IGameQuestTask, + IGameEvent, IGamePoll, IGameQuest, IGameQuestTask, } from '$lib/game/types' interface IEventServiceOptions { scene: GameScene } -export class EventService { - public events: Event[] = [] - public questService: QuestService - public pollService: PollService - public scene: GameScene +export class EventService implements GameSceneService { + events: Event[] = [] + questService: QuestService + pollService: PollService + scene: GameScene constructor({ scene }: IEventServiceOptions) { this.scene = scene + this.questService = new QuestService({ scene }) this.pollService = new PollService({ scene }) } - public update() { + update() { for (const event of this.events) { const status = event.checkStatus() @@ -143,7 +142,7 @@ export class EventService { (q) => q.action?.command === command, ) if (task?.action) { - return task.action as Action + return task.action as BaseAction } } } @@ -152,7 +151,7 @@ export class EventService { public findActionByCommandInPoll(command: string) { for (const event of this.events) { if (event.poll?.action && event.poll.action.command === command) { - return event.poll?.action as Action + return event.poll?.action as BaseAction } } } @@ -190,7 +189,7 @@ export class EventService { const updateProgress1: IGameQuestTask['updateProgress'] = () => { if ( - !this.scene.wagonService.routeService.route?.flags + !this.scene.routeService.route?.flags && this.events.find((e) => e.type === 'MAIN_QUEST_STARTED') ) { return { @@ -238,7 +237,7 @@ export class EventService { this.scene.wagonService.wagon.setCargo() if (this.scene.chunkNow instanceof Village) { - this.scene.wagonService.routeService.generateAdventure( + this.scene.routeService.generateAdventure( this.scene.chunkNow, event.quest.conditions.chunks ?? 3, ) diff --git a/src/lib/game/services/pollService.ts b/src/lib/game/scenes/services/pollService.ts similarity index 80% rename from src/lib/game/services/pollService.ts rename to src/lib/game/scenes/services/pollService.ts index f66724bc..c6a5a5d8 100644 --- a/src/lib/game/services/pollService.ts +++ b/src/lib/game/scenes/services/pollService.ts @@ -1,19 +1,23 @@ -import type { Player } from '../objects/units' -import type { GameScene } from '../scenes/gameScene' -import type { IGameObjectPlayer, IGamePoll } from '$lib/game/types' +import type { Player } from '../../objects/units' +import type { + GameScene, + GameSceneService, + IGameObjectPlayer, + IGamePoll, +} from '$lib/game/types' interface IPollServiceOptions { scene: GameScene } -export class PollService { - public scene: GameScene +export class PollService implements GameSceneService { + scene: GameScene constructor({ scene }: IPollServiceOptions) { this.scene = scene } - public update() { + update() { for (const event of this.scene.eventService.events) { if (!event.poll || event.poll.status !== 'ACTIVE') { continue diff --git a/src/lib/game/services/questService.ts b/src/lib/game/scenes/services/questService.ts similarity index 91% rename from src/lib/game/services/questService.ts rename to src/lib/game/scenes/services/questService.ts index fcb003b1..ae363cd7 100644 --- a/src/lib/game/services/questService.ts +++ b/src/lib/game/scenes/services/questService.ts @@ -1,28 +1,27 @@ import { createId } from '@paralleldrive/cuid2' -import { DonateWoodToVillageAction } from '../actions/donateWoodToVillageAction' -import { PlantTreeAction } from '../actions/plantTreeAction' -import { Village } from '../chunks' -import { NoTradingPostQuest } from '../quests/noTradingPostQuest' -import { TreesAreRunningOutQuest } from '../quests/treesAreRunningOutQuest' -import type { GameScene } from '../scenes/gameScene' +import { DonateWoodToVillageAction } from '../../actions/donateWoodToVillageAction' +import { PlantTreeAction } from '../../actions/plantTreeAction' +import { Village } from '../../chunks' +import { NoTradingPostQuest } from '../../quests/noTradingPostQuest' +import { TreesAreRunningOutQuest } from '../../quests/treesAreRunningOutQuest' import type { + GameSceneService, IGameQuest, - IGameQuestTask, - IGameQuestTaskFunc, + IGameQuestTask, IGameQuestTaskFunc, } from '$lib/game/types' interface IQuestServiceOptions { scene: GameScene } -export class QuestService { - public scene: GameScene +export class QuestService implements GameSceneService { + scene: GameScene constructor({ scene }: IQuestServiceOptions) { this.scene = scene } - public update() { + update() { this.updateAndFinishActiveQuests() if (this.scene.chunkNow instanceof Village) { diff --git a/src/lib/game/services/routeService.ts b/src/lib/game/scenes/services/routeService.ts similarity index 94% rename from src/lib/game/services/routeService.ts rename to src/lib/game/scenes/services/routeService.ts index 0c62a39f..314b6905 100644 --- a/src/lib/game/services/routeService.ts +++ b/src/lib/game/scenes/services/routeService.ts @@ -1,15 +1,19 @@ -import { Forest, LakeChunk, Village } from '../chunks' -import { Route } from '../common' -import { Stone, Tree } from '../objects' -import type { GameScene } from '../scenes/gameScene' +import { Forest, LakeChunk, Village } from '../../chunks' +import { Route } from '../../common' +import { Stone, Tree } from '../../objects' import { getRandomInRange } from '$lib/random' -import type { IGameChunkTheme, IGameRoute } from '$lib/game/types' +import type { + GameScene, + GameSceneService, + IGameChunkTheme, + IGameRoute, +} from '$lib/game/types' interface IRouteServiceOptions { scene: GameScene } -export class RouteService { +export class RouteService implements GameSceneService { public route: Route | undefined public scene: GameScene @@ -17,7 +21,7 @@ export class RouteService { this.scene = scene } - public update() { + update() { if (!this.route?.flags || this.route.flags.length <= 0) { if ( this.scene.eventService.events.find( diff --git a/src/lib/game/services/tradeService.ts b/src/lib/game/scenes/services/tradeService.ts similarity index 92% rename from src/lib/game/services/tradeService.ts rename to src/lib/game/scenes/services/tradeService.ts index e87b3156..53afdf95 100644 --- a/src/lib/game/services/tradeService.ts +++ b/src/lib/game/scenes/services/tradeService.ts @@ -1,20 +1,19 @@ -import { Village } from '../chunks' -import { Poll } from '../common' -import { Flag } from '../objects' -import type { Player } from '../objects/units' -import { Trader } from '../objects/units' -import type { GameScene } from '../scenes/gameScene' -import { MoveOffScreenAndSelfDestroyScript } from '../scripts/moveOffScreenAndSelfDestroyScript' -import { MoveToTargetScript } from '../scripts/moveToTargetScript' -import { MoveToTradePostAndTradeScript } from '../scripts/moveToTradePostAndTradeScript' +import { Village } from '../../chunks' +import { Poll } from '../../common' +import { Flag } from '../../objects' +import type { Player } from '../../objects/units' +import { Trader } from '../../objects/units' +import { MoveOffScreenAndSelfDestroyScript } from '../../scripts/moveOffScreenAndSelfDestroyScript' +import { MoveToTargetScript } from '../../scripts/moveToTargetScript' +import { MoveToTradePostAndTradeScript } from '../../scripts/moveToTradePostAndTradeScript' import { getRandomInRange } from '$lib/random' -import type { ITradeOffer } from '$lib/game/types' +import type { GameScene, GameSceneService, ITradeOffer } from '$lib/game/types' interface ITradeServiceOptions { scene: GameScene } -export class TradeService { +export class TradeService implements GameSceneService { public offers: ITradeOffer[] = [] public tradeWasSuccessful: boolean public traderIsMovingWithWagon: boolean diff --git a/src/lib/game/services/wagonService.ts b/src/lib/game/scenes/services/wagonService.ts similarity index 86% rename from src/lib/game/services/wagonService.ts rename to src/lib/game/scenes/services/wagonService.ts index 7f2d6060..e58f0f4a 100644 --- a/src/lib/game/services/wagonService.ts +++ b/src/lib/game/scenes/services/wagonService.ts @@ -1,28 +1,24 @@ -import { Flag, Wagon } from '../objects' -import type { GameScene } from '../scenes/gameScene' -import { RouteService } from './routeService' +import { Flag, Wagon } from '../../objects' import { getMinusOrPlus, getRandomInRange } from '$lib/random' +import type { GameScene, GameSceneService } from '$lib/game/types' interface IWagonServiceOptions { scene: GameScene } -export class WagonService { - public wagon!: Wagon - public outFlags: Flag[] = [] - public nearFlags: Flag[] = [] - public routeService: RouteService - public scene: GameScene +export class WagonService implements GameSceneService { + wagon!: Wagon + outFlags: Flag[] = [] + nearFlags: Flag[] = [] + scene: GameScene constructor({ scene }: IWagonServiceOptions) { this.scene = scene - this.routeService = new RouteService({ scene }) } - public update() { + update() { this.updateWagon() this.updateFlags() - this.routeService.update() } public initWagon({ x, y }: { x: number, y: number }) { @@ -67,7 +63,7 @@ export class WagonService { this.wagon.state = 'IDLE' } if (this.wagon.state === 'IDLE') { - const target = this.routeService.route?.getNextFlag() + const target = this.scene.routeService.route?.getNextFlag() if (target) { this.wagon.target = target this.wagon.state = 'MOVING' @@ -82,7 +78,7 @@ export class WagonService { this.wagon.target instanceof Flag && this.wagon.target.type === 'WAGON_MOVEMENT' ) { - this.routeService.route?.removeFlag(this.wagon.target) + this.scene.routeService.route?.removeFlag(this.wagon.target) this.wagon.target = undefined this.wagon.state = 'IDLE' this.wagon.speedPerSecond = 0 diff --git a/src/lib/game/scripts/buildScript.ts b/src/lib/game/scripts/buildScript.ts index d78604f0..eb0497e9 100644 --- a/src/lib/game/scripts/buildScript.ts +++ b/src/lib/game/scripts/buildScript.ts @@ -1,10 +1,9 @@ -import type { GameObject } from '../objects' import { Script } from './script' -import type { IGameObject, IGameTask } from '$lib/game/types' +import type { GameObject, IGameTask } from '$lib/game/types' interface IBuildScriptOptions { object: GameObject - target: IGameObject + target: GameObject buildFunc: () => boolean } diff --git a/src/lib/game/scripts/chopTreeScript.ts b/src/lib/game/scripts/chopTreeScript.ts index fb95847c..56f2ebaa 100644 --- a/src/lib/game/scripts/chopTreeScript.ts +++ b/src/lib/game/scripts/chopTreeScript.ts @@ -1,10 +1,9 @@ -import type { GameObject } from '../objects' import { Script } from './script' -import type { IGameObject, IGameTask } from '$lib/game/types' +import type { GameObject, IGameTask } from '$lib/game/types' interface IPlantNewTreeScriptOptions { object: GameObject - target: IGameObject + target: GameObject chopTreeFunc: () => boolean } diff --git a/src/lib/game/scripts/mineStoneScript.ts b/src/lib/game/scripts/mineStoneScript.ts index ddd615b1..dc971dd9 100644 --- a/src/lib/game/scripts/mineStoneScript.ts +++ b/src/lib/game/scripts/mineStoneScript.ts @@ -1,10 +1,9 @@ -import type { GameObject } from '../objects' import { Script } from './script' -import type { IGameObject, IGameTask } from '$lib/game/types' +import type { GameObject, IGameTask } from '$lib/game/types' interface IMineStoneScriptOptions { object: GameObject - target: IGameObject + target: GameObject mineStoneFunc: () => boolean } diff --git a/src/lib/game/scripts/moveOffScreenAndSelfDestroyScript.ts b/src/lib/game/scripts/moveOffScreenAndSelfDestroyScript.ts index 0b90ad7c..da9ce5ea 100644 --- a/src/lib/game/scripts/moveOffScreenAndSelfDestroyScript.ts +++ b/src/lib/game/scripts/moveOffScreenAndSelfDestroyScript.ts @@ -1,10 +1,9 @@ -import type { GameObject } from '../objects' import { Script } from './script' -import type { IGameObject, IGameTask } from '$lib/game/types' +import type { GameObject, IGameTask } from '$lib/game/types' interface IMoveOffScreenAndSelfDestroyScriptOptions { object: GameObject - target: IGameObject + target: GameObject selfDestroyFunc: () => void } diff --git a/src/lib/game/scripts/moveToTargetScript.ts b/src/lib/game/scripts/moveToTargetScript.ts index 1f04ec0e..5993db15 100644 --- a/src/lib/game/scripts/moveToTargetScript.ts +++ b/src/lib/game/scripts/moveToTargetScript.ts @@ -1,10 +1,9 @@ -import type { GameObject } from '../objects' import { Script } from './script' -import type { IGameObject } from '$lib/game/types' +import type { GameObject } from '$lib/game/types' interface IMoveToRandomTargetScriptOptions { object: GameObject - target: IGameObject + target: GameObject } export class MoveToTargetScript extends Script { diff --git a/src/lib/game/scripts/moveToTradePostAndTradeScript.ts b/src/lib/game/scripts/moveToTradePostAndTradeScript.ts index ce4a5be4..f862e3e0 100644 --- a/src/lib/game/scripts/moveToTradePostAndTradeScript.ts +++ b/src/lib/game/scripts/moveToTradePostAndTradeScript.ts @@ -1,10 +1,9 @@ -import type { GameObject } from '../objects' import { Script } from './script' -import type { IGameObject, IGameTask } from '$lib/game/types' +import type { GameObject, IGameTask } from '$lib/game/types' interface IMoveToTradePostAndTradeScriptOptions { object: GameObject - target: IGameObject + target: GameObject startTradeFunc: () => void } diff --git a/src/lib/game/scripts/placeItemInWarehouseScript.ts b/src/lib/game/scripts/placeItemInWarehouseScript.ts index a7621624..29e29668 100644 --- a/src/lib/game/scripts/placeItemInWarehouseScript.ts +++ b/src/lib/game/scripts/placeItemInWarehouseScript.ts @@ -1,10 +1,9 @@ -import type { GameObject } from '../objects' import { Script } from './script' -import type { IGameObject, IGameTask } from '$lib/game/types' +import type { GameObject, IGameTask } from '$lib/game/types' interface IPlaceItemInWarehouseScriptOptions { object: GameObject - target: IGameObject + target: GameObject placeItemFunc: () => void } diff --git a/src/lib/game/scripts/plantNewTreeScript.ts b/src/lib/game/scripts/plantNewTreeScript.ts index 0b3eba5c..7996348b 100644 --- a/src/lib/game/scripts/plantNewTreeScript.ts +++ b/src/lib/game/scripts/plantNewTreeScript.ts @@ -1,10 +1,9 @@ -import type { GameObject } from '../objects' import { Script } from './script' -import type { IGameObject, IGameTask } from '$lib/game/types' +import type { GameObject, IGameTask } from '$lib/game/types' interface IPlantNewTreeScriptOptions { object: GameObject - target: IGameObject + target: GameObject plantNewTreeFunc: () => void } diff --git a/src/lib/game/scripts/script.ts b/src/lib/game/scripts/script.ts index c0d274cc..c982e432 100644 --- a/src/lib/game/scripts/script.ts +++ b/src/lib/game/scripts/script.ts @@ -1,6 +1,5 @@ import { createId } from '@paralleldrive/cuid2' -import type { GameObject } from '../objects' -import type { IGameObject, IGameScript, IGameTask } from '$lib/game/types' +import type { GameObject, IGameScript, IGameTask } from '$lib/game/types' interface IScriptOptions { object: GameObject @@ -57,7 +56,7 @@ export class Script implements IGameScript { this.object.script = undefined } - setTarget(target: IGameObject): IGameTask { + setTarget(target: GameObject): IGameTask { return { id: createId(), status: 'IDLE', diff --git a/src/lib/game/types.ts b/src/lib/game/types.ts index 5148e47d..559428b7 100644 --- a/src/lib/game/types.ts +++ b/src/lib/game/types.ts @@ -1,3 +1,52 @@ +export interface Game { + +} + +export interface GameScene { + game: Game + live: () => void +} + +export interface GameSceneService { + scene: GameScene + update: () => void +} + +export interface GameObject { + id: string + x: number + y: number + type: GameObjectType + state: IGameObjectState + direction: IGameObjectDirection + target: GameObject | undefined + health: number + speedPerSecond: number + size: number + scene: GameScene + script: IGameScript | undefined + live: () => void + animate: () => void + move: () => boolean +} + +type GameObjectType = + | 'RABBIT' + | 'WOLF' + | 'PLAYER' + | 'RAIDER' + | 'TREE' + | 'STONE' + | 'WATER' + | 'LAKE' + | 'FLAG' + | 'AREA' + | 'TRADER' + | 'VILLAGE_UNIT' + | 'MECHANIC' + | 'WAGON' + | GameObjectBuildingType + export interface TwitchAccessTokenResponse { access_token: string refresh_token: string @@ -18,6 +67,10 @@ export interface TwitchAccessToken { export interface IGameAction { command: string commandDescription: string + live: ( + player: IGameObjectPlayer, + params: string[], + ) => Promise } export interface IGameActionResponse { @@ -130,19 +183,6 @@ export interface IGameForestChunk extends IGameChunk {} export interface IGameLakeChunk extends IGameChunk {} -export interface IGameObject { - id: string - x: number - y: number - state: IGameObjectState - direction: IGameObjectDirection - entity: IGameObjectEntity - target: IGameObject | undefined - health: number - speedPerSecond: number - size: number -} - export type IGameObjectState = | 'MOVING' | 'IDLE' @@ -150,23 +190,6 @@ export type IGameObjectState = | 'CHOPPING' | 'MINING' | 'DESTROYED' -export type IGameObjectEntity = - | 'RABBIT' - | 'WOLF' - | 'PLAYER' - | 'RAIDER' - | 'TREE' - | 'STONE' - | 'WATER' - | 'LAKE' - | 'FLAG' - | 'AREA' - | 'TRADER' - | 'COURIER' - | 'FARMER' - | 'MECHANIC' - | 'WAGON' - | IGameObjectBuildingType export type IGameObjectDirection = 'LEFT' | 'RIGHT' export interface WebSocketMessage { @@ -183,10 +206,10 @@ export interface WebSocketMessage { | 'SIDE_QUEST_STARTED' | 'TRADE_STARTED' | 'IDEA_CREATED' - object?: Partial + object?: Partial } -export interface IGameObjectWagon extends IGameObject { +export interface IGameObjectWagon extends GameObject { fuel: number visibilityArea: { startX: number @@ -197,14 +220,14 @@ export interface IGameObjectWagon extends IGameObject { cargoType: 'CHEST' | undefined } -export type IGameObjectBuildingType = +export type GameObjectBuildingType = | 'CAMPFIRE' | 'WAREHOUSE' | 'WAGON_STOP' | 'STORE' | 'CONSTRUCTION_AREA' -export interface IGameObjectBuilding extends IGameObject { +export interface IGameObjectBuilding extends GameObject { inventory: IGameInventory } @@ -218,8 +241,8 @@ export interface IGameBuildingWagonStop extends IGameObjectBuilding {} export interface IGameBuildingConstructionArea extends IGameObjectBuilding {} -export interface IGameObjectFlag extends IGameObject { - type: +export interface GameObjectFlag extends GameObject { + variant: | 'MOVEMENT' | 'WAGON_MOVEMENT' | 'WAGON_NEAR_MOVEMENT' @@ -230,13 +253,13 @@ export interface IGameObjectFlag extends IGameObject { | 'TRADE_POINT' } -export interface IGameObjectWater extends IGameObject {} +export interface IGameObjectWater extends GameObject {} -export interface IGameObjectLake extends IGameObject { +export interface IGameObjectLake extends GameObject { water: IGameObjectWater[] } -export interface IGameObjectArea extends IGameObject { +export interface IGameObjectArea extends GameObject { theme: IGameChunkTheme area: { startX: number @@ -246,19 +269,19 @@ export interface IGameObjectArea extends IGameObject { } } -export interface IGameObjectTree extends IGameObject { - type: '1' | '2' | '3' | '4' | '5' - variant: IGameChunkTheme +export interface GameObjectTree extends GameObject { + variant: '1' | '2' | '3' | '4' | '5' + theme: IGameChunkTheme resource: number isReadyToChop: boolean } -export interface IGameObjectStone extends IGameObject { - type: '1' +export interface GameObjectStone extends GameObject { + variant: '1' resource: number } -export interface IGameObjectUnit extends IGameObject { +export interface IGameObjectUnit extends GameObject { userName: string coins: number inventory: IGameInventory @@ -296,9 +319,9 @@ export interface IGameObjectPlayer extends IGameObjectUnit { export interface IGameObjectRaider extends IGameObjectUnit {} -export interface IGameObjectRabbit extends IGameObject {} +export interface IGameObjectRabbit extends GameObject {} -export interface IGameObjectWolf extends IGameObject {} +export interface IGameObjectWolf extends GameObject {} export interface ITradeOffer { id: string @@ -319,7 +342,7 @@ export interface IGameScript { export interface IGameTask { id: string status: 'IDLE' | 'ACTIVE' | 'DONE' - target?: IGameObject + target?: GameObject live: () => void } diff --git a/src/lib/game/utils/index.ts b/src/lib/game/utils/index.ts deleted file mode 100644 index 6ec8b8ef..00000000 --- a/src/lib/game/utils/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export { AssetsManager } from './assetsManager' -export { AudioManager } from './audioManager' -export { WebSocketManager } from './webSocketManager' diff --git a/src/lib/game/utils/webSocketManager.ts b/src/lib/game/utils/webSocketManager.ts index 27d7a900..5ae70db6 100644 --- a/src/lib/game/utils/webSocketManager.ts +++ b/src/lib/game/utils/webSocketManager.ts @@ -1,5 +1,5 @@ -import type { Game } from '../game' import { MessageController } from '$lib/game/utils/messageController' +import type { Game } from '$lib/game/types' export abstract class WebSocketManager { public static socket: WebSocket diff --git a/src/routes/(game)/play/+page.svelte b/src/routes/(game)/play/+page.svelte index 664224e6..fbcc2beb 100644 --- a/src/routes/(game)/play/+page.svelte +++ b/src/routes/(game)/play/+page.svelte @@ -1,9 +1,9 @@ -
+
{title}
diff --git a/src/routes/(website)/+page.svelte b/src/routes/(website)/+page.svelte index 9d53ccd1..2d2ecd56 100644 --- a/src/routes/(website)/+page.svelte +++ b/src/routes/(website)/+page.svelte @@ -2,7 +2,7 @@ import { onMount } from 'svelte' import type { PageServerData } from './$types' import { TWITCH_URL } from '$lib/config' - import { Game } from '$lib/game/game' + import { BaseGame } from '$lib/game/baseGame' import { ruWordWithEndings } from '$lib/locale' export let data: PageServerData @@ -14,7 +14,7 @@ 'профилей', ]) - const game = new Game() + const game = new BaseGame() let gameElement: HTMLElement let isGameReady = false let isGameElementActive = false