Skip to content

Commit

Permalink
👟 wip: movement
Browse files Browse the repository at this point in the history
  • Loading branch information
FelipeFTN committed Jan 13, 2024
1 parent 28527aa commit f3bc720
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
23 changes: 15 additions & 8 deletions frontend/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ class Warfrost extends Phaser.Scene {
this.playersData.forEach((player: Models.PlayerData) => {
// If player already exists, update its position
if (this.players[player.id]) {
// Update position & Check for selection
// this.players[player.id].p.setPosition(player.x, player.y);
this.players[player.id].move({x: player.x, y: player.y})
this.selection.handleSelection(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);
return;
}

Expand All @@ -114,15 +116,17 @@ class Warfrost extends Phaser.Scene {

this.input.on('pointerdown', (pointer: Phaser.Input.Pointer) => {
if (pointer.rightButtonDown()) {
const mouse_vector = pointer.position;
if (!p.player.getData('selected')) return;
// Movement happens here!!
console.log("Moving now!!")
p.move(mouse_vector);

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

// 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);
});

const playersIds = this.playersData.map((item: Models.PlayerData) => item.id);
Expand Down Expand Up @@ -159,6 +163,9 @@ const config: Phaser.Types.Core.GameConfig = {
height: 600,
scene: Warfrost,
pixelArt: true,
physics: {
default: 'arcade'
},
};

const game = new Phaser.Game(config);
Expand Down
22 changes: 10 additions & 12 deletions frontend/src/warfrost/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,17 @@ class Player {

constructor(scene: Warfrost, player: Models.PlayerData) {
this.scene = scene;
this.position.set(this.player.x, this.player.y);
this.id = player.id;
this.health = 100;
}

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

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

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

// If it's true, then the player moved
// if not... well, you know.
comparePositionMove(x: number, y: number) : boolean {
Expand All @@ -36,14 +31,17 @@ class Player {
}

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

const movement = this.getPosition().subtract(destination);
WF.physics.moveToObject(this.player, destination, 200);

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

this.setPosition(movement);
// console.log(movement);
}
}
export default Player;

0 comments on commit f3bc720

Please sign in to comment.