Skip to content

Commit

Permalink
Added counters, precompiled assets
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarnhart committed Jan 29, 2012
1 parent f8febef commit 94f57f4
Show file tree
Hide file tree
Showing 17 changed files with 131 additions and 45 deletions.
56 changes: 56 additions & 0 deletions app/assets/javascripts/FactoryUnit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//= require Unit

GLOBE.FactoryUnit = function() {
GLOBE.Unit.call(this, 'Factory', 10);

this.building = null;

this.joins = [];

this.addToScene = function(scene) {
var geometry = new THREE.CubeGeometry(50, 50, 50, 1, 1, 1);
material = new THREE.MeshLambertMaterial( { color: 0x999999 } );
this.building = new THREE.Mesh( geometry, material );

this.building.position = this.node.normal.clone().multiplyScalar(20);
this.building.position.addSelf(this.node.position);

this.building.lookAt(this.building.position.clone().addSelf(this.node.normal));

scene.add(this.building);
};

this.removeFromScene = function(scene) {
scene.removeFromScene(building);
delete this.building;
this.building=null;
for(i=0; i<this.joins.length; i++) {
scene.removeFromScene(this.joins[i]);
delete this.joins[i];
}
this.joins = [];
}

this.addNeighbour = function(neighbour, scene) {
if(neighbour.unit!=null && neighbour.unit.constructor == GLOBE.FactoryUnit) {
var midpoint = this.node.position.clone().addSelf(neighbour.position).multiplyScalar(0.5);
var avgnormal = this.node.normal.clone().addSelf(neighbour.normal);
var offset = avgnormal.multiplyScalar(40).addSelf(midpoint);
var joinCurve = new THREE.QuadraticBezierCurve3(this.node.position, offset, neighbour.position)
var joinGeom = new THREE.Geometry();
joinGeom.vertices = joinCurve.getSpacedPoints(8).map(function(value) { return new THREE.Vertex(value); });
var joinMaterial = new THREE.LineBasicMaterial( { color: 0x00ff00 });
var join = new THREE.Line( joinGeom, joinMaterial, THREE.LineStrip);

this.joins.push(join);
scene.add(join);
}
};
};

GLOBE.Unit.prototype.clone = function() {
return new GLOBE.FactoryUnit();
};

GLOBE.FactoryUnit.prototype = new GLOBE.Unit();
GLOBE.FactoryUnit.prototype.constructor = GLOBE.FactoryUnit;
66 changes: 24 additions & 42 deletions app/assets/javascripts/Gameplay.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,57 @@
//= require Globe
//= require FactoryUnit

GLOBE.Gameplay = function(network, scene) {
this.scene=scene;
this.network=network;

this.reset();
this.units = { factory: new GLOBE.FactoryUnit() };

//amount of manufactured goods required to build various things
this.FactoryCost = 10;
this.SpaceCost = 1000;

this.reset();
};

GLOBE.Gameplay.prototype.constructor = GLOBE.Gameplay;

