All files / src/utils/pixiUtils DisplayContainer.ts

0% Statements 0/29
0% Branches 0/4
0% Functions 0/11
0% Lines 0/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77                                                                                                                                                         
import {Container} from '@pixi/display';
import {Sprite} from '@pixi/sprite';
import {CollisionBox} from './CollisionBox';
import {World} from './World';
import {ResourceManager} from './ResourceManager';
import {CollisionBoxData} from './metaData/DataInterface';
import {PartsData} from './metaData/DataInterface';
 
export class DisplayContainer extends Container {
  world: World;
  collidable: boolean;
  collisionBox: CollisionBox | null;
  parts: Sprite[];
 
  constructor(world: World) {
    super();
 
    this.world = world;
    this.collidable = false;
    this.collisionBox = null;
    this.parts = [];
  }
 
  update(framesPassed: number): void {
    framesPassed;
    return;
  }
 
  addCollisionBox(data: CollisionBoxData): void {
    const collisionBox = new CollisionBox(data);
    this.addChild(collisionBox);
    this.collisionBox = collisionBox;
    this.collidable = true;
  }
 
  addParts(textureName: string[]): void {
    textureName.forEach(part => {
      const texture = ResourceManager.getTexture(part);
      if (texture === undefined) {
        console.error('Error: Resource parts undefined');
        return;
      }
      this.parts.push(Sprite.from(texture));
    });
  }
 
  addPart(sprite: Sprite): void {
    this.parts.push(sprite);
  }
 
  addPartsToChild(): void {
    this.parts.forEach(part => {
      this.addChild(part);
    });
  }
 
  makeSpriteFromPartsMD(part: PartsData): Sprite | undefined {
    const texture = ResourceManager.getTexture(
      part.textureName,
      part.spriteSheet,
    );
    if (!texture) return;
    const sprite = Sprite.from(texture);
    sprite.position.set(part.position.x, part.position.y);
    sprite.anchor.set(part.anchor.x, part.anchor.y);
    return sprite;
  }
 
  offCollidable(): void {
    this.collidable = false;
  }
 
  onCollidable(): void {
    this.collidable = true;
  }
}