import Entity from "../Entity.js"; import Texture from "../Texture.js"; import Tile from "../Tile.js"; import Shaker from "../Shaker.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 BrokenPlatform 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, "broken-platform")); this.shaker = new Shaker(); this.shake = 0; this.shakeAccel = 14; this.shaking = false; this.floating = true; this.reliableTime = 1; this.resetTime = 4; } init() { this.initialPos = this.bounds.pos.clone(); } reset() { this.shake = 0; this.shaking = false; this.floating = true; this.t.physics.velocity.set(0, 0); this.bounds.pos.set(this.initialPos.x, this.initialPos.y); } startShake() { this.shake = 0; this.shaking = true setTimeout(() => { this.floating = false; this.shaking = false; setTimeout(() => { this.reset(); }, this.resetTime * 1000); }, this.reliableTime * 1000); } update(dt) { if (!this.shaking && !this.falling) { this.t.collider.entities.forEach(e => { if (e.has("physics")) { this.startShake(); } }); } if (this.floating) this.t.physics.velocity.y -= this.t.physics.gravity * dt; if (this.shaking) { this.shaker.shake(this.shake); this.shake += this.shakeAccel * dt; } this.shaker.update(dt); } draw(ctx) { this.texture.draw( ctx, this.bounds.pos.pixelX + this.shaker.vec.pixelX, this.bounds.pos.pixelY + this.shaker.vec.pixelY); } }