From 4208376e9353dfc956f0c547a34591682073523e Mon Sep 17 00:00:00 2001 From: keshav2010 Date: Sat, 17 Feb 2024 01:12:28 +0530 Subject: [PATCH] soldier creation synced b/w client and server' --- gameserver/schema/PlayerState.ts | 8 ++- gameserver/schema/SoldierState.ts | 4 +- .../SessionStateBehaviour.ts | 2 +- public/scenes/GameScene.ts | 59 +++++++++++-------- public/scenes/PlayerStatisticHUD.ts | 3 +- public/soldiers/BaseSoldier.ts | 5 +- 6 files changed, 48 insertions(+), 33 deletions(-) diff --git a/gameserver/schema/PlayerState.ts b/gameserver/schema/PlayerState.ts index 1c95753..8a3bd87 100644 --- a/gameserver/schema/PlayerState.ts +++ b/gameserver/schema/PlayerState.ts @@ -2,6 +2,7 @@ import { Schema, MapSchema, ArraySchema, type } from "@colyseus/schema"; import { SoldierState } from "./SoldierState"; import { nanoid } from "nanoid"; import { SoldierType, SoldierTypeConfig } from "../../common/SoldierType"; +import { GameStateManager } from "../core/GameStateManager"; export class SpawnRequest extends Schema { @type("string") requestId: string = ""; @@ -86,6 +87,8 @@ export class PlayerState extends Schema { this.spawnRequestQueue.shift(); const soldierId = nanoid(); + + console.log('spawning a soldier ', requestInfo.unitType); const newSoldier = new SoldierState( this.id, requestInfo.unitType, @@ -95,11 +98,14 @@ export class PlayerState extends Schema { this.soldiers.set(soldierId, newSoldier); } - public tick(deltaTime: number) { + public tick(deltaTime: number, gameStateManager: GameStateManager) { this.resources += this.resourceGrowthRateHz * deltaTime; this.processSpawnRequest(deltaTime); //TODO: tick each soldier + this.soldiers.forEach((soldier) => { + soldier.tick(deltaTime, gameStateManager); + }); } public updatePosition(x: number, y: number) { diff --git a/gameserver/schema/SoldierState.ts b/gameserver/schema/SoldierState.ts index 45361d7..73ac0e6 100644 --- a/gameserver/schema/SoldierState.ts +++ b/gameserver/schema/SoldierState.ts @@ -7,6 +7,7 @@ import soldierStateBehaviours from "../stateMachines/soldier-state-machine/Soldi import { GameStateManager } from "../core/GameStateManager"; import { SceneObject } from "../core/SceneObject"; import { AllianceTypes } from "../AllianceTracker"; +import SAT from "sat"; function mapRange( val: number, @@ -281,7 +282,6 @@ export class SoldierState extends Schema { } tick(delta: number, stateManager: GameStateManager) { - //if object is moving, we apply -2 frictionForce to it. this.velocityVector.add(this.accelerationVector); if (this.velocityVector.len() > this.speed) @@ -302,7 +302,7 @@ export class SoldierState extends Schema { this, ( res: { a: any; b: any; overlapV: { x: number; y: number } }, - collidingBodies: any + collidingBodies ) => { let a = res.a; let b = res.b; diff --git a/gameserver/stateMachines/server-state-machine/SessionStateBehaviour.ts b/gameserver/stateMachines/server-state-machine/SessionStateBehaviour.ts index b84ed9a..d8a4987 100644 --- a/gameserver/stateMachines/server-state-machine/SessionStateBehaviour.ts +++ b/gameserver/stateMachines/server-state-machine/SessionStateBehaviour.ts @@ -79,7 +79,7 @@ export default { return; } playersConnected.forEach((player) => { - player.tick(deltaTime); + player.tick(deltaTime, gameStateManager); }); } catch (err) { console.log(err); diff --git a/public/scenes/GameScene.ts b/public/scenes/GameScene.ts index aad52ac..d010571 100644 --- a/public/scenes/GameScene.ts +++ b/public/scenes/GameScene.ts @@ -87,11 +87,12 @@ export class GameScene extends BaseScene { } onSoldierAdded(soldier: SoldierState, ownerPlayer: PlayerState) { + console.log('Adding a soldier'); const spearmen = new Spearman( this, soldier.currentPositionX, soldier.currentPositionY, - "SPEARMAN", + "spearman", null, { health: soldier.health, @@ -376,39 +377,47 @@ export class GameScene extends BaseScene { }); }) ); - this.AddStateChangeListener( - state.players.onChange((playerState) => { - this.AddStateChangeListener( - playerState.soldiers.onAdd((soldierState) => { - this.onSoldierAdded(soldierState, playerState); - }) - ); - this.AddStateChangeListener( - playerState.soldiers.onRemove((soldierState) => { - this.onSoldierRemoved(soldierState, playerState); - }) - ); - playerState.soldiers.forEach((soldier) => { - const cb = soldier.listen("health", (value, prevValue) => { - this.onSoldierHealthUpdate(soldier, value, prevValue); - }); - this.AddStateChangeListener(cb, soldier.id); - }); - playerState.soldiers.forEach((soldier) => { + // register soldier creation/removal listeners for eaech player. + state.players.forEach((player) => { + this.AddStateChangeListener( + player.soldiers.onAdd((soldier, key) => { + this.onSoldierAdded(soldier, player); + + // add relevant listeners for every soldier + this.AddStateChangeListener( + soldier.listen("health", (value, prevValue) => { + this.onSoldierHealthUpdate(soldier, value, prevValue); + }), + `health-${soldier.id}` + ); + this.AddStateChangeListener( soldier.listen("currentPositionX", (value, prevValue) => { this.onSoldierPositionChanged(soldier.id); - }) + }), + `currentPosX-${soldier.id}` ); this.AddStateChangeListener( soldier.listen("currentPositionY", (value, prevValue) => { this.onSoldierPositionChanged(soldier.id); - }) + }), + `currentPosY-${soldier.id}` ); - }); - }) - ); + }) + ); + + this.AddStateChangeListener( + player.soldiers.onRemove((soldier, key) => { + this.onSoldierRemoved(soldier, player); + + // remove relevant listeners for each soldier. + this.DestroyStateChangeListener(`health-${soldier.id}`); + this.DestroyStateChangeListener(`currentPosX-${soldier.id}`); + this.DestroyStateChangeListener(`currentPosY-${soldier.id}`); + }) + ); + }); this.AddStateChangeListener( state.players.onRemove((player) => { diff --git a/public/scenes/PlayerStatisticHUD.ts b/public/scenes/PlayerStatisticHUD.ts index 1e7f657..bd82618 100644 --- a/public/scenes/PlayerStatisticHUD.ts +++ b/public/scenes/PlayerStatisticHUD.ts @@ -124,10 +124,9 @@ export class PlayerStatisticHUD extends BaseScene { countdown: number; unitType: string; }) => { - console.log("test", countdown); const textObject = this.GetObject("obj_spawnETA"); - textObject?.setText(`Spawning Next In : ${countdown} x${count}`); + textObject?.setText(`Spawning Next In : ${Math.floor(countdown)} X${count}`); } ); diff --git a/public/soldiers/BaseSoldier.ts b/public/soldiers/BaseSoldier.ts index fe41b8b..56bf509 100644 --- a/public/soldiers/BaseSoldier.ts +++ b/public/soldiers/BaseSoldier.ts @@ -1,5 +1,6 @@ -const { GAMEEVENTS } = require("../constant"); -const LoadingBar = require("../LoadingBar"); +import CONSTANTS from "../constant"; +import LoadingBar from "../LoadingBar"; +const GAMEEVENTS = CONSTANTS.GAMEEVENTS; class BackgroundHighlight extends Phaser.GameObjects.Graphics { parent: any; r: number;