Skip to content
New issue

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

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

Already on GitHub? # to your account

Motion tweaking #923

Merged
merged 4 commits into from
Jun 30, 2015
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
67 changes: 34 additions & 33 deletions src/controls/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ Crafty.c("Multiway", {
this._keyDirection = {}; // keyCode -> direction
this._activeDirections = {}; // direction -> # of keys pressed for that direction
this._directionSpeed = {}; // direction -> {x: x_speed, y: y_speed}
this._speed = { x: 3, y: 3 };
this._speed = { x: 150, y: 150 };

this.bind("KeyDown", this._keydown)
.bind("KeyUp", this._keyup);
Expand Down Expand Up @@ -185,16 +185,16 @@ Crafty.c("Multiway", {
* #.multiway
* @comp Multiway
* @sign public this .multiway([Number speed,] Object keyBindings)
* @param speed - Amount of pixels to move the entity whilst a key is down
* @param speed - A speed in pixels per second
* @param keyBindings - What keys should make the entity go in which direction. Direction is specified in degrees
*
* Constructor to initialize the speed and keyBindings. Component will listen to key events and move the entity appropriately.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update speed here in example below

* Can be called while a key is pressed to change direction & speed on the fly.
*
* @example
* ~~~
* this.multiway(3, {UP_ARROW: -90, DOWN_ARROW: 90, RIGHT_ARROW: 0, LEFT_ARROW: 180});
* this.multiway({x:3,y:1.5}, {UP_ARROW: -90, DOWN_ARROW: 90, RIGHT_ARROW: 0, LEFT_ARROW: 180});
* this.multiway(150, {UP_ARROW: -90, DOWN_ARROW: 90, RIGHT_ARROW: 0, LEFT_ARROW: 180});
* this.multiway({x:150,y:75}, {UP_ARROW: -90, DOWN_ARROW: 90, RIGHT_ARROW: 0, LEFT_ARROW: 180});
* this.multiway({W: -90, S: 90, D: 0, A: 180});
* ~~~
*
Expand Down Expand Up @@ -234,12 +234,13 @@ Crafty.c("Multiway", {
* @sign public this .speed(Object speed)
* @param speed - New speed the entity has, for x and y axis.
*
* Change the speed that the entity moves with.
* Change the speed that the entity moves with, in units of pixels per second.
*
* Can be called while a key is pressed to change speed on the fly.
*
* @example
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update speed here in example below

* ~~~
* this.speed({ x: 3, y: 1 });
* this.speed({ x: 150, y: 50 });
* ~~~
*/
speed: function (speed) {
Expand Down Expand Up @@ -349,7 +350,7 @@ Crafty.c("Multiway", {


/**@
* #Jumpway
* #Jumper
* @category Controls
* @trigger NewDirection - When entity has changed direction due to velocity on either x or y axis a NewDirection event is triggered. The event is triggered once, if direction is different from last frame. - { x: -1 | 0 | 1, y: -1 | 0 | 1 } - New direction
* @trigger Moved - When entity has moved due to velocity/acceleration on either x or y axis a Moved event is triggered. If the entity has moved on both axes for diagonal movement the event is triggered twice. - { axis: 'x' | 'y', oldValue: Number } - Old position
Expand All @@ -359,20 +360,20 @@ Crafty.c("Multiway", {
*
* @see Supportable, Motion, Keyboard, Gravity
*/
Crafty.c("Jumpway", {
_jumpSpeed: 6,
Crafty.c("Jumper", {
_jumpSpeed: 300,

/**@
* #.canJump
* @comp Jumpway
* @comp Jumper
*
* The canJump function determines if the entity is allowed to jump or not (e.g. perhaps the entity should be able to double jump).
* The Jumpway component will trigger a "CheckJumping" event.
* The Jumper 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, Jumpway");
* var player = Crafty.e("2D, Jumper");
* player.hasDoubleJumpPowerUp = true; // allow player to double jump by granting him a powerup
* player.bind("CheckJumping", function(ground) {
* if (!ground && player.hasDoubleJumpPowerUp) { // allow player to double jump by using up his double jump powerup
Expand All @@ -389,7 +390,7 @@ Crafty.c("Jumpway", {

/**@
* #.enableControl
* @comp Jumpway
* @comp Jumper
* @sign public this .enableControl()
*
* Enable the component to listen to key events.
Expand All @@ -402,7 +403,7 @@ Crafty.c("Jumpway", {

/**@
* #.disableControl
* @comp Jumpway
* @comp Jumper
* @sign public this .disableControl()
*
* Disable the component to listen to key events.
Expand All @@ -421,10 +422,10 @@ Crafty.c("Jumpway", {
},

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

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

if (this._jumpKeys[e.key]) {
Expand All @@ -438,10 +439,10 @@ Crafty.c("Jumpway", {
},

/**@
* #.jumpway
* @comp Jumpway
* @sign public this .jumpway([Number jumpSpeed,] Array jumpKeys)
* @param jumpSpeed - Vertical jump speed
* #.jumper
* @comp Jumper
* @sign public this .jumper([Number jumpSpeed,] Array jumpKeys)
* @param jumpSpeed - Vertical jump speed in pixels per second
* @param jumpKeys - Keys to listen for and make entity jump in response
*
* Constructor to initialize the power of jump and keys to listen to. Component will
Expand All @@ -450,13 +451,13 @@ Crafty.c("Jumpway", {
*
* @example
* ~~~
* this.jumpway(6, ['UP_ARROW', 'W']);
* this.jumpway(['UP_ARROW', 'W']);
* this.jumper(300, ['UP_ARROW', 'W']);
* this.jumper(['UP_ARROW', 'W']);
* ~~~
*
* @see Supportable, Motion, Keyboard, Gravity
*/
jumpway: function (jumpSpeed, jumpKeys) {
jumper: function (jumpSpeed, jumpKeys) {
if (jumpKeys) {
this._jumpSpeed = jumpSpeed;
} else {
Expand All @@ -470,22 +471,22 @@ Crafty.c("Jumpway", {
this._jumpKeys[keyCode] = true;
}

this.uniqueBind("KeyDown", this._keydown_jumpway);
this.uniqueBind("KeyDown", this._keydown_jumper);

return this;
},

/**@
* #.jumpSpeed
* @comp Jumpway
* @comp Jumper
* @sign public this .jumpSpeed(Number jumpSpeed)
* @param jumpSpeed - new vertical jump speed
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update speed here in example below

* Change the vertical jump speed.
*
* @example
* ~~~
* this.jumpSpeed(6);
* this.jumpSpeed(300);
* ~~~
*/
jumpSpeed: function (jumpSpeed) {
Expand Down Expand Up @@ -515,7 +516,7 @@ Crafty.c("Fourway", {
* #.fourway
* @comp Fourway
* @sign public this .fourway([Number speed])
* @param speed - Amount of pixels to move the entity whilst a key is down
* @param speed - The speed of motion in pixels per second.
*
* Constructor to initialize the speed. Component will listen for key events and move the entity appropriately.
* This includes `Up Arrow`, `Right Arrow`, `Down Arrow`, `Left Arrow` as well as `W`, `A`, `S`, `D`.
Expand Down Expand Up @@ -551,20 +552,20 @@ Crafty.c("Fourway", {
*
* Move an entity left or right using the arrow keys or `D` and `A` and jump using up arrow or `W`.
*
* @see Multiway, Jumpway
* @see Multiway, Jumper
*/
Crafty.c("Twoway", {

init: function () {
this.requires("Multiway, Jumpway");
this.requires("Multiway, Jumper");
},

/**@
* #.twoway
* @comp Twoway
* @sign public this .twoway([Number speed[, Number jumpSpeed]])
* @param speed - Amount of pixels to move left or right
* @param jumpSpeed - Vertical jump speed
* @param speed - A speed in pixels per second
* @param jumpSpeed - Vertical jump speed in pixels per second
*
* Constructor to initialize the speed and power of jump. Component will
* listen for key events and move the entity appropriately. This includes
Expand All @@ -574,7 +575,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 Multiway, Jumpway
* @see Multiway, Jumper
*/
twoway: function (speed, jumpSpeed) {

Expand All @@ -586,7 +587,7 @@ Crafty.c("Twoway", {
Q: 180
});

this.jumpway(jumpSpeed || speed * 2 || this._jumpSpeed, [
this.jumper(jumpSpeed || speed * 2 || this._jumpSpeed, [
Crafty.keys.UP_ARROW,
Crafty.keys.W,
Crafty.keys.Z
Expand Down
Loading