-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
86 changed files
with
2,928 additions
and
2,457 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
howler.core.js | ||
async.js | ||
earcut.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.