GLOBE.Gameplay.prototype.reset = function() {
this.pollution=0; //planetary pollution
this.capacity=0; //rate of manufacture
this.manufactured_goods=this.FactoryCost; //current stock of manufactured goods
this.state = {
pollution: 0, //planetary pollution
//current stock of manufactured goods
manufactured_goods: Math.round(1.5*this.units.factory.cost)
};

for(i=0; i<this.network.length; i++) {
var node = this.network[i];
if("building" in node && node.building!=null) {
this.scene.removeObject(node.building);
delete node.building;
}
if("joins" in node) {
for(i=0; i<joins.length; i++) {
this.scene.removeObject(node.joins[i]);
delete node.joins[i];
}
if("unit" in node && node.unit!=null) {
node.unit.erase(this.scene)
delete node.unit;
}

this.network[i].building=null;
this.network[i].joins = [];
node.unit=null;
}
}

GLOBE.Gameplay.prototype.handlePlacement = function(placement) {
var node = this.network[placement];

if(node.building!=null) return;

var geometry = new THREE.CubeGeometry(50, 50, 50, 1, 1, 1);
material = new THREE.MeshLambertMaterial( { color: 0x999999 } );
building = new THREE.Mesh( geometry, material );

building.position = node.normal.clone().multiplyScalar(20);
building.position.addSelf(node.position);

building.lookAt(building.position.clone().addSelf(network[placement].normal));

this.scene.add(building);

node.building = building;
if(this.units.factory.clone().place(node, this.state, this.scene) == false) return;

for(i=0; i<node.neighbours.length; i++) {
var neighbour = this.network[node.neighbours[i]];
if(neighbour.building != null) {
var midpoint = node.position.clone().addSelf(neighbour.position).multiplyScalar(0.5);
var avgnormal = node.normal.clone().addSelf(neighbour.normal);
var offset = avgnormal.multiplyScalar(40).addSelf(midpoint);
var joinCurve = new THREE.QuadraticBezierCurve3(node.position, offset, neighbour.position)
var joinGeom = new THREE.Geometry();
joinGeom.vertices = joinCurve.getSpacedPoints(8).map(function(value) { return new THREE.Vertex(value); });
var joinMaterial = new THREE.LineBasicMaterial( { color: 0x00ff00 });
var join = new THREE.Line( joinGeom, joinMaterial, THREE.LineStrip);

node.joins.push(join);
this.scene.add(join);
if(neighbour.unit!=null) {
node.unit.addNeighbour(neighbour, this.scene);
//neighbour.addNeighbour(node.unit);
}
}
};

GLOBE.Gameplay.prototype.update = function() {
this.network
for(i=0; i<this.network.length; i++) {
var node = this.network[i];
if(node.unit == null) continue;
node.unit.update(this.state, this.scene);
}
}
42 changes: 42 additions & 0 deletions app/assets/javascripts/Unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
GLOBE.Unit = function(name, cost, production, pollution) {
this.name = name;
this.cost = cost;
this.production = production || cost;
this.pollution = pollution || this.production;
this.node = null;
};

GLOBE.Unit.prototype.addToScene = function(scene) {};

GLOBE.Unit.prototype.removeFromScene = function(scene) {};

GLOBE.Unit.prototype.addNeighbour = function(neighbour, scene) {};

GLOBE.Unit.prototype.place = function(node, state, scene) {
if(node.unit != null) return false;
//if(state.manufactured_goods < this.cost) return false;

this.node=node;
node.unit=this;
this.addToScene(scene);

state.manufactured_goods -= this.cost;

return true;
};

GLOBE.Unit.prototype.remove = function(state, scene) {
this.erase(scene);
}

GLOBE.Unit.prototype.erase = function(scene) {
this.node=null;
this.removeFromScene(scene);
}

GLOBE.Unit.prototype.update = function(state, scene) {
state.manufactured_goods += this.production;
state.pollution += this.pollution;
};

GLOBE.Unit.prototype.clone = function() {};
6 changes: 6 additions & 0 deletions app/views/static_pages/home.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<% provide(:title, 'Home') %>
<div>
<div style="text-align:right">Manufactured Goods: <span id="manufactured-goods">0</span> |||||| Pollution: <span id="pollution">0</span></div>
<%= link_to "Start the Game!", '#', id: "start-button", class: "start_button button orange" %>
<div id="view-element", style="display:none"></div>
</div>
Expand Down Expand Up @@ -111,6 +112,11 @@
// note: three.js includes requestAnimationFrame shim
requestAnimationFrame( animate );
trackball.update();
gameplay.update();

$('#manufactured-goods').text(gameplay.state.manufactured_goods);
$('#pollution').text(gameplay.state.pollution);

render();
}

Expand Down

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file modified public/assets/application.css.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion public/assets/application.js

Large diffs are not rendered by default.

Binary file modified public/assets/application.js.gz
Binary file not shown.
Binary file not shown.
Binary file modified public/assets/blueprint/ie.css.gz
Binary file not shown.
Binary file not shown.
Binary file modified public/assets/blueprint/print.css.gz
Binary file not shown.
Binary file not shown.
Binary file modified public/assets/blueprint/screen.css.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion public/assets/manifest.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
logo.png: logo-0591b12a5d6564be255211022e8e918b.png
rails.png: rails-782b548cc1ba7f898cdad2d9eb8420d2.png
application.js: application-874544aa383eeeeaf78a293246fb890e.js
application.js: application-49ed8df66b3e558853c30972f29d94d1.js
application.css: application-5287cc5b85a2d6eb4763b02f3f90fa4c.css
blueprint/ie.css: blueprint/ie-ad5fb044a45a197277638f4cedcc9411.css
blueprint/plugins/buttons/icons/cross.png: blueprint/plugins/buttons/icons/cross-c7cecbf2f882d83d76089292bd4b0ddf.png
Expand Down

0 comments on commit 94f57f4

Please sign in to comment.