Skip to content

Commit

Permalink
Unit: Prevent selection of other player's units
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanGrieb committed Nov 17, 2023
1 parent 0ac6cd5 commit 189b665
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
8 changes: 8 additions & 0 deletions client/src/Unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { Game } from "./Game";
import { GameMap } from "./map/GameMap";
import { Tile } from "./map/Tile";
import { NetworkEvents } from "./network/Client";
import { AbstractPlayer } from "./player/AbstractPlayer";
import { Actor } from "./scene/Actor";
import { ActorGroup } from "./scene/ActorGroup";
import { InGameScene } from "./scene/type/InGameScene";
import { UnitDisplayInfo } from "./ui/UnitDisplayInfo";

export class UnitActionManager {
Expand Down Expand Up @@ -113,6 +115,7 @@ export class Unit extends ActorGroup {
private unitDisplayInfo: UnitDisplayInfo;
private actions: UnitAction[];
private queuedMovementTiles: Tile[];
private player: AbstractPlayer;

constructor(tile: Tile, unitJSON: JSON) {
super({
Expand Down Expand Up @@ -142,6 +145,7 @@ export class Unit extends ActorGroup {
this.attackType = unitJSON["attackType"];
this.availableMovement = unitJSON["remainingMovement"];
this.defaultMoveDistance = unitJSON["defaultMoveDistance"];
this.player = AbstractPlayer.getPlayerByName(unitJSON["player"]);

this.queuedMovementTiles = [];
for (const jsonTile of unitJSON["queuedTiles"]) {
Expand Down Expand Up @@ -377,4 +381,8 @@ export class Unit extends ActorGroup {
this.addActor(actor);
}
}

public getPlayer() {
return this.player;
}
}
5 changes: 5 additions & 0 deletions client/src/player/ClientPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ export class ClientPlayer extends AbstractPlayer {
//TODO: Cycle through units on the tile
const unit = units[0];

// Don't allow selection of other player's units
if (unit.getPlayer() != this) {
return;
}

// Clear previously defined movement paths.
this.clearMovementPath();

Expand Down
11 changes: 9 additions & 2 deletions server/src/Unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,17 @@ export class Unit {
ServerEvents.on({
eventName: "moveUnit",
parentObject: this,
callback: (data) => {
callback: (data, websocket) => {
const targetTile =
GameMap.getInstance().getTiles()[data["targetX"]][data["targetY"]];
const player = Game.getPlayerFromWebsocket(websocket);

if (this.id !== data["id"] || this.tile === targetTile) return;
if (
this.id !== data["id"] ||
this.tile === targetTile ||
this.player != player
)
return;

// Move the furthest we can possibly go, and queue the rest of tiles for next turn.

Expand Down Expand Up @@ -245,6 +251,7 @@ export class Unit {
name: this.name,
tileX: this.tile.getX(),
tileY: this.tile.getY(),
player: this.player.getName(),
attackType: this.attackType,
id: this.id,
actions: this.getUnitActionsJSON(),
Expand Down

0 comments on commit 189b665

Please sign in to comment.