Skip to content

Commit

Permalink
Roomba scenes and moooreeee ahahahahahahaa
Browse files Browse the repository at this point in the history
  • Loading branch information
NouCake committed Feb 9, 2024
1 parent 8be7ae4 commit 78a3509
Show file tree
Hide file tree
Showing 34 changed files with 989 additions and 585 deletions.
Binary file added assets/roomba.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"devDependencies": {
"@babel/preset-typescript": "^7.23.3",
"@types/jest": "^29.5.11",
"eventemitter3": "^5.0.1",
"jest": "^29.7.0",
"typescript": "^5.2.2",
"vite": "^5.0.5"
Expand Down
75 changes: 22 additions & 53 deletions src/board/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ interface BoardPlayer {
position: number,
}

export interface BoardPacket extends PlayerPacket {
type: PacketType.BOARD,
data: {
function: string,
args: any[],
}
}

const PLAYER_SIZE = 16;
const BOARD_SIDE_LENGTH = 12;
export default class GameBoard extends Phaser.GameObjects.Container {
Expand All @@ -27,15 +19,12 @@ export default class GameBoard extends Phaser.GameObjects.Container {

private TILE_SIZE: number;

private aznopoly: AzNopolyGame;

private players: Map<string, BoardPlayer>;
private size: number;

constructor(aznopoly: AzNopolyGame, scene: Scene, x: number, y: number, size: number) {
super(scene, x, y);
this.size = size;
this.aznopoly = aznopoly;
this.players = new Map();

this.TILE_SIZE = size / BOARD_SIDE_LENGTH;
Expand All @@ -46,36 +35,9 @@ export default class GameBoard extends Phaser.GameObjects.Container {
const targetScale = size / background.width;
background.setScale(targetScale);
background.setOrigin(0, 0);

this.aznopoly.client.addEventListener(PacketType.BOARD, this.onBoardPacket.bind(this) as EventListener);
}

private onBoardPacket(event: CustomEvent<BoardPacket>) {
const packet = event.detail;
if (this.aznopoly.isHost) return;
if (packet.target && packet.target !== this.aznopoly.client.id) return;

switch (packet.data.function) {
case "addPlayer":
this.addPlayer(packet.data.args[0], packet.data.args[1]);
break;
case "movePlayer":
this.movePlayer(packet.data.args[0], packet.data.args[1]);
break;
}
}

addPlayer(uuid: string, startPos: number = 0) {
if (this.aznopoly.isHost) {
this.aznopoly.client.sendPacket({
type: PacketType.BOARD,
data: {
function: "addPlayer",
args: [uuid, startPos],
},
})
}

if (this.players.has(uuid)) {
throw new Error(`Player with UUID ${uuid} already exists!`);
}
Expand All @@ -88,37 +50,44 @@ export default class GameBoard extends Phaser.GameObjects.Container {
};
this.add(player.gameObject);
this.players.set(uuid, player)
this.movePlayer(uuid, 0);
this.movePlayerToPosition(uuid, 0);
return player;
}

movePlayer(uuid: string, distance: number) {
if (this.aznopoly.isHost) {
this.aznopoly.client.sendPacket({
type: PacketType.BOARD,
data: {
function: "movePlayer",
args: [uuid, distance],
},
})
}

if (!Number.isInteger(distance)) {
throw new Error(`Illegal parameter distance: Not an integer (${distance})`);
movePlayerToPosition(uuid: string, position: number) {
if (!Number.isInteger(position)) {
throw new Error(`Illegal parameter position: Not an integer (${position})`);
}

let player = this.players.get(uuid);
if (!player) {
throw new Error(`Player with UUID ${uuid} does not exist!`);
}

player.position += distance;
player.position = position;
const coords = GameBoard.getCoordForPos(player.position);

player.gameObject.setPosition(coords.x * this.TILE_SIZE, coords.y * this.TILE_SIZE)
this.checkPlayerColisions();
}

// movePlayerForward(uuid: string, distance: number) {
// if (!Number.isInteger(distance)) {
// throw new Error(`Illegal parameter distance: Not an integer (${distance})`);
// }

// let player = this.players.get(uuid);
// if (!player) {
// throw new Error(`Player with UUID ${uuid} does not exist!`);
// }

// player.position += distance;
// const coords = GameBoard.getCoordForPos(player.position);

// player.gameObject.setPosition(coords.x * this.TILE_SIZE, coords.y * this.TILE_SIZE)
// this.checkPlayerColisions();
// }

private checkPlayerColisions() {
const positions: { [key: number]: string[] } = {};
this.players.forEach((player, uuid) => {
Expand Down
1 change: 1 addition & 0 deletions src/debug-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function mock(aznopoly: AzNopolyGame) {
game._room = {
connectedPlayerIds: ["1111-2222-3333-4444"],
host: "1111-2222-3333-4444",
getPlayerName: () => "mockius maximus",
};
game._name = "mockius maximus";

Expand Down
6 changes: 5 additions & 1 deletion src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export default class AzNopolyGame {
name: this._name,
}
}

public get uuid(): string {
return this.client.id;
}

public get client(): AzNopolyClient {
return this._client;
Expand All @@ -35,7 +39,7 @@ export default class AzNopolyGame {
return this._room;
}

public get players(): string[] {
public get connectedUuids(): string[] {
return this.room.connectedPlayerIds;
}

Expand Down
12 changes: 8 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Phaser from 'phaser';
import TitleScene from './scene/title-scene';
import BoardScene from './scene/game-scene';
import BoardScene from './scene/board-scene';
import LobbyScene from './scene/lobby-scene';
import { SimonSaysScene } from './scene/minigame/simon-says-scene';
// import { SimonSaysScene } from './scene/minigame/simon-says-scene';
import AzNopolyGame from './game';
import { mock } from './debug-util';
import { RoombaScene } from './scene/minigame/roomba-scene';
// import { mock } from './debug-util';

export const WIDTH = 1280;
export const HEIGHT = 720;
Expand Down Expand Up @@ -41,10 +41,14 @@ window.onload = async () => {
game.scene.add('game', new BoardScene(aznopoly));

//Minigames
game.scene.add('minigame-simon-says', new SimonSaysScene(aznopoly))
//game.scene.add('minigame-simon-says', new SimonSaysScene(aznopoly))
game.scene.add('minigame-roomba', new RoombaScene(aznopoly))

//game.scene.start('minigame-roomba');

//mock(aznopoly);
//game.scene.start('game');

game.scene.start('title');

Object.assign(window, { game });
Expand Down
37 changes: 27 additions & 10 deletions src/minigame/roomba.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface RoombaConfig {
export class Roomba extends Phaser.GameObjects.Container {
static SIZE = SIZE;

private graphics: Phaser.GameObjects.Graphics;
private graphics: Phaser.GameObjects.Image;
private arrow: Phaser.GameObjects.Graphics;

private lastPaintPosition: Phaser.Math.Vector2;
Expand All @@ -25,8 +25,14 @@ export class Roomba extends Phaser.GameObjects.Container {
private paintColor: number;
private speed: number;

private stopped = false;

public readonly id: string;

static preload(scene: Phaser.Scene) {
scene.load.image('roomba', 'assets/roomba.png');
}

constructor(scene: Phaser.Scene, { id, x, y, angle, color, paintColor, speed} : RoombaConfig) {
super(scene, x, y);

Expand All @@ -35,14 +41,12 @@ export class Roomba extends Phaser.GameObjects.Container {
this.paintColor = paintColor;
this.speed = speed;

this.graphics = new Phaser.GameObjects.Graphics(scene);
this.graphics = new Phaser.GameObjects.Image(scene, 0, 0, 'roomba');
this.graphics.setScale(SIZE / this.graphics.width);

this.arrow = new Phaser.GameObjects.Graphics(scene);
this.lastPaintPosition = new Phaser.Math.Vector2(x, y);

this.graphics.fillStyle(this.color);
this.graphics.fillCircle(0, 0, SIZE / 2);
this.graphics.fillCircle(SIZE/5 * 2, 0, SIZE / 4);

scene.physics.world.enable(this);
const body = this.body! as Phaser.Physics.Arcade.Body;
body.setCollideWorldBounds(true)
Expand All @@ -57,24 +61,37 @@ export class Roomba extends Phaser.GameObjects.Container {
this.initDragEvents();
}

public stop() {
const body = this.body! as Phaser.Physics.Arcade.Body;
body.setVelocity(0, 0);
this.arrow.clear();
this.arrow.setVisible(false);
}

private initDragEvents() {
this.graphics.setInteractive({
this.setInteractive({
hitArea: new Phaser.Geom.Circle(0, 0, SIZE / 2),
hitAreaCallback: Phaser.Geom.Circle.Contains,
useHandCursor: false,
draggable: true
}, Phaser.Geom.Circle.Contains);

this.graphics.on('dragstart', () => {
this.on('dragstart', () => {
if (this.stopped) return;

this.arrow.visible = true;
});

this.graphics.on('drag', (event: any) => {
this.on('drag', (event: any) => {
if (this.stopped) return;

const dragOffset = new Phaser.Math.Vector2(event.x - this.x, event.y - this.y);
this.drawArrow(Phaser.Math.Vector2.ZERO, dragOffset);
});

this.graphics.on('dragend', (event: any) => {
this.on('dragend', (event: any) => {
if (this.stopped) return;

const dragOffset = new Phaser.Math.Vector2(event.x - this.x, event.y - this.y);
this.scene.events.emit('roomba-dragged', { id: this.id, offset: dragOffset});
this.drawArrow(new Phaser.Math.Vector2(0, 0), new Phaser.Math.Vector2(0, 0));
Expand Down
39 changes: 28 additions & 11 deletions src/scene-switcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ import { PacketType, SceneChangePacket, SceneReadyPacket } from "./types/client"

let switcher: any[] = []
export const SceneSwitcher = {
waitForPlayers: (aznopoly: AzNopolyGame, sceneKey: string, launchMethod: string) => {
sendSceneChangePacket(aznopoly, sceneKey, launchMethod);
return new Promise<void>((resolve) => {
switcher.push(new SceneReadyListener(aznopoly, sceneKey, () => {
resolve();
}));
});
waitForPlayers: (aznopoly: AzNopolyGame, sceneKey: string, callback: () => void) => {
switcher.push(new SceneReadyListener(aznopoly, sceneKey, callback));
sendSceneChangePacket(aznopoly, sceneKey, false);
},
updateScene: sendSceneReadyPacket
updateScene: sendSceneReadyPacket,
listen: listenForSceneSwitch,
}

/**
Expand Down Expand Up @@ -62,21 +59,41 @@ function sendSceneReadyPacket(aznopoly: AzNopolyGame, sceneName: string) {
}
}

console.log("Sending scene ready packet", packet);
if (aznopoly.isHost) {
switcher.find(l => l.sceneName == sceneName)?.listener({detail: packet})
const ss = switcher.find(l => l.sceneName == sceneName);
ss?.listener({detail: packet})
} else {
aznopoly.client.sendPacket(packet);
}
}

function sendSceneChangePacket(aznopoly: AzNopolyGame, sceneName: string, launchMethod: string) {
function sendSceneChangePacket(aznopoly: AzNopolyGame, sceneName: string, returnable: boolean) {
const packet: SceneChangePacket = {
type: PacketType.SCENE_CHANGE,
sender: aznopoly.client.id,
data: {
scene: sceneName,
launchMethod,
launchMethod: returnable ? "launch" : "start",
}
}
aznopoly.client.sendPacket(packet);
}

function listenForSceneSwitch(scene: Phaser.Scene, aznopoly: AzNopolyGame) {
const listener = aznopoly.addPacketListener(PacketType.SCENE_CHANGE, ((event: CustomEvent<SceneChangePacket>) => {
const packet = event.detail
if (!aznopoly.isPlayerHost(packet.sender)) {
console.warn("Received scene change packet from non-host player");
return;
}

if (packet.data.launchMethod == "launch") {
scene.scene.sleep();
scene.scene.launch(packet.data.scene, { returnScene: scene.scene.key});
} else {
scene.scene.start(packet.data.scene);
}
}) as EventListener);
scene.events.once(Phaser.Scenes.Events.SHUTDOWN, () => aznopoly.removePacketListener(PacketType.SCENE_CHANGE, listener));
}
Loading

0 comments on commit 78a3509

Please sign in to comment.