Fixed ship facing angle animation (avoid a 360 turn)
This commit is contained in:
parent
36b54e7b75
commit
38d40fbe58
|
@ -3,6 +3,8 @@ module SpaceTac.Game {
|
||||||
|
|
||||||
// Generic tools functions
|
// Generic tools functions
|
||||||
export class Tools {
|
export class Tools {
|
||||||
|
|
||||||
|
// Copy an object (only a shallow copy of immediate properties)
|
||||||
static copyObject<T> (object: T): T {
|
static copyObject<T> (object: T): T {
|
||||||
var objectCopy = <T>{};
|
var objectCopy = <T>{};
|
||||||
|
|
||||||
|
|
|
@ -76,7 +76,7 @@ module SpaceTac.View {
|
||||||
var tween_sprite = this.game.tweens.create(this.sprite);
|
var tween_sprite = this.game.tweens.create(this.sprite);
|
||||||
tween_group.to({x: x, y: y});
|
tween_group.to({x: x, y: y});
|
||||||
tween_group.start();
|
tween_group.start();
|
||||||
tween_sprite.to({rotation: facing_angle});
|
Tools.rotationTween(tween_sprite, facing_angle);
|
||||||
tween_sprite.start();
|
tween_sprite.start();
|
||||||
} else {
|
} else {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
|
|
39
src/scripts/view/common/Tools.ts
Normal file
39
src/scripts/view/common/Tools.ts
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
module SpaceTac.View {
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
// Common UI tools functions
|
||||||
|
export class Tools {
|
||||||
|
|
||||||
|
// Constraint an angle in radians the ]-pi;pi] range.
|
||||||
|
static normalizeAngle(angle: number): number {
|
||||||
|
angle = angle % (2 * Math.PI);
|
||||||
|
if (angle <= -Math.PI) {
|
||||||
|
return angle + 2 * Math.PI;
|
||||||
|
} else if (angle > Math.PI) {
|
||||||
|
return angle - 2 * Math.PI;
|
||||||
|
} else {
|
||||||
|
return angle;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Interpolate a rotation value
|
||||||
|
// This will take into account the 2*pi modulo
|
||||||
|
static rotationTween(tween: Phaser.Tween, dest: number, property: string = "rotation"): void {
|
||||||
|
// Immediately change the object's current rotation to be in range (-pi,pi)
|
||||||
|
var value = Tools.normalizeAngle(tween.target[property]);
|
||||||
|
tween.target[property] = value;
|
||||||
|
|
||||||
|
// Update the tween
|
||||||
|
dest = Tools.normalizeAngle(dest);
|
||||||
|
if (value - dest > Math.PI) {
|
||||||
|
dest += 2 * Math.PI;
|
||||||
|
} else if (value - dest < -Math.PI) {
|
||||||
|
dest -= 2 * Math.PI;
|
||||||
|
}
|
||||||
|
console.log(value, dest);
|
||||||
|
var changes: Object = {};
|
||||||
|
changes[property] = dest;
|
||||||
|
tween.to(changes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
16
src/scripts/view/specs/Tools.spec.ts
Normal file
16
src/scripts/view/specs/Tools.spec.ts
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
/// <reference path="../../definitions/jasmine.d.ts"/>
|
||||||
|
|
||||||
|
module SpaceTac.View.Specs {
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
describe("Tools", () => {
|
||||||
|
it("normalizes angles", () => {
|
||||||
|
expect(Tools.normalizeAngle(0)).toEqual(0);
|
||||||
|
expect(Tools.normalizeAngle(0.1)).toBeCloseTo(0.1, 0.000001);
|
||||||
|
expect(Tools.normalizeAngle(Math.PI)).toBeCloseTo(Math.PI, 0.000001);
|
||||||
|
expect(Tools.normalizeAngle(Math.PI + 0.5)).toBeCloseTo(-Math.PI + 0.5, 0.000001);
|
||||||
|
expect(Tools.normalizeAngle(-Math.PI)).toBeCloseTo(Math.PI, 0.000001);
|
||||||
|
expect(Tools.normalizeAngle(-Math.PI - 0.5)).toBeCloseTo(Math.PI - 0.5, 0.000001);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in a new issue