Skip to content

Commit

Permalink
Merge branch 'release/v1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelpicosean committed Dec 9, 2016
2 parents 58ca185 + a21af3c commit 6ea7699
Show file tree
Hide file tree
Showing 86 changed files with 2,928 additions and 2,457 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
howler.core.js
async.js
earcut.js
6 changes: 3 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ module.exports = {

"comma-dangle": ["error", "always-multiline"],
"no-console": "off",
"no-fallthrough": "off"
"no-fallthrough": "off",

/*"require-jsdoc": ["error", {
"require-jsdoc": ["error", {
"require": {
"FunctionDeclaration": true,
"MethodDefinition": true,
Expand All @@ -52,6 +52,6 @@ module.exports = {
}],
"valid-jsdoc": ["error", {
"requireReturn": false,
}],*/
}],
},
};
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ LesserPanda has a lot of features, and the list is still growing:
## Samples

Samples are moved to its own [repo here](https://github.com/pixelpicosean/lesser-panda-samples).
Currently the samples are located inside `src/game/samples` folder, and each is just a simple `Scene` focusing on one or more particular feature.
Currently the samples are located inside `src/game/samples` folder, and each is just a simple `Game` focusing on one or more particular feature.

**Note: Samples have not been converted to latest v1.0.0 yet.**
**Note: Samples have not been converted to latest v1.x yet.**

## Document

- [Getting Start Guide](https://github.com/pixelpicosean/lesser-panda/wiki/Getting-Start) at [Wiki](https://github.com/pixelpicosean/lesser-panda/wiki/Home) page.
- [API document](https://pixelpicosean.github.io/lesser-panda/)
- [Getting Start Guide(old **v0.x** version)](https://github.com/pixelpicosean/lesser-panda/wiki/Getting-Start) at [Wiki](https://github.com/pixelpicosean/lesser-panda/wiki/Home) page.
- [API document(old **v0.x** version)](https://pixelpicosean.github.io/lesser-panda/)

**Devlog** posts what happened to LesserPanda, read them at [wiki](https://github.com/pixelpicosean/lesser-panda/wiki/Home), it will be updated on each Wednesday.

Expand All @@ -55,7 +55,9 @@ Currently the samples are located inside `src/game/samples` folder, and each is
- `Game` is the main hub for your game.
- `Timer` provides timers with callbacks. Use `Timer.later` or `Timer.interval` to create instances.
- `utils` provides utility functions and constants for array, color, math, object.
- `Vector` provide a `Vector` class that is used everywhere `PIXI.Point` is also an alias of it.
- `Vector` is the 2D vector class, with tons of useful methods
- `Behavior` interface for behaviors of `Entity`
- `System` interface for all the sub-systems of `Game`

## Progress

Expand All @@ -64,6 +66,13 @@ Github issue and milestone maybe better for progress tracking~

## ChangeLog

### 1.1

- Update behaviors for new `Entity` API
- Cache arrays for physics updating
- Add "canHitMap" to `Collider` to toggle map collision
- More JSDoc comments and better API document

### 1.0.2

This version contains some breaking change, but I won't bump it to v1.1 since all the changes
Expand Down
132 changes: 132 additions & 0 deletions src/behaviors/AnchorToScreen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* Anchor object to screen left/right/top/bottom,
* by percentage or pixel.
*
* Note: Set both `right` and `left` will stretch the width of target.
*/

const core = require('engine/core');
const Behavior = require('engine/Behavior');

function pct2Num(pctStr) {
if (pctStr.length === 0) return 0;
if (pctStr[pctStr.length - 1] !== '%') return 0;

return parseFloat(pctStr.slice(0, -1)) * 0.01;
}

const DefaultSettings = {
left: undefined,
right: undefined,
top: undefined,
bottom: undefined,
};

class AnchorToScreen extends Behavior {
constructor() {
super();

this.type = 'AnchorToScreen';

this.left = undefined;
this.right = undefined;
this.top = undefined;
this.bottom = undefined;
}

init(ent, settings) {
super.init(ent);

this.entity.canFixedTick = true;

Object.assign(this, DefaultSettings, settings);

this.calc('left');
this.calc('right');
this.calc('top');
this.calc('bottom');
}
fixedUpdate() {
this.applyAnchor();
}
applyAnchor() {
const bounds = this.entity.gfx.getLocalBounds();

let left, right, top, bottom, width, height;

// x-axis
if (this.left !== undefined) {
// this.entity.position.x = this.left + bounds.x;
left = this.left;
}
else if (this.leftPct !== undefined) {
// this.entity.position.x = core.width * this.leftPct - bounds.x;
left = core.width * this.leftPct;
}
if (this.right !== undefined) {
// this.entity.position.x = core.width - this.right - (bounds.x + bounds.width);
right = core.width - this.right;
}
else if (this.rightPct !== undefined) {
// this.entity.position.x = core.width * (1 - this.rightPct) - (bounds.x + bounds.width);
right = core.width * (1 - this.rightPct);
}
// Stretch if both left and right is set
if (left !== undefined && right !== undefined) {
width = right - left;
}

if (width !== undefined) {
this.entity.gfx.width = width;
}
if (left !== undefined) {
this.entity.position.x = left - bounds.x;
}
else if (right !== undefined) {
this.entity.position.x = right - (bounds.x + bounds.width);
}

// y-axis
if (this.top !== undefined) {
// this.entity.position.y = this.top + bounds.y;
top = this.top;
}
else if (this.topPct !== undefined) {
// this.entity.position.y = core.height * this.topPct - bounds.y;
top = core.height * this.topPct;
}
if (this.bottom !== undefined) {
// this.entity.position.y = core.height - this.bottom - (bounds.y + bounds.height);
bottom = core.height - this.bottom;
}
else if (this.bottomPct !== undefined) {
// this.entity.position.y = core.height * (1 - this.bottomPct) - (bounds.y + bounds.height);
bottom = core.height * (1 - this.bottomPct);
}
// Stretch if both top and bottom is set
if (top !== undefined && bottom !== undefined) {
height = bottom - top;
}

if (height !== undefined) {
this.entity.gfx.height = height;
}
if (top !== undefined) {
this.entity.position.y = top - bounds.y;
}
else if (bottom !== undefined) {
this.entity.position.y = bottom - (bounds.y + bounds.height);
}
}

calc(dir) {
if (typeof(this[dir]) === 'string') {
this[`${dir}Pct`] = pct2Num(this[dir]);
this[dir] = undefined;
}
}
}

Behavior.register('AnchorToScreen', AnchorToScreen);

module.exports = AnchorToScreen;
125 changes: 125 additions & 0 deletions src/behaviors/AsteroidsMove.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/**
* Make the target behavive like the ship of
* classic Asteroid game.
*/

const keyboard = require('engine/keyboard');
const Behavior = require('engine/Behavior');
const Vector = require('engine/Vector');
const { clamp } = require('engine/utils/math');

const DefaultSettings = {
/* Whether use keyboard to control */
UseKeyboard: true,
/* Hold to push forward, when `UseKeyboard` is true */
ForwardKey: 'UP',
/* Hold to push backward, when `UseKeyboard` is true */
BackwardKey: 'DOWN',
/* Hold to turn left, when `UseKeyboard` is true */
LeftKey: 'LEFT',
/* Hold to turn right, when `UseKeyboard` is true */
RightKey: 'RIGHT',

/* The force to move forward */
ForwardForce: 10,
/* The force to move backward */
BackwardForce: 8,

/* The force to turn */
Torque: 1,

/* Same as Body.velocityLimit */
MaxVelocity: Vector.create(400),
/* Max turn speed */
MaxTurnSpeed: 3,

/* Damping of velocity */
Damping: 0.2,
/* Damping of angular velocity(turn) */
AngularDamping: 0.2,
};

class AsteroidsMove extends Behavior {
constructor() {
super();

this.type = 'AsteroidsMove';

// Constants
this.UseKeyboard = true;
this.ForwardKey = 'UP';
this.BackwardKey = 'DOWN';
this.LeftKey = 'LEFT';
this.RightKey = 'RIGHT';

this.ForwardForce = 10;
this.BackwardForce = 8;

this.Torque = 1;

this.MaxVelocity = Vector.create(400);
this.MaxTurnSpeed = 3;

this.Damping = 0.2;
this.AngularDamping = 0.2;

// Properties
this.dir = Vector.create(1, 0);
this.turnSpeed = 0;
this.turning = 0;
}
init(ent, settings) {
super.init(ent);

Object.assign(this, DefaultSettings, settings);

this.entity.canFixedTick = true;
this.entity.coll.damping = this.Damping;
}
fixedUpdate(_, dt) {
if (this.UseKeyboard) {
this.entity.coll.force.set(0);
this.turning = 0;
if (keyboard.down(this.ForwardKey)) this.pushForward();
if (keyboard.down(this.BackwardKey)) this.pushBackward();
if (keyboard.down(this.LeftKey)) this.turning -= 1;
if (keyboard.down(this.RightKey)) this.turning += 1;
}

// Update turning
if (this.turning !== 0) {
this.turnSpeed = clamp(this.turnSpeed + this.turning * this.Torque * dt, -this.MaxTurnSpeed, this.MaxTurnSpeed);
}
if (this.AngularDamping !== 0) {
this.turnSpeed *= Math.pow(1 - this.AngularDamping, dt);
}
this.entity.rotation += this.turnSpeed * dt;
this.dir.set(1, 0).rotate(this.entity.rotation);
}

// Actions
// Move forward
pushForward() {
this.entity.coll.force
.copy(this.dir)
.multiply(this.ForwardForce)
}
// Move backward
pushBackward() {
this.entity.coll.force
.copy(this.dir)
.multiply(-this.BackwardForce)
}
// Turn left
turnLeft() {
this.turning = -1;
}
// Turn right
turnRight() {
this.turning = 1;
}
}

Behavior.register('AsteroidsMove', AsteroidsMove);

module.exports = AsteroidsMove;
42 changes: 42 additions & 0 deletions src/behaviors/FaceTheMouse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Let the actor always face to the mouse
*/

const Behavior = require('engine/Behavior');
require('engine/gfx/interaction');

class FaceTheMouse extends Behavior {
constructor() {
super();

this.type = 'FaceTheMouse';

this.mousePos = null;
this.posCache = null;

this.rotation = 0;
}
init(ent) {
super.init(ent);

this.entity.canEverTick = true;
}
update(_, dt) {
if (!this.mousePos) {
this.mousePos = this.entity.game.sysGfx.mouse;
this.posCache = this.mousePos.clone();
}

this.rotation = this.posCache.copy(this.mousePos)
.subtract(this.entity.position)
.angle();

if (this.entity.gfx) {
this.entity.gfx.rotation = this.rotation;
}
}
}

Behavior.register('FaceTheMouse', FaceTheMouse);

module.exports = FaceTheMouse;
Loading

0 comments on commit 6ea7699

Please sign in to comment.