Skip to content
Closed
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
55 changes: 38 additions & 17 deletions src/controls/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -1222,16 +1222,36 @@ Crafty.c("Fourway", {
* #Twoway
* @category Input
* @trigger NewDirection - When direction changes a NewDirection event is triggered with an object detailing the new direction: {x: x_movement, y: y_movement}. This is consistent with Fourway and Multiway components.
* @trigger Moved - When entity has moved on x-axis a Moved event is triggered with an object specifying the old position {x: old_x, y: old_y}
*
* @trigger Moved - triggered on movement on either x or y axis. If the entity has moved on both axes for diagonal movement the event is triggered twice - { x:Number, y:Number } - Old position
* @trigger CheckJumping - When entity is about to jump. This event is triggered with the object the entity is about to jump from (if it exists). Third parties can respond to this event and enable the entity to jump.
*
* Move an entity left or right using the arrow keys or `D` and `A` and jump using up arrow or `W`.
*/
Crafty.c("Twoway", {
_speed: 3,
_up: false,
/**@
* #.canJump
* @comp Twoway
*
* The canJump function determines if the entity is allowed to jump or not (e.g. perhaps the entity should not jump if it's already falling).
* The Twoway component will trigger a "CheckJumping" event.
* Interested parties can listen to this event and enable the entity to jump by setting `canJump` to true.
*
* @example
* ~~~
* var player = Crafty.e("2D, Twoway");
* player.bind("CheckJumping", function(ground) {
* if (!ground && player.hasDoubleJumpPowerUp) { // custom behaviour
* player.hasDoubleJumpPowerUp = false;
* player.canJump = true;
* }
* });
* ~~~
*/
canJump: true,

init: function () {
this.requires("Fourway, Keyboard, Gravity");
this.requires("Fourway, Motion, Supportable");
},

/**@
Expand All @@ -1249,7 +1269,7 @@ Crafty.c("Twoway", {
* The key presses will move the entity in that direction by the speed passed in
* the argument. Pressing the `Up Arrow` or `W` will cause the entity to jump.
*
* @see Gravity, Fourway
* @see Gravity, Fourway, Motion
*/
twoway: function (speed, jump) {

Expand All @@ -1262,22 +1282,23 @@ Crafty.c("Twoway", {
});

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

this.bind("EnterFrame", function () {
var ground;
this.bind("KeyDown", function (e) {
if (this.disableControls) return;
if (this._up) {
this.y -= this._jumpSpeed;
this._falling = true;
this.trigger('Moved', { x: this._x, y: this._y + this._jumpSpeed });
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; // Motion component will trigger Move events
}
}
}).bind("KeyDown", function (e) {
if (!this._falling && (e.key === Crafty.keys.UP_ARROW || e.key === Crafty.keys.W || e.key === Crafty.keys.Z))
this._up = true;
});

return this;
Expand Down
82 changes: 74 additions & 8 deletions src/core/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,7 @@ Crafty.fn = Crafty.prototype = {
return clone;
},


/**@
* #.setter
* @comp Crafty Core
Expand All @@ -887,16 +888,43 @@ Crafty.fn = Crafty.prototype = {
* Will watch a property waiting for modification and will then invoke the
* given callback when attempting to modify.
*
* This feature is deprecated; use .defineField() instead.
* @see .defineField
*/
setter: function (prop, callback) {
if (Crafty.support.setter) {
this.__defineSetter__(prop, callback);
} else if (Crafty.support.defineProperty) {
Object.defineProperty(this, prop, {
set: callback,
configurable: true
});
}
return this.defineField(prop, function(){}, callback);
},

/**@
* #.defineField
* @comp Crafty Core
* @sign public this .defineField(String property, Function getCallback, Function setCallback)
* @param property - Property name to assign getter & setter to
* @param getCallback - Method to execute if the property is accessed
* @param setCallback - Method to execute if the property is mutated
*
* Assigns getters and setters to the property.
* A getter will watch a property waiting for access and will then invoke the
* given getCallback when attempting to retrieve.
* A setter will watch a property waiting for mutation and will then invoke the
* given setCallback when attempting to modify.
*
* @example
* ~~~
* var ent = Crafty.e("2D");
* ent.defineField("customData", function() {
* return this._customData;
* }, function(newValue) {
* this._customData = newValue;
* });
*
* ent.customData = "2" // set customData to 2
* console.log(ent.customData) // prints 2
* ~~~
* @see Crafty.defineField
*/
defineField: function (prop, getCallback, setCallback) {
Crafty.defineField(this, prop, getCallback, setCallback);
return this;
},

Expand Down Expand Up @@ -1745,6 +1773,44 @@ Crafty.extend({
};
})(),

/**@
* #Crafty.defineField
* @category Core
* @sign public void Crafty.defineField(Object object, String property, Function getCallback, Function setCallback)
* @param object - Object to define property on
* @param property - Property name to assign getter & setter to
* @param getCallback - Method to execute if the property is accessed
* @param setCallback - Method to execute if the property is mutated
*
* Assigns getters and setters to the property in the given object.
* A getter will watch a property waiting for access and will then invoke the
* given getCallback when attempting to retrieve.
* A setter will watch a property waiting for mutation and will then invoke the
* given setCallback when attempting to modify.
*
* @example
* ~~~
* var ent = Crafty.e("2D");
* Crafty.defineField(ent, "customData", function() {
* return this._customData;
* }, function(newValue) {
* this._customData = newValue;
* });
*
* ent.customData = "2" // set customData to 2
* console.log(ent.customData) // prints 2
* ~~~
* @see .defineField
*/
defineField: function(obj, prop, getCallback, setCallback) {
Object.defineProperty(obj, prop, {
get: getCallback,
set: setCallback,
configurable: false,
enumerable: true,
});
},

clone: clone
});

Expand Down
3 changes: 2 additions & 1 deletion src/graphics/canvas-layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ Crafty.extend({
l = changed.length,
dirty = this._dirtyRects,
rectManager = Crafty.rectManager,
overlap = rectManager.overlap,
ctx = this.context,
dupes = [],
objs = [];
Expand Down Expand Up @@ -205,7 +206,7 @@ Crafty.extend({
for (j = 0, len = objs.length; j < len; ++j) {
obj = objs[j];
var area = obj._mbr || obj;
if (rectManager.overlap(area, rect))
if (overlap(area, rect))
obj.draw();
obj._changed = false;
}
Expand Down
Loading