Skip to content

Commit

Permalink
tried adding support for ghost player
Browse files Browse the repository at this point in the history
  • Loading branch information
insulationman committed Aug 30, 2023
1 parent 355a2ae commit c5dea3b
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions src/Scenes/Game/OrustScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,39 @@ export default class OrustScene extends Phaser.Scene {
private noncollidingobjectslayer!: Tilemaps.TilemapLayer;
private noncollidinghigherlayer!: Tilemaps.TilemapLayer;
private house!: GameObjects.GameObject;
private websocket!: WebSocket;
private lastUpdate = 0;
private ghost!: GameObjects.Sprite;

//function that will run every half second
private updateGhost() {
if (this.lastUpdate > 0) {
this.lastUpdate--;
return;
}
this.lastUpdate = 50;
const updatemessage = JSON.stringify({
type: "ghost",
ghostCoords: {
x: this.player.x,
y: this.player.y,
},
});

this.websocket.send(updatemessage);
}

preload() {
this.websocket = new WebSocket("wss://realorfakehub.azurewebsites.net");

this.websocket.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.type === "ghost") {
this.ghost.setX(data.ghostCoords.x);
this.ghost.setY(data.ghostCoords.y);
}
};

this.cameras.main.setBackgroundColor("#696969");
//Load tilemap
this.load.tilemapTiledJSON("Ground", tileMapJsonUrl);
Expand Down Expand Up @@ -59,22 +90,22 @@ export default class OrustScene extends Phaser.Scene {
create() {
this.player = new Player(this, 300, 500);
this.initMap();
this.ghost = this.add.sprite(0, 0, "playerspritesheet", 0);
this.ghost.setDepth(100);
this.ghost.setTint(0x555555);
// this.initMusic();
this.initCollidingActions();
//get the animated tiles plugin
this.animatedTiles.init(this.map);
this.initMushrooms();
//create a dialog
const dialog = new Dialog("Holy shit, a dialog box!");

this.scene.get('castle-scene').events.on('player-exit-house', () => {
this.player.setX(this.house.body.position.x);
this.player.setY(this.house.body.position.y+100)
});
}

update() {
this.player.updatePlayer();
//send update message every half second
this.updateGhost();
}

private initMusic(): void {
Expand Down Expand Up @@ -141,7 +172,7 @@ export default class OrustScene extends Phaser.Scene {
house.removeFromDisplayList();
//alert when the player collides with the house
this.physics.add.overlap(this.player, house, () => {
this.events.emit('player-enter-house');
this.events.emit("player-enter-house");
this.scene.switch("castle-scene");
});
}
Expand Down

0 comments on commit c5dea3b

Please sign in to comment.