|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- 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, "platform");
-
- 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 => {
- let fall =
- e.has("physics") &&
- e.bounds.intersectSide(this.bounds) === "top";
-
- if (fall) {
- 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);
- }
- }
|