12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- import Entity from "../Entity.js";
-
- import TPlatform from "../traits/TPlatform.js";
- import TCollider from "../traits/TCollider.js";
- import TPhysics from "../traits/TPhysics.js";
-
- export default class Platform extends Entity {
- constructor(level) {
- super(level);
-
- this.bounds.size.set(5, 1);
- this.addTrait(new TCollider(this));
- this.addTrait(new TPlatform(this));
- this.addTrait(new TPhysics(this));
-
- this.targetSpeed = 3;
- this.accel = 5;
- this.dir = 1;
- }
-
- init() {
- this.targetTop = this.bounds.pos.y;
- this.targetBottom = this.targetTop + 5;
-
- this.t.physics.gravity = 0;
- this.t.physics.velocity.y = 0;
- }
-
- update(dt) {
- let phys = this.t.physics;
-
- if (this.bounds.pos.y <= this.targetTop)
- this.dir = 1;
- else if (this.bounds.pos.y >= this.targetBottom)
- this.dir = -1;
-
- if (this.dir == 1) {
- if (phys.velocity.y < this.targetSpeed)
- phys.velocity.y += this.accel * dt;
- } else {
- if (phys.velocity.y > -this.targetSpeed)
- phys.velocity.y -= this.accel * dt;
- }
- }
-
- draw(ctx) {
- this.bounds.draw(ctx);
- }
- }
|