Skip to content

Commit

Permalink
add vectorstate schema to avoid sync for individual posX,posY for player
Browse files Browse the repository at this point in the history
  • Loading branch information
keshav2010 committed May 7, 2024
1 parent 134c199 commit e9d6116
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 59 deletions.
15 changes: 7 additions & 8 deletions gameserver/schema/PlayerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Scene } from "../core/Scene";
import { SceneObject } from "../core/types/SceneObject";
import SAT from "sat";
import { ISceneItem } from "../core/types/ISceneItem";
import { VectorState } from "./VectorState";
export type GameStateManagerType = GameStateManager<PlayerState>;

export class SpawnRequest extends Schema {
Expand All @@ -30,8 +31,7 @@ export class PlayerState extends Schema implements ISceneItem {
@type("number") resources: number = 100;
@type("boolean") readyStatus: boolean = false;

@type("number") posX: number = 0;
@type("number") posY: number = 0;
@type(VectorState) pos: VectorState = new VectorState();

@type("number") spawnFlagHealth: number = 100;

Expand Down Expand Up @@ -64,8 +64,7 @@ export class PlayerState extends Schema implements ISceneItem {
this.name = name;
this.resources = 100;
this.readyStatus = false;
this.posX = x;
this.posY = y;
this.pos = new VectorState();
this.spawnFlagHealth = 100;
this.soldiers = new MapSchema<SoldierState>();
this.spawnRequestDetailMap = new MapSchema<SpawnRequest>();
Expand Down Expand Up @@ -101,7 +100,7 @@ export class PlayerState extends Schema implements ISceneItem {

public addNewSoldier(type: SoldierType, scene: Scene) {
console.log("spawning a soldier ", type);
const newSoldier = new SoldierState(this.id, type, this.posX, this.posY);
const newSoldier = new SoldierState(this.id, type, this.pos.x, this.pos.y);
this.soldiers.set(newSoldier.id, newSoldier);
scene.addSceneItem(newSoldier);
return newSoldier.id;
Expand All @@ -121,9 +120,9 @@ export class PlayerState extends Schema implements ISceneItem {
}

public updatePosition(x: number, y: number) {
this.posX = x;
this.posY = y;
this.sceneItemRef.setPosition(new SAT.Vector(x, y));
const v = new SAT.Vector(x, y);
this.pos.setVector(v);
this.sceneItemRef.setPosition(v);
}

//TODO:
Expand Down
19 changes: 19 additions & 0 deletions gameserver/schema/VectorState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Schema, type } from "@colyseus/schema";
import SAT from "sat";
export class VectorState extends Schema {
// Key : sessionId
@type("number") x = 0;
@type("number") y = 0;
constructor(x: number = 0, y: number = 0) {
super();
this.x = x;
this.y = y;
}
getVector() {
return new SAT.Vector(this.x, this.y);
}
setVector(v: SAT.Vector) {
this.x = v.x;
this.y = v.y;
}
}
6 changes: 4 additions & 2 deletions public/scenes/GameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,14 +569,16 @@ export class GameScene extends BaseScene {

//show initial spawnpoint choice on map for player
networkManager.getState()?.players.forEach((player) => {
const objKey = `obj_playerCastle_${player.id}`;
this.AddObject(
new PlayerCastle(this, player.posX, player.posY, "castle", null, {
new PlayerCastle(this, player.pos.x, player.pos.y, "castle", null, {
health: 500,
player: player,
}),
`obj_playerCastle_${player.id}`
objKey
);
});


this.AddSceneEvent("shutdown", (data: any) => {
console.log("shutdown ", data.config.key);
Expand Down
96 changes: 48 additions & 48 deletions public/scenes/SpawnSelectionScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,10 @@ export class SpawnSelectionScene extends BaseScene {
const sessionState = networkManager.getState()!;
SessionStateClientHelpers.getPlayers(sessionState).forEach((player) => {
this.AddStateChangeListener(
player.listen("posX", (value) => {
this.showSpawnFlag(networkManager, value, player.posY, player.id);
player.listen("pos", (value) => {
this.showSpawnFlag(networkManager, value.x, value.y, player.id);
}),
`${player.id}_posX_spawnFlag_update`
);
this.AddStateChangeListener(
player.listen("posY", (value) => {
this.showSpawnFlag(networkManager, player.posX, value, player.id);
}),
`${player.id}_posY_spawnFlag_update`
`${player.id}_pos_spawnFlag_update`
);
});

Expand Down Expand Up @@ -250,49 +244,55 @@ export class SpawnSelectionScene extends BaseScene {
posY: number,
playerId?: string
) {
//show new choice on map for player
const clientId = playerId || networkManager.getClientId();
if (!clientId) return;
const sessionState = networkManager.getState();
if (!sessionState) return;
try {
//show new choice on map for player
const clientId = playerId || networkManager.getClientId();
if (!clientId) return;
const sessionState = networkManager.getState();
if (!sessionState) return;

let player = SessionStateClientHelpers.getPlayer(sessionState, clientId);
// spawn a castle.
const flagKey = `obj_spawnFlag_${clientId}`;
const spawnFlag = this.GetObject<PlayerCastle>(flagKey);
let player = SessionStateClientHelpers.getPlayer(sessionState, clientId);
if (!player) return;

const playerState = SessionStateClientHelpers.getPlayer(
sessionState,
clientId
);
if (!playerState) {
console.error("unable to find player state in spawn selection");
return;
}
const x = posX || posX === 0 ? posX : player!.posX;
const y = posY || posY === 0 ? posY : player!.posY;
// spawn a castle.
const flagKey = `obj_spawnFlag_${clientId}`;
const spawnFlag = this.GetObject<PlayerCastle>(flagKey);

const sceneBoundingBox = new SAT.Box(
new SAT.Vector(64, 64),
this.canvasWidth - 64 * 2,
this.canvasHeight - 64 * 2
);
const requestedPoint = new SAT.Vector(x - 64 / 2, y - 64 / 2);
const pointInPolygon = SAT.pointInPolygon(
requestedPoint,
sceneBoundingBox.toPolygon()
);
if (!pointInPolygon) return;
const playerState = SessionStateClientHelpers.getPlayer(
sessionState,
clientId
);
if (!playerState) {
console.error("unable to find player state in spawn selection");
return;
}
const x = posX || posX === 0 ? posX : player.pos.x;
const y = posY || posY === 0 ? posY : player.pos.y;

if (spawnFlag) {
spawnFlag.setPosition(x, y);
spawnFlag.setHealth(2);
} else {
const castle = new PlayerCastle(this, x, y, "castle", null, {
health: playerState.spawnFlagHealth,
player: playerState,
});
this.AddObject(castle, flagKey);
const sceneBoundingBox = new SAT.Box(
new SAT.Vector(64, 64),
this.canvasWidth - 64 * 2,
this.canvasHeight - 64 * 2
);
const requestedPoint = new SAT.Vector(x - 64 / 2, y - 64 / 2);
const pointInPolygon = SAT.pointInPolygon(
requestedPoint,
sceneBoundingBox.toPolygon()
);
if (!pointInPolygon) return;

if (spawnFlag) {
spawnFlag.setPosition(x, y);
spawnFlag.setHealth(2);
} else {
const castle = new PlayerCastle(this, x, y, "castle", null, {
health: playerState.spawnFlagHealth,
player: playerState,
});
this.AddObject(castle, flagKey);
}
} catch (error) {
console.error(error);
}
}
}
1 change: 0 additions & 1 deletion public/soldiers/BaseSoldier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ export class BaseSoldier extends Phaser.GameObjects.Sprite {
);
return;
}
console.log(this.getServerPosition());
const session = networkManager.getState();
if (!session || !this.playerId) return;

Expand Down

0 comments on commit e9d6116

Please sign in to comment.