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.2KB

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