Skip to content

Commit

Permalink
🚀 new movement system
Browse files Browse the repository at this point in the history
  • Loading branch information
FelipeFTN committed Jan 14, 2024
1 parent f3bc720 commit 23f965f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 34 deletions.
41 changes: 20 additions & 21 deletions frontend/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,45 +88,41 @@ class Warfrost extends Phaser.Scene {
if (this.players[player.id]) {
// console.log(`player: ${this.players[player.id].player}`)
const p = this.players[player.id];

// p.move(this)

this.selection.handleSelection(this.players[player.id].player);
this.selection.handleSelection(p);
p.move(this);
return;
}

// ... If not, then, create it
const p : Player = new Player(this, player);

p.player = this.add.sprite(player.x, player.y, "player");
p.player.setInteractive();
p.player.setDepth(1);
p.player.setData('id', player.id);
p.player.setData('selected', false);
p.setInteractive();
p.setDepth(1);
p.setData('id', player.id);
p.setData('selected', false);

// Set player into players object
this.players[p.getId()] = p;

// Checks for any player interation
p.player.on("pointerdown", (pointer: Phaser.Input.Pointer) => {
p.on("pointerdown", (pointer: Phaser.Input.Pointer) => {
if (pointer.leftButtonDown()) {
p.player.setData('selected', true);
p.setData('selected', true);
}
});

this.input.on('pointerdown', (pointer: Phaser.Input.Pointer) => {
if (pointer.rightButtonDown()) {
if (!p.player.getData('selected')) return;
this.input.on('pointerup', (pointer: Phaser.Input.Pointer) => {
if (pointer.rightButtonReleased()) {
if (!p.getData('selected')) return;

const mouse_vector = pointer.position;
p.player.setData("destination", new Phaser.Math.Vector2(mouse_vector));
this.physics.moveToObject(p.player, mouse_vector, 200);
p.setData("destination", new Phaser.Math.Vector2(mouse_vector));

// Needs formatation to send players::update::[{id: 0, x: 10, y: 10...}, {...}]
// this.socket.send(`players::move::[{"id": ${player.id}, "x": ${x}, "y": ${y}}]`);
}
});
// p.move(this);
p.move(this);
});

const playersIds = this.playersData.map((item: Models.PlayerData) => item.id);
Expand All @@ -144,12 +140,15 @@ class Warfrost extends Phaser.Scene {
}

onPointerDown(pointer: Phaser.Input.Pointer): void {
if (pointer.rightButtonDown()) return;
this.selection.startSelection(pointer.x, pointer.y);
if (pointer.leftButtonDown()) {
this.selection.startSelection(pointer.x, pointer.y);
}
}

onPointerUp(_pointer: Phaser.Input.Pointer): void {
this.selection.endSelection();
onPointerUp(pointer: Phaser.Input.Pointer): void {
if (pointer.leftButtonReleased()) {
this.selection.endSelection();
}
}

onPointerMove(pointer: Phaser.Input.Pointer): void {
Expand Down
39 changes: 26 additions & 13 deletions frontend/src/warfrost/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,33 @@ import * as Models from '../warfrost/models';
import * as Phaser from 'phaser';
import Warfrost from '../game';

class Player {
class Player extends Phaser.GameObjects.Sprite {
body: Phaser.Physics.Arcade.Body;

private id : number;
private speed : number; // TODO: Acceleration too
private health : number;
private speed : number = 100; // TODO: Acceleration too
private health : number = 100;
private position : Phaser.Math.Vector2;
public player : Phaser.GameObjects.Sprite;
private scene : Warfrost;
private WF : Warfrost;

constructor(scene: Warfrost, player: Models.PlayerData) {
this.scene = scene;
constructor(WF: Warfrost, player: Models.PlayerData) {
super(WF, player.x, player.y, "player");
this.setTexture("player");
this.id = player.id;
this.health = 100;
this.scene = WF;

// GLOBAL STUFF
this.scene.physics.world.enableBody(this);
this.body.setAllowGravity(false);
this.scene.add.existing(this);
}

// Getters & Setters
getId() : number { return this.id; }

getPosition() : Phaser.Math.Vector2 {
return new Phaser.Math.Vector2(this.player.x, this.player.y);
return new Phaser.Math.Vector2(this.x, this.y);
}

// If it's true, then the player moved
Expand All @@ -32,11 +40,16 @@ class Player {

// This should make the player move properly
move(WF: Warfrost) {
const destination : Phaser.Math.Vector2 = this.player.getData("destination");
if (destination == null) { return }
if (this.getPosition().equals(destination)) { return; }

WF.physics.moveToObject(this.player, destination, 200);
const destination : Phaser.Math.Vector2 = this.getData("destination");
if (destination == null) { return; }
if (this.speed > 0) {
if (Phaser.Math.Distance.BetweenPoints(this, destination) <= 2) {
this.body.reset(destination.x, destination.y);
return;
}
}

WF.physics.moveToObject(this, destination, this.speed);

// const movement = this.getPosition().subtract(destination);
// const movement = destination.subtract(this.getPosition());
Expand Down

0 comments on commit 23f965f

Please sign in to comment.