Skip to content

Commit

Permalink
fixed movement
Browse files Browse the repository at this point in the history
  • Loading branch information
keshav2010 committed Apr 9, 2024
1 parent 62e823d commit 59804e6
Showing 1 changed file with 10 additions and 23 deletions.
33 changes: 10 additions & 23 deletions gameserver/schema/SoldierState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,17 @@ export class SoldierState extends Schema implements ISceneItem, IBoidAgent {
getDistanceFromExpectedPosition() {
const expectedPos = this.getExpectedPosition().clone();
let distanceToExpectedPos = expectedPos.sub(this.getSceneItem().pos).len();
if (
distanceToExpectedPos <=
MOVABLE_UNIT_CONSTANTS.MAX_DISTANCE_OFFSET_ALLOWED_FROM_EXPECTED_POSITION
) {
this.isAtDestination = true;
} else this.isAtDestination = false;
return distanceToExpectedPos;
}

hasReachedDestination() {
let distanceToExpectedPos = this.getDistanceFromExpectedPosition();
const currentState = this.stateMachine.currentState;
this.isAtDestination =
distanceToExpectedPos <=
MOVABLE_UNIT_CONSTANTS.MAX_DISTANCE_OFFSET_ALLOWED_FROM_EXPECTED_POSITION;
distanceToExpectedPos <= 2 ||
(distanceToExpectedPos <=
MOVABLE_UNIT_CONSTANTS.MAX_DISTANCE_OFFSET_ALLOWED_FROM_EXPECTED_POSITION &&
currentState !== "Idle");
return this.isAtDestination;
}

Expand Down Expand Up @@ -184,21 +181,11 @@ export class SoldierState extends Schema implements ISceneItem, IBoidAgent {
const desiredVector = new SAT.Vector()
.copy(expectedPos)
.sub(this.getSceneItem().pos);

const distanceFromExpectedPosition = desiredVector.len();

if (
distanceFromExpectedPosition <=
MOVABLE_UNIT_CONSTANTS.MAX_DISTANCE_OFFSET_ALLOWED_FROM_EXPECTED_POSITION
) {
desiredVector.scale(0);
} else desiredVector.normalize().scale(this.speed);

desiredVector.normalize().scale(this.speed);
const steerVector = desiredVector.clone().sub(this.velocityVector);

if (steerVector.len() > MOVABLE_UNIT_CONSTANTS.MAX_STEER_FORCE)
steerVector.normalize().scale(MOVABLE_UNIT_CONSTANTS.MAX_STEER_FORCE);
return steerVector;
return steerVector.len() > MOVABLE_UNIT_CONSTANTS.MAX_STEER_FORCE
? steerVector.normalize().scale(MOVABLE_UNIT_CONSTANTS.MAX_STEER_FORCE)
: steerVector;
}

getSeperationVector(
Expand Down Expand Up @@ -301,7 +288,7 @@ export class SoldierState extends Schema implements ISceneItem, IBoidAgent {
if (!groupLeaderRef) {
this.groupLeaderId = null;
this.offsetFromPosition = new SAT.Vector(0, 0);
}
}
stateManager.scene.checkCollisionOnObject(
this,
(
Expand Down

0 comments on commit 59804e6

Please sign in to comment.