From 0fac751eab2b100b3c8e40e73093f3a026070868 Mon Sep 17 00:00:00 2001 From: Nick Kosarev Date: Tue, 16 Apr 2024 14:15:01 +0200 Subject: [PATCH] types rework --- ...ency-cruiser.js => .dependency-cruiser.cjs | 8 +- apps/api/src/game/common/event.ts | 7 +- apps/api/src/game/common/group.ts | 17 ++- apps/api/src/game/common/inventory.ts | 11 +- apps/api/src/game/common/raid.ts | 19 ++- apps/api/src/game/common/skill.ts | 19 +-- apps/api/src/game/objects/flag.ts | 18 ++- apps/api/src/game/objects/gameObject.ts | 29 ++-- apps/api/src/game/objects/index.ts | 1 + apps/api/src/game/objects/player.ts | 19 +-- apps/api/src/game/objects/rabbit.ts | 11 +- apps/api/src/game/objects/raider.ts | 8 +- apps/api/src/game/objects/stone.ts | 41 +----- apps/api/src/game/objects/tree.ts | 40 +----- apps/api/src/game/objects/wolf.ts | 11 +- apps/api/src/game/scenes/gameScene.ts | 4 +- apps/api/src/game/scenes/villageScene.ts | 16 ++- .../client/src/components/groupPlayerCard.tsx | 8 +- apps/client/src/components/groupPlayers.tsx | 4 +- .../src/components/player-full-body.tsx | 4 +- apps/client/src/components/player-skill.tsx | 6 +- apps/client/src/components/tool.tsx | 4 +- apps/client/src/components/topPlayerCard.tsx | 7 +- .../src/game/components/graphicsContainer.ts | 8 +- .../src/game/components/playerInterface.ts | 6 +- apps/client/src/game/game.ts | 78 ++++++----- apps/client/src/game/objects/gameContainer.ts | 15 -- .../src/game/objects/gameObjectContainer.ts | 33 +++++ apps/client/src/game/objects/index.ts | 2 +- apps/client/src/game/objects/player.ts | 34 ++--- apps/client/src/game/objects/rabbit.ts | 25 ++-- apps/client/src/game/objects/raider.ts | 26 ++-- apps/client/src/game/objects/stone.ts | 32 ++--- apps/client/src/game/objects/tree.ts | 34 ++--- apps/client/src/game/objects/wolf.ts | 25 ++-- apps/client/src/store/players.ts | 6 +- apps/client/src/store/rabbits.ts | 6 +- apps/client/src/store/stones.ts | 6 +- apps/client/src/store/trees.ts | 6 +- apps/client/src/store/wolfs.ts | 6 +- biome.json | 2 +- packages/api-sdk/src/lib/client.ts | 4 +- packages/api-sdk/src/lib/types.ts | 132 +++++------------- 43 files changed, 367 insertions(+), 431 deletions(-) rename .dependency-cruiser.js => .dependency-cruiser.cjs (97%) delete mode 100644 apps/client/src/game/objects/gameContainer.ts create mode 100644 apps/client/src/game/objects/gameObjectContainer.ts diff --git a/.dependency-cruiser.js b/.dependency-cruiser.cjs similarity index 97% rename from .dependency-cruiser.js rename to .dependency-cruiser.cjs index e0c16c43..0cc7caf8 100644 --- a/.dependency-cruiser.js +++ b/.dependency-cruiser.cjs @@ -327,11 +327,7 @@ module.exports = { */ // extensions: [".js", ".jsx", ".ts", ".tsx", ".d.ts"], /* What to consider a 'main' field in package.json */ - - // if you migrate to ESM (or are in an ESM environment already) you will want to - // have "module" in the list of mainFields, like so: - // mainFields: ["module", "main", "types", "typings"], - mainFields: ["main", "types", "typings"], + mainFields: ["module", "main", "types", "typings"], /* A list of alias fields in package.jsons See [this specification](https://github.com/defunctzombie/package-browser-field-spec) and @@ -390,4 +386,4 @@ module.exports = { } } }; -// generated: dependency-cruiser@16.3.0 on 2024-04-08T09:50:19.700Z +// generated: dependency-cruiser@16.3.1 on 2024-04-16T10:36:05.632Z diff --git a/apps/api/src/game/common/event.ts b/apps/api/src/game/common/event.ts index ba247430..5ec6b1d1 100644 --- a/apps/api/src/game/common/event.ts +++ b/apps/api/src/game/common/event.ts @@ -2,22 +2,23 @@ import { createId } from "@paralleldrive/cuid2"; import { type EventStatus, type EventType, + type IGameEvent, getDatePlusSeconds, } from "../../../../../packages/api-sdk/src"; -interface EventOptions { +interface IEventOptions { type: EventType; secondsToEnd: number; } -export class Event { +export class Event implements IGameEvent { public id: string; public type: EventType; public status: EventStatus; public endsAt: Date; public deletesAt: Date; - constructor({ type, secondsToEnd }: EventOptions) { + constructor({ type, secondsToEnd }: IEventOptions) { this.id = createId(); this.type = type; this.endsAt = getDatePlusSeconds(secondsToEnd); diff --git a/apps/api/src/game/common/group.ts b/apps/api/src/game/common/group.ts index 15e36e1f..d8c076ad 100644 --- a/apps/api/src/game/common/group.ts +++ b/apps/api/src/game/common/group.ts @@ -1,23 +1,28 @@ import { createId } from "@paralleldrive/cuid2"; import type { - GameGroup, GameSceneType, - Player, + IGameGroup, + IGameObjectPlayer, } from "../../../../../packages/api-sdk/src"; -export class Group implements GameGroup { +interface IGroupOptions { + creator: IGameObjectPlayer; + target: GameSceneType; +} + +export class Group implements IGameGroup { id: string; - players: Player[] = []; + players: IGameObjectPlayer[] = []; target: GameSceneType; - constructor(creator: Player, target: GameSceneType) { + constructor({ creator, target }: IGroupOptions) { this.id = createId(); this.join(creator); this.target = target; } - join(player: Player): boolean { + join(player: IGameObjectPlayer): boolean { const check = this.findPlayer(player.id); if (check) { return false; diff --git a/apps/api/src/game/common/inventory.ts b/apps/api/src/game/common/inventory.ts index 298c40ec..6e083076 100644 --- a/apps/api/src/game/common/inventory.ts +++ b/apps/api/src/game/common/inventory.ts @@ -1,17 +1,22 @@ import { createId } from "@paralleldrive/cuid2"; import type { - Inventory as IInventory, + IGameInventory, InventoryItem, ItemType, } from "../../../../../packages/api-sdk/src"; import { db } from "../../db/db.client"; -export class Inventory implements IInventory { +interface IInventoryOptions { + objectId: string; + id: string; +} + +export class Inventory implements IGameInventory { public id: string; public objectId: string; public items: InventoryItem[] = []; - constructor(objectId: string, id: string) { + constructor({ id, objectId }: IInventoryOptions) { this.id = id; this.objectId = objectId; } diff --git a/apps/api/src/game/common/raid.ts b/apps/api/src/game/common/raid.ts index e5904ae4..66ebf7c8 100644 --- a/apps/api/src/game/common/raid.ts +++ b/apps/api/src/game/common/raid.ts @@ -1,13 +1,20 @@ -import { getDatePlusMinutes } from "../../../../../packages/api-sdk/src"; -import type { Flag } from "../objects"; -import { Raider } from "../objects/raider"; +import { + type IGameObjectFlag, + type IGameRaid, + getDatePlusMinutes, +} from "../../../../../packages/api-sdk/src"; +import { Raider } from "../objects"; -export class Raid { +interface IRaidOptions { + raidersCount: number; +} + +export class Raid implements IGameRaid { public raiders: Raider[] = []; public raidEndsAt!: Date; public raidDeletesAt!: Date; - constructor(raidersCount: number) { + constructor({ raidersCount }: IRaidOptions) { this.init(raidersCount); } @@ -25,7 +32,7 @@ export class Raid { return raiders; } - moveAllRaidersBackToCamp(flag: Flag) { + moveAllRaidersBackToCamp(flag: IGameObjectFlag) { for (const raider of this.raiders) { raider.target = flag; raider.state = "MOVING"; diff --git a/apps/api/src/game/common/skill.ts b/apps/api/src/game/common/skill.ts index e7036dd2..b251f6d9 100644 --- a/apps/api/src/game/common/skill.ts +++ b/apps/api/src/game/common/skill.ts @@ -1,20 +1,21 @@ import { createId } from "@paralleldrive/cuid2"; -import type { - Skill as ISkill, - SkillType, -} from "../../../../../packages/api-sdk/src"; +import type { IGameSkill } from "../../../../../packages/api-sdk/src"; import { db } from "../../db/db.client"; -export class Skill implements ISkill { +interface ISkillOptions { + id: string; +} + +export class Skill implements IGameSkill { public id: string; public objectId: string | null = null; - public type: SkillType | null = null; + public type!: IGameSkill["type"]; public lvl = 0; public xp = 0; public xpNextLvl = 0; - constructor(id: string) { + constructor({ id }: ISkillOptions) { this.id = id; } @@ -58,7 +59,7 @@ export class Skill implements ISkill { } this.objectId = skill.objectId; - this.type = skill.type as SkillType; + this.type = skill.type as IGameSkill["type"]; this.lvl = skill.lvl; this.xp = skill.xp; this.xpNextLvl = skill.xpNextLvl; @@ -70,7 +71,7 @@ export class Skill implements ISkill { }); } - public static createInDB(objectId: string, type: SkillType) { + public static createInDB(objectId: string, type: IGameSkill["type"]) { return db.skill.create({ data: { id: createId(), diff --git a/apps/api/src/game/objects/flag.ts b/apps/api/src/game/objects/flag.ts index 6002eb31..1db1f22e 100644 --- a/apps/api/src/game/objects/flag.ts +++ b/apps/api/src/game/objects/flag.ts @@ -1,18 +1,28 @@ import { createId } from "@paralleldrive/cuid2"; -import { getRandomInRange } from "../../../../../packages/api-sdk/src"; +import { + type IGameObjectFlag, + getRandomInRange, +} from "../../../../../packages/api-sdk/src"; import { MAX_X, MAX_Y, MIN_X, MIN_Y } from "../../config"; import { GameObject } from "./gameObject"; -export class Flag extends GameObject { +interface IFlagOptions { + x?: number; + y?: number; + id?: string; + isOnScreen?: boolean; +} + +export class Flag extends GameObject implements IGameObjectFlag { public readonly entity = "FLAG"; public isOnScreen = true; - constructor(x?: number, y?: number, id?: string, isOnScreen?: boolean) { + constructor({ x, y, id, isOnScreen }: IFlagOptions) { const finalId = id ?? createId(); const finalX = x ?? getRandomInRange(MIN_X, MAX_X); const finalY = y ?? getRandomInRange(MIN_Y, MAX_Y); - super(finalId, finalX, finalY); + super({ id: finalId, x: finalX, y: finalY }); this.isOnScreen = isOnScreen ?? true; } diff --git a/apps/api/src/game/objects/gameObject.ts b/apps/api/src/game/objects/gameObject.ts index ff2f6d58..d38c5df0 100644 --- a/apps/api/src/game/objects/gameObject.ts +++ b/apps/api/src/game/objects/gameObject.ts @@ -1,25 +1,32 @@ -import type { - GameObjectEntity, - GameObjectTree, - GameObject as IGameObject, +import { + type IGameObject, + type IGameObjectDirection, + type IGameObjectEntity, + type IGameObjectState, + getRandomInRange, } from "../../../../../packages/api-sdk/src"; -import { getRandomInRange } from "../../../../../packages/api-sdk/src"; import { MAX_X, MAX_Y, MIN_X, MIN_Y } from "../../config"; import { sendMessage } from "../../websocket/websocket.server"; +interface IGameObjectOptions { + id: string; + x: number; + y: number; +} + export class GameObject implements IGameObject { public id: string; public x: number; public y: number; - public entity: GameObjectEntity; public health = 100; - public direction: IGameObject["direction"] = "RIGHT"; - public state: IGameObject["state"] = "IDLE"; + public entity: IGameObjectEntity; + public direction: IGameObjectDirection = "RIGHT"; + public state: IGameObjectState = "IDLE"; - public target: GameObject | GameObjectTree | undefined; + public target: IGameObject | undefined; - constructor(id: string, x: number, y: number) { + constructor({ id, x, y }: IGameObjectOptions) { this.id = id; this.x = x; this.y = y; @@ -101,7 +108,7 @@ export class GameObject implements IGameObject { return Math.abs(this.target.y - this.y); } - public setTarget(target: GameObject | GameObjectTree) { + public setTarget(target: IGameObject) { this.target = target; this.state = "MOVING"; } diff --git a/apps/api/src/game/objects/index.ts b/apps/api/src/game/objects/index.ts index 2486ccc9..a858ea87 100644 --- a/apps/api/src/game/objects/index.ts +++ b/apps/api/src/game/objects/index.ts @@ -5,3 +5,4 @@ export { Wolf } from "./wolf"; export { Tree } from "./tree"; export { Stone } from "./stone"; export { Flag } from "./flag"; +export { Raider } from "./raider"; diff --git a/apps/api/src/game/objects/player.ts b/apps/api/src/game/objects/player.ts index a09c137a..c2e92a3e 100644 --- a/apps/api/src/game/objects/player.ts +++ b/apps/api/src/game/objects/player.ts @@ -1,9 +1,9 @@ import { createId } from "@paralleldrive/cuid2"; -import type { - GameObjectPlayer, - ItemType, +import { + type IGameObjectPlayer, + type ItemType, + getRandomInRange, } from "../../../../../packages/api-sdk/src"; -import { getRandomInRange } from "../../../../../packages/api-sdk/src"; import { MAX_X, MAX_Y, MIN_X, MIN_Y } from "../../config"; import { db } from "../../db/db.client"; import { Inventory, Skill } from "../common"; @@ -11,7 +11,7 @@ import { GameObject } from "./gameObject"; import { Stone } from "./stone"; import { Tree } from "./tree"; -export class Player extends GameObject implements GameObjectPlayer { +export class Player extends GameObject implements IGameObjectPlayer { public readonly entity = "PLAYER"; public coins = 0; public reputation = 0; @@ -30,7 +30,7 @@ export class Player extends GameObject implements GameObjectPlayer { const x = getRandomInRange(MIN_X, MAX_X); const y = getRandomInRange(MIN_Y, MAX_Y); - super(objectId, x, y); + super({ id: objectId, x, y }); console.log(`Creating player ${objectId}!`); } @@ -299,7 +299,10 @@ export class Player extends GameObject implements GameObjectPlayer { return; } - const inventory = new Inventory(this.id, this.inventoryId); + const inventory = new Inventory({ + objectId: this.id, + id: this.inventoryId, + }); await inventory.init(); this.inventory = inventory; } @@ -308,7 +311,7 @@ export class Player extends GameObject implements GameObjectPlayer { this.skills = []; const skills = await Skill.findAllInDB(this.id); for (const skill of skills) { - const instance = new Skill(skill.id); + const instance = new Skill({ id: skill.id }); await instance.init(); this.skills.push(instance); } diff --git a/apps/api/src/game/objects/rabbit.ts b/apps/api/src/game/objects/rabbit.ts index 9d241868..933c9ed7 100644 --- a/apps/api/src/game/objects/rabbit.ts +++ b/apps/api/src/game/objects/rabbit.ts @@ -1,9 +1,12 @@ import { createId } from "@paralleldrive/cuid2"; -import { getRandomInRange } from "../../../../../packages/api-sdk/src"; +import { + type IGameObjectRabbit, + getRandomInRange, +} from "../../../../../packages/api-sdk/src"; import { MAX_X, MAX_Y, MIN_X, MIN_Y } from "../../config"; import { GameObject } from "./gameObject"; -export class Rabbit extends GameObject { +export class Rabbit extends GameObject implements IGameObjectRabbit { public readonly entity = "RABBIT"; constructor() { @@ -11,9 +14,7 @@ export class Rabbit extends GameObject { const x = getRandomInRange(MIN_X, MAX_X); const y = getRandomInRange(MIN_Y, MAX_Y); - super(id, x, y); - - console.log("Creating new rabbit!"); + super({ id, x, y }); } live() { diff --git a/apps/api/src/game/objects/raider.ts b/apps/api/src/game/objects/raider.ts index 4134211b..3345659a 100644 --- a/apps/api/src/game/objects/raider.ts +++ b/apps/api/src/game/objects/raider.ts @@ -1,6 +1,6 @@ import { createId } from "@paralleldrive/cuid2"; import { - type GameObjectRaider, + type IGameObjectRaider, getRandomInRange, } from "../../../../../packages/api-sdk/src"; import { @@ -11,7 +11,7 @@ import { } from "../../config"; import { GameObject } from "./gameObject"; -export class Raider extends GameObject implements GameObjectRaider { +export class Raider extends GameObject implements IGameObjectRaider { public readonly entity = "RAIDER"; public userName = "рейдер"; public colorIndex = 0; @@ -22,9 +22,7 @@ export class Raider extends GameObject implements GameObjectRaider { const finalX = getRandomInRange(RAIDER_CAMP_MIN_X, RAIDER_CAMP_MAX_X); const finalY = getRandomInRange(RAIDER_CAMP_MIN_Y, RAIDER_CAMP_MAX_Y); - super(objectId, finalX, finalY); - - console.log(`Creating raider ${objectId}!`); + super({ id: objectId, x: finalX, y: finalY }); } live() { diff --git a/apps/api/src/game/objects/stone.ts b/apps/api/src/game/objects/stone.ts index 40346704..191d164f 100644 --- a/apps/api/src/game/objects/stone.ts +++ b/apps/api/src/game/objects/stone.ts @@ -1,13 +1,14 @@ import { createId } from "@paralleldrive/cuid2"; -import type { GameObjectStone } from "../../../../../packages/api-sdk/src"; -import { getRandomInRange } from "../../../../../packages/api-sdk/src"; +import { + type IGameObjectStone, + getRandomInRange, +} from "../../../../../packages/api-sdk/src"; import { MAX_X, MAX_Y, MIN_X, MIN_Y } from "../../config"; -import { db } from "../../db/db.client"; import { GameObject } from "./gameObject"; -export class Stone extends GameObject implements GameObjectStone { +export class Stone extends GameObject implements IGameObjectStone { public readonly entity = "STONE"; - public type: GameObjectStone["type"] = "1"; + public type: IGameObjectStone["type"] = "1"; public resource = 0; public size = 100; public health = 100; @@ -19,12 +20,10 @@ export class Stone extends GameObject implements GameObjectStone { const x = getRandomInRange(MIN_X, MAX_X); const y = getRandomInRange(MIN_Y, MAX_Y); - super(objectId, x, y); + super({ id: objectId, x, y }); this.state = "IDLE"; this.resource = getRandomInRange(1, 5); - - console.log(`Created stone ${objectId}!`); } live() { @@ -73,30 +72,4 @@ export class Stone extends GameObject implements GameObjectStone { this.state = "DESTROYED"; this.handleChange(); } - - public async readFromDB() { - const stone = await db.stone.findUnique({ where: { id: this.id } }); - if (!stone) { - return; - } - - this.x = stone.x; - this.y = stone.y; - this.type = stone.type as GameObjectStone["type"]; - this.size = stone.size; - this.resource = stone.resource; - } - - public async updateInDB() { - await db.stone.update({ - where: { id: this.id }, - data: { - x: this.x, - y: this.y, - size: this.size, - type: this.type, - resource: this.resource, - }, - }); - } } diff --git a/apps/api/src/game/objects/tree.ts b/apps/api/src/game/objects/tree.ts index 1c72a021..3885dcb5 100644 --- a/apps/api/src/game/objects/tree.ts +++ b/apps/api/src/game/objects/tree.ts @@ -1,16 +1,14 @@ import { createId } from "@paralleldrive/cuid2"; import { - type GameObjectTree, - type GameObjectTreeType, + type IGameObjectTree, getRandomInRange, } from "../../../../../packages/api-sdk/src"; import { MAX_X, MAX_Y, MIN_X, MIN_Y } from "../../config"; -import { db } from "../../db/db.client"; import { GameObject } from "./gameObject"; -export class Tree extends GameObject implements GameObjectTree { +export class Tree extends GameObject implements IGameObjectTree { public readonly entity = "TREE"; - public type: GameObjectTree["type"] = "1"; + public type: IGameObjectTree["type"] = "1"; public resource = 0; public size = 100; public health = 100; @@ -23,7 +21,7 @@ export class Tree extends GameObject implements GameObjectTree { const x = getRandomInRange(MIN_X, MAX_X); const y = getRandomInRange(MIN_Y, MAX_Y); - super(objectId, x, y); + super({ id: objectId, x, y }); this.state = "IDLE"; this.resource = getRandomInRange(1, 5); @@ -92,35 +90,9 @@ export class Tree extends GameObject implements GameObjectTree { this.handleChange(); } - getNewTreeType(): GameObjectTreeType { - const types: GameObjectTreeType[] = ["1", "2", "3"]; + getNewTreeType(): IGameObjectTree["type"] { + const types: IGameObjectTree["type"][] = ["1", "2", "3"]; const index = getRandomInRange(0, types.length - 1); return types[index]; } - - public async readFromDB() { - const tree = await db.tree.findUnique({ where: { id: this.id } }); - if (!tree) { - return; - } - - this.x = tree.x; - this.y = tree.y; - this.type = tree.type as GameObjectTree["type"]; - this.size = tree.size; - this.resource = tree.resource; - } - - public async updateInDB() { - await db.tree.update({ - where: { id: this.id }, - data: { - x: this.x, - y: this.y, - size: this.size, - type: this.type, - resource: this.resource, - }, - }); - } } diff --git a/apps/api/src/game/objects/wolf.ts b/apps/api/src/game/objects/wolf.ts index b82f56a8..b66cf7ca 100644 --- a/apps/api/src/game/objects/wolf.ts +++ b/apps/api/src/game/objects/wolf.ts @@ -1,9 +1,12 @@ import { createId } from "@paralleldrive/cuid2"; -import { getRandomInRange } from "../../../../../packages/api-sdk/src"; +import { + type IGameObjectWolf, + getRandomInRange, +} from "../../../../../packages/api-sdk/src"; import { MAX_X, MAX_Y, MIN_X, MIN_Y } from "../../config"; import { GameObject } from "./gameObject"; -export class Wolf extends GameObject { +export class Wolf extends GameObject implements IGameObjectWolf { public readonly entity = "WOLF"; constructor() { @@ -11,9 +14,7 @@ export class Wolf extends GameObject { const x = getRandomInRange(MIN_X, MAX_X); const y = getRandomInRange(MIN_Y, MAX_Y); - super(id, x, y); - - console.log("Creating new wolf!"); + super({ id, x, y }); } live() { diff --git a/apps/api/src/game/scenes/gameScene.ts b/apps/api/src/game/scenes/gameScene.ts index a55a6f1c..2203db77 100644 --- a/apps/api/src/game/scenes/gameScene.ts +++ b/apps/api/src/game/scenes/gameScene.ts @@ -391,7 +391,7 @@ export class GameScene { }; } - this.group = new Group(player, target); + this.group = new Group({ creator: player, target }); this.initEvent("FORMING_GROUP", 60); sendMessage("GROUP_FORM_STARTED"); @@ -470,7 +470,7 @@ export class GameScene { } public startRaidAction(raidersCount = 0) { - const raid = new Raid(raidersCount); + const raid = new Raid({ raidersCount }); this.raids.push(raid); sendMessage("RAID_STARTED"); return { diff --git a/apps/api/src/game/scenes/villageScene.ts b/apps/api/src/game/scenes/villageScene.ts index 19f93217..ff0c2f8f 100644 --- a/apps/api/src/game/scenes/villageScene.ts +++ b/apps/api/src/game/scenes/villageScene.ts @@ -57,11 +57,21 @@ export class VillageScene extends GameScene { } private initFlags(count: number) { - const outsideFlag = new Flag(-100, 600, "outside", false); - const outsideRaidersCampFlag = new Flag(4000, 650, "raiders-camp", false); + const outsideFlag = new Flag({ + x: -100, + y: 600, + id: "outside", + isOnScreen: false, + }); + const outsideRaidersCampFlag = new Flag({ + x: 4000, + y: 650, + id: "raiders-camp", + isOnScreen: false, + }); this.objects.push(outsideFlag, outsideRaidersCampFlag); for (let i = 0; i < count; i++) { - this.objects.push(new Flag()); + this.objects.push(new Flag({})); } } } diff --git a/apps/client/src/components/groupPlayerCard.tsx b/apps/client/src/components/groupPlayerCard.tsx index db89fd4d..45314aa4 100644 --- a/apps/client/src/components/groupPlayerCard.tsx +++ b/apps/client/src/components/groupPlayerCard.tsx @@ -1,15 +1,11 @@ -import type { Player } from "../../../../packages/api-sdk/src"; +import type { IGameObjectPlayer } from "../../../../packages/api-sdk/src"; import { PlayerFullBody } from "./player-full-body"; export const GroupPlayerCard = ({ player, }: { - player: Player | null | undefined; + player: IGameObjectPlayer; }) => { - if (!player) { - return null; - } - return (
{ +}: { group: IGameGroup | undefined }) => { if (!group) { return null; } diff --git a/apps/client/src/components/player-full-body.tsx b/apps/client/src/components/player-full-body.tsx index 8e5a0c57..2d5962f0 100644 --- a/apps/client/src/components/player-full-body.tsx +++ b/apps/client/src/components/player-full-body.tsx @@ -1,7 +1,7 @@ -import type { Player } from "packages/api-sdk/src"; +import type { IGameObjectPlayer } from "packages/api-sdk/src"; import { PlayerTopBlock } from "./player-top"; -export const PlayerFullBody = ({ player }: { player: Player }) => { +export const PlayerFullBody = ({ player }: { player: IGameObjectPlayer }) => { const size = 100; const height = (size * 64) / 100; diff --git a/apps/client/src/components/player-skill.tsx b/apps/client/src/components/player-skill.tsx index 6d9f60ba..84262e2d 100644 --- a/apps/client/src/components/player-skill.tsx +++ b/apps/client/src/components/player-skill.tsx @@ -1,10 +1,10 @@ -import type { Skill, SkillType } from "../../../../packages/api-sdk/src"; +import type { IGameSkill } from "../../../../packages/api-sdk/src"; export const PlayerSkillBlock = ({ skill, isVisible, }: { - skill: Skill | undefined; + skill: IGameSkill | undefined; isVisible: boolean; }) => { if (!skill) { @@ -32,7 +32,7 @@ export const PlayerSkillBlock = ({ ); }; -function getSkillTypeDescription(type: SkillType | null) { +function getSkillTypeDescription(type: IGameSkill["type"] | null) { if (type === "WOODSMAN") return "лесоруб"; if (type === "MINER") return "шахтер"; } diff --git a/apps/client/src/components/tool.tsx b/apps/client/src/components/tool.tsx index fe7b34f9..0d3565fb 100644 --- a/apps/client/src/components/tool.tsx +++ b/apps/client/src/components/tool.tsx @@ -1,11 +1,11 @@ import { Howl } from "howler"; import { useEffect, useMemo } from "react"; -import type { GameObjectPlayer } from "../../../../packages/api-sdk/src"; +import type { IGameObjectPlayer } from "../../../../packages/api-sdk/src"; export const ToolBlock = ({ object, }: { - object: GameObjectPlayer; + object: IGameObjectPlayer; }) => { const axe = object.inventory?.items.find((item) => item.type === "AXE"); const pickaxe = object.inventory?.items.find( diff --git a/apps/client/src/components/topPlayerCard.tsx b/apps/client/src/components/topPlayerCard.tsx index 2ddbe1a7..d676221f 100644 --- a/apps/client/src/components/topPlayerCard.tsx +++ b/apps/client/src/components/topPlayerCard.tsx @@ -1,4 +1,7 @@ -import type { Player, PlayerTitle } from "../../../../packages/api-sdk/src"; +import type { + IGameObjectPlayer, + PlayerTitle, +} from "../../../../packages/api-sdk/src"; import { PlayerFullBody } from "./player-full-body"; export const TopPlayerCard = ({ @@ -6,7 +9,7 @@ export const TopPlayerCard = ({ title, points, }: { - player: Player | null | undefined; + player: IGameObjectPlayer | undefined; title: PlayerTitle; points: string; }) => { diff --git a/apps/client/src/game/components/graphicsContainer.ts b/apps/client/src/game/components/graphicsContainer.ts index a237aea0..c142bb49 100644 --- a/apps/client/src/game/components/graphicsContainer.ts +++ b/apps/client/src/game/components/graphicsContainer.ts @@ -1,5 +1,5 @@ import { ColorMatrixFilter, Container, Sprite } from "pixi.js"; -import type { GameObjectDirection } from "../../../../../packages/api-sdk/src"; +import type { IGameObjectDirection } from "../../../../../packages/api-sdk/src"; type GraphicsContainerType = | "INTERFACE" @@ -17,9 +17,9 @@ type GraphicsContainerType = export class GraphicsContainer extends Container { public type: GraphicsContainerType; - public direction: GameObjectDirection = "RIGHT"; + public direction: IGameObjectDirection = "RIGHT"; - constructor(type: GraphicsContainerType, direction?: GameObjectDirection) { + constructor(type: GraphicsContainerType, direction?: IGameObjectDirection) { super(); this.type = type; @@ -30,7 +30,7 @@ export class GraphicsContainer extends Container { static createWithSprite( spriteIndex: string, - direction: GameObjectDirection, + direction: IGameObjectDirection, type: GraphicsContainerType, ) { const sprite = Sprite.from(spriteIndex); diff --git a/apps/client/src/game/components/playerInterface.ts b/apps/client/src/game/components/playerInterface.ts index 2d55d746..a0361ac9 100644 --- a/apps/client/src/game/components/playerInterface.ts +++ b/apps/client/src/game/components/playerInterface.ts @@ -1,10 +1,10 @@ import { Graphics, Sprite, Text } from "pixi.js"; -import type { Player } from "../objects"; +import type { IGameObjectPlayer } from "../../../../../packages/api-sdk/src"; import { GraphicsContainer } from "./graphicsContainer"; export class PlayerInterface extends GraphicsContainer { public children: GraphicsContainer[] = []; - public player: Player; + public player: IGameObjectPlayer; public coins = 0; public wood = 0; @@ -21,7 +21,7 @@ export class PlayerInterface extends GraphicsContainer { public havePickaxe = false; public showPickaxe = false; - constructor(player: Player) { + constructor(player: IGameObjectPlayer) { super("INTERFACE"); this.x = -15; diff --git a/apps/client/src/game/game.ts b/apps/client/src/game/game.ts index d3d41e49..322c9d17 100644 --- a/apps/client/src/game/game.ts +++ b/apps/client/src/game/game.ts @@ -1,15 +1,17 @@ import { Container } from "pixi.js"; import type { - GameObject, - GameObjectPlayer, - GameObjectRaider, - GameObjectStone, - GameObjectTree, + IGameObject, + IGameObjectPlayer, + IGameObjectRabbit, + IGameObjectRaider, + IGameObjectStone, + IGameObjectTree, + IGameObjectWolf, WebSocketMessage, } from "../../../../packages/api-sdk/src"; import { addBackground } from "./addBackground"; import { - type GameContainer, + type GameObjectContainer, Player, Rabbit, Raider, @@ -30,7 +32,7 @@ export interface GameOptions { } export class Game extends Container { - public children: GameContainer[] = []; + public children: GameObjectContainer[] = []; public audio: AudioManager; public scene: SceneManager; @@ -81,72 +83,72 @@ export class Game extends Container { return this.children.find((obj) => obj.id === id); } - initTree(object: GameObjectTree) { - const tree = new Tree(this, object); + initTree(object: IGameObjectTree) { + const tree = new Tree({ game: this, object }); this.addChild(tree); } - updateTree(object: GameObjectTree) { + updateTree(object: IGameObjectTree) { const tree = this.findObject(object.id); if (tree instanceof Tree) { tree.update(object); } } - initStone(object: GameObjectStone) { - const stone = new Stone(this, object); + initStone(object: IGameObjectStone) { + const stone = new Stone({ game: this, object }); this.addChild(stone); } - updateStone(object: GameObjectStone) { + updateStone(object: IGameObjectStone) { const stone = this.findObject(object.id); if (stone instanceof Stone) { stone.update(object); } } - initPlayer(object: GameObjectPlayer) { - const player = new Player(this, object); + initPlayer(object: IGameObjectPlayer) { + const player = new Player({ game: this, object }); this.addChild(player); } - updatePlayer(object: GameObjectPlayer) { + updatePlayer(object: IGameObjectPlayer) { const player = this.findObject(object.id); if (player instanceof Player) { player.update(object); } } - initRaider(object: GameObjectRaider) { - const raider = new Raider(this, object); + initRaider(object: IGameObjectRaider) { + const raider = new Raider({ game: this, object }); this.addChild(raider); } - updateRaider(object: GameObjectRaider) { + updateRaider(object: IGameObjectRaider) { const raider = this.findObject(object.id); if (raider instanceof Raider) { raider.update(object); } } - initRabbit(object: GameObject) { - const rabbit = new Rabbit(this, object); + initRabbit(object: IGameObjectRabbit) { + const rabbit = new Rabbit({ game: this, object }); this.addChild(rabbit); } - updateRabbit(object: GameObject) { + updateRabbit(object: IGameObjectRabbit) { const rabbit = this.findObject(object.id); if (rabbit instanceof Rabbit) { rabbit.update(object); } } - initWolf(object: GameObject) { - const wolf = new Wolf(this, object); + initWolf(object: IGameObjectWolf) { + const wolf = new Wolf({ game: this, object }); this.addChild(wolf); } - updateWolf(object: GameObject) { + updateWolf(object: IGameObjectWolf) { const wolf = this.findObject(object.id); if (wolf instanceof Wolf) { wolf.update(object); @@ -168,58 +170,58 @@ export class Game extends Container { } } - handleMessageObject(object: GameObject) { + handleMessageObject(object: IGameObject) { const obj = this.findObject(object.id); if (!obj) { if (object.entity === "TREE") { - this.initTree(object as GameObjectTree); + this.initTree(object as IGameObjectTree); return; } if (object.entity === "STONE") { - this.initStone(object as GameObjectStone); + this.initStone(object as IGameObjectStone); return; } if (object.entity === "PLAYER") { - this.initPlayer(object as GameObjectPlayer); + this.initPlayer(object as IGameObjectPlayer); return; } if (object.entity === "RAIDER") { - this.initRaider(object as GameObjectRaider); + this.initRaider(object as IGameObjectRaider); return; } if (object.entity === "RABBIT") { - this.initRabbit(object as GameObject); + this.initRabbit(object as IGameObjectRabbit); return; } if (object.entity === "WOLF") { - this.initWolf(object as GameObject); + this.initWolf(object as IGameObjectWolf); return; } return; } if (object.entity === "TREE") { - this.updateTree(object as GameObjectTree); + this.updateTree(object as IGameObjectTree); return; } if (object.entity === "STONE") { - this.updateStone(object as GameObjectStone); + this.updateStone(object as IGameObjectStone); return; } if (object.entity === "PLAYER") { - this.updatePlayer(object as GameObjectPlayer); + this.updatePlayer(object as IGameObjectPlayer); return; } if (object.entity === "RAIDER") { - this.updateRaider(object as GameObjectRaider); + this.updateRaider(object as IGameObjectRaider); return; } if (object.entity === "RABBIT") { - this.updateRabbit(object as GameObject); + this.updateRabbit(object as IGameObjectRabbit); return; } if (object.entity === "WOLF") { - this.updateWolf(object as GameObject); + this.updateWolf(object as IGameObjectWolf); return; } } diff --git a/apps/client/src/game/objects/gameContainer.ts b/apps/client/src/game/objects/gameContainer.ts deleted file mode 100644 index ce3dade4..00000000 --- a/apps/client/src/game/objects/gameContainer.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Container } from "pixi.js"; -import type { Game } from "../game"; - -export class GameContainer extends Container { - public game: Game; - public id: string; - - constructor(game: Game, id: string) { - super(); - this.game = game; - this.id = id; - } - - animate(): void {} -} diff --git a/apps/client/src/game/objects/gameObjectContainer.ts b/apps/client/src/game/objects/gameObjectContainer.ts new file mode 100644 index 00000000..984aae46 --- /dev/null +++ b/apps/client/src/game/objects/gameObjectContainer.ts @@ -0,0 +1,33 @@ +import { Container } from "pixi.js"; +import type { IGameObject } from "../../../../../packages/api-sdk/src"; +import type { Game } from "../game"; + +interface IGameObjectContainerOptions extends IGameObject { + game: Game; +} + +export class GameObjectContainer 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 game: Game; + + constructor({ game, ...object }: IGameObjectContainerOptions) { + super(); + + this.id = object.id; + this.state = object.state; + this.direction = object.direction; + this.entity = object.entity; + this.target = object.target; + this.health = object.health; + + this.game = game; + } + + animate(): void {} +} diff --git a/apps/client/src/game/objects/index.ts b/apps/client/src/game/objects/index.ts index 8dba66bb..e50c67fc 100644 --- a/apps/client/src/game/objects/index.ts +++ b/apps/client/src/game/objects/index.ts @@ -1,4 +1,4 @@ -export { GameContainer } from "./gameContainer"; +export { GameObjectContainer } from "./gameObjectContainer"; export { Player } from "./player"; export { Rabbit } from "./rabbit"; export { Raider } from "./raider"; diff --git a/apps/client/src/game/objects/player.ts b/apps/client/src/game/objects/player.ts index e7aee502..47f17674 100644 --- a/apps/client/src/game/objects/player.ts +++ b/apps/client/src/game/objects/player.ts @@ -1,37 +1,35 @@ import type { AnimatedSprite } from "pixi.js"; import type { - GameObjectDirection, - GameObjectEntity, - GameObjectPlayer, - GameObjectState, - Inventory, - Skill, + IGameInventory, + IGameObjectPlayer, + IGameSkill, } from "../../../../../packages/api-sdk/src"; import { GraphicsContainer } from "../components/graphicsContainer"; import { PlayerInterface } from "../components/playerInterface"; import type { Game } from "../game"; import { AssetsManager } from "../utils"; -import { GameContainer } from "./gameContainer"; +import { GameObjectContainer } from "./gameObjectContainer"; -export class Player extends GameContainer implements GameObjectPlayer { - children: GraphicsContainer[] = []; +interface IPlayerOptions { + game: Game; + object: IGameObjectPlayer; +} - entity: GameObjectEntity; - state!: GameObjectState; - direction!: GameObjectDirection; +export class Player extends GameObjectContainer implements IGameObjectPlayer { coins!: number; reputation!: number; userName!: string; colorIndex!: number; - inventory!: Inventory | null; - skills!: Skill[]; + inventory!: IGameInventory | null; + skills!: IGameSkill[]; + children: GraphicsContainer[] = []; interface!: PlayerInterface; animationMovingLeft!: AnimatedSprite; animationMovingRight!: AnimatedSprite; - constructor(game: Game, object: GameObjectPlayer) { - super(game, object.id); + constructor({ game, object }: IPlayerOptions) { + super({ game, ...object }); this.update(object); this.animationMovingLeft = AssetsManager.getAnimatedSpriteHeroLeft(); @@ -183,7 +181,7 @@ export class Player extends GameContainer implements GameObjectPlayer { } } - update(object: GameObjectPlayer) { + update(object: IGameObjectPlayer) { this.x = object.x; this.y = object.y; this.zIndex = Math.round(object.y + 1); @@ -198,5 +196,7 @@ export class Player extends GameContainer implements GameObjectPlayer { this.colorIndex = object.colorIndex; this.inventory = object.inventory; this.skills = object.skills; + + this.health = object.health; } } diff --git a/apps/client/src/game/objects/rabbit.ts b/apps/client/src/game/objects/rabbit.ts index 02424fd8..33a7f033 100644 --- a/apps/client/src/game/objects/rabbit.ts +++ b/apps/client/src/game/objects/rabbit.ts @@ -1,22 +1,18 @@ import { Sprite } from "pixi.js"; -import type { - GameObject, - GameObjectDirection, - GameObjectEntity, - GameObjectState, -} from "../../../../../packages/api-sdk/src"; +import type { IGameObjectRabbit } from "../../../../../packages/api-sdk/src"; import type { Game } from "../game"; -import { GameContainer } from "./gameContainer"; +import { GameObjectContainer } from "./gameObjectContainer"; -export class Rabbit extends GameContainer implements GameObject { - entity: GameObjectEntity; - state!: GameObjectState; - direction!: GameObjectDirection; +interface IRabbitOptions { + game: Game; + object: IGameObjectRabbit; +} +export class Rabbit extends GameObjectContainer implements IGameObjectRabbit { public animationAngle = 0; - constructor(game: Game, object: GameObject) { - super(game, object.id); + constructor({ game, object }: IRabbitOptions) { + super({ game, ...object }); this.update(object); this.init(); @@ -61,7 +57,7 @@ export class Rabbit extends GameContainer implements GameObject { this.animationAngle += 0.08; } - update(object: GameObject) { + update(object: IGameObjectRabbit) { this.x = object.x; this.y = object.y; this.zIndex = Math.round(object.y); @@ -69,5 +65,6 @@ export class Rabbit extends GameContainer implements GameObject { this.entity = object.entity; this.state = object.state; this.direction = object.direction; + this.health = object.health; } } diff --git a/apps/client/src/game/objects/raider.ts b/apps/client/src/game/objects/raider.ts index 87d03007..7e915050 100644 --- a/apps/client/src/game/objects/raider.ts +++ b/apps/client/src/game/objects/raider.ts @@ -1,22 +1,19 @@ import { ColorMatrixFilter, Container, Graphics, Sprite, Text } from "pixi.js"; -import type { - GameObjectDirection, - GameObjectEntity, - GameObjectRaider, - GameObjectState, -} from "../../../../../packages/api-sdk/src"; +import type { IGameObjectRaider } from "../../../../../packages/api-sdk/src"; import type { Game } from "../game"; -import { GameContainer } from "./gameContainer"; +import { GameObjectContainer } from "./gameObjectContainer"; -export class Raider extends GameContainer implements GameObjectRaider { - entity: GameObjectEntity; - state!: GameObjectState; - direction!: GameObjectDirection; +interface IRaiderOptions { + game: Game; + object: IGameObjectRaider; +} + +export class Raider extends GameObjectContainer implements IGameObjectRaider { userName!: string; colorIndex!: number; - constructor(game: Game, object: GameObjectRaider) { - super(game, object.id); + constructor({ game, object }: IRaiderOptions) { + super({ game, ...object }); this.update(object); this.init(); @@ -91,7 +88,7 @@ export class Raider extends GameContainer implements GameObjectRaider { } } - update(object: GameObjectRaider) { + update(object: IGameObjectRaider) { this.x = object.x; this.y = object.y; this.zIndex = Math.round(object.y + 1); @@ -102,5 +99,6 @@ export class Raider extends GameContainer implements GameObjectRaider { this.userName = object.userName; this.colorIndex = object.colorIndex; + this.health = object.health; } } diff --git a/apps/client/src/game/objects/stone.ts b/apps/client/src/game/objects/stone.ts index 0f54ceac..d9d49e0b 100644 --- a/apps/client/src/game/objects/stone.ts +++ b/apps/client/src/game/objects/stone.ts @@ -1,27 +1,23 @@ import { Sprite } from "pixi.js"; -import type { - GameObjectDirection, - GameObjectEntity, - GameObjectState, - GameObjectStone, -} from "../../../../../packages/api-sdk/src"; +import type { IGameObjectStone } from "../../../../../packages/api-sdk/src"; import type { Game } from "../game"; -import { GameContainer } from "./gameContainer"; +import { GameObjectContainer } from "./gameObjectContainer"; -export class Stone extends GameContainer implements GameObjectStone { - entity: GameObjectEntity; - state!: GameObjectState; - direction!: GameObjectDirection; - health!: number; - resource!: number; - size!: number; - public type!: GameObjectStone["type"]; +interface IStoneOptions { + game: Game; + object: IGameObjectStone; +} + +export class Stone extends GameObjectContainer implements IGameObjectStone { + public type!: IGameObjectStone["type"]; + public resource!: number; + public size!: number; public animationAngle = 0; public animationHighSpeed = 0.05; - constructor(game: Game, object: GameObjectStone) { - super(game, object.id); + constructor({ game, object }: IStoneOptions) { + super({ game, ...object }); this.update(object); this.init(); @@ -60,7 +56,7 @@ export class Stone extends GameContainer implements GameObjectStone { this.angle = this.animationAngle; } - update(object: GameObjectStone) { + update(object: IGameObjectStone) { this.x = object.x; this.y = object.y; this.zIndex = Math.round(object.y); diff --git a/apps/client/src/game/objects/tree.ts b/apps/client/src/game/objects/tree.ts index 071f7e94..29446899 100644 --- a/apps/client/src/game/objects/tree.ts +++ b/apps/client/src/game/objects/tree.ts @@ -1,29 +1,25 @@ import { Sprite } from "pixi.js"; -import type { - GameObjectDirection, - GameObjectEntity, - GameObjectState, - GameObjectTree, -} from "../../../../../packages/api-sdk/src"; +import type { IGameObjectTree } from "../../../../../packages/api-sdk/src"; import type { Game } from "../game"; -import { GameContainer } from "./gameContainer"; +import { GameObjectContainer } from "./gameObjectContainer"; -export class Tree extends GameContainer implements GameObjectTree { - entity: GameObjectEntity; - state!: GameObjectState; - direction!: GameObjectDirection; - health!: number; - isReadyToChop!: boolean; - resource!: number; - size!: number; - public type!: GameObjectTree["type"]; +interface ITreeOptions { + game: Game; + object: IGameObjectTree; +} + +export class Tree extends GameObjectContainer implements IGameObjectTree { + public type!: IGameObjectTree["type"]; + public resource!: number; + public size!: number; + public isReadyToChop!: boolean; public animationAngle = 0; public animationSlowSpeed = 0.04; public animationHighSpeed = 0.15; - constructor(game: Game, object: GameObjectTree) { - super(game, object.id); + constructor({ game, object }: ITreeOptions) { + super({ game, ...object }); this.update(object); this.init(); @@ -80,7 +76,7 @@ export class Tree extends GameContainer implements GameObjectTree { this.angle = this.animationAngle; } - update(object: GameObjectTree) { + update(object: IGameObjectTree) { this.x = object.x; this.y = object.y; this.zIndex = Math.round(object.y); diff --git a/apps/client/src/game/objects/wolf.ts b/apps/client/src/game/objects/wolf.ts index 2bf5a93e..62650153 100644 --- a/apps/client/src/game/objects/wolf.ts +++ b/apps/client/src/game/objects/wolf.ts @@ -1,23 +1,19 @@ import { Sprite } from "pixi.js"; -import type { - GameObject, - GameObjectDirection, - GameObjectEntity, - GameObjectState, -} from "../../../../../packages/api-sdk/src"; +import type { IGameObjectWolf } from "../../../../../packages/api-sdk/src"; import type { Game } from "../game"; -import { GameContainer } from "./gameContainer"; +import { GameObjectContainer } from "./gameObjectContainer"; -export class Wolf extends GameContainer implements GameObject { - entity: GameObjectEntity; - state!: GameObjectState; - direction!: GameObjectDirection; +interface IWolfOptions { + game: Game; + object: IGameObjectWolf; +} +export class Wolf extends GameObjectContainer implements IGameObjectWolf { public animationAngle = 0; public animationSlowSpeed = 0.1; - constructor(game: Game, object: GameObject) { - super(game, object.id); + constructor({ game, object }: IWolfOptions) { + super({ game, ...object }); this.update(object); this.init(); @@ -62,7 +58,7 @@ export class Wolf extends GameContainer implements GameObject { this.angle = this.animationAngle; } - update(object: GameObject) { + update(object: IGameObjectWolf) { this.x = object.x; this.y = object.y; this.zIndex = Math.round(object.y); @@ -70,5 +66,6 @@ export class Wolf extends GameContainer implements GameObject { this.entity = object.entity; this.state = object.state; this.direction = object.direction; + this.health = object.health; } } diff --git a/apps/client/src/store/players.ts b/apps/client/src/store/players.ts index c313928d..e333079c 100644 --- a/apps/client/src/store/players.ts +++ b/apps/client/src/store/players.ts @@ -1,9 +1,9 @@ import { create } from "zustand"; -import type { GameObjectPlayer } from "../../../../packages/api-sdk/src"; +import type { IGameObjectPlayer } from "../../../../packages/api-sdk/src"; type PlayersState = { - players: GameObjectPlayer[]; - update: (obj: GameObjectPlayer) => void; + players: IGameObjectPlayer[]; + update: (obj: IGameObjectPlayer) => void; }; export const usePlayersStore = create((set) => ({ diff --git a/apps/client/src/store/rabbits.ts b/apps/client/src/store/rabbits.ts index 361fc8b2..d5c7257c 100644 --- a/apps/client/src/store/rabbits.ts +++ b/apps/client/src/store/rabbits.ts @@ -1,9 +1,9 @@ import { create } from "zustand"; -import type { GameObject } from "../../../../packages/api-sdk/src"; +import type { IGameObject } from "../../../../packages/api-sdk/src"; type RabbitsState = { - objects: GameObject[]; - update: (obj: GameObject) => void; + objects: IGameObject[]; + update: (obj: IGameObject) => void; }; export const useRabbitsStore = create((set) => ({ diff --git a/apps/client/src/store/stones.ts b/apps/client/src/store/stones.ts index 568647d7..1e211b9c 100644 --- a/apps/client/src/store/stones.ts +++ b/apps/client/src/store/stones.ts @@ -1,9 +1,9 @@ import { create } from "zustand"; -import type { GameObjectStone } from "../../../../packages/api-sdk/src"; +import type { IGameObjectStone } from "../../../../packages/api-sdk/src"; type StonesState = { - objects: GameObjectStone[]; - update: (obj: GameObjectStone) => void; + objects: IGameObjectStone[]; + update: (obj: IGameObjectStone) => void; }; export const useStonesStore = create((set) => ({ diff --git a/apps/client/src/store/trees.ts b/apps/client/src/store/trees.ts index a034e5f5..8352950f 100644 --- a/apps/client/src/store/trees.ts +++ b/apps/client/src/store/trees.ts @@ -1,9 +1,9 @@ import { create } from "zustand"; -import type { GameObjectTree } from "../../../../packages/api-sdk/src"; +import type { IGameObjectTree } from "../../../../packages/api-sdk/src"; type TreesState = { - objects: GameObjectTree[]; - update: (obj: GameObjectTree) => void; + objects: IGameObjectTree[]; + update: (obj: IGameObjectTree) => void; }; export const useTreesStore = create((set) => ({ diff --git a/apps/client/src/store/wolfs.ts b/apps/client/src/store/wolfs.ts index 1002dde3..002a7518 100644 --- a/apps/client/src/store/wolfs.ts +++ b/apps/client/src/store/wolfs.ts @@ -1,9 +1,9 @@ import { create } from "zustand"; -import type { GameObject } from "../../../../packages/api-sdk/src"; +import type { IGameObject } from "../../../../packages/api-sdk/src"; type WolfsState = { - objects: GameObject[]; - update: (obj: GameObject) => void; + objects: IGameObject[]; + update: (obj: IGameObject) => void; }; export const useWolfsStore = create((set) => ({ diff --git a/biome.json b/biome.json index 182f39ae..2cbd60bc 100644 --- a/biome.json +++ b/biome.json @@ -8,7 +8,7 @@ "rules": { "recommended": true, "complexity": { - "noStaticOnlyClass": "warn" + "noStaticOnlyClass": "off" } }, "ignore": ["node_modules", "tmp"] diff --git a/packages/api-sdk/src/lib/client.ts b/packages/api-sdk/src/lib/client.ts index d8bebc8d..264baf5e 100644 --- a/packages/api-sdk/src/lib/client.ts +++ b/packages/api-sdk/src/lib/client.ts @@ -1,7 +1,7 @@ -import type { GetSceneResponse, Player, Village } from "./types"; +import type { GetSceneResponse, IGameObjectPlayer, Village } from "./types"; interface PlayerWithPoints { - player: Player; + player: IGameObjectPlayer; points: number; } diff --git a/packages/api-sdk/src/lib/types.ts b/packages/api-sdk/src/lib/types.ts index fce395b3..52ab183a 100644 --- a/packages/api-sdk/src/lib/types.ts +++ b/packages/api-sdk/src/lib/types.ts @@ -1,5 +1,3 @@ -import type { Group } from "../../../../apps/api/src/game/common"; - export interface Village { id: string; createdAt: Date; @@ -10,17 +8,6 @@ export interface Village { globalTargetSuccess: number | null; } -export interface Command { - id: string; - createdAt: Date; - updatedAt: Date; - playerId: string; - command: string; - player?: Player; -} - -export type TargetType = "TREE" | "STONE"; - export type ChatAction = | "HELP" | "GIFT" @@ -33,34 +20,13 @@ export type ChatAction = | "JOIN_GROUP" | "START_RAID"; -export interface Player { - id: string; - createdAt: Date; - updatedAt: Date; - lastActionAt: Date; - x: number; - y: number; - targetX: number | null; - targetY: number | null; - targetId: string | null; - userName: string; - twitchId: string; - isBusy: boolean; - businessType: PlayerBusinessType; - colorIndex: number; - handsItemType: null | ItemType; - handsItemAmount: number; - coins: number; - reputation: number; - viewerPoints: number; - health: number; -} - -export type PlayerBusinessType = null | "RUNNING" | "CHOPPING" | "MINING"; - export type ItemType = "WOOD" | "STONE" | "AXE" | "PICKAXE"; -export interface Inventory { +export interface IGameRaid { + raiders: IGameObjectRaider[]; +} + +export interface IGameInventory { id: string; objectId: string; items: InventoryItem[]; @@ -76,61 +42,33 @@ export interface InventoryItem { durability: number; } -export interface Tree { - id: string; - createdAt: Date; - updatedAt: Date; - x: number; - y: number; - size: number; - resource: number; - isReserved: boolean; - inProgress: boolean; - progressFinishAt: Date; - type: "1" | "2" | "3"; -} - -export interface Stone { - id: string; - createdAt: Date; - updatedAt: Date; - x: number; - y: number; - size: number; - resource: number; - isReserved: boolean; - inProgress: boolean; - progressFinishAt: Date; - type: "1"; -} - -export interface Skill { +export interface IGameSkill { id: string; - type: SkillType | null; + type: "WOODSMAN" | "MINER"; objectId: string | null; lvl: number; xp: number; xpNextLvl: number; } -export type SkillType = "WOODSMAN" | "MINER"; - -export interface GameObject { +export interface IGameObject { id: string; x: number; y: number; - state: GameObjectState; - direction: GameObjectDirection; - entity: GameObjectEntity; + state: IGameObjectState; + direction: IGameObjectDirection; + entity: IGameObjectEntity; + target: IGameObject | undefined; + health: number; } -export type GameObjectState = +export type IGameObjectState = | "MOVING" | "IDLE" | "CHOPPING" | "MINING" | "DESTROYED"; -export type GameObjectEntity = +export type IGameObjectEntity = | undefined | "RABBIT" | "WOLF" @@ -139,7 +77,7 @@ export type GameObjectEntity = | "TREE" | "STONE" | "FLAG"; -export type GameObjectDirection = "LEFT" | "RIGHT"; +export type IGameObjectDirection = "LEFT" | "RIGHT"; export interface WebSocketMessage { id: string; @@ -148,41 +86,45 @@ export interface WebSocketMessage { | "RAID_STARTED" | "GROUP_FORM_STARTED" | "SCENE_CHANGED"; - object?: GameObject; + object?: IGameObject; +} + +export interface IGameObjectFlag extends IGameObject { + isOnScreen: boolean; } -export interface GameObjectTree extends GameObject { - type: GameObjectTreeType; +export interface IGameObjectTree extends IGameObject { + type: "1" | "2" | "3"; resource: number; size: number; - health: number; isReadyToChop: boolean; } -export type GameObjectTreeType = "1" | "2" | "3"; - -export interface GameObjectStone extends GameObject { +export interface IGameObjectStone extends IGameObject { type: "1"; resource: number; size: number; - health: number; } -export interface GameObjectPlayer extends GameObject { +export interface IGameObjectPlayer extends IGameObject { coins: number; reputation: number; userName: string; colorIndex: number; - inventory: Inventory | null; - skills: Skill[]; + inventory: IGameInventory | null; + skills: IGameSkill[]; } -export interface GameObjectRaider extends GameObject { +export interface IGameObjectRaider extends IGameObject { userName: string; colorIndex: number; } -export interface GameEvent { +export interface IGameObjectRabbit extends IGameObject {} + +export interface IGameObjectWolf extends IGameObject {} + +export interface IGameEvent { type: EventType; status: EventStatus; endsAt: Date; @@ -196,14 +138,14 @@ export type GameSceneType = "VILLAGE" | "DEFENCE"; export interface GetSceneResponse { id: string; commands: string[]; - events: GameEvent[]; - group: Group | undefined; + events: IGameEvent[]; + group: IGameGroup | undefined; } -export interface GameGroup { +export interface IGameGroup { id: string; target: GameSceneType; - players: Player[]; + players: IGameObjectPlayer[]; } export interface PlayerTitle {