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.

Platform.js 1.0KB

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