system.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. var 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. 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('mousemove', system.handleMouseMove);
  21. canvas.addEventListener('mousedown', system.handleMouseClick);
  22. canvas.addEventListener('contextmenu', system.handleContextMenu);
  23. canvas.addEventListener('touchstart', system.handleMouseClick);
  24. system.handleResize(null);
  25. game.init(system, canvas);
  26. window.requestAnimationFrame(system.handleRequestAnimationFrame);
  27. };
  28. this.getCanvasSize = function() {
  29. return {width: canvas.width + 0, height: canvas.height + 0};
  30. }
  31. this.preRender = function() {
  32. if(this.backgroundContext == null) {
  33. this.backgroundCanvas = document.createElement("canvas");
  34. this.backgroundCanvas.width = canvas.width;
  35. this.backgroundCanvas.height = canvas.height;
  36. this.backgroundContext = this.backgroundCanvas.getContext("2d");
  37. this.backgroundContext.fillStyle = backgroundColor;
  38. this.backgroundContext.fillRect(0, 0, this.backgroundCanvas.width, this.backgroundCanvas.height);
  39. }
  40. }
  41. this.handleResize = function(event) {
  42. canvas.width = container.offsetWidth;
  43. canvas.height = container.offsetHeight;
  44. context = canvas.getContext(contextType, contextAttributes);
  45. context.translate(0.5, 0.5);
  46. //system.backgroundContext = null;
  47. //system.preRender();
  48. };
  49. this.handleRequestAnimationFrame = function(event) {
  50. var currentTime = new Date().getTime();
  51. var delta = (currentTime - lastTime) / (1000 / framerate);
  52. if(delta > 100) {
  53. delta = 1;
  54. }
  55. game.updateDelta(canvas, delta);
  56. lastTime = currentTime;
  57. //system.preRender();
  58. //context.drawImage(system.backgroundCanvas, 0, 0, system.backgroundCanvas.width, system.backgroundCanvas.height);
  59. context.fillStyle = backgroundColor;
  60. context.fillRect(0, 0, canvas.width, canvas.height);
  61. //console.log("clearing background to: ", backgroundColor, canvas.width, canvas.height);
  62. game.draw(context);
  63. window.requestAnimationFrame(system.handleRequestAnimationFrame);
  64. };
  65. this.handleMouseMove = function(event) {
  66. game.mouseMove(canvas, event.offsetX, event.offsetY);
  67. };
  68. this.handleMouseClick = function(event) {
  69. game.mouseClick(canvas, event.button, event.offsetX, event.offsetY);
  70. };
  71. this.handleContextMenu = function(event) {
  72. game.contextMenu(canvas, event);
  73. event.preventDefault();
  74. };
  75. this.handleServerMessage = function(data) {
  76. game.receiveMessage(data);
  77. }
  78. }