import Entity from "../Entity.js"; import Texture from "../Texture.js"; import Tile from "../Tile.js"; import assets from "../assets.js"; import TPlatform from "../traits/TPlatform.js"; import TCollider from "../traits/TCollider.js"; import TPhysics from "../traits/TPhysics.js"; export default class FloatingPlatform extends Entity { constructor(level, width = 5) { super(level); this.bounds.size.set(width, 0.5); this.addTrait(new TCollider(this)); this.addTrait(new TPlatform(this)); this.addTrait(new TPhysics(this)); this.texture = new Texture(assets.tiles, Tile.createLine(this.bounds.size.x, "platform")); this.targetSpeed = 3; this.accel = 5; this.dir = 1; this.distance = 5; } init() { this.targetTop = this.bounds.pos.y; this.targetBottom = this.targetTop + this.distance; 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.texture.drawAt(ctx, this.bounds.pos); } }