Skip to content

Commit

Permalink
GameMap: Preserve unit information on resize/reload
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanGrieb committed Nov 17, 2023
1 parent 5152063 commit 0ac6cd5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 38 deletions.
54 changes: 30 additions & 24 deletions client/src/Unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,48 +114,54 @@ export class Unit extends ActorGroup {
private actions: UnitAction[];
private queuedMovementTiles: Tile[];

constructor(options: options) {
constructor(tile: Tile, unitJSON: JSON) {
super({
x: options.tile.getCenterPosition()[0] - 28 / 2,
y: options.tile.getCenterPosition()[1] - 28 / 2,
x: tile.getCenterPosition()[0] - 28 / 2,
y: tile.getCenterPosition()[1] - 28 / 2,
z: 2,
width: 28,
height: 28,
});

this.tile = tile;
this.name = unitJSON["name"];

this.unitActor = new Actor({
image: Game.getImage(GameImage.SPRITESHEET),
spriteRegion: SpriteRegion[options.name.toUpperCase()],
x: options.tile.getCenterPosition()[0] - 28 / 2,
y: options.tile.getCenterPosition()[1] - 28 / 2,
spriteRegion: SpriteRegion[this.name.toUpperCase()],
x: tile.getCenterPosition()[0] - 28 / 2,
y: tile.getCenterPosition()[1] - 28 / 2,
z: 2,
width: 28,
height: 28,
});

this.addActor(this.unitActor);

this.name = options.name;
this.id = options.id;
this.tile = options.tile;
this.attackType = options.attackType;
this.id = unitJSON["id"];
this.attackType = unitJSON["attackType"];
this.availableMovement = unitJSON["remainingMovement"];
this.defaultMoveDistance = unitJSON["defaultMoveDistance"];

this.queuedMovementTiles = [];
for (const jsonTile of unitJSON["queuedTiles"]) {
this.queuedMovementTiles.push(
GameMap.getInstance().getTiles()[jsonTile["x"]][jsonTile["y"]]
);
}

this.selectionActors = [];
this.defaultMoveDistance = 2; // TODO: Have server define this.
this.availableMovement = this.defaultMoveDistance;
this.actions = [];
this.queuedMovementTiles = [];

if (options.actionsJSONList) {
for (const actionJSON of options.actionsJSONList) {
this.actions.push(
new UnitAction(
actionJSON.name,
actionJSON.desc,
actionJSON.requirements,
SpriteRegion[actionJSON.icon]
)
);
}
for (const actionJSON of unitJSON["actions"]) {
this.actions.push(
new UnitAction(
actionJSON.name,
actionJSON.desc,
actionJSON.requirements,
SpriteRegion[actionJSON.icon]
)
);
}

console.log("new unit with id: " + this.id);
Expand Down
18 changes: 7 additions & 11 deletions client/src/map/GameMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export class GameMap {

private topLayerMapChunks: Map<Actor, Tile[]>;
private topLayerTileActorList: Tile[] = [];
private unitActorList: Unit[] = [];

public static getInstance() {
return this.instance;
Expand Down Expand Up @@ -238,6 +237,7 @@ export class GameMap {
const baseLayerTiles = [];
const riverActors = [];
const cityJSONS: JSON[] = [];
const unitJSONS: JSON[] = [];

WebsocketClient.sendMessage({ event: "requestMap" });

Expand Down Expand Up @@ -345,15 +345,7 @@ export class GameMap {
}

for (const jsonUnit of jsonUnits) {
const unit = new Unit({
name: jsonUnit["name"],
id: jsonUnit["id"],
attackType: jsonUnit["attackType"],
tile: tile,
actionsJSONList: jsonUnit["actions"],
});
tile.addUnit(unit);
this.unitActorList.push(unit);
unitJSONS.push(jsonUnit);
}
}

Expand Down Expand Up @@ -407,7 +399,11 @@ export class GameMap {
scene.addActor(chunkActor);
});

for (const unit of this.unitActorList) {
// Now create any units that already exist on the map
for (const unitJSON of unitJSONS) {
const tile = this.tiles[unitJSON["tileX"]][unitJSON["tileY"]];
const unit = new Unit(tile, unitJSON);
tile.addUnit(unit);
scene.addActor(unit);
}

Expand Down
11 changes: 9 additions & 2 deletions client/src/player/ClientPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export class ClientPlayer extends AbstractPlayer {
this.hoveredTile = new HoveredTile(9999, 9999);
this.hoveredTile.loadImage().then(() => {
Game.getCurrentScene().addActor(this.hoveredTile);
console.log("update hovered tile.");
this.updateHoveredTile(Game.getMouseX(), Game.getMouseY());
});
});
Expand Down Expand Up @@ -170,6 +169,15 @@ export class ClientPlayer extends AbstractPlayer {
this.unselectUnit();
}
});

NetworkEvents.on({
eventName: "newTurn",
parentObject: this,
callback: (data) => {
this.unselectUnit();
this.clearMovementPath();
},
});
}

private onMouseRightClick() {
Expand Down Expand Up @@ -238,7 +246,6 @@ export class ClientPlayer extends AbstractPlayer {
unit.select();
this.selectedUnit = unit;

// TOOD: Draw full outline of final queued tile or hovered tile.
if (this.selectedUnit.hasMovementQueue()) {
const isQueuedMovement = this.drawMovementPathFromTiles([
unit.getTile(),
Expand Down
1 change: 0 additions & 1 deletion client/src/scene/type/InGameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ export class InGameScene extends Scene {
height: 42,
fontColor: "white",
onClicked: () => {
console.log("next turn!");
WebsocketClient.sendMessage({ event: "nextTurnRequest" });
},
});
Expand Down
10 changes: 10 additions & 0 deletions server/src/Unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,21 @@ export class Unit {
}

public asJSON() {
const queuedTilesJSON = this.queuedMovementTiles.map((tile) => ({
x: tile.getX(),
y: tile.getY(),
}));

return {
name: this.name,
tileX: this.tile.getX(),
tileY: this.tile.getY(),
attackType: this.attackType,
id: this.id,
actions: this.getUnitActionsJSON(),
queuedTiles: queuedTilesJSON,
remainingMovement: this.availableMovement,
defaultMoveDistance: this.defaultMoveDistance,
};
}

Expand Down

0 comments on commit 0ac6cd5

Please sign in to comment.