Skip to content

Commit

Permalink
added next spawn message / eta'
Browse files Browse the repository at this point in the history
  • Loading branch information
keshav2010 committed Feb 16, 2024
1 parent 38ce48e commit f110e7a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 20 deletions.
2 changes: 2 additions & 0 deletions common/PacketType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export enum ServerToClientPacketType {
SOLDIER_POSITION_UPDATED = "spu",
COUNTDOWN_TIME = "cdwn",
SOLDIER_STATE_DEBUG = "debug_soldierState",

SOLDIER_SPAWN_REQUEST_UPDATED = "spawn_request_updated",
}

export const PacketType = {
Expand Down
4 changes: 2 additions & 2 deletions gameserver/commands/OnSoldierCreateCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export class OnSoldierCreateCommand extends Command<
> {
execute({ client, message }: { client: Client; message: any }) {
console.log('received soldier create request, queueing');
let lengthBefore = this.state.getPlayer(client.id)?.unitSpawnRequests.size;
let lengthBefore = this.state.getPlayer(client.id)?.spawnRequestDetailMap.size;
this.state.getPlayer(client.id)?.queueSpawnRequest(message.soldierType);
let lengthAfter = this.state.getPlayer(client.id)?.unitSpawnRequests.size;
let lengthAfter = this.state.getPlayer(client.id)?.spawnRequestDetailMap.size;
console.log(`added to queue ${lengthAfter} <-- ${lengthBefore}`)
}
}
14 changes: 7 additions & 7 deletions gameserver/schema/PlayerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class SpawnRequest extends Schema {
this.requestId = requestId;
this.unitType = soldierType;
this.count = count;
this.countdown = 10000;
this.countdown = 10;
}
}

Expand All @@ -39,7 +39,7 @@ export class PlayerState extends Schema {
@type("number") colorB = Math.random() * 255;

// Spawn Requests (key: requestId , val: obj {unitType, count, countdown})
@type({ map: SpawnRequest }) unitSpawnRequests: MapSchema<SpawnRequest> =
@type({ map: SpawnRequest }) spawnRequestDetailMap: MapSchema<SpawnRequest> =
new MapSchema<SpawnRequest>();

@type(["string"]) spawnRequestQueue: ArraySchema<string> =
Expand All @@ -59,7 +59,7 @@ export class PlayerState extends Schema {
this.posY = y;
this.spawnFlagHealth = 100;
this.soldiers = new MapSchema<SoldierState>();
this.unitSpawnRequests = new MapSchema<SpawnRequest>();
this.spawnRequestDetailMap = new MapSchema<SpawnRequest>();
this.spawnRequestQueue = new ArraySchema<string>();
}

Expand All @@ -68,7 +68,7 @@ export class PlayerState extends Schema {
const requestId = this.spawnRequestQueue.at(0);
if (!requestId) return;

const requestInfo = this.unitSpawnRequests.get(requestId);
const requestInfo = this.spawnRequestDetailMap.get(requestId);
if (!requestInfo) return;

const spawnCost = SoldierTypeConfig[requestInfo.unitType].cost;
Expand All @@ -82,7 +82,7 @@ export class PlayerState extends Schema {
}

// spawn a unit, and clear queue entry.
this.unitSpawnRequests.delete(requestId);
this.spawnRequestDetailMap.delete(requestId);
this.spawnRequestQueue.shift();

const soldierId = nanoid();
Expand All @@ -99,7 +99,7 @@ export class PlayerState extends Schema {
this.resources += this.resourceGrowthRateHz * deltaTime;
this.processSpawnRequest(deltaTime);

//TODO tick each soldier
//TODO: tick each soldier
}

public updatePosition(x: number, y: number) {
Expand Down Expand Up @@ -128,7 +128,7 @@ export class PlayerState extends Schema {
const requestId = nanoid();
this.spawnRequestQueue.push(requestId);
const request: SpawnRequest = new SpawnRequest(requestId, soldierType, 1);
this.unitSpawnRequests.set(requestId, request);
this.spawnRequestDetailMap.set(requestId, request);
this.resources -= costOfUnit;
}

Expand Down
9 changes: 9 additions & 0 deletions public/helpers/SessionStateClientHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ function getPlayers(state: SessionState) {
return [...state.players.values()];
}

function getLastSpawnRequest(state: SessionState, playerId: string) {
const requestId = getPlayer(state, playerId)?.spawnRequestQueue.at(0);

const requestDetail = getPlayer(state, playerId)?.spawnRequestDetailMap.get(requestId!);

return requestDetail;
}

function getSoldier(
state: SessionState,
playerState: PlayerState,
Expand All @@ -31,4 +39,5 @@ export default {
getPlayers,
getSoldier,
getSoldiers,
getLastSpawnRequest,
};
42 changes: 32 additions & 10 deletions public/scenes/GameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,18 +426,40 @@ export class GameScene extends BaseScene {
})!
);

const playerId = networkManager.getClientId()!;
this.AddStateChangeListener(
SessionStateClientHelpers.getPlayer(state, playerId)!.listen(
"resources",
(value) => {
this.events.emit(PacketType.ByServer.PLAYER_RESOURCE_UPDATED, {
playerId: networkManager.getClientId()!,
resources: value,
});
}
)
player.listen("resources", (value) => {
this.events.emit(PacketType.ByServer.PLAYER_RESOURCE_UPDATED, {
playerId: networkManager.getClientId()!,
resources: value,
});
})
);

this.AddStateChangeListener(
player.spawnRequestDetailMap.onAdd((item, key) => {
this.AddStateChangeListener(
item.onChange(() => {
this.events.emit(
PacketType.ByServer.SOLDIER_SPAWN_REQUEST_UPDATED,
{
count: item.count,
countdown: item.countdown,
requestId: item.requestId,
unitType: item.unitType,
}
);
}),
`${item.requestId}`
);
})
);

this.AddStateChangeListener(
player.spawnRequestDetailMap.onRemove((item, key) => {
this.DestroyStateChangeListener(item.requestId);
})
);


this.AddSceneEvent(GAMEEVENTS.SOLDIER_SELECTED, (d: BaseSoldier) => {
this.onSoldierSelected(d.id);
Expand Down
30 changes: 29 additions & 1 deletion public/scenes/PlayerStatisticHUD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,35 @@ export class PlayerStatisticHUD extends BaseScene {
}
);

this.AddObject(
this.add.text(50, 110, "Soldiers Queued: 0"),
"obj_soldiersQueued"
);
this.AddObject(
this.add.text(50, 140, "Next Spawn In: 0"),
"obj_spawnETA"
);

gameScene.AddSceneEvent(
PacketType.ByServer.SOLDIER_SPAWN_REQUEST_UPDATED,
({
requestId,
count,
countdown,
unitType,
}: {
requestId: string;
count: number;
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}`);
}
);

gameScene.AddSceneEvent(
PacketType.ByServer.PLAYER_RESOURCE_UPDATED,
({ playerId, resources }: { playerId: string; resources: number }) => {
Expand All @@ -114,7 +143,6 @@ export class PlayerStatisticHUD extends BaseScene {
}
);

this.AddObject(this.add.text(50, 110, "Soldiers Queued: 0"), "obj_soldiersQueued");
gameScene.AddSceneEvent(
PacketType.ByServer.SOLDIER_SPAWN_SCHEDULED,
({ playerId, queueSize }: { playerId: string; queueSize: number }) => {
Expand Down

0 comments on commit f110e7a

Please sign in to comment.