Skip to content

Commit

Permalink
Fixed a bug if multi input keyboards and added some hero controls
Browse files Browse the repository at this point in the history
  • Loading branch information
alamboley committed Apr 3, 2012
1 parent 23653fc commit 025c60a
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 14 deletions.
Binary file modified Export/flash/bin/CitruxEngineExample.swf
Binary file not shown.
46 changes: 39 additions & 7 deletions Export/html5/bin/CitruxEngineExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -2710,10 +2710,9 @@ com.citruxengine.core.Input.prototype.initialize = function() {
}
com.citruxengine.core.Input.prototype.update = function() {
if(!this._enabled) return;
var $it0 = this._keys.iterator();
var $it0 = this._keys.keys();
while( $it0.hasNext() ) {
var value = $it0.next();
var key = this._keys.keys().next();
var key = $it0.next();
if(this._keys.get(key) == 0) this._keys.set(key,1);
}
this._keysReleased = [];
Expand Down Expand Up @@ -17107,9 +17106,10 @@ com.citruxengine.objects.platformer.Hero = function(name,params) {
this._maxVelocity = 8;
this._jumpHeight = 14;
this._jumpAcceleration = 0.9;
this._friction = 0.75;
this._controlsEnabled = true;
com.citruxengine.objects.PhysicsObject.call(this,name,params);
this._playerMovingHero = false;
this._controlsEnabled = true;
}
com.citruxengine.objects.platformer.Hero.__name__ = ["com","citruxengine","objects","platformer","Hero"];
com.citruxengine.objects.platformer.Hero.__super__ = com.citruxengine.objects.PhysicsObject;
Expand All @@ -17118,12 +17118,15 @@ com.citruxengine.objects.platformer.Hero.prototype.acceleration = null;
com.citruxengine.objects.platformer.Hero.prototype.maxVelocity = null;
com.citruxengine.objects.platformer.Hero.prototype.jumpHeight = null;
com.citruxengine.objects.platformer.Hero.prototype.jumpAcceleration = null;
com.citruxengine.objects.platformer.Hero.prototype.controlsEnabled = null;
com.citruxengine.objects.platformer.Hero.prototype.friction = null;
com.citruxengine.objects.platformer.Hero.prototype._playerMovingHero = null;
com.citruxengine.objects.platformer.Hero.prototype._controlsEnabled = null;
com.citruxengine.objects.platformer.Hero.prototype._acceleration = null;
com.citruxengine.objects.platformer.Hero.prototype._maxVelocity = null;
com.citruxengine.objects.platformer.Hero.prototype._jumpHeight = null;
com.citruxengine.objects.platformer.Hero.prototype._jumpAcceleration = null;
com.citruxengine.objects.platformer.Hero.prototype._controlsEnabled = null;
com.citruxengine.objects.platformer.Hero.prototype._friction = null;
com.citruxengine.objects.platformer.Hero.prototype.destroy = function() {
com.citruxengine.objects.PhysicsObject.prototype.destroy.call(this);
}
Expand All @@ -17132,8 +17135,21 @@ com.citruxengine.objects.platformer.Hero.prototype.update = function(timeDelta)
var velocity = this._body.getLinearVelocity();
if(this._controlsEnabled) {
var moveKeyPressed = false;
if(this._ce.getInput().isDown(jeash.ui.Keyboard.RIGHT)) velocity.add(new box2D.common.math.B2Vec2(5,0));
if(this._ce.getInput().isDown(jeash.ui.Keyboard.LEFT)) velocity.subtract(new box2D.common.math.B2Vec2(5,0));
if(this._ce.getInput().isDown(jeash.ui.Keyboard.RIGHT)) {
velocity.add(new box2D.common.math.B2Vec2(5,0));
moveKeyPressed = true;
}
if(this._ce.getInput().isDown(jeash.ui.Keyboard.LEFT)) {
velocity.subtract(new box2D.common.math.B2Vec2(5,0));
moveKeyPressed = true;
}
if(moveKeyPressed && !this._playerMovingHero) {
this._playerMovingHero = true;
this._fixture.setFriction(0);
} else if(!moveKeyPressed && this._playerMovingHero) {
this._playerMovingHero = false;
this._fixture.setFriction(this._friction);
}
if(this._ce.getInput().justPressed(jeash.ui.Keyboard.SPACE)) velocity.y = -this._jumpHeight;
if(this._ce.getInput().isDown(jeash.ui.Keyboard.SPACE)) velocity.y -= this._jumpAcceleration;
if(velocity.x > this._maxVelocity) velocity.x = this._maxVelocity; else if(velocity.x < -this._maxVelocity) velocity.x = -this._maxVelocity;
Expand Down Expand Up @@ -17171,6 +17187,22 @@ com.citruxengine.objects.platformer.Hero.prototype.getJumpAcceleration = functio
com.citruxengine.objects.platformer.Hero.prototype.setJumpAcceleration = function(value) {
return this._jumpAcceleration = value;
}
com.citruxengine.objects.platformer.Hero.prototype.getControlsEnabled = function() {
return this._controlsEnabled;
}
com.citruxengine.objects.platformer.Hero.prototype.setControlsEnabled = function(value) {
this._controlsEnabled = value;
if(!this._controlsEnabled) this._fixture.setFriction(this._friction);
return this._controlsEnabled;
}
com.citruxengine.objects.platformer.Hero.prototype.getFriction = function() {
return this._friction;
}
com.citruxengine.objects.platformer.Hero.prototype.setFriction = function(value) {
this._friction = value;
if(this._fixture != null) this._fixture.setFriction(this._friction);
return this._friction;
}
com.citruxengine.objects.platformer.Hero.prototype.__class__ = com.citruxengine.objects.platformer.Hero;
haxe.Log = function() { }
haxe.Log.__name__ = ["haxe","Log"];
Expand Down
4 changes: 1 addition & 3 deletions Source/com/citruxengine/core/Input.hx
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ class Input {
if (!_enabled)
return;

for (value in _keys) {

var key:Int = _keys.keys().next();
for (key in _keys.keys()) {

if (_keys.get(key) == JUST_PRESSED) {
_keys.set(key, DOWN);
Expand Down
69 changes: 65 additions & 4 deletions Source/com/citruxengine/objects/platformer/Hero.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,30 @@ class Hero extends PhysicsObject {
public var maxVelocity(getMaxVelocity, setMaxVelocity):Float;
public var jumpHeight(getJumpHeight, setJumpHeight):Float;
public var jumpAcceleration(getJumpAcceleration, setJumpAcceleration):Float;
public var controlsEnabled(getControlsEnabled, setControlsEnabled):Bool;
public var friction(getFriction, setFriction):Float;

private var _playerMovingHero:Bool;
private var _controlsEnabled:Bool;

var _acceleration:Float;
var _maxVelocity:Float;
var _jumpHeight:Float;
var _jumpAcceleration:Float;
var _controlsEnabled:Bool;
var _friction:Float;

public function new(name:String, params:Dynamic = null) {

_acceleration = 1;
_maxVelocity = 8;
_jumpHeight = 14;
_jumpAcceleration = 0.9;
_friction = 0.75;
_controlsEnabled = true;

super(name, params);

_playerMovingHero = false;
_controlsEnabled = true;
}

override public function destroy():Void {
Expand All @@ -58,11 +62,33 @@ class Hero extends PhysicsObject {

var moveKeyPressed:Bool = false;

if (_ce.input.isDown(Keyboard.RIGHT))
if (_ce.input.isDown(Keyboard.RIGHT)) {

velocity.add(new B2Vec2(5, 0));
moveKeyPressed = true;
}

if (_ce.input.isDown(Keyboard.LEFT))
if (_ce.input.isDown(Keyboard.LEFT)) {
velocity.subtract(new B2Vec2(5, 0));
moveKeyPressed = true;
}

//If player just started moving the hero this tick.
if (moveKeyPressed && !_playerMovingHero) {

_playerMovingHero = true;

//Take away friction so he can accelerate.
_fixture.setFriction(0);

} else if (!moveKeyPressed && _playerMovingHero) {

//Player just stopped moving the hero this tick.
_playerMovingHero = false;

//Add friction so that he stops running
_fixture.setFriction(_friction);
}

if (_ce.input.justPressed(Keyboard.SPACE))
velocity.y = -_jumpHeight;
Expand Down Expand Up @@ -125,4 +151,39 @@ class Hero extends PhysicsObject {
public function setJumpAcceleration(value:Float):Float {
return _jumpAcceleration = value;
}

/**
* Whether or not the player can move and jump with the hero.
*/
public function getControlsEnabled():Bool {
return _controlsEnabled;
}

public function setControlsEnabled(value:Bool):Bool {

_controlsEnabled = value;

if (!_controlsEnabled)
_fixture.setFriction(_friction);

return _controlsEnabled;
}

/**
* This is the amount of friction that the hero will have. Its value is multiplied against the
* friction value of other physics objects.
*/
public function getFriction():Float {
return _friction;
}

public function setFriction(value:Float):Float {

_friction = value;

if (_fixture != null)
_fixture.setFriction(_friction);

return _friction;
}
}

0 comments on commit 025c60a

Please sign in to comment.