Skip to content

Commit

Permalink
Add VectorState for soldier position vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
keshav2010 committed May 8, 2024
1 parent e9d6116 commit de499b0
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 74 deletions.
10 changes: 4 additions & 6 deletions gameserver/core/types/IBoidAgent.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
export interface IBoidAgent {
currentPositionX: number;
currentPositionY: number;
currentPosition: { x: number; y: number };
setPosition: (arg: SAT.Vector) => void;

// position ideally intended by client
targetPositionX: number;
targetPositionY: number;
targetPosition: { x: number; y: number };
setTargetPosition: (arg: SAT.Vector) => void;

// fallback position, over target-pos
expectedPositionX: number;
expectedPositionY: number;
expectedPosition: {x: number; y: number};

getExpectedPosition: (...args: any) => SAT.Vector;
setExpectedPosition: (arg: SAT.Vector) => void;

Expand Down
40 changes: 15 additions & 25 deletions gameserver/schema/SoldierState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ISceneItem } from "../core/types/ISceneItem";
import { TypeQuadtreeItem } from "../core/types/TypeQuadtreeItem";
import { IBoidAgent } from "../core/types/IBoidAgent";
import * as helper from "../helpers";
import { VectorState } from "./VectorState";
function mapRange(
val: number,
mapRangeStart: number,
Expand All @@ -27,16 +28,13 @@ function mapRange(
}

export class SoldierState extends Schema implements ISceneItem, IBoidAgent {
@type("number") currentPositionX: number = 0;
@type("number") currentPositionY: number = 0;
@type(VectorState) currentPosition: VectorState = new VectorState();

// pos possible for agent. (fallback for targetPosition)
@type("number") expectedPositionX: number = 0;
@type("number") expectedPositionY: number = 0;
@type(VectorState) expectedPosition: VectorState = new VectorState();

// pos requested by client.
@type("number") targetPositionX: number = 0;
@type("number") targetPositionY: number = 0;
@type(VectorState) targetPosition: VectorState = new VectorState();

@type("string") type: SoldierType = "SPEARMAN";

Expand Down Expand Up @@ -83,14 +81,10 @@ export class SoldierState extends Schema implements ISceneItem, IBoidAgent {
this.attackTarget = null;
this.targetVector = null;

this.currentPositionX = x;
this.currentPositionY = y;

this.expectedPositionX = x;
this.expectedPositionY = y;

this.targetPositionX = x;
this.targetPositionY = y;
const v = new SAT.Vector(x,y);
this.currentPosition.setVector(v);
this.expectedPosition.setVector(v);
this.targetPosition.setVector(v);

this.type = soldierType;

Expand All @@ -114,7 +108,7 @@ export class SoldierState extends Schema implements ISceneItem, IBoidAgent {
}

getExpectedPosition() {
return new SAT.Vector(this.expectedPositionX, this.expectedPositionY);
return this.expectedPosition.getVector();
}

setAttackTarget(target: SoldierState | null) {
Expand All @@ -135,8 +129,7 @@ export class SoldierState extends Schema implements ISceneItem, IBoidAgent {
this.sceneItemRef.pos.copy(vec);
this.sceneItemRef.x = vec.x;
this.sceneItemRef.y = vec.y;
this.currentPositionX = vec.x;
this.currentPositionY = vec.y;
this.currentPosition.setVector(vec);
}

getDistanceFromExpectedPosition() {
Expand All @@ -157,19 +150,16 @@ export class SoldierState extends Schema implements ISceneItem, IBoidAgent {
}

setTargetPosition(vec: SAT.Vector) {
this.targetPositionX = vec.x;
this.targetPositionY = vec.y;
this.expectedPositionX = vec.x;
this.expectedPositionY = vec.y;
this.targetPosition.setVector(vec);
this.expectedPosition.setVector(vec);
this.hasReachedDestination();
this.attackTarget = null;
this.steeringVector.scale(0);
this.velocityVector.scale(0);
this.stateMachine.controller.send("Move");
}
setExpectedPosition(vec: SAT.Vector) {
this.expectedPositionX = vec.x;
this.expectedPositionY = vec.y;
this.expectedPosition.setVector(vec);
}

getState() {
Expand Down Expand Up @@ -304,8 +294,8 @@ export class SoldierState extends Schema implements ISceneItem, IBoidAgent {
// update position so it doesnt overlap with colliding body.
soldierA.setPosition(
new SAT.Vector(
soldierA.currentPositionX - res.overlapV.x,
soldierA.currentPositionY - res.overlapV.y
soldierA.currentPosition.x - res.overlapV.x,
soldierA.currentPosition.y - res.overlapV.y
)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default {
const isSameTeam = sceneItem.playerId === soldier.playerId;
return !isSelf && isSameTeam;
});

for (let i = 0; i < nearbyAllyUnitsAttacked.length; i++) {
let unit = stateManager.scene.getSceneItemById<SoldierState>(
nearbyAllyUnitsAttacked[i].id
Expand All @@ -48,10 +48,11 @@ export default {
const isDifferentTeam = unit.playerId !== soldier.playerId;
if (!unit || isSelf || isDifferentTeam) continue;

const isAllyDefendingOrAttacking = ["Defend", "Attack"].includes(unit.getState());
if(!isAllyDefendingOrAttacking)
continue;

const isAllyDefendingOrAttacking = ["Defend", "Attack"].includes(
unit.getState()
);
if (!isAllyDefendingOrAttacking) continue;

//if nearby friendly unit is either defending/attacking, then assist it.
const enemy = unit.getAttackTarget();
if (!enemy) continue;
Expand Down Expand Up @@ -92,14 +93,18 @@ export default {
if (unit.id === soldier.id) return;

const nearbySoldierUnit =
stateManager.scene.getSceneItemById<SoldierState>(unit.id);
stateManager.scene.getSceneItemById<SoldierState>(unit.id);
if (!nearbySoldierUnit) return;
// if nearby unit (of same team) has same destination (approx.)
let overlapExpectedPos =
new SAT.Vector()
.copy(nearbySoldierUnit.getExpectedPosition())
.sub(soldier.getExpectedPosition())
.len() <= Math.max(MOVABLE_UNIT_CONSTANTS.MAX_TARGETPOS_OVERLAP_DIST, soldier.radius*2.1);
.len() <=
Math.max(
MOVABLE_UNIT_CONSTANTS.MAX_TARGETPOS_OVERLAP_DIST,
soldier.radius * 2.1
);

let anyOneAtDest =
nearbySoldierUnit.hasReachedDestination() ||
Expand Down Expand Up @@ -132,7 +137,7 @@ export default {
.copy(attackTarget.getSceneItem().pos)
.sub(soldier.getSceneItem().pos)
.len();

if (distToTarget > MOVABLE_UNIT_CONSTANTS.DISTANCE_DURING_ATTACK) {
soldier.stateMachine.controller.send("TargetNotInRange");
return;
Expand Down Expand Up @@ -190,9 +195,8 @@ export default {
stateManager.getAlliance(soldier.playerId, unitSoldier.playerId) !==
AllianceTypes.ENEMIES;

if (!unitSoldier || isSelf || isAllianceStable)
continue;

if (!unitSoldier || isSelf || isAllianceStable) continue;

let distBetweenUnits = new SAT.Vector()
.copy(unitSoldier.getSceneItem().pos)
.sub(soldier.getSceneItem().pos)
Expand Down Expand Up @@ -255,14 +259,14 @@ export default {
soldier.applyForce(seperationForce);
soldier.applyForce(steerForce);

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

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

soldier.move(delta, stateManager);

const distToTarget = new SAT.Vector()
.copy(soldierAttackTarget.getSceneItem().pos)
.sub(soldier.getSceneItem().pos)
Expand All @@ -274,13 +278,9 @@ export default {
console.error(err);
if (soldierAttackTarget) {
const targetPos = soldierAttackTarget.getSceneItem().getPosition();
soldier.targetPositionX = targetPos.x;
soldier.targetPositionY = targetPos.y;

soldier.expectedPositionX = targetPos.x;
soldier.expectedPositionY = targetPos.y;
}
else soldier.setAttackTarget(null);
soldier.targetPosition.setVector(targetPos);
soldier.setExpectedPosition(targetPos);
} else soldier.setAttackTarget(null);
}
},
} as IStateActions;
21 changes: 7 additions & 14 deletions public/scenes/GameScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ export class GameScene extends BaseScene {
const spearmen = new Spearman(
this,
soldier.id,
soldier.currentPositionX,
soldier.currentPositionY,
soldier.currentPosition.x,
soldier.currentPosition.y,
"texture_spearman",
null,
[ownerPlayer.colorR, ownerPlayer.colorG, ownerPlayer.colorB],
Expand Down Expand Up @@ -182,8 +182,8 @@ export class GameScene extends BaseScene {
}

phaserSceneObject.setServerPosition(
soldierState.currentPositionX,
soldierState.currentPositionY
soldierState.currentPosition.x,
soldierState.currentPosition.y
);
}

Expand Down Expand Up @@ -468,16 +468,10 @@ export class GameScene extends BaseScene {
);

this.AddStateChangeListener(
soldier.listen("currentPositionX", (value, prevValue) => {
soldier.currentPosition.onChange(() => {
this.onSoldierPositionChanged(player.id, soldier.id);
}),
`currentPosX-${soldier.id}`
);
this.AddStateChangeListener(
soldier.listen("currentPositionY", (value, prevValue) => {
this.onSoldierPositionChanged(player.id, soldier.id);
}),
`currentPosY-${soldier.id}`
`currentPos-${soldier.id}`
);
})
);
Expand All @@ -487,8 +481,7 @@ export class GameScene extends BaseScene {
const soldierId = soldier.id;
// remove relevant listeners for each soldier.
this.DestroyStateChangeListener(`health-${soldierId}`);
this.DestroyStateChangeListener(`currentPosX-${soldierId}`);
this.DestroyStateChangeListener(`currentPosY-${soldierId}`);
this.DestroyStateChangeListener(`currentPos-${soldierId}`);

this.onSoldierRemoved(soldierId, player.id);
})
Expand Down
4 changes: 2 additions & 2 deletions public/scenes/SpawnSelectionScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ export class SpawnSelectionScene extends BaseScene {
const sessionState = networkManager.getState()!;
SessionStateClientHelpers.getPlayers(sessionState).forEach((player) => {
this.AddStateChangeListener(
player.listen("pos", (value) => {
this.showSpawnFlag(networkManager, value.x, value.y, player.id);
player.pos.onChange(() => {
this.showSpawnFlag(networkManager, player.pos.x, player.pos.y, player.id);
}),
`${player.id}_pos_spawnFlag_update`
);
Expand Down
8 changes: 4 additions & 4 deletions public/soldiers/BaseSoldier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ export class BaseSoldier extends Phaser.GameObjects.Sprite {
this.debugBrush.setDepth(1);
this.debugBrush.fillStyle(0xffffff, 0.3);
this.debugBrush.fillCircle(
soldierState?.expectedPositionX! + this.width/2,
soldierState?.expectedPositionY! + this.width/2,
soldierState?.expectedPosition.x! + this.width/2,
soldierState?.expectedPosition.y! + this.width/2,
this.height / 4
);

Expand All @@ -206,8 +206,8 @@ export class BaseSoldier extends Phaser.GameObjects.Sprite {
new Phaser.Geom.Line(
this.x + this.width/2,
this.y + this.width/2,
soldierState!.expectedPositionX + this.width/2,
soldierState!.expectedPositionY + this.width/2
soldierState!.expectedPosition.x + this.width/2,
soldierState!.expectedPosition.y + this.width/2
)
);

Expand Down

0 comments on commit de499b0

Please sign in to comment.