Skip to content
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
65 changes: 36 additions & 29 deletions src/controls/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -1017,8 +1017,6 @@ Crafty.c("Keyboard", {
* @see Motion
*/
Crafty.c("Multiway", {
_speed: 3,

init: function () {
this.requires("Motion");

Expand Down Expand Up @@ -1161,8 +1159,8 @@ Crafty.c("Multiway", {
direction = this._keyDirection[keyCode];
// add new data
this._directionSpeed[direction] = {
x: this.__convertPixelsToMeters(Math.round(Math.cos(direction * (Math.PI / 180)) * 1000 * speed.x) / 1000),
y: this.__convertPixelsToMeters(Math.round(Math.sin(direction * (Math.PI / 180)) * 1000 * speed.y) / 1000)
x: Math.round(Math.cos(direction * (Math.PI / 180)) * 1000 * speed.x) / 1000,
y: Math.round(Math.sin(direction * (Math.PI / 180)) * 1000 * speed.y) / 1000
};
}
},
Expand Down Expand Up @@ -1249,7 +1247,7 @@ Crafty.c("Fourway", {
/**@
* #.fourway
* @comp Fourway
* @sign public this .fourway(Number speed)
* @sign public this .fourway([Number speed])
* @param speed - Amount of pixels to move the entity whilst a key is down
*
* Constructor to initialize the speed. Component will listen for key events and move the entity appropriately.
Expand All @@ -1260,7 +1258,7 @@ Crafty.c("Fourway", {
* @see Multiway, Motion
*/
fourway: function (speed) {
this.multiway(speed, {
this.multiway(speed || this._speed, {
UP_ARROW: -90,
DOWN_ARROW: 90,
RIGHT_ARROW: 0,
Expand Down Expand Up @@ -1289,7 +1287,8 @@ Crafty.c("Fourway", {
* @see Gravity, Multiway, Fourway, Motion
*/
Crafty.c("Twoway", {
_speed: 3,
_jumpSpeed: 6,

/**@
* #.canJump
* @comp Twoway
Expand All @@ -1301,11 +1300,15 @@ Crafty.c("Twoway", {
* @example
* ~~~
* var player = Crafty.e("2D, Twoway");
* player.hasDoubleJumpPowerUp = true; // allow player to double jump by granting him a powerup
* player.bind("CheckJumping", function(ground) {
* if (!ground && player.hasDoubleJumpPowerUp) { // custom behaviour
* player.hasDoubleJumpPowerUp = false;
* player.canJump = true;
* }
* if (!ground && player.hasDoubleJumpPowerUp) { // allow player to double jump by using up his double jump powerup
* player.canJump = true;
* player.hasDoubleJumpPowerUp = false;
* }
* });
* player.bind("LandedOnGround", function(ground) {
* player.hasDoubleJumpPowerUp = true; // give player new double jump powerup upon landing
* });
* ~~~
*/
Expand All @@ -1315,10 +1318,27 @@ Crafty.c("Twoway", {
this.requires("Fourway, Motion, Supportable");
},

remove: function() {
this.unbind("KeyDown", this._keydown_twoway);
},

_keydown_twoway: function (e) {
if (this.disableControls) return;

if (e.key === Crafty.keys.UP_ARROW || e.key === Crafty.keys.W || e.key === Crafty.keys.Z) {
var ground = this.ground;
this.canJump = !!ground;
this.trigger("CheckJumping", ground);
if (this.canJump) {
this.vy = -this._jumpSpeed;
}
}
},

/**@
* #.twoway
* @comp Twoway
* @sign public this .twoway(Number speed[, Number jump])
* @sign public this .twoway([Number speed[, Number jump]])
* @param speed - Amount of pixels to move left or right
* @param jump - Vertical jump speed
*
Expand All @@ -1334,33 +1354,20 @@ Crafty.c("Twoway", {
*/
twoway: function (speed, jump) {

this.multiway(speed, {
this.multiway(speed || this._speed, {
RIGHT_ARROW: 0,
LEFT_ARROW: 180,
D: 0,
A: 180,
Q: 180
});

if (speed) this._speed = speed;
if (arguments.length < 2) {
this._jumpSpeed = this.__convertPixelsToMeters(this._speed * 2);
this._jumpSpeed = this._speed.y * 2;
} else {
this._jumpSpeed = this.__convertPixelsToMeters(jump);
this._jumpSpeed = jump;
}

var ground;
this.bind("KeyDown", function (e) {
if (this.disableControls) return;
if (e.key === Crafty.keys.UP_ARROW || e.key === Crafty.keys.W || e.key === Crafty.keys.Z) {
ground = this.ground();
this.canJump = !!ground;
this.trigger("CheckJumping", ground);
if (this.canJump) {
this.vy = -this._jumpSpeed;
}
}
});
this.uniqueBind("KeyDown", this._keydown_twoway);

return this;
}
Expand Down
2 changes: 2 additions & 0 deletions src/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,7 @@ Crafty.extend({
* Returns the target frames per second. This is not an actual frame rate.
* @sign public void Crafty.timer.FPS(Number value)
* @param value - the target rate
* @trigger FPSChange - Triggered when the target FPS is changed by user - Number - new target FPS
* Sets the target frames per second. This is not an actual frame rate.
* The default rate is 50.
*/
Expand All @@ -1421,6 +1422,7 @@ Crafty.extend({
else {
FPS = value;
milliSecPerFrame = 1000 / FPS;
Crafty.trigger("FPSChange", value);
}
},

Expand Down
Loading