|
|
@ -4,7 +4,7 @@ |
|
|
|
|
|
|
|
'use strict'; |
|
|
|
|
|
|
|
import { NiceMath } from "dtp-nice-game"; |
|
|
|
import { NiceMath, NiceVector2d } from "dtp-nice-game"; |
|
|
|
|
|
|
|
export class BasicNavigator { |
|
|
|
|
|
|
@ -89,15 +89,33 @@ export class BasicNavigator { |
|
|
|
|
|
|
|
// update gameObject rotation towards the desired rotation
|
|
|
|
if (this.gameObject.rotation !== this.desiredRotation) { |
|
|
|
const ldist = Math.abs(this.gameObject.rotation - this.desiredRotation); |
|
|
|
const rdist = Math.abs(this.desiredRotation - this.gameObject.rotation); |
|
|
|
if (ldist <= rdist) { |
|
|
|
this.gameObject.rotation += TURN_AMOUNT; |
|
|
|
if (AT_DESTINATION) { |
|
|
|
if (this.gameObject.rotation < this.desiredRotation) { |
|
|
|
this.gameObject.rotation += TURN_AMOUNT; |
|
|
|
if (this.gameObject.rotation > this.desiredRotation) { |
|
|
|
this.gameObject.rotation = this.desiredRotation; |
|
|
|
} |
|
|
|
} |
|
|
|
if (this.gameObject.rotation > this.desiredRotation) { |
|
|
|
this.gameObject.rotation -= TURN_AMOUNT; |
|
|
|
if (this.gameObject.rotation < this.desiredRotation) { |
|
|
|
this.gameObject.rotation = this.desiredRotation; |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
this.gameObject.rotation -= TURN_AMOUNT; |
|
|
|
} |
|
|
|
if (NiceMath.isAngleNear(this.gameObject.rotation, this.desiredRotation, TURN_AMOUNT)) { |
|
|
|
this.gameObject.rotation = this.desiredRotation; |
|
|
|
let dir = this.targetPosition |
|
|
|
.clone() |
|
|
|
.subtract(this.gameObject.position) |
|
|
|
.normalize() |
|
|
|
.rotate(-this.gameObject.rotation - (Math.PI / 2.0)); |
|
|
|
if (dir.x < 0) { |
|
|
|
this.gameObject.rotation -= TURN_AMOUNT; |
|
|
|
} else { |
|
|
|
this.gameObject.rotation += TURN_AMOUNT; |
|
|
|
} |
|
|
|
if (NiceMath.isAngleNear(this.gameObject.rotation, this.desiredRotation, TURN_AMOUNT)) { |
|
|
|
this.gameObject.rotation = this.desiredRotation; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -110,10 +128,12 @@ export class BasicNavigator { |
|
|
|
* the gameObject's current rotation |
|
|
|
*/ |
|
|
|
|
|
|
|
let deltaX = Math.cos(this.gameObject.rotation) * this.moveSpeed * TIME_SCALE; |
|
|
|
let deltaY = Math.sin(this.gameObject.rotation) * this.moveSpeed * TIME_SCALE; |
|
|
|
this.gameObject.position.x += deltaX; |
|
|
|
this.gameObject.position.y += deltaY; |
|
|
|
this.lastStep = new NiceVector2d( |
|
|
|
Math.cos(this.gameObject.rotation) * MOVE_AMOUNT, |
|
|
|
Math.sin(this.gameObject.rotation) * MOVE_AMOUNT |
|
|
|
); |
|
|
|
this.gameObject.position.x += this.lastStep.x; |
|
|
|
this.gameObject.position.y += this.lastStep.y; |
|
|
|
|
|
|
|
if (this.gameObject.position.isNear(this.targetPosition, MOVE_AMOUNT)) { |
|
|
|
this.gameObject.position = this.targetPosition.clone(); |
|
|
|