Port to entity script

This commit is contained in:
Atlante45 2016-03-02 18:02:57 -08:00
parent 553300a783
commit 811ed91e02

View file

@ -8,18 +8,24 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
// Constants
var OBJECTS_LIFETIME = 1;
var G = 4.0;
(function() {
// Constants
var TRIGGER_CONTROLS = [
Controller.Standard.LT,
Controller.Standard.RT,
];
var entityManager = new EntityManager();
var OBJECTS_LIFETIME = 1;
var G = 4.0;
Number.prototype.clamp = function(min, max) {
var entityManager = new EntityManager();
Number.prototype.clamp = function(min, max) {
return Math.min(Math.max(this, min), max);
};
};
// Class definitions
function Bird(DEFAULT_X, DEFAULT_Y, to3DPosition) {
// Class definitions
function Bird(DEFAULT_X, DEFAULT_Y, to3DPosition) {
var DIMENSION = 0.05;
var JUMP_VELOCITY = 1.0;
var xPosition = DEFAULT_X;
@ -59,9 +65,9 @@ function Bird(DEFAULT_X, DEFAULT_Y, to3DPosition) {
yPosition = DEFAULT_Y;
yVelocity = 0.0;
}
}
}
function Pipe(xPosition, yPosition, height, gap, to3DPosition) {
function Pipe(xPosition, yPosition, height, gap, to3DPosition) {
var velocity = 0.6;
var width = 0.05;
var color = { red: 0, green: 255, blue: 0 };
@ -109,9 +115,9 @@ function Pipe(xPosition, yPosition, height, gap, to3DPosition) {
entityManager.remove(idUp);
entityManager.remove(idDown);
}
}
}
function Pipes(newPipesPosition, newPipesHeight, to3DPosition) {
function Pipes(newPipesPosition, newPipesHeight, to3DPosition) {
var lastPipe = 0;
var pipesInterval = 1.0;
@ -161,31 +167,45 @@ function Pipes(newPipesPosition, newPipesHeight, to3DPosition) {
});
pipes = new Array();
}
}
}
function Game() {
function Game() {
// public methods
this.start = function() {
if (!isRunning) {
isRunning = true;
setup();
Script.update.connect(idle);
// Script.update.connect(idle);
}
}
this.stop = function() {
if (isRunning) {
Script.update.disconnect(idle);
// Script.update.disconnect(idle);
cleanup();
isRunning = false;
}
}
this.keyPressed = function(event) {
if (event.text === "SPACE" && (gameTime - lastLost) > coolDown) {
isJumping = true;
startedPlaying = true;
// Game loop setup
var timestamp = 0;
this.idle = function() {
var now = Date.now();
var deltaTime = (now - timestamp) / 1000.0;
if (timestamp === 0) {
deltaTime = 0;
}
inputs();
update(deltaTime);
draw();
timestamp = now;
}
// this.keyPressed = function(event) {
// if (event.text === "SPACE" && (gameTime - lastLost) > coolDown) {
// isJumping = true;
// startedPlaying = true;
// }
// }
// Constants
var spaceDimensions = { x: 1.5, y: 0.8, z: 0.01 };
@ -209,13 +229,6 @@ function Game() {
var bird = null;
var pipes = null;
// Game loop setup
function idle(deltaTime) {
inputs();
update(deltaTime);
draw();
}
function setup() {
print("setup");
@ -225,20 +238,21 @@ function Game() {
dimensions: getSpaceDimensions()
}
board = entityManager.add({
type: "Box",
position: space.position,
rotation: space.orientation,
dimensions: space.dimensions,
color: { red: 100, green: 200, blue: 200 }
});
// board = entityManager.add({
// type: "Box",
// position: space.position,
// rotation: space.orientation,
// dimensions: space.dimensions,
// color: { red: 100, green: 200, blue: 200 }
// });
bird = new Bird(space.dimensions.x / 2.0, space.dimensions.y / 2.0, to3DPosition);
pipes = new Pipes(space.dimensions.x, space.dimensions.y, to3DPosition);
}
function inputs() {
//print("inputs");
function inputs(triggerValue) {
isJumping = true;
startedPlaying = true;
}
function update(deltaTime) {
//print("update: " + deltaTime);
@ -335,9 +349,9 @@ function Game() {
return position2D;
}
}
}
function EntityManager() {
function EntityManager() {
var entities = new Array();
var lifetime = OBJECTS_LIFETIME;
@ -379,19 +393,61 @@ function EntityManager() {
// Remove all from array
entities = new Array();
}
}
}
// Script logic
function scriptStarting() {
var game = new Game();
PartableGame = function() {
this.equipped = false;
this.triggerValue = 0.0;
this.hand = 0;
this.game = null;
};
Controller.keyPressEvent.connect(function(event) {
game.keyPressed(event);
});
Script.scriptEnding.connect(function() {
game.stop();
});
game.start();
}
PartableGame.prototype = {
preload: function(entityID) {
this.entityID = entityID;
},
unload: function() {
},
startEquip: function(id, params) {
this.equipped = true;
this.hand = params[0] == "left" ? 0 : 1;
this.game = new Game();
this.game.start();
},
releaseEquip: function(id, params) {
this.equipped = false;
this.game.stop();
delete this.game;
},
continueEquip: function(id, params) {
if (!this.equipped) {
return;
}
this.triggerValue = Controller.getValue(TRIGGER_CONTROLS[this.hand]);
this.game.idle();
},
};
// entity scripts always need to return a newly constructed object of our type
return new PartableGame();
});
// // Script logic
// function scriptStarting() {
// var game = new Game();
// Controller.keyPressEvent.connect(function(event) {
// game.keyPressed(event);
// });
// Script.scriptEnding.connect(function() {
// game.stop();
// });
// game.start();
// }
// scriptStarting();
scriptStarting();