system.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. include("keyboardinput.js");
  2. include("assetloader.js");
  3. function System() {
  4. var container = null;
  5. var canvas = null;
  6. var contextType = '2d';
  7. var contextAttributes = {};
  8. var context = null;
  9. var system = null;
  10. this.theater = null;
  11. this.assets = null;
  12. var backgroundColor = "#D3D3D3";
  13. var framerate = 60;
  14. var lastTime = 0;
  15. this.keyboard = null;
  16. this.create = function(elementName, gameObject, defaultBackgroundColor) {
  17. system = this;
  18. system.theater = gameObject;
  19. backgroundColor = defaultBackgroundColor || backgroundColor;
  20. this.keyboard = new KeyboardInput();
  21. this.keyboard.attach();
  22. container = document.getElementById(elementName);
  23. canvas = document.createElement("canvas");
  24. container.appendChild(canvas);
  25. window.addEventListener('resize', system.handleResize);
  26. this.assets = new AssetLoader();
  27. system.theater.loadAssets(function() {
  28. system.theater.init(system, canvas);
  29. system.handleResize(null);
  30. window.requestAnimationFrame(system.handleRequestAnimationFrame);
  31. });
  32. };
  33. this.handleResize = function(event) {
  34. canvas.width = container.offsetWidth;
  35. canvas.height = container.offsetHeight;
  36. context = canvas.getContext(contextType, contextAttributes);
  37. context.translate(0.5, 0.5);
  38. system.theater.handleResize(canvas);
  39. };
  40. this.handleRequestAnimationFrame = function(event) {
  41. var currentTime = new Date().getTime();
  42. var delta = (currentTime - lastTime) / (1000 / framerate);
  43. if(delta > 100) {
  44. delta = (1000 / framerate);
  45. }
  46. system.theater.updateDelta(delta, canvas);
  47. lastTime = currentTime;
  48. context.fillStyle = backgroundColor;
  49. context.fillRect(0, 0, canvas.width, canvas.height);
  50. system.theater.draw(context);
  51. window.requestAnimationFrame(system.handleRequestAnimationFrame);
  52. };
  53. this.formatFloat = function(val) {
  54. if (Number.isNaN(parseFloat(val))) {
  55. return 0;
  56. }
  57. return parseFloat(val.toFixed(6));
  58. };
  59. this.distanceTo = function(source, target) {
  60. return this.formatFloat(Math.sqrt(Math.pow(target.x - source.x, 2) + Math.pow(target.y - source.y, 2)));
  61. }
  62. this.angleTo = function(source, target) {
  63. return this.formatFloat(Math.atan2(target.y - source.y , target.x - source.x));
  64. }
  65. this.ellipse = function(ctx, cx, cy, w, h){
  66. ctx.beginPath();
  67. var lx = cx - w/2,
  68. rx = cx + w/2,
  69. ty = cy - h/2,
  70. by = cy + h/2;
  71. var magic = 0.551784;
  72. var xmagic = magic*w/2;
  73. var ymagic = h*magic/2;
  74. ctx.moveTo(cx,ty);
  75. ctx.bezierCurveTo(cx+xmagic,ty,rx,cy-ymagic,rx,cy);
  76. ctx.bezierCurveTo(rx,cy+ymagic,cx+xmagic,by,cx,by);
  77. ctx.bezierCurveTo(cx-xmagic,by,lx,cy+ymagic,lx,cy);
  78. ctx.bezierCurveTo(lx,cy-ymagic,cx-xmagic,ty,cx,ty);
  79. }
  80. this.objectMerge = function(base, delta) {
  81. for(var index in delta) {
  82. var mergeData = delta[index];
  83. if(base[index] instanceof Object) {
  84. base[index] = system.objectMerge(base[index], mergeData);
  85. } else {
  86. base[index] = mergeData;
  87. }
  88. }
  89. return base;
  90. }
  91. };