Skip to content

Commit

Permalink
Merge pull request #39 from keshav2010/feat/territory-flag
Browse files Browse the repository at this point in the history
Feat/territory flag
  • Loading branch information
keshav2010 authored May 26, 2024
2 parents 3ab5ad9 + 3ab0c06 commit 18bb4c1
Show file tree
Hide file tree
Showing 31 changed files with 1,252 additions and 493 deletions.
4 changes: 3 additions & 1 deletion common/PacketType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ export enum ClientToServerPacketType {
PLAYER_UNREADY = "plur",
SOLDIER_CREATE_REQUESTED = "slcrreq",
SPAWN_POINT_REQUESTED = "spwnpntreq",
SOLDIER_DELETED = "sldel",
SOLDIERS_DELETE_REQUESTED = "sldrdelrqstd",
SOLDIER_MOVE_REQUESTED = "slmovreq",
PLAYER_JOINED = "pljoin",
SOLDIER_ATTACK_REQUESTED = "sldierattkreq",
CLIENT_SENT_CHAT = "clientchat",
SPAWN_POINT_SELECTED = "spwnpntslct",
SOLDIER_SPAWN_REQUESTED = "unitspwnreq",
CAPTURE_FLAG_CREATE_REQUESTED = "cptrflgrqst",
CAPTURE_FLAG_DELETE_REQUESTED = "cptrflgdltrqst",
}
export enum ServerToClientPacketType {
NEW_CHAT_MESSAGE = "newcm",
Expand Down
29 changes: 29 additions & 0 deletions gameserver/commands/OnCaptureFlagCreateCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// OnJoinCommand.ts
import { Command } from "@colyseus/command";
import { SessionRoom } from "../SessionRoom";
import { CommandPayload } from "./CommandPayloadType";

interface ICaptureFlagCreate {
x: number;
y: number;
}

export class OnCaptureFlagCreateCommand extends Command<
SessionRoom,
CommandPayload
> {
execute({
client,
message,
gameManager,
}: CommandPayload<ICaptureFlagCreate>) {
try {
const sessionState = this.state;
gameManager
?.getPlayer(client.id)
?.createCaptureFlag(message.x, message.y, sessionState, gameManager);
} catch (error) {
console.error(error);
}
}
}
32 changes: 32 additions & 0 deletions gameserver/commands/OnCaptureFlagDeleteCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// OnJoinCommand.ts
import { Command } from "@colyseus/command";
import { SessionRoom } from "../SessionRoom";
import { CommandPayload } from "./CommandPayloadType";

interface ICaptureFlagDelete {
captureFlagIds: string[];
}

export class OnCaptureFlagDeleteCommand extends Command<
SessionRoom,
CommandPayload
> {
execute({
client,
message,
gameManager,
}: CommandPayload<ICaptureFlagDelete>) {
try {
const sessionState = this.state;
gameManager
?.getPlayer(client.id)
?.removeBulkCaptureFlag(
message.captureFlagIds,
sessionState,
gameManager
);
} catch (error) {
console.error(error);
}
}
}
14 changes: 6 additions & 8 deletions gameserver/commands/OnChatBroadcastCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ import { Command } from "@colyseus/command";
import { SessionRoom } from "../SessionRoom";
import { CommandPayload } from "./CommandPayloadType";
import { PacketType } from "../../common/PacketType";

interface IChatBroadcast {
sessionId: string;
message: string;
}
export class OnChatBroadcastCommand extends Command<
SessionRoom,
CommandPayload
> {
execute({
client,
message,
gameManager,
}: CommandPayload<{
sessionId: string;
message: string;
}>) {
execute({ client, message, gameManager }: CommandPayload<IChatBroadcast>) {
const sessionId = client.sessionId;
console.log(`[OnChatBroadcastCommand]: Received Chat.`, message);
this.room.broadcast(PacketType.ByServer.NEW_CHAT_MESSAGE, {
Expand Down
19 changes: 11 additions & 8 deletions gameserver/commands/OnJoinCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,29 @@ import { Command } from "@colyseus/command";
import { SessionRoom } from "../SessionRoom";
import { nanoid } from "nanoid";
import { CommandPayload } from "./CommandPayloadType";
import * as helper from '../helpers';
import SAT from 'sat';
import * as helper from "../helpers";
import SAT from "sat";
interface IJoin {
playerName?: string;
}
export class OnJoinCommand extends Command<SessionRoom, CommandPayload> {
execute({ client, message, gameManager }: CommandPayload) {
execute({ client, message, gameManager }: CommandPayload<IJoin>) {
const sessionId = client.sessionId;
const playerPos = new SAT.Vector(
(Math.random() * gameManager?.scene.getDimension().x!),
(Math.random() * gameManager?.scene.getDimension().y!)
Math.random() * gameManager?.scene.getDimension().x!,
Math.random() * gameManager?.scene.getDimension().y!
);

const clampedPos = helper.clampVector(
playerPos,
new SAT.Vector(100,100),
gameManager!.scene.getDimension()!.clone().sub(new SAT.Vector(100,100))
new SAT.Vector(100, 100),
gameManager!.scene.getDimension()!.clone().sub(new SAT.Vector(100, 100))
);
const playerState = this.state.addPlayer(
sessionId,
message?.playerName || `Player_${nanoid()}`,
clampedPos.x,
clampedPos.y,
clampedPos.y
);
if (!playerState) return;
gameManager?.addPlayer(playerState);
Expand Down
1 change: 1 addition & 0 deletions gameserver/commands/OnLeaveCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export class OnLeaveCommand extends Command<SessionRoom, CommandPayload> {
execute({ client, message, gameManager }: CommandPayload) {
const sessionId = client.sessionId;
this.state.removePlayer(sessionId, gameManager!);
this.state.tilemap.updateOwnershipMap(this.state.getPlayers());
}
}
13 changes: 12 additions & 1 deletion gameserver/commands/OnSoldierDeleteCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,16 @@ export class OnSoldierDeleteCommand extends Command<
SessionRoom,
CommandPayload
> {
execute({ client, message, gameManager }: CommandPayload) {}
execute({
client,
message,
gameManager,
}: CommandPayload<{
soldierIds: string[];
}>) {
const sessionId = client.sessionId;
message.soldierIds.forEach((id) =>
this.state.getPlayer(sessionId)?.removeSoldier(id, gameManager!)
);
}
}
13 changes: 11 additions & 2 deletions gameserver/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { OnSpawnPointSelectCommand } from "./OnSpawnPointSelectCommand";
import { CommandPayload } from "./CommandPayloadType";
import { OnChatBroadcastCommand } from "./OnChatBroadcastCommand";
import { OnPlayerLoadedCommand } from "./OnPlayerLoadedCommand";
import { OnCaptureFlagCreateCommand } from "./OnCaptureFlagCreateCommand";
import { OnCaptureFlagDeleteCommand } from "./OnCaptureFlagDeleteCommand";

export class CommandFactory {
static createCommand(
Expand All @@ -27,7 +29,7 @@ export class CommandFactory {
case PacketType.ByClient.SOLDIER_SPAWN_REQUESTED:
return new OnSpawnSoldierCommand();

case PacketType.ByClient.SOLDIER_DELETED:
case PacketType.ByClient.SOLDIERS_DELETE_REQUESTED:
return new OnSoldierDeleteCommand();

case PacketType.ByClient.SOLDIER_MOVE_REQUESTED:
Expand All @@ -41,9 +43,16 @@ export class CommandFactory {

case PacketType.ByClient.CLIENT_SENT_CHAT:
return new OnChatBroadcastCommand();

case PacketType.ByClient.CLIENT_MAP_LOADED:
return new OnPlayerLoadedCommand();

case PacketType.ByClient.CAPTURE_FLAG_CREATE_REQUESTED:
return new OnCaptureFlagCreateCommand();

case PacketType.ByClient.CAPTURE_FLAG_DELETE_REQUESTED:
return new OnCaptureFlagDeleteCommand();

default:
return null;
}
Expand Down
1 change: 0 additions & 1 deletion gameserver/core/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export class Scene extends Quadtree<TypeQuadtreeItem> {

removeSceneItem(itemId: string) {
if (!this.sceneItemMap.has(itemId)) {
console.log(`item ${itemId} not found in scene`);
return;
}
const item = this.sceneItemMap.get(itemId)!;
Expand Down
4 changes: 2 additions & 2 deletions gameserver/core/types/SceneObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import SAT from "sat";
* @class SceneObject
* @classdesc Any object that is meant to be part of the Scene should extend this class.
*/
export type SceneObjectType = "FIXED" | "MOVABLE";
export type SceneObjectType = "FIXED" | "MOVABLE" | "CAPTURE_FLAG";

export class SceneObject extends SAT.Box {
readonly id: string;
Expand All @@ -24,7 +24,7 @@ export class SceneObject extends SAT.Box {
y: number,
size = 32,
type: SceneObjectType,
collidable: boolean = true
collidable: boolean = true,
) {
super(new SAT.Vector(x, y), size, size);
this.width = size;
Expand Down
39 changes: 39 additions & 0 deletions gameserver/schema/CaptureFlagState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Schema, type } from "@colyseus/schema";
import { VectorState } from "./VectorState";
import { nanoid } from "nanoid";
import { SessionState } from "./SessionState";
import { ISceneItem } from "../core/types/ISceneItem";
import { SceneObject } from "../core/types/SceneObject";

export enum CaptureFlagStatus {
IN_PROGRESS = "in_progress",
READY = "ready",
}
export class CaptureFlagState extends Schema implements ISceneItem {
// Key : sessionId
static cost: number = 30;
@type("string") id = nanoid();
@type("number") health: number = 5;
@type(VectorState) pos = new VectorState();
@type("string") flagState: CaptureFlagStatus = CaptureFlagStatus.IN_PROGRESS;
msUntilReady: number = 10 * 1000;
sceneItemRef: SceneObject;

constructor(x: number, y: number) {
super();
this.pos = new VectorState(x, y);
this.sceneItemRef = new SceneObject(this.id, x, y, 64, "CAPTURE_FLAG", false);
}
setHealth(health: number) {
this.health = Math.max(0,health);
}
getSceneItem() {
return this.sceneItemRef;
}
tick(delta: number, sessionState: SessionState) {
if (this.flagState === CaptureFlagStatus.IN_PROGRESS) {
this.health = Math.min(this.health + delta * 2, 100);
if (this.health === 100) this.flagState = CaptureFlagStatus.READY;
}
}
}
Loading

0 comments on commit 18bb4c1

Please sign in to comment.