Skip to content

Commit

Permalink
refine gameplay.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarnhart committed Jan 29, 2012
1 parent 94f57f4 commit bde07b0
Show file tree
Hide file tree
Showing 64 changed files with 75 additions and 294 deletions.
45 changes: 25 additions & 20 deletions app/assets/javascripts/FactoryUnit.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
//= require Unit

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

this.building = null;

this.joins = [];

this.addNeighbour = function(neighbour, scene) {
if(neighbour.unit instanceof 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);
}
};

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

this.addToScene = function(scene) {
var geometry = new THREE.CubeGeometry(50, 50, 50, 1, 1, 1);
material = new THREE.MeshLambertMaterial( { color: 0x999999 } );
Expand All @@ -31,26 +51,11 @@ GLOBE.FactoryUnit = function() {
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);
}
this.place = fuction(node, state, scene) {
//if(node.)
};
};

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

GLOBE.FactoryUnit.prototype = new GLOBE.Unit();
GLOBE.FactoryUnit.prototype.constructor = GLOBE.FactoryUnit;
GLOBE.FactoryUnit.prototype.constructor = GLOBE.FactoryUnit;
GLOBE.FactoryUnit.prototype.supr = GLOBE.Unit.prototype;
14 changes: 5 additions & 9 deletions app/assets/javascripts/Gameplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,16 @@ GLOBE.Gameplay = function(network, scene) {

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.constructor = GLOBE.Gameplay;

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

for(i=0; i<this.network.length; i++) {
Expand Down Expand Up @@ -48,10 +44,10 @@ GLOBE.Gameplay.prototype.handlePlacement = function(placement) {
}
};

GLOBE.Gameplay.prototype.update = function() {
GLOBE.Gameplay.prototype.update = function(timestep) {
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);
node.unit.update(timestep, this.state, this.scene);
}
}
26 changes: 19 additions & 7 deletions app/assets/javascripts/Unit.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//= require Globe

GLOBE.Unit = function(name, cost, production, pollution) {
this.name = name;
this.cost = cost;
Expand All @@ -14,7 +16,7 @@ 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;
if(state.manufactured_goods < this.cost) return false;

this.node=node;
node.unit=this;
Expand All @@ -27,16 +29,26 @@ GLOBE.Unit.prototype.place = function(node, state, scene) {

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.getProduction = function(state) {
return this.production;
};

GLOBE.Unit.prototype.getPollution = function(state) {
return this.production;
};

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

GLOBE.Unit.prototype.clone = function() {};
GLOBE.Unit.prototype.clone = function() {
return new GLOBE.Unit(this.name, this.cost, this.production, this.pollution);
};
3 changes: 2 additions & 1 deletion app/assets/javascripts/static_pages.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

//= require Three
//= require Three
//= require Gameplay
40 changes: 24 additions & 16 deletions app/views/static_pages/home.html.erb
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
<% provide(:title, 'Home') %>
<div>
<div style="text-align:right">Manufactured Goods: <span id="manufactured-goods">0</span> |||||| Pollution: <span id="pollution">0</span></div>
<div id="status-bar" style="text-align:right; display:none">Manufactured Goods: <span id="manufactured-goods", style="padding-right:40px">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>
<script>
var camera, scene, renderer,
geometry, material, globe;
var camera, scene, renderer, geometry, material, globe;
var trackball, projector;
var windowWidth=670, windowHeight=400;
var network, gameplay;
var viewer = $('#view-element').get(0);
var lastTime;

$('#start-button').click(function() {
init();
$('#view-element').append(renderer.domElement);
$('#start-button').fadeOut('fast');
$('#view-element').show('slow');
animate();
$('#status-bar').fadeIn('slow');
lastTime = +new Date;
queueFrame();
});

function closestVertex(face, point, vertices) {
Expand Down Expand Up @@ -70,6 +72,7 @@
camera.add(pointLight);

var ambientLight = new THREE.AmbientLight(0x777777);
ambientLight
scene.add(ambientLight);

projector = new THREE.Projector();
Expand Down Expand Up @@ -108,22 +111,27 @@
}, false);
}

function animate() {
// note: three.js includes requestAnimationFrame shim
requestAnimationFrame( animate );
trackball.update();
gameplay.update();
function queueFrame() {
// note: three.js includes requestAnimationFrame shim
requestAnimationFrame( queueFrame );
var currentTime = +new Date;
var timestep = (currentTime - lastTime)/1000.0;
if(timestep > 0.160) return;
update(timestep);
render(timestep);
lastTime=currentTime;
}

$('#manufactured-goods').text(gameplay.state.manufactured_goods);
$('#pollution').text(gameplay.state.pollution);
function update(timestep) {

trackball.update();
gameplay.update(timestep);

render();
$('#manufactured-goods').text(Math.floor(gameplay.state.manufactured_goods));
$('#pollution').text(Math.ceil(gameplay.state.pollution));
}

function render() {
//mesh.rotation.x += 0.01;
//mesh.rotation.y += 0.02;

function render(timestep) {
renderer.render( scene, camera );
}
</script>
30 changes: 0 additions & 30 deletions public/assets/application-49ed8df66b3e558853c30972f29d94d1.js

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file not shown.
1 change: 0 additions & 1 deletion public/assets/application.css

This file was deleted.

Binary file removed public/assets/application.css.gz
Binary file not shown.
30 changes: 0 additions & 30 deletions public/assets/application.js

This file was deleted.

Binary file removed public/assets/application.js.gz
Binary file not shown.

This file was deleted.

Binary file not shown.
1 change: 0 additions & 1 deletion public/assets/blueprint/ie.css

This file was deleted.

Binary file removed public/assets/blueprint/ie.css.gz
Binary file not shown.
Binary file not shown.
Binary file removed public/assets/blueprint/plugins/buttons/icons/cross.png
Binary file not shown.
Binary file not shown.
Binary file removed public/assets/blueprint/plugins/buttons/icons/key.png
Binary file not shown.
Binary file not shown.
Binary file removed public/assets/blueprint/plugins/buttons/icons/tick.png
Binary file not shown.

This file was deleted.

32 changes: 0 additions & 32 deletions public/assets/blueprint/plugins/buttons/readme.txt

This file was deleted.

This file was deleted.

14 changes: 0 additions & 14 deletions public/assets/blueprint/plugins/fancy-type/readme.txt

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed public/assets/blueprint/plugins/link-icons/icons/im.png
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

This file was deleted.

18 changes: 0 additions & 18 deletions public/assets/blueprint/plugins/link-icons/readme.txt

This file was deleted.

This file was deleted.

Loading

0 comments on commit bde07b0

Please sign in to comment.