Skip to content

Commit

Permalink
change soldier speed based on tile type
Browse files Browse the repository at this point in the history
  • Loading branch information
keshav2010 committed May 15, 2024
1 parent c46c800 commit bc12b2a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
9 changes: 7 additions & 2 deletions gameserver/schema/PlayerState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { SceneObject } from "../core/types/SceneObject";
import SAT from "sat";
import { ISceneItem } from "../core/types/ISceneItem";
import { VectorState } from "./VectorState";
import { SessionState } from "./SessionState";
export type GameStateManagerType = GameStateManager<PlayerState>;

export class SpawnRequest extends Schema {
Expand Down Expand Up @@ -111,13 +112,17 @@ export class PlayerState extends Schema implements ISceneItem {
return newSoldier.id;
}

public tick(deltaTime: number, gameStateManager: GameStateManagerType) {
public tick(
deltaTime: number,
gameStateManager: GameStateManagerType,
sessionState: SessionState
) {
this.resources += this.resourceGrowthRateHz * deltaTime;
this.processSpawnRequest(deltaTime, gameStateManager.scene);

//TODO: tick each soldier
this.soldiers.forEach((soldier) => {
soldier.tick(deltaTime, gameStateManager);
soldier.tick(deltaTime, gameStateManager, sessionState);
});

this.resourceGrowthRateHz = this.resourceGrowthRateHz - 0.1 * deltaTime;
Expand Down
16 changes: 15 additions & 1 deletion gameserver/schema/SoldierState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { TypeQuadtreeItem } from "../core/types/TypeQuadtreeItem";
import { IBoidAgent } from "../core/types/IBoidAgent";
import * as helper from "../helpers";
import { VectorState } from "./VectorState";
import { SessionState } from "./SessionState";
function mapRange(
val: number,
mapRangeStart: number,
Expand Down Expand Up @@ -269,10 +270,23 @@ export class SoldierState extends Schema implements ISceneItem, IBoidAgent {
return this.velocityVector.clone();
}

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

const centerPos = this.currentPosition
.getVector()
.add(new SAT.Vector(16, 16));
const currentTile = sessionState.tilemap.getTileTypeAt(
centerPos.x,
centerPos.y
);
if (currentTile === "water") {
this.speed = SoldierTypeConfig[this.type].speed * 0.4;
} else if (currentTile === "dirt") {
this.speed = SoldierTypeConfig[this.type].speed * 0.7;
} else this.speed = SoldierTypeConfig[this.type].speed;

stateManager.scene.checkCollisionOnObject(
this,
["MOVABLE"],
Expand Down
13 changes: 13 additions & 0 deletions gameserver/schema/TilemapState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ const TilesType = {
grass: 56,
water: 154,
};
const TilesTypeById: { [key: number]: string } = {
16: "dirt",
56: "grass",
154: "water",
};
export class TilemapState extends Schema {
// Key : sessionId
@type(["number"]) tilemap1D = new ArraySchema<number>();
Expand All @@ -28,6 +33,14 @@ export class TilemapState extends Schema {
this.generateTilemap();
}

getTileTypeAt(x: number, y: number) {
const col = Math.floor(x/this.tilewidth);
const row = Math.floor(y/this.tileheight);
const index1D = col + row * this.tilemapWidth;
const tileId = this.tilemap1D.at(index1D);
return TilesTypeById[tileId];
}

async generateTilemap() {
this.ready = false;
const { tilemapWidth, tilemapHeight } = this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default {
const allClientsLoaded =
sessionState.countLoadedPlayers() >= sessionState.players.size;

if(!allClientsLoaded) {
if (!allClientsLoaded) {
sessionState.countdown = SERVER_CONFIG.COUNTDOWN;
}
if (allClientsLoaded && atleastMinimumPlayersJoined) {
Expand Down Expand Up @@ -100,7 +100,7 @@ export default {
}
var deltaTime = delta / 1000;
playersConnected.forEach((player) => {
player.tick(deltaTime, gameStateManager);
player.tick(deltaTime, gameStateManager, sessionState);
if (player.castleHealth === 0) {
sessionState.removePlayer(player.id, gameStateManager);
}
Expand Down

0 comments on commit bc12b2a

Please sign in to comment.