Source: Frontend/game/browser/monster.mjs

  1. /* eslint-disable max-len,curly,complexity,prefer-template */
  2. /* global PIXI */
  3. /** @module Monster */
  4. import MonsterCommon from "../common/monster.mjs";
  5. export default class Monster extends MonsterCommon {
  6. constructor(...args) {
  7. super(...args);
  8. this._lastMove = Date.now();
  9. }
  10. /**
  11. * Generates sprite for a specific monster and adds it to the floor.
  12. */
  13. createSprite() {
  14. this.sprite = new PIXI.Sprite(PIXI.loader.resources.demon.textures[this.name]);
  15. this.sprite.position.set(this.x, this.y);
  16. this.sprite.width = MonsterCommon.SPRITE_SIZE * this.size;
  17. this.sprite.height = MonsterCommon.SPRITE_SIZE * this.size;
  18. this.floor.monsterSprites.addChild(this.sprite);
  19. this.regularTint = this.sprite.tint;
  20. this._textStyle2 = new PIXI.TextStyle({
  21. fill: "#ff0000",
  22. fontSize: 24,
  23. fontFamily: "Tahoma",
  24. fontWeight: "bold"
  25. });
  26. this.hpNotificationSprite = new PIXI.Text("", this._textStyle2);
  27. this.hpNotificationSpriteOffset = (this.sprite.width / 2) - (this.hpNotificationSprite.width / 2);
  28. this.hpNotificationSprite.position.set(this.sprite.position.x + this.hpNotificationSpriteOffset, this.sprite.position.y - 40);
  29. this.floor.monsterSprites.addChild(this.hpNotificationSprite);
  30. }
  31. /**
  32. * Updates the sprite's position for all monsters on the floor.
  33. * @param viewX
  34. * @param viewY
  35. */
  36. update(viewX, viewY) {
  37. let now = Date.now();
  38. let prevx = this.x;
  39. let prevy = this.y;
  40. this.move(this._lastMove - now);
  41. this._lastMove = now;
  42. this.sprite.position.set(this.x - viewX, this.y - viewY);
  43. this.hpNotificationSprite.position.set(this.x - viewX + this.hpNotificationSpriteOffset, this.y - viewY - 25);
  44. if(this.tinted !== -1) {
  45. if(this.sprite.tint === this.regularTint) {
  46. this.tint();
  47. this.hpDamageTaken = Math.abs(this.hpDamageTaken) * -1;
  48. this.hpNotificationSprite.setText(this.hpDamageTaken);
  49. this.hpNotificationSpriteOffset = (this.sprite.width / 2) - (this.hpNotificationSprite.width / 2);
  50. }
  51. if(new Date().getTime() - this.tinted > 200) {
  52. this.tinted = -1;
  53. this.untint();
  54. this.hpNotificationSprite.setText();
  55. }
  56. }
  57. if(this.collisionEntities(this.floor.monsters, this.SPRITE_SIZE) >= -1 || this.collisionEntities(this.floor.players, this.floor.players[0].SPRITE_SIZE) >= -1) {
  58. this.x = prevx;
  59. this.y = prevy;
  60. this.sprite.position.set(this.x - viewX, this.y - viewY);
  61. this.hpNotificationSprite.position.set(this.x - viewX + this.hpNotificationSpriteOffset, this.y - viewY - 25);
  62. }
  63. }
  64. /**
  65. * Handle state updates from the server
  66. */
  67. handleState(state) {
  68. let oldHP = this.hp;
  69. let oldName = this.name;
  70. Object.assign(this, state);
  71. // update the sprite
  72. if(oldName !== this.name) {
  73. this.sprite.texture = PIXI.loader.resources.demon.textures[this.name];
  74. }
  75. if(oldHP !== this.hp) {
  76. this.tinted = new Date().getTime();
  77. this.hpDamageTaken = oldHP - this.hp;
  78. }
  79. }
  80. /**
  81. * Remove a monster from a PIXI.Container
  82. * @param {PIXI.Container} container
  83. */
  84. remove() {
  85. this.floor.monsterSprites.removeChild(this.sprite);
  86. this.floor.monsterSprites.removeChild(this.hpNotificationSprite);
  87. }
  88. /**
  89. * ~WIP drop items down the road
  90. *
  91. * Monster dies.
  92. */
  93. die() {
  94. this.floor.monsterSprites.removeChild(this.sprite);
  95. this.x = -1; // (-1, -1) coordinate tells us that the monster is dead
  96. this.y = -1;
  97. this.alive = false;
  98. }
  99. /**
  100. * Tints player's sprite red.
  101. */
  102. tint() {
  103. this.tinted = new Date().getTime();
  104. this.sprite.tint = 0xFF0000;
  105. }
  106. /**
  107. * Untints player's sprite.
  108. */
  109. untint() {
  110. this.sprite.tint = this.regularTint;
  111. this.tinted = -1;
  112. }
  113. /**
  114. * Get the hp of the monster
  115. */
  116. getHp() {
  117. return this.hp;
  118. }
  119. }