Skip to content

Commit

Permalink
reduce health of tower when attacked
Browse files Browse the repository at this point in the history
  • Loading branch information
keshav2010 committed Apr 9, 2024
1 parent 59804e6 commit a03daf3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
5 changes: 4 additions & 1 deletion gameserver/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export const MOVABLE_UNIT_CONSTANTS = {

MAX_DISTANCE_OFFSET_ALLOWED_FROM_EXPECTED_POSITION: 50,

// how much distance between two units so they can ttack each other
DISTANCE_DURING_ATTACK: 70,

/**
* NEARBY_SEARCH_RADI serves following purpose
* 1. detect nearby allies under attack
Expand All @@ -37,5 +40,5 @@ export const MOVABLE_UNIT_CONSTANTS = {
*
*/
MINIMUM_SEPERATION_DISTANCE_BETWEEN_UNITS: 30, //to initiate repulsion force
MAX_TARGETPOS_OVERLAP_DIST: 70,
MAX_TARGETPOS_OVERLAP_DIST: 50,
};
12 changes: 7 additions & 5 deletions gameserver/core/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ export class Scene extends Quadtree<TypeQuadtreeItem> {
y: number,
searchRadius: number,
type?: SceneObjectType[]
) {
let result = this.colliding({x, y}, (a, b) => {
) {
let result = this.colliding({ x, y }, (a, b) => {
// Create circles for each object
const aCircle = new SAT.Circle(
new SAT.Vector(a.x, a.y),
Math.max(searchRadius, a.r || 0),
Math.max(searchRadius, a.r || 0)
);
const bCircle = new SAT.Circle(new SAT.Vector(b.x, b.y), b.r);

Expand All @@ -66,19 +66,21 @@ export class Scene extends Quadtree<TypeQuadtreeItem> {

if (type) result = result.filter((body) => type?.includes(body.type));
return result;
}
}

//Check if unit/sceneItem is colliding with other units/soldiers
checkCollisionOnObject(
sceneItem: ISceneItem,
bodyTypeToCheck: SceneObjectType[] | undefined,
callback: (arg0: SAT.Response, arg1: ISceneItem[]) => void
) {
const mainCollidingObject = sceneItem.getSceneItem();
//fetch all bodies which are colliding with the soldier specified by x,y,r in arg.
let collidingBodies = this.getNearbyUnits(
mainCollidingObject.pos.x,
mainCollidingObject.pos.y,
mainCollidingObject.r
mainCollidingObject.r,
bodyTypeToCheck
);
//Colliding Bodies will always have 1 element, which is the soldier itself.

Expand Down
17 changes: 16 additions & 1 deletion gameserver/schema/SoldierState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SceneObject } from "../core/types/SceneObject";
import { AllianceTypes } from "../AllianceTracker";
import SAT from "sat";
import { MOVABLE_UNIT_CONSTANTS } from "../config";
import { GameStateManagerType } from "./PlayerState";
import { GameStateManagerType, PlayerState } from "./PlayerState";
import { ISceneItem } from "../core/types/ISceneItem";
import { TypeQuadtreeItem } from "../core/types/TypeQuadtreeItem";
import { IBoidAgent } from "../core/types/IBoidAgent";
Expand Down Expand Up @@ -291,6 +291,7 @@ export class SoldierState extends Schema implements ISceneItem, IBoidAgent {
}
stateManager.scene.checkCollisionOnObject(
this,
['MOVABLE'],
(
res: {
a: SoldierState;
Expand Down Expand Up @@ -333,6 +334,20 @@ export class SoldierState extends Schema implements ISceneItem, IBoidAgent {
}
);

let enemyTowers = stateManager.scene.getNearbyUnits(
this.currentPositionX,
this.currentPositionY,
50,
["FIXED"]
);
enemyTowers = enemyTowers.filter((quadtreeItem) =>
(stateManager.getPlayer(quadtreeItem.id) && quadtreeItem.id !== this.playerId)
);
enemyTowers
.map((d) => stateManager.getPlayer(d.id))
.forEach((playerBase) => {
if (playerBase) playerBase.spawnFlagHealth -= 0.5 * delta;
});
this.stateMachine.tick({ delta, stateManager, soldier: this });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default {
new SAT.Vector()
.copy(nearbySoldierUnit.getExpectedPosition())
.sub(soldier.getExpectedPosition())
.len() <= MOVABLE_UNIT_CONSTANTS.MAX_TARGETPOS_OVERLAP_DIST;
.len() <= Math.max(MOVABLE_UNIT_CONSTANTS.MAX_TARGETPOS_OVERLAP_DIST, soldier.radius*2.1);

let anyOneAtDest =
nearbySoldierUnit.hasReachedDestination() ||
Expand Down Expand Up @@ -254,7 +254,7 @@ export default {
.copy(soldierAttackTarget.getSceneItem().pos)
.sub(soldier.getSceneItem().pos)
.len();
if (distToTarget <= MOVABLE_UNIT_CONSTANTS.NEARBY_SEARCH_RADI) {
if (distToTarget <= MOVABLE_UNIT_CONSTANTS.DISTANCE_DURING_ATTACK) {
soldier.stateMachine.controller.send("TargetInRange");
}
} catch (err) {
Expand Down

0 comments on commit a03daf3

Please sign in to comment.