Skip to content

Commit

Permalink
soldier creation synced b/w client and server'
Browse files Browse the repository at this point in the history
  • Loading branch information
keshav2010 committed Feb 16, 2024
1 parent f110e7a commit 4208376
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 33 deletions.
8 changes: 7 additions & 1 deletion gameserver/schema/PlayerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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,
Expand All @@ -95,11 +98,14 @@ export class PlayerState extends Schema {
this.soldiers.set(soldierId, newSoldier);
}

public tick(deltaTime: number) {
public tick(deltaTime: number, gameStateManager: GameStateManager<SoldierState>) {
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) {
Expand Down
4 changes: 2 additions & 2 deletions gameserver/schema/SoldierState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -281,7 +282,6 @@ export class SoldierState extends Schema {
}

tick(delta: number, stateManager: GameStateManager<SoldierState>) {

//if object is moving, we apply -2 frictionForce to it.
this.velocityVector.add(this.accelerationVector);
if (this.velocityVector.len() > this.speed)
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default {
return;
}
playersConnected.forEach((player) => {
player.tick(deltaTime);
player.tick(deltaTime, gameStateManager);
});
} catch (err) {
console.log(err);
Expand Down
59 changes: 34 additions & 25 deletions public/scenes/GameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) => {
Expand Down
3 changes: 1 addition & 2 deletions public/scenes/PlayerStatisticHUD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,9 @@ export class PlayerStatisticHUD extends BaseScene {
countdown: number;
unitType: string;
}) => {
console.log("test", countdown);
const textObject =
this.GetObject<Phaser.GameObjects.Text>("obj_spawnETA");
textObject?.setText(`Spawning Next In : ${countdown} x${count}`);
textObject?.setText(`Spawning Next In : ${Math.floor(countdown)} X${count}`);
}
);

Expand Down
5 changes: 3 additions & 2 deletions public/soldiers/BaseSoldier.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 4208376

Please sign in to comment.