Skip to content

Commit

Permalink
chase mechanism fix
Browse files Browse the repository at this point in the history
  • Loading branch information
keshav2010 committed Mar 2, 2024
1 parent c071879 commit 948a0b4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 21 deletions.
6 changes: 4 additions & 2 deletions .env-example
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
TICKRATE=24
COUNTDOWN=5
COUNTDOWN=5000
COUNTDOWN_SPAWN_SELECTION=10000
COUNTDOWN_DEFAULT=15000
MAX_SESSION_PER_WORKER=10
MINIMUM_PLAYERS_PER_SESSION=2
MINIMUM_PLAYERS_PER_SESSION=1
29 changes: 28 additions & 1 deletion gameserver/commands/OnSoldierAttackCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,36 @@ import { Command } from "@colyseus/command";
import { SessionRoom } from "../SessionRoom";
import { Client } from "colyseus";
import { CommandPayload } from "./CommandPayloadType";
import { SoldierState } from "../schema/SoldierState";
export class OnSoldierAttackCommand extends Command<
SessionRoom,
CommandPayload
> {
execute({ client, message, gameManager }: CommandPayload) {}
execute({
client,
message,
gameManager,
}: CommandPayload<{
soldiers: string[];
targetPlayerId: string;
targetSoldierId: string;
}>) {
console.log("Soldier Attack Requested ", message);
const attacker = this.state.getPlayer(client.id);
const victim = this.state.getPlayer(message.targetPlayerId);
if (!attacker || !victim) {
return;
}
const targetSoldier = victim.getSoldier(message.targetSoldierId);
if (!targetSoldier) {
return;
}

const attackerUnits = attacker
.getAllSoldiers()
.filter((soldier) => message.soldiers.includes(soldier.id));
attackerUnits.forEach((unit: SoldierState) => {
unit.attackUnit(targetSoldier, gameManager!);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export default {
soldier: SoldierState;
}) => {
if (!soldier.hasReachedDestination()) {
console.log("soldier not at destination, moving..");
soldier.stateMachine.controller.send("Move");
return;
}
Expand Down Expand Up @@ -129,13 +128,7 @@ export default {

//if attacked soldier unit dead, update server-state and schedule update for client.
if (attackTarget.health === 0) {
let isRemoved = stateManager.removeSoldier(attackTarget);
if (!isRemoved)
console.log(
`Soldier ID#${
attackTarget?.getSceneItem().id
} is probably already removed.`
);
stateManager.removeSoldier(attackTarget);
soldier.setAttackTarget(attackTarget);
soldier.stateMachine.controller.send("TargetKilled");
}
Expand Down Expand Up @@ -229,30 +222,37 @@ export default {
if (!soldierAttackTarget) {
soldier.stateMachine.controller.send("TargetLost");
return;
}
}

let seperationForce = soldier.getSeperationVector(stateManager);
let steerForce = soldier.getSteerVector(
const seperationForce = soldier.getSeperationVector(stateManager);
const steerForce = soldier.getSteerVector(
soldierAttackTarget.getSceneItem().pos
);
soldier.applyForce(seperationForce, delta);
soldier.applyForce(steerForce, delta);

soldier.setTargetPosition(soldierAttackTarget.getSceneItem().pos);
soldier.targetPositionX = soldierAttackTarget.getSceneItem().pos.x;
soldier.targetPositionY = soldierAttackTarget.getSceneItem().pos.y;

soldier.expectedPositionX = soldierAttackTarget.getSceneItem().pos.x;
soldier.expectedPositionY = soldierAttackTarget.getSceneItem().pos.y;

let distToTarget = new SAT.Vector()
const distToTarget = new SAT.Vector()
.copy(soldierAttackTarget.getSceneItem().pos)
.sub(soldier.getSceneItem().pos)
.len();
if (distToTarget <= SoldierConstants.DESIRED_DIST_FROM_TARGET) {
soldier.stateMachine.controller.send("TargetInRange");
}
} catch (err) {
console.log(err);
console.error(err);
if (soldierAttackTarget) {
soldier.setTargetPosition(soldierAttackTarget.getSceneItem().pos);
soldier.setExpectedPosition(soldierAttackTarget.getSceneItem().pos);
}
soldier.targetPositionX = soldierAttackTarget.getSceneItem().pos.x;
soldier.targetPositionY = soldierAttackTarget.getSceneItem().pos.y;

soldier.expectedPositionX = soldierAttackTarget.getSceneItem().pos.x;
soldier.expectedPositionY = soldierAttackTarget.getSceneItem().pos.y;
}
soldier.setAttackTarget(null);
}
},
Expand Down
2 changes: 1 addition & 1 deletion public/scenes/GameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export class GameScene extends BaseScene {
networkManager.sendEventToServer(
PacketType.ByClient.SOLDIER_ATTACK_REQUESTED,
{
soldiers: selectedSoldiersForAttack.map((v) => v.id).join(","),
soldiers: selectedSoldiersForAttack.map((v) => v.id),
targetPlayerId: targetSoldier.playerId,
targetSoldierId: targetSoldier.id,
}
Expand Down

0 comments on commit 948a0b4

Please sign in to comment.