Skip to content

Commit

Permalink
Merge pull request #25 from codecon-dev/feat/coop-system
Browse files Browse the repository at this point in the history
Feat/coop system
  • Loading branch information
Markkop authored Jun 17, 2023
2 parents 690e6ec + 65a02c1 commit 0eea47a
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 102 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getSpacesIdsFromEnv } from "./utils/spaces";
import { GatherManagers } from './types'
import AIManager from "./services/ai";
import ChatBotSystem from "./systems/chatbot";
import CoopSystem from "./systems/coop";
require('dotenv').config()

// Global object with GatherManager instances for each SpaceId
Expand All @@ -27,6 +28,7 @@ async function start() {
gatherManagers[spaceId] = gatherManager
}))

new CoopSystem()
new BugsSystem()
new AIManager()
new ChatBotSystem()
Expand Down
6 changes: 1 addition & 5 deletions src/interactions/playerMoves/triggerSystems.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { Game } from "@gathertown/gather-game-client";
import { ServerClientEventContext } from "@gathertown/gather-game-client/dist/src/public/utils";
import BossSystem from "../../systems/boss";
import { PlayerMovesEventData } from "../../types";
import { getPosition } from "../../utils/movement";

export async function triggerSystems(data: PlayerMovesEventData, context: ServerClientEventContext, game: Game) {
try {
const mapId = context.player?.map
if (!mapId) return
const playerNewPosition = getPosition(data)

const bossSystem = BossSystem.getInstance()
bossSystem.triggerBoss(playerNewPosition, mapId, game)
// Currently, no system is triggered by player moves

} catch (error) {
console.log(error)
Expand Down
97 changes: 0 additions & 97 deletions src/systems/boss.ts

This file was deleted.

91 changes: 91 additions & 0 deletions src/systems/coop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Game, Player } from "@gathertown/gather-game-client";
import { hasMatchingCoordinates } from "../utils/movement";
import { getMapObjectById } from "../utils/objects";
import { gatherManagers } from "..";

class CoopSystem {
private static instance: CoopSystem;
private toggles = {
isPlate1Active: false,
isPlate2Active: false,
isChestActive: false
}
private spaceId = 'cfTnBsETAY2ODiMS/Codecon 2023 - Atividades'
private mapId = 'UMRC-_nAY2kcoptLFmdTD'
private plate1Location = { x: 41, y: 20 }
private plate2Location = { x: 41, y: 24 }
private chestObjectId = 'TreasureChestClosed - rh35enYGoIjzE5wEfWAFL_2c1f045e-c237-4a56-b83c-60ab1fa5f1c7'
private blankImage = 'https://cdn.gather.town/v0/b/gather-town-dev.appspot.com/o/objects%2Fblank.png?alt=media&token=6564fd34-433a-4e08-843a-5c4b50d6f9e5';
private chestImage = 'https://cdn.gather.town/storage.googleapis.com/gather-town.appspot.com/internal-dashboard/images/onUu0ikT0JkdF5W8_T3mQ'


constructor() {
console.log(`[COOP SYSTEM] Starting coop system`)
const gatherManager = gatherManagers[this.spaceId]
if (!gatherManager) return

const game = gatherManager.getGame()
setInterval(() => {
const players = Object.values(game.players)
const playerOnPlate1 = players.find((player: Player) => {
return hasMatchingCoordinates(player, this.plate1Location)
})
const playerOnPlate2 = players.find((player: Player) => {
return hasMatchingCoordinates(player, this.plate2Location)
})
if (playerOnPlate1 && playerOnPlate2 && !this.toggles.isChestActive) {
console.log(`[COOP SYSTEM] All players are on the plates`)
this.activateChestObject(this.mapId, game)
return
}

if ((!playerOnPlate1 || !playerOnPlate2) && this.toggles.isChestActive) {
console.log(`[COOP SYSTEM] At least one of the plates is now empty`)
this.deactivateChestObject(this.mapId, game)
return
}
}, 500)
}

public static getInstance(): CoopSystem {
if (!CoopSystem.instance) {
CoopSystem.instance = new CoopSystem()
}

return CoopSystem.instance
}

setChestObject(mapId: string, active: boolean, game: Game) {
const { key } = getMapObjectById(game, this.chestObjectId, mapId)
game.engine.sendAction({
$case: "mapSetObjects",
mapSetObjects: {
mapId,
objects: {
[key as number]: {
type: active ? 6 : 0,
previewMessage: 'Press X to challenge the chest',
highlighted: active ? this.chestImage : this.blankImage,
normal: active ? this.chestImage : this.blankImage,
propertiesJson: JSON.stringify({
message: 'Grr'
}),
_tags: []
}
}
},
});
}

activateChestObject(mapId: string, game: Game) {
this.toggles.isChestActive = true
this.setChestObject(mapId, true, game)
}

deactivateChestObject(mapId: string, game: Game) {
this.setChestObject(mapId, false, game)
this.toggles.isChestActive = false
}
}

export default CoopSystem

0 comments on commit 0eea47a

Please sign in to comment.