import Entity from "../Entity.js"; import {Trait} from "../Entity.js"; import Texture from "../Texture.js"; import Tile from "../Tile.js"; import Shaker from "../Shaker.js"; import Vec2 from "../Vec2.js"; import assets from "../assets.js"; import TPlatform from "../traits/TPlatform.js"; import TCollider from "../traits/TCollider.js"; import TPhysics from "../traits/TPhysics.js"; class Behavior extends Trait { constructor(entity) { super(entity, "behavior"); this.shaker = new Shaker(); this.shake = 0; this.shakeAccel = 14; this.reliableTime = 1; this.resetTime = 4; this.state = 0; this.timer = 0; } init() { this.initialPos = this.entity.pos.clone(); this.entity.t.physics.enabled = false; } reset() { this.entity.t.physics.enabled = false; this.shake = 0; this.entity.t.physics.velocity.set(0, 0); this.entity.pos.set(this.initialPos.x, this.initialPos.y); this.state = 0; this.timer = 0; } update(dt) { if (this.state === 0) { this.entity.t.collider.entities.forEach(e => { let fall = e.has("physics") && e.bounds.intersectSide(this.entity.bounds) === "top"; if (fall) { this.timer = this.reliableTime; this.state = 1; } }); } if (this.state === 1) { this.shaker.shake(this.shake); this.shake += this.shakeAccel * dt; this.timer -= dt; if (this.timer <= 0) { this.entity.t.physics.enabled = true; this.state = 2; this.timer = this.resetTime; } } else if (this.state === 2) { this.timer -= dt; if (this.timer <= 0) { this.reset(); } } this.shaker.update(dt); } } export default class BrokenPlatform extends Entity { constructor(level, width = 5) { super(level, "platform"); this.bounds.size.set(width, 0.5); this.addTrait(new TCollider(this)); this.addTrait(new TPlatform(this)); this.addTrait(new TPhysics(this)); this.addTrait(new Behavior(this)); this.texture = new Texture(assets.tiles, Tile.createLine( Vec2.zero, this.bounds.size.x, "broken-platform")); } draw(ctx) { let shaker = this.t.behavior.shaker; this.texture.draw( ctx, this.pos.pixelX + shaker.vec.pixelX, this.pos.pixelY + shaker.vec.pixelY); } }