diff --git a/client/src/assets/CIRKULARKA.png b/client/src/assets/CIRKULARKA.png new file mode 100644 index 0000000..654b796 Binary files /dev/null and b/client/src/assets/CIRKULARKA.png differ diff --git a/client/src/index.js b/client/src/index.js index 3d8f8b3..8ef0eda 100644 --- a/client/src/index.js +++ b/client/src/index.js @@ -9,11 +9,13 @@ import { Player } from './objects/player'; import grassTileImg from './assets/grassTile.jpg'; import dirtTileImg from './assets/dirtTile.jpg'; import backgroundImg from './assets/oblakiBG.jpg'; +import cirkularkaImg from './assets/CIRKULARKA.png'; import playerImg from './assets/player_image.png'; import animationPng from './assets/player/animation_white.png'; import animationJson from './assets/player/animation.json'; +import { Cirkularka } from './objects/cirkularka'; class Game extends Phaser.Scene { constructor() { @@ -29,6 +31,7 @@ class Game extends Phaser.Scene { this.load.image('dirtTile', dirtTileImg); this.load.image('grassTile', grassTileImg); this.load.image('background', backgroundImg); + this.load.image('cirkularka', cirkularkaImg); //this.load.image('player', playerImg); this.load.atlas('player', animationPng, animationJson); @@ -46,6 +49,7 @@ class Game extends Phaser.Scene { this.cursors = this.input.keyboard.createCursorKeys(); this.wasd = this.input.keyboard.addKeys('W,S,A,D'); + this.spaceKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE); //this.inputKeys = [ // this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE), @@ -59,6 +63,7 @@ class Game extends Phaser.Scene { update() { this.handlePlayerMove(); this.background.update(); + this.handleCirkularka(); } handlePlayerMove() { @@ -85,6 +90,36 @@ class Game extends Phaser.Scene { } } + handleCirkularka() { + if (Phaser.Input.Keyboard.JustDown(this.spaceKey)) { + if (this.player.body.velocity.x >= 0) { + this.cirkularka = new Cirkularka(this, this.player.x + 150, this.player.y, 'cirkularka'); + this.cirkularka.setVelocityX(400); + } else { + this.cirkularka = new Cirkularka(this, this.player.x - 150, this.player.y, 'cirkularka'); + this.cirkularka.setVelocityX(-400); + } + this.physics.add.collider(this.terrain, this.cirkularka); + this.physics.add.overlap(this.player, this.cirkularka, this.playerDeath, null, this); + + this.websocket.cikrularkaSend(this.cirkularka.x, this.cirkularka.y, this.cirkularka.body.velocity.x); + } + } + + playerDeath() { + //TODO: explosion + this.player.setPosition(100, 650); + this.websocket.playerMoveSend(this.player.x, this.player.y, this.player.body.velocity.x, this.player.body.velocity.y); + this.cirkularka.destroy(); + } + + cirkularkaRecieve(id, x, y, dx) { + this.cirkularka = new Cirkularka(this, x, y, 'cirkularka'); + this.cirkularka.setVelocityX(dx); + this.physics.add.collider(this.terrain, this.cirkularka); + this.physics.add.overlap(this.player, this.cirkularka, this.playerDeath, null, this); + } + handleMessage(msg) { if (msg.hasJoin()) { this.joinRecieve(msg.getPlayerId()); @@ -93,6 +128,9 @@ class Game extends Phaser.Scene { this.playerMoveRecieve(msg.getPlayerId(), move.getX(), move.getY(), move.getDx(), move.getDy()); } else if (msg.hasLeave()) { this.leaveReceive(msg.getPlayerId()); + } else if (msg.hasCirkularka()) { + const move = msg.getCirkularka(); + this.cirkularkaRecieve(msg.getCirkularkaId(), move.getX(), move.getY(), move.getDx()); } } diff --git a/client/src/network/websocket.js b/client/src/network/websocket.js index 3fb8e19..eebd9c6 100644 --- a/client/src/network/websocket.js +++ b/client/src/network/websocket.js @@ -48,4 +48,14 @@ export class Websocket { move.setDy(dy); this.sendMessage(message); } + + cikrularkaSend(x, y, dx) { + var message = new messages.Message(); + var cirkularka = new messages.Cirkularka(); + message.setCirkularka(cirkularka); + cirkularka.setX(x); + cirkularka.setY(y); + cirkularka.setDx(dx); + this.sendMessage(message); + } } diff --git a/client/src/objects/cirkularka.js b/client/src/objects/cirkularka.js new file mode 100644 index 0000000..be31f74 --- /dev/null +++ b/client/src/objects/cirkularka.js @@ -0,0 +1,17 @@ +import Phaser from 'phaser'; + +export class Cirkularka extends Phaser.Physics.Arcade.Sprite { + constructor(scene, x, y, texture) { + super(scene, x, y, texture); + this.scene.physics.world.enable(this); + this.scene.add.existing(this); + this.setDisplaySize(70, 70); + this.setDepth(70); + this.setBounce(0.2); + // this.setCircle(35); + + setTimeout(() => { + this.destroy(); + }, 3000); + } +} diff --git a/messages/messages.proto b/messages/messages.proto index d4ea1f8..3ec2ae4 100644 --- a/messages/messages.proto +++ b/messages/messages.proto @@ -14,6 +14,13 @@ message Move { float dy = 4; } +message Cirkularka { + float x = 1; + float y = 2; + float dx = 3; + optional int32 cirkularka_id = 4; +} + message Leave {} message Message { @@ -23,5 +30,6 @@ message Message { Join join = 4; Move move = 5; Leave leave = 6; + Cirkularka cirkularka = 7; } }