-
Notifications
You must be signed in to change notification settings - Fork 8
Home
James Baicoianu edited this page Aug 25, 2013
·
10 revisions
The Elation Engine is a lightweight framework for developing 3d world simulations using HTML5 and JavaScript.
// First, we define the entities which make up our game.
// This is a simple soccer game, so we have a field, some players, a ball, and some goals
elation.component.add("engine.things.field", function() {
// Size reference: http://upload.wikimedia.org/wikipedia/commons/c/cf/Football_pitch_metric.svg
this.init = function() {
this.defineProperties({
'size': { type: 'vector', default: [100, 0, 75] },
'circlesize': { type: 'float': default: 9.15 }
});
}
this.getPlayerPosition = function(playernum) {
var pos = this.getCenterPosition();
// We could support more players if we added teams, but for now it's just 1v1
switch (playernum) {
case 1:
pos.x -= this.properties.circlesize;
break;
case 2:
pos.x += this.properties.circlesize;
break;
}
return pos;
}
this.getGoalPosition = function(playernum) {
return new THREE.Vector3((playernum == 1 ? 0 : this.size[1]), 0, this.size[1] / 2);
}
this.getCenterPosition = function() {
return new THREE.Vector3(this.size[0] / 2, 0, this.size[1] / 2);
}
}, elation.engine.things.generic);
elation.component.add("engine.things.player", function() {
this.init = function() {
this.defineProperties({
'speed': { type: 'float', default: 1.0 },
'strength': { type: 'float', default: 1.0 }
});
}
});
// Next, we create the engine and pass in an initialization function which sets up our entities
// Once the engine is loaded, it will execute this function and start simulating the world
var engine = elation.engine.create("websoccer", ["physics", "render", "controls", "ai", "world"], function(engine) {
// Create field
var field = engine.systems.world.spawn("soccerfield", "playground");
// Spawn players
var player1 = field.spawn("player", "player1", {position: field.getPlayerPosition(1), playernum: 1});
var player2 = field.spawn("player", "player2", {position: field.getPlayerPosition(2), playernum: 2});
// Define goals
var goal1 = field.spawn("goal", "goal1", {position: field.getGoalPosition(1), owner: player1});
var goal2 = field.spawn("goal", "goal2", {position: field.getGoalPosition(2), owner: player2});
// Add ball
var ball = field.spawn("ball", "ball", {position: field.getCenterPosition()});
// Let's go!
engine.start();
});