character.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. function Character(game) {
  2. this.id = 0;
  3. this.position = {x: 340, y: 320};
  4. this.targetPosition = {x: 300, y: 320};
  5. this.width = 20;
  6. this.height = 20;
  7. this.maxSpeed = 2;
  8. this.targetAngle = 0;
  9. this.angleRadians = 0;
  10. this.color = "cornflowerblue";
  11. this.playerName = "Character";
  12. this.spriteCanvas = null;
  13. this.spriteContext = null;
  14. this.isDead = false;
  15. this.health = 5;
  16. this.maxHealth = 5;
  17. this.seen = [];
  18. this.brain = function(){};
  19. this.init = function(intelligence) {
  20. this.brain = intelligence;
  21. //this.controller.init(this);
  22. this.color = "cornflowerblue";
  23. //this.respawn();
  24. }
  25. this.update = function(delta) {
  26. if(this.isDead) {
  27. return;
  28. }
  29. if(this.health <= 0) {
  30. this.die();
  31. return;
  32. }
  33. if(this.waitHere-- > 0) {
  34. return;
  35. }
  36. this.angleRadians = system.angleTo(this.position, this.targetPosition);
  37. if(this.targetPosition.x != this.position.x) {
  38. if(Math.abs(this.position.x - this.targetPosition.x) < this.maxSpeed) {
  39. this.position.x = this.targetPosition.x;
  40. this.targetAngle = this.angleRadians;
  41. } else {
  42. this.targetAngle = this.angleRadians;
  43. this.position.x += this.maxSpeed * Math.cos(this.angleRadians) * delta;
  44. }
  45. }
  46. if(this.targetPosition.y != this.position.y) {
  47. if(Math.abs(this.position.y - this.targetPosition.y) < this.maxSpeed) {
  48. this.position.y = this.targetPosition.y;
  49. this.targetAngle = this.angleRadians;
  50. } else {
  51. this.targetAngle = this.angleRadians;
  52. this.position.y += (this.maxSpeed) * Math.sin(this.angleRadians) * delta;
  53. }
  54. }
  55. this.brain(this);
  56. }
  57. this.control = function(input) {
  58. this.controller.makeDecisions(this);
  59. }
  60. this.respawn = function() {
  61. this.position.x = 0;
  62. this.position.y = 0;
  63. }
  64. this.die = function() {
  65. this.health = 0;
  66. if(!this.isDead) {
  67. var player = this;
  68. setTimeout(function() {player.respawn();}, 3000);
  69. this.isDead = true;
  70. }
  71. }
  72. this.walkUp = function() {
  73. this.targetPosition.y = this.position.y - this.maxSpeed;
  74. }
  75. this.walkDown = function() {
  76. this.targetPosition.y = this.position.y + this.maxSpeed;
  77. }
  78. this.walkLeft = function() {
  79. this.targetPosition.x = this.position.x - this.maxSpeed;
  80. }
  81. this.walkRight = function() {
  82. this.targetPosition.x = this.position.x + this.maxSpeed;
  83. }
  84. this.walkTowards = function(newPosition) {
  85. this.targetPosition.x = parseInt(newPosition.x);
  86. this.targetPosition.y = parseInt(newPosition.y);
  87. }
  88. this.preRender = function(context, angle) {
  89. if(angle == null) {
  90. angle = 0;
  91. }
  92. context.fillStyle = this.color;
  93. context.strokeStyle = this.color;
  94. context.lineWidth = 4;
  95. context.save();
  96. context.translate(this.width / 2, this.height / 2);
  97. context.rotate(angle);
  98. context.beginPath();
  99. //context.moveTo(this.width / 2, 0);
  100. //context.lineTo(-(this.width / 2), -(this.height / 3));
  101. //context.lineTo(-(this.width / 2), this.height / 3);
  102. //context.lineTo(this.width / 2, 0);
  103. context.rect(-(this.width / 2), -(this.height / 2), this.width, this.height);
  104. context.fill();
  105. context.restore();
  106. }
  107. this.draw = function(context) {
  108. if(this.spriteContext == null) {
  109. this.spriteCanvas = document.createElement("canvas");
  110. this.spriteCanvas.width = this.width;
  111. this.spriteCanvas.height = this.height;
  112. this.spriteContext = this.spriteCanvas.getContext("2d");
  113. this.preRender(this.spriteContext);
  114. }
  115. if(this.isDead) {
  116. return;
  117. }
  118. context.save();
  119. context.translate(this.position.x, this.position.y);
  120. context.rotate(this.targetAngle);
  121. context.drawImage(this.spriteCanvas, 0, 0, this.spriteCanvas.width, this.spriteCanvas.height,
  122. -this.spriteCanvas.width / 2, -this.spriteCanvas.height /2 , this.width, this.height);
  123. context.restore();
  124. }
  125. this.trigger = function(eventId) {
  126. if(this.seen.indexOf(eventId) == -1) {
  127. this.seen.push(eventId);
  128. return true;
  129. }
  130. return false;
  131. }
  132. };