Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gameObject): add angular velocity and acceleration #400

Merged
merged 4 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/pages/reducing-file-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ __Note:__ Some of the more advance classes – such as Button or Grid – requir

| Name | Functionality Enabled |
| --------------- | ------------- |
| `acceleration` | `acceleration`, `ddx`, `ddy` |
| `acceleration` | `acceleration`, `ddx`, `ddy`, 'ddrotation' |
| `anchor` | `anchor` |
| `group` | `children`, `parent`, `addChild`, `removeChild` |
| `opacity` | `opacity` |
| `rotation` | `rotation` |
| `scale` | `scaleX`, `scaleY`, `setScale()` |
| `ttl` | `ttl`, `isAlive` |
| `velocity` | `velocity`, `dx`, `dy` |
| `velocity` | `velocity`, `dx`, `dy`, 'drotation' |

#### `sprite` Options:

Expand Down
43 changes: 43 additions & 0 deletions src/gameObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import { noop, removeFromArray } from './utils.js';
* @param {GameObject[]} [properties.children] - Children to add to the game object.
* @param {Number} [properties.opacity=1] - The opacity of the game object.
* @param {Number} [properties.rotation=0] - The rotation around the anchor in radians.
* @param {Number} [properties.drotation=0] - The angular velocity of the rotation in radians.
* @param {Number} [properties.ddrotation=0] - The angular acceleration of the rotation in radians.
* @param {Number} [properties.scaleX=1] - The x scale of the game object.
* @param {Number} [properties.scaleY=1] - The y scale of the game object.
*
Expand Down Expand Up @@ -164,6 +166,24 @@ class GameObject extends Updatable {
* @property {Number} rotation
*/
rotation = 0,

// @ifdef GAMEOBJECT_VELOCITY
/**
* Angular velocity of the rotation in radians.
* @memberof GameObject
* @property {Number} drotation
*/
drotation = 0,
// @endif

// @ifdef GAMEOBJECT_ACCELERATION
/**
* Angular acceleration of the rotation in radians.
* @memberof GameObject
* @property {Number} ddrotation
*/
ddrotation = 0,
// @endif
// @endif

// @ifdef GAMEOBJECT_SCALE
Expand Down Expand Up @@ -208,6 +228,14 @@ class GameObject extends Updatable {

// @ifdef GAMEOBJECT_ROTATION
rotation,

// @ifdef GAMEOBJECT_VELOCITY
drotation,
// @endif

// @ifdef GAMEOBJECT_ACCELERATION
ddrotation,
// @endif
// @endif

// @ifdef GAMEOBJECT_SCALE
Expand Down Expand Up @@ -626,6 +654,21 @@ class GameObject extends Updatable {
this._rot = value;
this._pc();
}

// @ifdef GAMEOBJECT_VELOCITY||GAMEOBJECT_ACCELERATION
advance(dt) {
super.advance(dt);

// @ifdef GAMEOBJECT_VELOCITY
// @ifdef GAMEOBJECT_ACCELERATION
this.drotation += this.ddrotation;
// @endif

this.rotation += this.drotation;
// @endif
}
// @endif

// @endif

// --------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions test/typings/gameObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ let height: number = gameObject.height;
let context: CanvasRenderingContext2D = gameObject.context;
let children: kontra.GameObject[] = gameObject.children;
let rotation: number = gameObject.rotation;
let drotation: number = gameObject.drotation;
let ddrotation: number = gameObject.ddrotation;
let ttl: number = gameObject.ttl;
let anchor: {x: number, y: number} = gameObject.anchor;
let sx: number = gameObject.sx;
Expand Down Expand Up @@ -62,6 +64,8 @@ kontra.GameObject({
ddy: 2,
ttl: 10,
rotation: 10,
drotation: 10,
ddrotation: 12,
anchor: {x: 2, y: 2},
scaleX: 2,
scaleY: 2,
Expand Down
111 changes: 106 additions & 5 deletions test/unit/gameObject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import GameObject, { GameObjectClass } from '../../src/gameObject.js';
import { _reset, init, getContext } from '../../src/core.js';
import { noop } from '../../src/utils.js';
import { degToRad } from '../../src/helpers.js';
import Vector from '../../src/vector.js';

// test-context:start
let testContext = {
GAMEOBJECT_ANCHOR: true,
GAMEOBJECT_GROUP: true,
GAMEOBJECT_OPACITY: true,
GAMEOBJECT_ROTATION: true,
GAMEOBJECT_SCALE: true
GAMEOBJECT_SCALE: true,
GAMEOBJECT_VELOCITY: true,
GAMEOBJECT_ACCELERATION: true
};
// test-context:end

Expand Down Expand Up @@ -177,7 +180,7 @@ describe(
}

if (testContext.GAMEOBJECT_ROTATION) {
it('should set default camera', () => {
it('should set default rotation', () => {
expect(gameObject.rotation).to.equal(0);
});

Expand All @@ -187,13 +190,51 @@ describe(
expect(gameObject.rotation).to.equal(0.5);
});
} else {
it('should not default camera', () => {
it('should not default rotation', () => {
expect(gameObject.rotation).to.not.exist;
});
}

if (
testContext.GAMEOBJECT_ROTATION &&
testContext.GAMEOBJECT_VELOCITY
) {
it('should set default drotation', () => {
expect(gameObject.drotation).to.equal(0);
});

it('should set drotation property', () => {
gameObject = GameObject({ drotation: 0.5 });

expect(gameObject.drotation).to.equal(0.5);
});
} else {
it('should not default drotation', () => {
expect(gameObject.drotation).to.not.exist;
});
}

if (
testContext.GAMEOBJECT_ROTATION &&
testContext.GAMEOBJECT_ACCELERATION
) {
it('should set default ddrotation', () => {
expect(gameObject.ddrotation).to.equal(0);
});

it('should set ddrotation property', () => {
gameObject = GameObject({ ddrotation: 0.5 });

expect(gameObject.ddrotation).to.equal(0.5);
});
} else {
it('should not default ddrotation', () => {
expect(gameObject.ddrotation).to.not.exist;
});
}

if (testContext.GAMEOBJECT_SCALE) {
it('should set default camera', () => {
it('should set default scale', () => {
expect(gameObject.scaleX).to.equal(1);
expect(gameObject.scaleY).to.equal(1);
});
Expand All @@ -205,7 +246,7 @@ describe(
expect(gameObject.scaleY).to.equal(20);
});
} else {
it('should not default camera', () => {
it('should not default scale', () => {
expect(gameObject.scaleY).to.not.exist;
expect(gameObject.scaleY).to.not.exist;
});
Expand Down Expand Up @@ -692,6 +733,66 @@ describe(
}
});

// --------------------------------------------------
// advance
// --------------------------------------------------
describe('advance', () => {
if (
(testContext.GAMEOBJECT_ROTATION &&
testContext.GAMEOBJECT_VELOCITY) ||
testContext.GAMEOBJECT_ACCELERATION
) {
it('should call parent advance', () => {
gameObject.position = Vector(5, 10);
gameObject.velocity = Vector(15, 20);

gameObject.advance();

expect(gameObject.position.x).to.equal(20);
expect(gameObject.position.y).to.equal(30);
});
}

if (
testContext.GAMEOBJECT_ROTATION &&
testContext.GAMEOBJECT_VELOCITY &&
testContext.GAMEOBJECT_ACCELERATION
) {
it('should add the angular acceleration to the angular velocity', () => {
gameObject.drotation = 0.5;
gameObject.ddrotation = 0.5;

gameObject.advance();

expect(gameObject.drotation).to.equal(1);
});
}

if (
testContext.GAMEOBJECT_ROTATION &&
testContext.GAMEOBJECT_VELOCITY
) {
it('should add the angular velocity to the rotation', () => {
gameObject.rotation = 0.5;
gameObject.drotation = 0.5;

gameObject.advance();

expect(gameObject.rotation).to.equal(1);
});
} else {
it('should not modify the rotation', () => {
gameObject.rotation = 0;
gameObject.drotation = 0.5;
gameObject.ddrotation = 0.5;

gameObject.advance();

expect(gameObject.rotation).to.equal(0);
});
}
});

// --------------------------------------------------
// group
// --------------------------------------------------
Expand Down
Loading