Skip to content

Commit

Permalink
allow player to destroy castle
Browse files Browse the repository at this point in the history
  • Loading branch information
keshav2010 committed May 26, 2024
1 parent 3fa5457 commit 3ab0c06
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 11 deletions.
2 changes: 1 addition & 1 deletion gameserver/commands/OnCaptureFlagCreateCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class OnCaptureFlagCreateCommand extends Command<
const sessionState = this.state;
gameManager
?.getPlayer(client.id)
?.createCaptureFlag(message.x, message.y, sessionState);
?.createCaptureFlag(message.x, message.y, sessionState, gameManager);
} catch (error) {
console.error(error);
}
Expand Down
4 changes: 2 additions & 2 deletions gameserver/core/types/SceneObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import SAT from "sat";
* @class SceneObject
* @classdesc Any object that is meant to be part of the Scene should extend this class.
*/
export type SceneObjectType = "FIXED" | "MOVABLE";
export type SceneObjectType = "FIXED" | "MOVABLE" | "CAPTURE_FLAG";

export class SceneObject extends SAT.Box {
readonly id: string;
Expand All @@ -24,7 +24,7 @@ export class SceneObject extends SAT.Box {
y: number,
size = 32,
type: SceneObjectType,
collidable: boolean = true
collidable: boolean = true,
) {
super(new SAT.Vector(x, y), size, size);
this.width = size;
Expand Down
5 changes: 4 additions & 1 deletion gameserver/schema/CaptureFlagState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export class CaptureFlagState extends Schema implements ISceneItem {
constructor(x: number, y: number) {
super();
this.pos = new VectorState(x, y);
this.sceneItemRef = new SceneObject(this.id, x, y, 64, "FIXED", false);
this.sceneItemRef = new SceneObject(this.id, x, y, 64, "CAPTURE_FLAG", false);
}
setHealth(health: number) {
this.health = Math.max(0,health);
}
getSceneItem() {
return this.sceneItemRef;
Expand Down
24 changes: 22 additions & 2 deletions gameserver/schema/PlayerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ export class PlayerState extends Schema implements ISceneItem {
return this.sceneItemRef;
}

createCaptureFlag(x: number, y: number, sessionState: SessionState) {
createCaptureFlag(
x: number,
y: number,
sessionState: SessionState,
gameManager: GameStateManagerType
) {
const scene = gameManager.scene;
if (this.captureFlags.length >= 4) {
return;
}
Expand All @@ -111,10 +117,15 @@ export class PlayerState extends Schema implements ISceneItem {
if (tileOwner !== "NONE") return;

this.resources = this.resources - CaptureFlagState.cost;

const captureFlag = new CaptureFlagState(x, y);
this.captureFlags.push(captureFlag);
scene.addSceneItem(captureFlag);

this.resourceGrowthRateHz =
this.resourceGrowthRateHz * (1 + 0.2 * this.captureFlags.length);

sessionState.onCaptureFlagAdded(captureFlag.id, this.id);
sessionState.tilemap.updateOwnershipMap(sessionState.getPlayers());
}

Expand All @@ -123,16 +134,24 @@ export class PlayerState extends Schema implements ISceneItem {
sessionState: SessionState,
gameManager: GameStateManagerType
) {
const scene = gameManager.scene;
const flagObj = this.captureFlags.findIndex((flag) => flag.id === flagId);
if (flagObj < 0) {
return;
}
gameManager.scene.removeSceneItem(flagId);
this.captureFlags.deleteAt(flagObj);
scene.removeSceneItem(flagId);

sessionState.onCaptureFlagRemoved(flagId);
this.resourceGrowthRateHz = 2 * (1 + 0.2 * this.captureFlags.length);
sessionState.tilemap.updateOwnershipMap(sessionState.getPlayers());
}
removeBulkCaptureFlag(flagIds: string[], sessionState: SessionState, gameManager: GameStateManagerType) {
removeBulkCaptureFlag(
flagIds: string[],
sessionState: SessionState,
gameManager: GameStateManagerType
) {
let flagsRemoved = 0;
flagIds.forEach((flagId) => {
const flagIndex = this.captureFlags.findIndex(
Expand All @@ -142,6 +161,7 @@ export class PlayerState extends Schema implements ISceneItem {
return;
}
gameManager.scene.removeSceneItem(flagId);
sessionState.onCaptureFlagRemoved(flagId);
flagsRemoved += this.captureFlags.deleteAt(flagIndex) ? 1 : 0;
});

Expand Down
10 changes: 10 additions & 0 deletions gameserver/schema/SessionState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export class SessionState extends Schema {
@type("number") countdown: number = SERVER_CONFIG.COUNTDOWN_DEFAULT;
@type("string") mapId: string = "map1";
@type(TilemapState) tilemap = new TilemapState();

captureFlagIdToParentId: Map<string, string> = new Map<string, string>();
constructor() {
super();
}
Expand All @@ -38,6 +40,14 @@ export class SessionState extends Schema {
}
return playersArr;
}

onCaptureFlagAdded(flagId: string, parentId: string) {
this.captureFlagIdToParentId.set(flagId, parentId);
}
onCaptureFlagRemoved(flagId: string) {
this.captureFlagIdToParentId.delete(flagId);
}

public removePlayer(sessionId: string, gameManager: GameStateManagerType) {
const player = this.players.get(sessionId);
if (!player) {
Expand Down
47 changes: 42 additions & 5 deletions gameserver/schema/SoldierState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class SoldierState extends Schema implements ISceneItem, IBoidAgent {
this.attackTarget = null;
this.targetVector = null;

const v = new SAT.Vector(x,y);
const v = new SAT.Vector(x, y);
this.currentPosition.setVector(v);
this.expectedPosition.setVector(v);
this.targetPosition.setVector(v);
Expand Down Expand Up @@ -270,7 +270,11 @@ export class SoldierState extends Schema implements ISceneItem, IBoidAgent {
return this.velocityVector.clone();
}

tick(delta: number, stateManager: GameStateManagerType, sessionState: SessionState) {
tick(
delta: number,
stateManager: GameStateManagerType,
sessionState: SessionState
) {
this.currentState = this.stateMachine
.currentState as keyof typeof SoldierStateMachineJSON.states;

Expand Down Expand Up @@ -341,7 +345,7 @@ export class SoldierState extends Schema implements ISceneItem, IBoidAgent {
);
const soldierCenterPosition = this.getSceneItem().getCircleCenter();

const enemyTowers = stateManager.scene
const enemyCastles = stateManager.scene
.getNearbyUnits(soldierCenterPosition.x, soldierCenterPosition.y, 100, [
"FIXED",
])
Expand All @@ -359,15 +363,48 @@ export class SoldierState extends Schema implements ISceneItem, IBoidAgent {
)
.map((d) => stateManager.getPlayer(d.id));

enemyTowers.forEach((enemyTower) => {
enemyCastles.forEach((enemyTower) => {
if (!enemyTower) return;
enemyTower.castleHealth = Math.max(
0,
enemyTower.castleHealth - 0.5 * delta
);
if(enemyTower.castleHealth <= 0)
if (enemyTower.castleHealth <= 0)
sessionState.removePlayer(enemyTower.id, stateManager);
});

let nearbyCaptureFlags = stateManager.scene.getNearbyUnits(
soldierCenterPosition.x,
soldierCenterPosition.y,
100,
["CAPTURE_FLAG"]
);

const enemyCaptureFlags = nearbyCaptureFlags.filter(
(quadtreeItem) =>
sessionState.captureFlagIdToParentId.get(quadtreeItem.id) !==
this.playerId
);

enemyCaptureFlags.forEach((flagQuatree) => {
const flagOwnerId = sessionState.captureFlagIdToParentId.get(
flagQuatree.id
);
if (!flagOwnerId) return;
const flag = sessionState
.getPlayer(flagOwnerId)
?.captureFlags.toArray()
.filter((flag) => flag.id === flagQuatree.id);

if (flag && flag?.[0]) {
flag[0].setHealth(flag[0].health - 0.5 * delta);
if(flag[0].health === 0)
sessionState
.getPlayer(flagOwnerId)
?.removeCaptureFlag(flag[0].id, sessionState, stateManager);
}
});

this.stateMachine.tick({ delta, stateManager, soldier: this });
}

Expand Down

0 comments on commit 3ab0c06

Please sign in to comment.