Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

BrokenPlatform.js 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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);
  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. if (e.has("physics")) {
  51. this.startShake();
  52. }
  53. });
  54. }
  55. if (this.floating)
  56. this.t.physics.velocity.y -= this.t.physics.gravity * dt;
  57. if (this.shaking) {
  58. this.shaker.shake(this.shake);
  59. this.shake += this.shakeAccel * dt;
  60. }
  61. this.shaker.update(dt);
  62. }
  63. draw(ctx) {
  64. this.texture.draw(
  65. ctx,
  66. this.bounds.pos.pixelX + this.shaker.vec.pixelX,
  67. this.bounds.pos.pixelY + this.shaker.vec.pixelY);
  68. }
  69. }