Skip to content

Commit

Permalink
1 - inputs: fix global coordinates + avoid vec2 memory allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
Donorhan committed Oct 18, 2024
1 parent 05ab68c commit c47c2c8
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions core/client/user-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,17 @@ userManager = {

// when the mouse left click is down, we want to apply steering to the current direction of the character
if (input.activePointer.isDown && allowPhaserMouseInputs()) {
// we compute the current direction vector between the mouse and our character on the screen
const directionVector = (new Phaser.Math.Vector2(input.activePointer.x, input.activePointer.y))
.subtract(new Phaser.Math.Vector2(this.controlledCharacter.x, this.controlledCharacter.y))
.normalize();

// we steer the character in the direction of the mouse, so it can be used along with other controls (keyboard etc.)
this.inputVector.x += directionVector.x;
this.inputVector.y += directionVector.y;
const worldPoint = input.activePointer.positionToCamera(this.scene.cameras.main);
const directionVector = [worldPoint.x - this.controlledCharacter.x, worldPoint.y - this.controlledCharacter.y];
const directionLength = Math.sqrt(Math.abs(directionVector[0] * directionVector[0] + directionVector[1] * directionVector[1]));

if (Math.abs(directionLength) > 2) {
const len = 1 / Math.sqrt(directionLength);

// we steer the character in the direction of the mouse, so it can be used along with other controls (keyboard etc.)
this.inputVector.x += directionVector[0] * len;
this.inputVector.y += directionVector[1] * len;
}
}

const moving = this.inputVector.x !== 0 || this.inputVector.y !== 0;
Expand Down

0 comments on commit c47c2c8

Please sign in to comment.