Game.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. (function(){
  2. function Game(){
  3. this._loopfns = [];
  4. this.animating = true;
  5. this.animateable = [];
  6. this._ctime = 0;
  7. }
  8. Game.prototype.init = function() {
  9. this.container = document.createElement('div');
  10. document.body.appendChild(this.container);
  11. this.scene = new THREE.Scene();
  12. this.camera = this._createCamera();
  13. this.scene.add(this.camera);
  14. // renderer
  15. this.renderer = new THREE.WebGLRenderer({antialias: true});
  16. this.renderer.setPixelRatio(window.devicePixelRatio);
  17. this.renderer.setSize(window.innerWidth, window.innerHeight);
  18. this.renderer.setClearColor(new THREE.Color( 0x0aa000 ));
  19. this.container.appendChild(this.renderer.domElement);
  20. this.renderer.gammaInput = true;
  21. this.renderer.gammaOutput = true;
  22. this.renderer.shadowMap.enabled = true;
  23. this.renderer.shadowMap.type = THREE.PCFSoftShadowMap; // other THREE. PCFShadowMap
  24. var self = this;
  25. window.addEventListener('resize', function(){onWindowResize(self)}, false);
  26. };
  27. function onWindowResize(self){
  28. self.camera.aspect = window.innerWidth / window.innerHeight;
  29. self.camera.updateProjectionMatrix();
  30. self.renderer.setSize(window.innerWidth, window.innerHeight);
  31. };
  32. Game.prototype._createCamera = function(){
  33. var camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 100000);
  34. camera.position.x = 0;
  35. camera.position.y = 23;
  36. camera.position.z = 25;
  37. return camera;
  38. };
  39. var kk = 0;
  40. var frameRate = [];
  41. Game.prototype.animate = function(time){
  42. this._time = time/1000.0;
  43. var dt;
  44. if(this._frame){
  45. dt = this._time - this._frame;
  46. this._ctime += dt;
  47. this._frame = this._time;
  48. }
  49. else this._frame = this._time;
  50. frameRate[kk] = dt;
  51. kk = (kk+1) % 60;
  52. // Custom timing
  53. this.render(this._ctime);
  54. this._lastReq = requestAnimationFrame(this.animate.bind(this));
  55. };
  56. Game.prototype.showFramerate = function(){
  57. var s = 0;
  58. for(var i =0 ;i < frameRate.length;i++)
  59. s+=frameRate[i];
  60. console.log(s/frameRate.length);
  61. }
  62. Game.prototype.render = function(time) {
  63. if(this.animating)
  64. for(var i in this.animateable)
  65. this.animateable[i].animate(this._time);
  66. for(var i = 0; i < this._loopfns.length; i ++ )
  67. this._loopfns[i].call(this, this._time);
  68. //this.scene.updateMatrixWorld();
  69. this.renderer.render(this.scene, this.camera);
  70. };
  71. Game.prototype.addActor = function(a){
  72. this._animateable.push(a);
  73. };
  74. Game.prototype.addLoopFn = function(fn){
  75. this._loopfns.push(fn);
  76. };
  77. Game.prototype.startAnimations = function(){this.animating = true;this.frame = window.performance.now()/1000.0;};
  78. Game.prototype.stopAnimations = function(){this.animating = false;this.frame = null;};
  79. window.Game = Game;
  80. })();