Refactoring and added tests for platformerobjectruntimeautomatism.js

This commit is contained in:
Florian Rival
2015-04-19 22:53:02 +12:00
parent 47ef1d2152
commit ac7337ea67
4 changed files with 70 additions and 19 deletions

View File

@@ -402,6 +402,7 @@ bool PlatformerObjectAutomatism::SeparateFromPlatforms(const std::set<PlatformAu
std::set<PlatformAutomatism*> PlatformerObjectAutomatism::GetPlatformsCollidingWith(const std::set<PlatformAutomatism*> & candidates,
const std::set<PlatformAutomatism*> & exceptTheseOnes)
{
//TODO: This function could be refactored to return only the first colliding platform.
std::set<PlatformAutomatism*> result;
for (std::set<PlatformAutomatism*>::iterator it = candidates.begin();
it != candidates.end();

View File

@@ -36,7 +36,6 @@ gdjs.PlatformerObjectRuntimeAutomatism = function(runtimeScene, automatismData,
this._downKey = false;
this._jumpKey = false;
this._potentialCollidingObjects = []; //Platforms near the object, updated with _updatePotentialCollidingObjects.
this._collidingObjects = [];
this._overlappedJumpThru =[];
this._oldHeight = 0;//owner.getHeight(); //Be careful, object might not be initialized.
this._hasReallyMoved = false;
@@ -294,24 +293,19 @@ gdjs.PlatformerObjectRuntimeAutomatism.prototype.doStepPreEvents = function(runt
}
else{
//Check if landing on a new floor: (Exclude already overlapped jump truh)
this._updateCollidingObjects();
var collidingWithAnObject = false;
for (var i = 0;i < this._collidingObjects.length;++i ) { //TODO: No loop needed
var collidingPlatform = this._getCollidingPlatform();
if (collidingPlatform !== null) {
this._isOnFloor = true;
this._canJump = true;
this._jumping = false;
this._currentJumpSpeed = 0;
this._currentFallSpeed = 0;
//Register one of the colliding platforms as the floor.
this._floorPlatform = this._collidingObjects[i];
//Register the colliding platform as the floor.
this._floorPlatform = collidingPlatform;
this._floorLastX = this._floorPlatform.owner.getX();
this._floorLastY = this._floorPlatform.owner.getY();
collidingWithAnObject = true;
break;
}
if (!collidingWithAnObject) { //In the air
} else { //In the air
this._canJump = false;
this._isOnFloor = false;
this._floorPlatform = null;
@@ -414,24 +408,23 @@ gdjs.PlatformerObjectRuntimeAutomatism.prototype._isCollidingWithExcluding = fun
};
/**
* Update _collidingObjects member, so that it contains all the platforms which are colliding with
* the automatism owner object. Overlapped jump thru are excluded.
* Ladders are *always* excluded from the test.
* _updatePotentialCollidingObjects and _updateOverlappedJumpThru should have been called before
* Return (one of) the platform which is colliding with the automatism owner object.
* Overlapped jump thru and ladders are excluded.
* _updatePotentialCollidingObjects and _updateOverlappedJumpThru should have been called before.
*/
gdjs.PlatformerObjectRuntimeAutomatism.prototype._updateCollidingObjects = function()
gdjs.PlatformerObjectRuntimeAutomatism.prototype._getCollidingPlatform = function()
{
//TODO: _collidingObjects seems useless, we just need one.
this._collidingObjects.length = 0;
for (var i = 0;i < this._potentialCollidingObjects.length;++i) {
var platform = this._potentialCollidingObjects[i];
if ( platform.getPlatformType() !== gdjs.PlatformRuntimeAutomatism.LADDER
&& !this._isIn(this._overlappedJumpThru, platform.owner.id)
&& gdjs.RuntimeObject.collisionTest(this.owner, platform.owner) ) {
this._collidingObjects.push(platform);
return platform;
}
}
return null; //Nothing is being colliding with the automatism object.
};
/**

View File

@@ -0,0 +1,55 @@
describe('gdjs.PlatformerObjectRuntimeAutomatism', function() {
var inputManager = new gdjs.InputManager();
it('test', function(){
var runtimeGame = new gdjs.RuntimeGame({variables: [], properties: {windowWidth: 800, windowHeight: 600}});
var runtimeScene = new gdjs.RuntimeScene(runtimeGame, null);
runtimeScene.loadFromScene({
layers:[{name:"", visibility: true}],
variables: [],
automatismsSharedData: [],
objects: [],
instances: []
});
runtimeScene.getElapsedTime = function() { return 1/60*1000; };
//Put a platformer object in the air.
var object = new gdjs.RuntimeObject(runtimeScene, {name: "obj1", type: "", automatisms: [{
type: "PlatformAutomatism::PlatformerObjectAutomatism",
name: "auto1",
gravity: 900,
maxFallingSpeed: 1500,
acceleration: 500,
deceleration: 500,
maxSpeed: 500,
jumpSpeed: 1500,
ignoreDefaultControls: true,
slopeMaxAngle: 60
}]});
runtimeScene.addObject(object);
object.setPosition(0, -100);
//Put a platform
var object2 = new gdjs.RuntimeObject(runtimeScene, {name: "obj2", type: "", automatisms: [{type: "PlatformAutomatism::PlatformAutomatism"}]});
object2.getWidth = function() { return 60; }
object2.getHeight = function() { return 32; }
runtimeScene.addObject(object2);
object2.setPosition(0, -10);
for(var i = 0;i<30;++i) {
runtimeScene.renderAndStep();
}
//Check the platform stopped the platformer object.
expect(object.getY()).to.be(-11);
for(var i = 0;i<35;++i) { //Check that the platformer object can fall.
object.getAutomatism("auto1").simulateRightKey();
runtimeScene.renderAndStep();
}
expect(object.getX()).to.be.within(87.50, 87.51);
expect(object.getY()).to.be(-5.75);
});
});

View File

@@ -39,6 +39,8 @@ module.exports = function(config) {
//Extensions:
'../../Extensions/DraggableAutomatism/draggableruntimeautomatism.js',
'../../Extensions/PlatformAutomatism/platformerobjectruntimeautomatism.js',
'../../Extensions/PlatformAutomatism/platformruntimeautomatism.js',
//All tests files:
'../../Extensions/**/tests/**.spec.js',