Skip to content
Browse files

Working on unit movement commands

  • Loading branch information...
1 parent 37c96ed commit 157e11f60ad4c32c57ed37094d61624bc4a881bb @mansoor-s mansoor-s committed Mar 30, 2012
Showing with 180 additions and 50 deletions.
  1. +3 −0 data/systemData.js
  2. +1 −1 index.html
  3. +24 −15 js/app/EventLoop.js
  4. +1 −1 js/app/controlers.app.js
  5. +37 −8 js/app/controlers.webgl.js
  6. +79 −19 js/app/stage.starsystem.js
  7. +35 −6 js/app/units.ships.base.js
View
3 data/systemData.js
@@ -94,6 +94,7 @@ systemData = {
ships:[
{
type:"frigate",
+ id: 0,
subtype:"cruiser",
position:{
x:2780,
@@ -109,6 +110,7 @@ systemData = {
{
type:"frigate",
subtype:"cruiser",
+ id: 1,
position:{
x:2820,
y:50,
@@ -123,6 +125,7 @@ systemData = {
{
type:"frigate",
subtype:"cruiser",
+ id: 2,
position:{
x:2800,
y:50,
View
2 index.html
@@ -66,7 +66,7 @@
<script src="./js/lib/postprocessing/MaskPass.js"></script>
<script src="./js/lib/postprocessing/BloomPass.js"></script>
- <script type="text/javascript" src="./data/systemData.js"></script>
+ <script src="./data/systemData.js"></script>
<script src="./js/app/Settings.js"></script>
View
39 js/app/EventLoop.js
@@ -13,27 +13,36 @@
};
EventLoop.prototype._getEventLoop = function() {
+
+ var x = 2780;
+ var y = 50;
+ var z = 50;
+
var self = this;
return function() {
+ x -= 1;
+ y -= 1;
+ z -= 1;
+
var tasks = self.sockets.recv();
self.sockets.flush();
+ tasks.push({
+ id: 0,
+ task: {
+ unitId: 0,
+ order: 0,
+ pos: {
+ x: x,
+ y: y,
+ z: z
+ }
+ }
+
+ });
//route the tasks
-
- for(var i = 0, len = tasks.length; i < len; ++i) {
- var task = tasks[i];
-
- if (task.id === 0 || task.id === 1) {
- self._webglController.handleUnitUpdates(task);
- } else if (task.id === 2) {
- self._webglController.handlePlayerUpdates(task);
- } else if (task.id === 3) {
- self._webglController.handlePrivateChat(task);
- } else if (task.id === 4) {
- self._webglController.handleAllianceChat(task);
- }
- }
+ self._webglController.routeUpdateEvents(tasks);
};
};
@@ -57,7 +66,7 @@ tasks: [
id: 0,
task: {
order: 0
- destinaion: {
+ pos: {
x:
y:
z:
View
2 js/app/controlers.app.js
@@ -3,7 +3,7 @@
App.Controllers.App = function() {
var webglController = new App.Controllers.Webgl($('.gs-viewport'));
- var eventLoop = new App.eventLoop(webglController);
+ var eventLoop = new App.EventLoop(webglController);
eventLoop.start();
View
45 js/app/controlers.webgl.js
@@ -117,22 +117,51 @@
};
- Webgl.prototype.handleUnitUpdates = function() {
-
+
+
+/*
+
+ task ids:
+
+ 0 - unit movement
+ 1 - unit position update
+ 2 - player/infrustrcuture updates
+ 3 - private chat
+ 4 - alliance chat
+
+*/
+ Webgl.prototype.routeUpdateEvents = function(tasks) {
+ for(var i = 0, len = tasks.length; i < len; ++i) {
+ var task = tasks[i];
+
+ if (task.id === 0 || task.id === 1) {
+ this.currentStage.handleUnitUpdates(task.task);
+ } else if (task.id === 2) {
+ this.currentStage.handlePlayerUpdates(task.task);
+ } else if (task.id === 3) {
+ this.currentStage.handlePrivateChat(task.task);
+ } else if (task.id === 4) {
+ this.currentStage.handleAllianceChat(task.task);
+ }
+ }
};
+ Webgl.prototype.handleUnitUpdates = function(data) {
+ this.currentStage.handleUnitUpdates(data);
+ };
- Webgl.prototype.handlePlayerUpdates = function() {
-
+
+ Webgl.prototype.handlePlayerUpdates = function(data) {
+ this.currentStage.handlePlayerUpdates(data);
};
- Webgl.prototype.handlePrivateChat = function() {
-
+ Webgl.prototype.handlePrivateChat = function(data) {
+ this.currentStage.handlePrivateChat(data);
};
- Webgl.prototype.handleAllianceChat = function() {
-
+ Webgl.prototype.handleAllianceChat = function(data) {
+ this.currentStage.handleAllianceChat(data);
};
})();
View
98 js/app/stage.starsystem.js
@@ -1,5 +1,5 @@
(function() {
- "use strict";
+ 'use strict';
var StarSystem = App.Stages.StarSystem = function(webglController) {
this._controller = webglController;
this.scene;
@@ -22,6 +22,7 @@
//ships and planets arrays
this.ships = [];
+
this.fighterPool = new App.Utilities.Pool(App.Units.Ships.Fighter);
this.carrierPool = new App.Utilities.Pool(App.Units.Ships.Carrier);
this.frigatePool = new App.Utilities.Pool(App.Units.Ships.Frigate);
@@ -37,6 +38,7 @@
'mousemove': 'onMouseMove'
};
+
// Initialize camera
this.camera = new THREE.PerspectiveCamera( 45, this._controller.$viewport.width() / this._controller.$viewport.height(), 1, 999999 );
//camera control
@@ -57,17 +59,22 @@
this.loadShips(systemData);
};
+
+
StarSystem.prototype.onDoubleClick = function(event){
var clickedPosition = this._controller.getIntersectionWithYPlane(this.camera,this.mouse,0);
new TWEEN.Tween( this.cameraLookTarget)
.to(clickedPosition, 1500)
.start()
- }
+ };
+
+
StarSystem.prototype._initializeHelpers = function(){
this.planetSelector = new THREE.Mesh( App.Res.geometries.sphere, App.Res.materials.etc.selector );
this.planetSelector.visible = false;
this.scene.add(this.planetSelector);
- }
+ };
+
StarSystem.prototype._initializeLights = function(){
var ambient = new THREE.AmbientLight( 0xffffff );
@@ -79,6 +86,7 @@
this.scene.add(pointLight);
};
+
StarSystem.prototype.loadSystem = function(data) {
@@ -109,6 +117,8 @@
}
}
};
+
+
StarSystem.prototype.loadShips = function(data){
this.fighterPool.freeAll();
this.frigatePool.freeAll();
@@ -119,7 +129,9 @@
ship.load(data.ships[i],this.scene);
this.ships.push(ship);
}
- }
+ };
+
+
StarSystem.prototype.updateCamera = function(){
var distanceVector = new THREE.Vector3(0,0,-this.cameraDistance);
App.Res.misc.rotationMatrix.setRotationX(App.Utill.degreesToRadians(this.cameraRotations.x));
@@ -134,14 +146,17 @@
this.camera.position.addSelf(distanceVector);
this.camera.lookAt(this.cameraLookTarget);
- }
+ };
+
+
StarSystem.prototype.update = function(){
//updating camera position depending on controlls
this.updateCamera();
TWEEN.update();
};
+
StarSystem.prototype.render = function(){
//not used
//postprocessing render
@@ -159,7 +174,8 @@
this.mouse.x = ( event.offsetX / $viewport.width()) * 2 - 1;
this.mouse.y = - ( event.offsetY / $viewport.height()) * 2 + 1;
this.hoverOnOneShip();
- }
+ };
+
StarSystem.prototype.selectPlanet= function(planetObject){
this.planetSelector.position = planetObject.position.clone();
@@ -171,17 +187,23 @@
new TWEEN.Tween( this.cameraLookTarget )
.to(planetObject.position, 1500 )
.start()
- }
+ };
+
+
StarSystem.prototype.deselectPlanet= function(planetObject){
this.planetSelector.visible = false;
- }
+ };
+
+
StarSystem.prototype.selectShip= function(shipObject){
this.selectedShips.push(shipObject);
shipObject.select();
new TWEEN.Tween( this.cameraLookTarget )
.to(shipObject.position, 1500 )
.start()
- }
+ };
+
+
StarSystem.prototype.deselectAll = function(){
if(this.selectedPlanet !== undefined) this.deselectPlanet(this.selectedPlanet);
for(var i = 0;i<this.selectedShips.length;i++){
@@ -190,9 +212,11 @@
this.selectedPlanet = undefined;
this.selectedShips = [];
- }
+ };
+
+
StarSystem.prototype.hoverOnOneShip = function(){
- for(var i = 0;i<this.ships.length;i++){
+ for(var i = 0; i < this.ships.length; i++){
if(this.ships[i].hovered) this.ships[i].unhover();
}
@@ -215,7 +239,42 @@
}
}
}
- }
+ };
+
+
+
+ /*
+ unitOrders: [
+ 0: move specify vector, distance, speed
+ 1: stop
+ 3: rotate specify x, y and z
+ 4: fire specify target location
+
+ ]
+ */
+ StarSystem.prototype.handleUnitUpdates = function(update) {
+ var order = update.order;
+ var unitId = update.unitId;
+
+ if (order === 0) {
+ var ship = this.getUnit(unitId);
+ ship.moveTo(update.pos);
+
+ }
+ };
+
+
+ StarSystem.prototype.getUnit = function(id) {
+ for (var i = 0, len = this.ships.length; i < len; ++i) {
+
+ if (this.ships[i].id == id) {
+ return this.ships[i];
+ }
+ }
+ };
+
+
+
StarSystem.prototype.onMouseClick = function(event){
// find intersections
var vector = new THREE.Vector3( this.mouse.x, this.mouse.y, 1 );
@@ -226,13 +285,13 @@
if ( intersects.length > 0 ) {
- for(var i = 0;i<intersects.length;i++){
+ for(var i = 0; i < intersects.length; i++){
if (intersects[ i ].object.visible === true){
if(intersects[ i ].object.tag instanceof App.Units.Ships.Base){
this.deselectAll();
this.selectShip(intersects[ i ].object.tag);
- }else if(intersects[ i ].object.tag instanceof App.Objects.Planet){
+ } else if(intersects[ i ].object.tag instanceof App.Objects.Planet){
this.deselectAll();
this.selectPlanet(intersects[ i ].object.tag);
}
@@ -252,8 +311,7 @@
.to({
cameraDistance: newDistance
- }, 500 )
- .start();
+ }, 500 ).start();
};
@@ -264,11 +322,11 @@
.to({
cameraDistance: newDistance
- }, 500 )
- .start();
+ }, 500 ).start();
};
+
StarSystem.prototype.onKeyDown =function(e){
var yRot = this.cameraRotations.y, xRot = this.cameraRotations.x;
@@ -315,10 +373,12 @@
.start();
};
+
StarSystem.prototype.onKeyUp = function(e){
};
+
StarSystem.prototype.onMouseWheel = function(event,delta){
event.preventDefault();
@@ -327,9 +387,9 @@
} else{
this.zoomOut();
}
-
};
+
StarSystem.prototype.onEvent = function(event, delta){
for (var type in this.events){
View
41 js/app/units.ships.base.js
@@ -29,6 +29,8 @@
this.owner = '';
this.orders = [];
+
+ this.id = '';
}
//creates ship grid
Base.prototype._createGrid = function(){
@@ -69,11 +71,14 @@
this.grid.connectingLine = new THREE.Line(connectingGeometry,App.Res.materials.etc.gridDefault)
this.grid.connectingLine.visible = false;
- }; //load data when the system loads
- Base.prototype.load = function(data,scene){
+ };
+
+ //load data when the system loads
+ Base.prototype.load = function(data, scene){
this.scene = scene;
//todo load appropriate mesh
+ this.id = data.id;
this.mesh = new THREE.Mesh(App.Res.geometries.ships[data.subtype],App.Res.materials.etc.meshFace);
this.mesh.position.set(data.position.x, data.position.y, data.position.z);
this.mesh.rotation.set(data.rotation.x, data.rotation.y, data.rotation.z);
@@ -95,17 +100,23 @@
scene.add(this.grid.futureCircle);
scene.add(this.grid.futureAnchor);
};
+
+
// updates every frame
Base.prototype.update= function(){
};
+
+
//selects this ship
Base.prototype.select= function(){
this.selected = true;
this.grid.shipCircle.material = App.Res.materials.etc.gridSelected;
this.grid.shipAnchor.material = App.Res.materials.etc.gridSelected;
};
+
+
//deselects ship
Base.prototype.deselect= function(){
this.selected = false;
@@ -117,13 +128,17 @@
this.grid.shipAnchor.material = App.Res.materials.etc.gridDefault;
}
};
+
+
//hover management
Base.prototype.hover = function(){
this.hovered = true;
this.grid.shipCircle.material = App.Res.materials.etc.gridHover;
this.grid.shipAnchor.material = App.Res.materials.etc.gridHover;
};
+
+
//hover management
Base.prototype.unhover = function(){
this.hovered = false;
@@ -135,18 +150,32 @@
this.grid.shipAnchor.material = App.Res.materials.etc.gridDefault;
}
};
+
+
//issue order to move
- Base.prototype.moveTo= function(vector3){
-
+ Base.prototype.moveTo = function(pos){
+ new TWEEN.Tween( this.position )
+ .to({
+ x: pos.x,
+ y: pos.y,
+ z: pos.z
+
+ }, 250 ).start();
};
+
+
//issue order to face a target
- Base.prototype.rotateTo= function(vector3){
+ Base.prototype.rotateTo = function(vector3){
};
+
+
// issue order to rotate
- Base.prototype.rotate= function(vector3){
+ Base.prototype.rotate = function(vector3){
};
+
+
Base.prototype.onReturnToPool = function(){
if (this.scene!==null){
this.scene.remove(this.grid.connectingLine);

0 comments on commit 157e11f

Please sign in to comment.
Something went wrong with that request. Please try again.