You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BrokenPlatform.js 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import Entity from "../Entity.js";
  2. import Texture from "../Texture.js";
  3. import Tile from "../Tile.js";
  4. import Shaker from "../Shaker.js";
  5. import assets from "../assets.js";
  6. import TPlatform from "../traits/TPlatform.js";
  7. import TCollider from "../traits/TCollider.js";
  8. import TPhysics from "../traits/TPhysics.js";
  9. export default class BrokenPlatform extends Entity {
  10. constructor(level, width = 5) {
  11. super(level, "platform");
  12. this.bounds.size.set(width, 0.5);
  13. this.addTrait(new TCollider(this));
  14. this.addTrait(new TPlatform(this));
  15. this.addTrait(new TPhysics(this));
  16. this.texture = new Texture(assets.tiles,
  17. Tile.createLine(this.bounds.size.x, "broken-platform"));
  18. this.shaker = new Shaker();
  19. this.shake = 0;
  20. this.shakeAccel = 14;
  21. this.shaking = false;
  22. this.floating = true;
  23. this.reliableTime = 1;
  24. this.resetTime = 4;
  25. }
  26. init() {
  27. this.initialPos = this.bounds.pos.clone();
  28. }
  29. reset() {
  30. this.shake = 0;
  31. this.shaking = false;
  32. this.floating = true;
  33. this.t.physics.velocity.set(0, 0);
  34. this.bounds.pos.set(this.initialPos.x, this.initialPos.y);
  35. }
  36. startShake() {
  37. this.shake = 0;
  38. this.shaking = true
  39. setTimeout(() => {
  40. this.floating = false;
  41. this.shaking = false;
  42. setTimeout(() => {
  43. this.reset();
  44. }, this.resetTime * 1000);
  45. }, this.reliableTime * 1000);
  46. }
  47. update(dt) {
  48. if (!this.shaking && !this.falling) {
  49. this.t.collider.entities.forEach(e => {
  50. let fall =
  51. e.has("physics") &&
  52. e.bounds.intersectSide(this.bounds) === "top";
  53. if (fall) {
  54. this.startShake();
  55. }
  56. });
  57. }
  58. if (this.floating)
  59. this.t.physics.velocity.y -= this.t.physics.gravity * dt;
  60. if (this.shaking) {
  61. this.shaker.shake(this.shake);
  62. this.shake += this.shakeAccel * dt;
  63. }
  64. this.shaker.update(dt);
  65. }
  66. draw(ctx) {
  67. this.texture.draw(
  68. ctx,
  69. this.bounds.pos.pixelX + this.shaker.vec.pixelX,
  70. this.bounds.pos.pixelY + this.shaker.vec.pixelY);
  71. }
  72. }