examplegame.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. function ExampleGame(connection) {
  2. var game = null;
  3. var system = null;
  4. var player = null;
  5. var otherPlayers = [];
  6. var keyboard = null;
  7. var camera = {x: 0, y: 0, speed: 5};
  8. this.init = function(systemObject, canvas) {
  9. game = this;
  10. system = systemObject;
  11. keyboard = new KeyboardInput();
  12. keyboard.attach();
  13. camera = {x: 0, y: 0, speed: 5};
  14. var defaultData = {
  15. position: {x: 60, y: 60},
  16. velocity: {x: 0, y: 0},
  17. moveToTarget: false,
  18. targetDestination: {x: 60, y: 60},
  19. targetAngle: 0,
  20. shipColor: "#DD0000"
  21. };
  22. player = new Player(camera, defaultData);
  23. };
  24. this.sendMessage = function(data) {
  25. connection.send(JSON.stringify(data, null, "\t"));
  26. }
  27. this.receiveMessage = function(data) {
  28. if(data.hasOwnProperty("player")) {
  29. player.sync(data.timestamp, data.player);
  30. }
  31. if(data.hasOwnProperty("other")) {
  32. if(otherPlayers[data.other.id] == null) {
  33. otherPlayers[data.other.id] = new Player(camera, data.other);
  34. } else {
  35. otherPlayers[data.other.id].sync(data.timestamp, data.other);
  36. }
  37. }
  38. if(data.hasOwnProperty("remove")) {
  39. delete otherPlayers[data.remove];
  40. }
  41. }
  42. this.updateDelta = function(canvas, delta){
  43. this.update(canvas);
  44. player.update(delta);
  45. for(var index in otherPlayers) {
  46. var other = otherPlayers[index];
  47. other.update(delta);
  48. }
  49. };
  50. this.update = function(canvas) {
  51. var time = new Date().getTime();
  52. if(keyboard.isDown(Keys.Up) || keyboard.isDown(Keys.W)) {
  53. camera.y += camera.speed;
  54. }
  55. if(keyboard.isDown(Keys.Down) || keyboard.isDown(Keys.S)) {
  56. camera.y -= camera.speed;
  57. }
  58. if(keyboard.isDown(Keys.Left)|| keyboard.isDown(Keys.A)) {
  59. camera.x += camera.speed;
  60. }
  61. if(keyboard.isDown(Keys.Right)|| keyboard.isDown(Keys.D)) {
  62. camera.x -= camera.speed;
  63. }
  64. if(keyboard.isPressed(Keys.Space)) {
  65. camera.x = -player.position.x + (canvas.width / 2);
  66. camera.y = -player.position.y + (canvas.height / 2);
  67. }
  68. };
  69. this.draw = function(context) {
  70. for(var index in otherPlayers) {
  71. var other = otherPlayers[index];
  72. other.draw(context);
  73. }
  74. player.draw(context);
  75. };
  76. this.mouseMove = function(canvas, x, y) {
  77. //calculate angle between player and cursor
  78. if(!player.moveToTarget) {
  79. var angleRadians = Math.atan2(y - (player.position.y + camera.y), x - (player.position.x + camera.x));
  80. player.targetAngle = angleRadians;
  81. }
  82. };
  83. this.mouseClick = function(canvas, button, x, y) {
  84. var time = new Date().getTime();
  85. switch(button) {
  86. case 0:
  87. var origin = {x: player.position.x, y: player.position.y};
  88. var target = {x: x - camera.x, y: y - camera.y};
  89. this.sendMessage({"action":"target", "start": time, "origin": origin, "position": target});
  90. player.moveToTarget = true;
  91. player.targetDestination = target;
  92. break;
  93. case 1:
  94. break;
  95. case 2:
  96. break;
  97. }
  98. };
  99. this.contextMenu = function(canvas, event) {
  100. };
  101. this.disconnect = function() {
  102. delete player;
  103. }
  104. }