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.

FloatingPlatform.js 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import Entity from "../Entity.js";
  2. import Texture from "../Texture.js";
  3. import Tile from "../Tile.js";
  4. import assets from "../assets.js";
  5. import TPlatform from "../traits/TPlatform.js";
  6. import TCollider from "../traits/TCollider.js";
  7. import TPhysics from "../traits/TPhysics.js";
  8. export default class FloatingPlatform extends Entity {
  9. constructor(level, width = 5) {
  10. super(level);
  11. this.bounds.size.set(width, 0.5);
  12. this.addTrait(new TCollider(this));
  13. this.addTrait(new TPlatform(this));
  14. this.addTrait(new TPhysics(this));
  15. this.texture = new Texture(assets.tiles,
  16. Tile.createLine(this.bounds.size.x, "platform"));
  17. this.targetSpeed = 3;
  18. this.accel = 5;
  19. this.dir = 1;
  20. this.distance = 5;
  21. }
  22. init() {
  23. this.targetTop = this.bounds.pos.y;
  24. this.targetBottom = this.targetTop + this.distance;
  25. this.t.physics.gravity = 0;
  26. this.t.physics.velocity.y = 0;
  27. }
  28. update(dt) {
  29. let phys = this.t.physics;
  30. if (this.bounds.pos.y <= this.targetTop)
  31. this.dir = 1;
  32. else if (this.bounds.pos.y >= this.targetBottom)
  33. this.dir = -1;
  34. if (this.dir == 1) {
  35. if (phys.velocity.y < this.targetSpeed)
  36. phys.velocity.y += this.accel * dt;
  37. } else {
  38. if (phys.velocity.y > -this.targetSpeed)
  39. phys.velocity.y -= this.accel * dt;
  40. }
  41. }
  42. draw(ctx) {
  43. this.texture.drawAt(ctx, this.bounds.pos);
  44. }
  45. }