PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/client/scripts/modules/gamefield.module.js

https://bitbucket.org/Fenchurch/isoel
JavaScript | 311 lines | 189 code | 90 blank | 32 comment | 14 complexity | 4e75d80db778364b107729a8af82986b MD5 | raw file
Possible License(s): GPL-2.0, WTFPL, MIT, BSD-3-Clause, Apache-2.0, 0BSD
  1. define([
  2. 'jquery',
  3. 'underscore',
  4. 'backbone',
  5. 'three', // THREE JS
  6. 'util/Stats', // THREE JS STATS HELPER
  7. 'util/PRNG', // Pseudo Random Number Generator
  8. 'util/SimplexNoise', // Simplex Noise
  9. 'util/THREE-window-resize', // helps resizing the canvas on window changes
  10. 'libs/Tween',
  11. 'views/avatar.view',
  12. 'models/avatar.model',
  13. 'views/tinyworld.view',
  14. 'models/tinyworld.model',
  15. 'models/faction.model',
  16. 'models/actions/select.action'
  17. ], function($, _, Backbone, three, stats, PRNG, SimplexNoise,
  18. resize, TWEEN, avatarView, avatarModel, tileView, tinyworldModel, factionModel, selectAction){
  19. var container, stats;
  20. var camera, scene, projector, renderer;
  21. var objects = [], plane;
  22. var MARKER = null;
  23. var mouse = new THREE.Vector2(),
  24. offset = new THREE.Vector3(),
  25. INTERSECTED, SELECTED, SELECTEDn;
  26. var gamefield = Backbone.Collection.extend({
  27. description: "gamefield - holds objects we draw on the canvas",
  28. model: tinyworldModel,
  29. hapticObjects: [], // all pickable objects
  30. activeSelection: null, // the currently active object
  31. ActionPrimary: null, // the currently active action
  32. ActionSecondary: null, // currently active primary action
  33. initialize: function(items, options){
  34. _.extend(this, Backbone.Events);
  35. var self = this;
  36. this.ludare = options.ludare;
  37. this.socket = options.socket;
  38. this.cinematic = options.cinematic;
  39. this.ui = options.ui;
  40. this.controls = options.controls;
  41. this.on("add", function(item) {
  42. // on adding items to this module perform a check for event capability and add them to the scene (field)
  43. // console.log("FUCK ADDING HAPTICS");
  44. if(item.view.hapticNode){
  45. // console.log("HAPTICS", self.hapticObjects.length, item.view.hapticNode);
  46. self.hapticObjects.push(item.view.hapticNode); // objects will be queried for mouse ray intersections later on
  47. }
  48. self.field.add(item.view.node);
  49. });
  50. //assign a primary action, will be triggered via UI
  51. this.ActionPrimary = selectAction;
  52. this.field = new THREE.Object3D();
  53. this.avatar = new avatarModel({world:{x:1, y:1, z:1}});
  54. //this.add(this.avatar);
  55. //assign active Selection, will be affected by actions
  56. this.activeSelection = this.avatar;
  57. container = document.createElement('div');
  58. document.body.appendChild( container );
  59. var prng = new PRNG("sapd3");
  60. this.simplexNoise = new SimplexNoise(prng);
  61. scene = new THREE.Scene();
  62. this.cinematic.target = this.avatar.view.node;
  63. scene.add( this.cinematic.camera );
  64. // add Axis helper
  65. var axis = new THREE.AxisHelper();
  66. //scene.add(axis);
  67. //scene.add( new THREE.AmbientLight( 0x131313 ) );
  68. var light = new THREE.SpotLight( 0xffffff, 1.5 );
  69. light.position.set( 0, 500, 2000 );
  70. light.castShadow = true;
  71. light.shadowCameraNear = 200;
  72. light.shadowCameraFar = this.cinematic.camera.far;
  73. light.shadowCameraFov = 90;
  74. light.shadowBias = -0.00022;
  75. light.shadowDarkness = 0.5;
  76. light.shadowMapWidth = 1024;
  77. light.shadowMapHeight = 1024;
  78. this.light = light;
  79. //scene.add( light );
  80. var field_width = 1;
  81. var padding = 0.2;
  82. var tilesize = 20;
  83. var geometry = new THREE.PlaneGeometry( tilesize, tilesize );
  84. var mergedGeo = new THREE.Geometry();
  85. var j = 0;
  86. var noise_x, noise_y;
  87. // create the particle variables
  88. var particles = new THREE.Geometry(),
  89. pMaterial =
  90. new THREE.ParticleBasicMaterial({
  91. color: 0xFFFFFF,
  92. size: tilesize
  93. });
  94. // create the particle system
  95. var particleSystem =
  96. new THREE.ParticleSystem(
  97. particles,
  98. pMaterial);
  99. // add it to the scene
  100. //field.add(particleSystem);
  101. //field.position.x = field_width *(tilesize +(tilesize *padding)) / -2;
  102. //field.position.z = field_width *(tilesize +(tilesize *padding)) / -2;
  103. scene.add(this.field);
  104. plane = new THREE.Mesh( new THREE.PlaneGeometry( 9000, 9000, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xFF0000, opacity: 0.9, transparent: false, wireframe: true } ) );
  105. //plane.lookAt( this.cinematic.camera.position );
  106. plane.visible = false;
  107. scene.add( plane );
  108. projector = new THREE.Projector();
  109. renderer = new THREE.WebGLRenderer( { antialias: true } );
  110. renderer.sortObjects = false;
  111. renderer.setSize( window.innerWidth, window.innerHeight );
  112. //renderer.shadowMapEnabled = true;
  113. //renderer.shadowMapSoft = true;
  114. container.appendChild( renderer.domElement );
  115. stats = new Stats();
  116. stats.domElement.style.position = 'absolute';
  117. stats.domElement.style.top = '0px';
  118. container.appendChild( stats.domElement );
  119. //closure scope
  120. var self = this;
  121. camera = this.cinematic.camera;
  122. renderer.domElement.addEventListener( 'mousemove', function(event){self.onDocumentMouseMove(event);}, false );
  123. renderer.domElement.addEventListener( 'mousedown', function(event){self.onDocumentMouseDown(event);}, false );
  124. renderer.domElement.addEventListener( 'mouseup', function(event){self.onDocumentMouseUp(event);}, false );
  125. this.scene = scene;
  126. this.animate();
  127. },
  128. // Update Models with new data from sever
  129. //
  130. //
  131. updateModels: function(models){
  132. var self = this;
  133. var clientModel = null;
  134. _.each(models, function(item){
  135. // console.log("MAP::UPDATE::MODEL::", item);
  136. clientModel = self.get(item.id);
  137. if(clientModel){
  138. clientModel.set(item);
  139. clientModel.trigger("change");
  140. }
  141. });
  142. },
  143. //
  144. animate: function(){
  145. var self = this;
  146. requestAnimationFrame( function(){ self.animate();});
  147. this.render();
  148. this.controls.controls.update();
  149. this.cinematic.update();
  150. this.ludare.update();
  151. stats.update();
  152. },
  153. render: function(){
  154. var timer = new Date().getTime() * 0.0001;
  155. //this.light.position = (this.cinematic.camera.position);
  156. //update all tweens
  157. TWEEN.update();
  158. renderer.render( scene, this.cinematic.camera );
  159. },
  160. onDocumentMouseMove: function(event){
  161. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  162. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  163. event.preventDefault();
  164. var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
  165. projector.unprojectVector( vector, camera );
  166. var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
  167. var intersects = ray.intersectObjects( this.hapticObjects );
  168. //console.log("EVENT::Move", intersects.length);
  169. if ( intersects.length != 0 ) {
  170. SELECTED = intersects[ 0 ].object;
  171. if(SELECTEDn && (SELECTED != SELECTEDn)){
  172. // console.log("MOVER", SELECTED.view);
  173. SELECTED.view.trigger("mouseover");
  174. container.style.cursor = 'pointer';
  175. SELECTEDn.view.trigger("mouseout");
  176. } else {
  177. SELECTED.view.trigger("mouseover");
  178. container.style.cursor = 'pointer';
  179. }
  180. } else {
  181. if(SELECTEDn){
  182. //console.log("SELECTEDn", SELECTEDn);
  183. SELECTEDn.view.trigger("mouseout");
  184. }
  185. SELECTED = false;
  186. container.style.cursor = 'auto';
  187. }
  188. SELECTEDn = SELECTED;
  189. return false; // kill dragging for now
  190. },
  191. onDocumentMouseDown: function(event){
  192. event.preventDefault();
  193. var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
  194. projector.unprojectVector( vector, camera );
  195. var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
  196. var intersects = ray.intersectObjects( this.hapticObjects );
  197. if ( intersects.length > 0 ) {
  198. // console.log("CL");
  199. SELECTED.view.trigger("click");// not really a click but a mouseDown
  200. }
  201. },
  202. onDocumentMouseUp: function(event){
  203. event.preventDefault();
  204. var vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 );
  205. projector.unprojectVector( vector, camera );
  206. var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
  207. var intersects = ray.intersectObjects( this.hapticObjects );
  208. if ( intersects.length > 0 ) {
  209. //console.log("mouseup");
  210. SELECTED.view.trigger("mouseUp");
  211. }
  212. if(this.ActionSecondary){
  213. this.ActionSecondary.trigger("click", {target: SELECTED, selection: this.activeSelection});
  214. }
  215. if(this.ActionPrimary){
  216. this.ActionPrimary.trigger("click", {target: SELECTED, selection: this.activeSelection});
  217. }
  218. }
  219. });
  220. return gamefield;
  221. });