humancontroller.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. function HumanController() {
  2. this.keyboard = null;
  3. this.player = null;
  4. this.mousePosition = {x: 0, y: 0};
  5. this.init = function(player) {
  6. this.keyboard = new KeyboardInput();
  7. this.keyboard.attach();
  8. this.player = player;
  9. };
  10. this.reset = function() {
  11. };
  12. this.makeDecisions = function(player) {
  13. if(system.keyboard.isDown(Keys.Up) || system.keyboard.isDown(Keys.W)) {
  14. this.player.walkUp();
  15. }
  16. if(system.keyboard.isDown(Keys.Down) || system.keyboard.isDown(Keys.S)) {
  17. this.player.walkDown();
  18. }
  19. if(system.keyboard.isDown(Keys.Left)|| system.keyboard.isDown(Keys.A)) {
  20. this.player.walkLeft();
  21. }
  22. if(system.keyboard.isDown(Keys.Right)|| system.keyboard.isDown(Keys.D)) {
  23. this.player.walkRight();
  24. }
  25. if(system.keyboard.isPressed(Keys.Space)) {
  26. this.player.jump();
  27. }
  28. };
  29. this.mouseMove = function(camera, x, y) {
  30. this.mousePosition.x = x - camera.position.x;
  31. this.mousePosition.y = y - camera.position.y;
  32. this.player.targetAngle = system.angleTo(this.player.position, this.mousePosition);
  33. }
  34. this.mouseDown = function(camera, button, x, y) {
  35. this.mousePosition.x = x - camera.position.x;
  36. this.mousePosition.y = y - camera.position.y;
  37. this.player.targetAngle = system.angleTo(this.player.position, this.mousePosition);
  38. if(button == 2) {
  39. this.player.walkTowards(this.mousePosition);
  40. }
  41. }
  42. this.mouseUp = function(camera, button, x, y) {
  43. };
  44. this.touchStart = function(camera, x, y) {
  45. this.mousePosition.x = x - camera.position.x;
  46. this.mousePosition.y = y - camera.position.y;
  47. this.player.targetAngle = system.angleTo(this.player.position, this.mousePosition);
  48. this.player.walkTowards(this.mousePosition);
  49. };
  50. this.touchMove = function(camera, x, y) {
  51. this.mousePosition.x = x - camera.position.x;
  52. this.mousePosition.y = y - camera.position.y;
  53. this.player.targetAngle = system.angleTo(this.player.position, this.mousePosition);
  54. };
  55. this.touchEnd = function(camera, x, y) {
  56. };
  57. this.contextMenu = function() {
  58. };
  59. this.debugDraw = function(context) {
  60. /*
  61. context.save();
  62. context.translate(this.player.position.x, this.player.position.y);
  63. context.font = "16px sans-serif";
  64. context.textAlign = "center";
  65. context.textBaseline = "middle";
  66. context.fillStyle = "#DDDDDD";
  67. context.strokeStyle = "#333333";
  68. context.lineWidth = 2;
  69. var message = parseInt(this.player.position.x) + ", " + parseInt(this.player.position.y);
  70. var textMetrics = context.measureText(message);
  71. var textWidth = textMetrics.width;
  72. context.fillText(message, 0, 0);
  73. context.restore();
  74. */
  75. /*context.strokeStyle = "crimson";
  76. context.save();
  77. context.translate(this.mousePosition.x, this.mousePosition.y);
  78. context.beginPath();
  79. context.arc(0, 0, 10, 0, 2 * Math.PI);
  80. context.stroke();
  81. context.restore(); */
  82. };
  83. };