examplegame.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. function ExampleGame() {
  2. var game = null;
  3. var system = null;
  4. var box = null;
  5. var helloText = null;
  6. var worldText = null;
  7. var track = false;
  8. this.init = function(systemObject, canvas) {
  9. game = this;
  10. system = systemObject;
  11. track = false;
  12. box = {position: {x: 10, y: 10}, width: 50, height: 50, velocity: {x: 1, y: 1}};
  13. helloText = {text: "Hello", width: 100, height: 100, offset: {x:0, y:0}};
  14. worldText = {text: "World", width: 100, height: 100, offset: {x:0, y:0}};
  15. };
  16. this.updateDelta = function(canvas, delta){
  17. this.update(canvas);
  18. if(!track) {
  19. box.position.x += box.velocity.x * delta;
  20. box.position.y += box.velocity.y * delta;
  21. if(box.position.x < 0) {
  22. box.velocity.x *= -1;
  23. box.position.x = 0;
  24. }
  25. if(box.position.y < 0) {
  26. box.velocity.y *= -1;
  27. box.position.y = 0;
  28. }
  29. if(box.position.x + box.width > canvas.width) {
  30. box.velocity.x *= -1;
  31. box.position.x = canvas.width - box.width;
  32. }
  33. if(box.position.y + box.height > canvas.height) {
  34. box.velocity.y *= -1;
  35. box.position.y = canvas.height - box.height;
  36. }
  37. }
  38. helloText.offset.x = box.position.x + (box.width / 2);
  39. helloText.offset.y = box.position.y + 16;
  40. worldText.offset.x = box.position.x + (box.width / 2);
  41. worldText.offset.y = box.position.y + 32;
  42. };
  43. this.update = function(canvas) {
  44. };
  45. this.draw = function(context) {
  46. context.fillStyle = "#33DD33";
  47. context.strokeStyle = "#33DD33";
  48. context.beginPath();
  49. context.fillRect(box.position.x, box.position.y, box.width, box.height);
  50. context.rect(box.position.x, box.position.y, box.width, box.height);
  51. context.stroke();
  52. context.font = "16px sans-serif";
  53. context.textAlign = "center";
  54. context.fillStyle = "#3333DD";
  55. var helloMetrics = context.measureText("Hello");
  56. var worldMetrics = context.measureText("World");
  57. helloText.width = helloMetrics.width;
  58. worldText.width = worldMetrics.width;
  59. context.fillText(helloText.text, helloText.offset.x, helloText.offset.y);
  60. context.fillText(worldText.text, worldText.offset.x, worldText.offset.y);
  61. };
  62. this.mouseMove = function(canvas, x, y) {
  63. if(track) {
  64. box.position.x = x - box.width / 2;
  65. box.position.y = y - box.height / 2;
  66. }
  67. };
  68. this.mouseClick = function(canvas, button, x, y) {
  69. track = !track;
  70. if(track) {
  71. box.position.x = x - box.width / 2;
  72. box.position.y = y - box.height / 2;
  73. }
  74. };
  75. this.contextMenu = function(canvas, event) {
  76. };
  77. }