system.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. function System() {
  2. var container = null;
  3. var canvas = null;
  4. var contextType = '2d';
  5. var contextAttributes = {};
  6. var context = null;
  7. var system = null;
  8. this.game = null;
  9. var backgroundColor = "#D3D3D3";
  10. var framerate = 60;
  11. var lastTime = 0;
  12. this.create = function(elementName, gameObject, defaultBackgroundColor) {
  13. system = this;
  14. system.game = gameObject;
  15. backgroundColor = defaultBackgroundColor || backgroundColor;
  16. container = document.getElementById(elementName);
  17. canvas = document.createElement("canvas");
  18. container.appendChild(canvas);
  19. window.addEventListener('resize', system.handleResize);
  20. canvas.addEventListener('touchmove', system.handleTouchMove);
  21. canvas.addEventListener('mousemove', system.handleMouseMove);
  22. canvas.addEventListener('mousedown', system.handleMouseDown);
  23. canvas.addEventListener('contextmenu', system.handleContextMenu);
  24. canvas.addEventListener('touchstart', system.handleTouchStart);
  25. canvas.addEventListener('mouseup', system.handleMouseUp);
  26. canvas.addEventListener('touchend', system.handleTouchEnd);
  27. this.game.init(system, canvas);
  28. system.handleResize(null);
  29. window.requestAnimationFrame(system.handleRequestAnimationFrame);
  30. };
  31. this.getCanvasSize = function() {
  32. return {width: canvas.width + 0, height: canvas.height + 0};
  33. }
  34. this.preRender = function() {
  35. if(this.backgroundContext == null) {
  36. this.backgroundCanvas = document.createElement("canvas");
  37. this.backgroundCanvas.width = canvas.width;
  38. this.backgroundCanvas.height = canvas.height;
  39. this.backgroundContext = this.backgroundCanvas.getContext("2d");
  40. this.backgroundContext.fillStyle = backgroundColor;
  41. this.backgroundContext.fillRect(0, 0, this.backgroundCanvas.width, this.backgroundCanvas.height);
  42. }
  43. }
  44. this.handleResize = function(event) {
  45. canvas.width = container.offsetWidth;
  46. canvas.height = container.offsetHeight;
  47. context = canvas.getContext(contextType, contextAttributes);
  48. context.translate(0.5, 0.5);
  49. system.game.handleResize(canvas);
  50. };
  51. this.handleRequestAnimationFrame = function(event) {
  52. var currentTime = new Date().getTime();
  53. var delta = (currentTime - lastTime) / (1000 / framerate);
  54. if(delta > 100) {
  55. delta = (1000 / framerate);
  56. }
  57. system.game.updateDelta(canvas, delta);
  58. lastTime = currentTime;
  59. context.fillStyle = backgroundColor;
  60. context.fillRect(0, 0, canvas.width, canvas.height);
  61. system.game.draw(context);
  62. window.requestAnimationFrame(system.handleRequestAnimationFrame);
  63. };
  64. this.handleMouseMove = function(event) {
  65. system.game.mouseMove(canvas, event.offsetX, event.offsetY);
  66. };
  67. this.handleMouseDown = function(event) {
  68. system.game.mouseDown(canvas, event.button, event.offsetX, event.offsetY);
  69. };
  70. this.handleMouseUp = function(event) {
  71. system.game.mouseUp(canvas, event.button, event.offsetX, event.offsetY);
  72. };
  73. this.handleTouchStart = function(event) {
  74. system.game.touchStart(canvas, event.touches[0].clientX, event.touches[0].clientY);
  75. }
  76. this.handleTouchMove = function(event) {
  77. system.game.touchMove(canvas, event.touches[0].clientX, event.touches[0].clientY);
  78. }
  79. this.handleTouchEnd = function(event) {
  80. system.game.touchEnd(canvas, event.offsetX, event.offsetY);
  81. }
  82. this.handleContextMenu = function(event) {
  83. system.game.contextMenu(canvas, event);
  84. event.preventDefault();
  85. return false;
  86. };
  87. this.handleServerMessage = function(data) {
  88. system.game.receiveMessage(data);
  89. }
  90. this.formatFloat = function(val) {
  91. //console.log(val, Number.parseFloat(val), val.toFixed(4), Number.parseFloat(val).toFixed(4), parseFloat(val.toFixed(4)));
  92. if (Number.isNaN(parseFloat(val))) {
  93. return 0;
  94. }
  95. //return val;
  96. return parseFloat(val.toFixed(6));
  97. };
  98. };