HumanController.js 2.6 KB

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