All files / src/utils/pixiUtils Stuff.ts

0% Statements 0/53
0% Branches 0/32
0% Functions 0/11
0% Lines 0/46

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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113                                                                                                                                                                                                                                 
import {Sprite} from '@pixi/sprite';
import {checkIntersect} from './CheckIntersect';
import {DisplayContainer} from './DisplayContainer';
import {ResourceManager} from './ResourceManager';
import {World} from './World';
import {StuffData, YoutubeStuffData} from './metaData/DataInterface';
import {CollisionBox} from './CollisionBox';
import {DisplayObject} from 'pixi.js';
import YoutubeEmbedRenderer from '../../components/YoutubeEmbed';
 
export class Stuff extends DisplayContainer {
  protected alphaChangable = true;
 
  constructor(world: World, data: StuffData) {
    super(world);
 
    if (data.position) this.position.set(data.position.x, data.position.y);
    data.parts.forEach(part => {
      const texture = ResourceManager.getTexture(
        part.textureName,
        part.spriteSheet,
      );
      if (!texture) {
        console.log('No texture:', texture);
        console.log(part.textureName);
        console.log(part.spriteSheet);
        return;
      }
      this.alphaChangable = data.alphaChangable;
      const sprite = Sprite.from(texture);
      sprite.position.set(part.position.x, part.position.y);
      sprite.anchor.set(part.anchor.x, part.anchor.y);
      this.addChild(sprite);
 
      if (data.collisionBox) this.addCollisionBox(data.collisionBox);
    });
    this.zIndex = this.y;
  }
 
  update(): void {
    if (this.alphaChangable) this.changeAlpha();
  }
 
  protected changeAlpha(): void {
    if (this.world.player === null) return;
    if (this.world.player.collisionBox === null) return;
    if (checkIntersect(this.world.player.collisionBox, this)) {
      this.alpha = 0.5;
    } else this.alpha = 1;
  }
}
 
interface IInteractStuff {
  interact: (target: DisplayObject) => void; //
}
 
export class YoutubeStuff extends Stuff implements IInteractStuff {
  private interactBox: CollisionBox;
  private youtubeContainer: HTMLDivElement | null;
  private youtubeVideoID: string;
  private youtubeContainerTopPosition: number;
  private youtubeContainerLeftPosition: number;
  constructor(world: World, data: YoutubeStuffData) {
    super(world, data);
    this.interactBox = new CollisionBox(data.interactBox);
    this.addChild(this.interactBox);
    this.youtubeVideoID = data.youtubeVideoID;
    this.youtubeContainerLeftPosition = data.youtubeContainerLeftPosition;
    this.youtubeContainerTopPosition = data.youtubeContainerTopPosition;
    this.youtubeContainer = null;
  }
 
  interact(target: DisplayObject): void {
    if (this.isReadyToInteract(target)) this.interactStart();
    else this.interactEnd();
  }
 
  interactStart(): void {
    if (!this.youtubeContainer) {
      this.youtubeContainer = YoutubeEmbedRenderer.render({
        left: this.youtubeContainerLeftPosition,
        top: this.youtubeContainerTopPosition,
        videoID: this.youtubeVideoID,
      });
    }
  }
 
  interactEnd(): void {
    if (this.youtubeContainer) {
      YoutubeEmbedRenderer.delete(this.youtubeContainer);
      this.youtubeContainer = null;
    }
  }
 
  isReadyToInteract(target: DisplayObject): boolean {
    return checkIntersect(target, this.interactBox as DisplayObject);
  }
 
  changeAlpha(): void {
    if (this.world.player && this.world.player.collisionBox) {
      if (checkIntersect(this.world.player.collisionBox, this.children[0])) {
        this.alpha = 0.5;
      } else this.alpha = 1;
    } else this.alpha = 1;
  }
 
  update(): void {
    if (this.alphaChangable) this.changeAlpha();
    if (this.world.player && this.world.player.collisionBox)
      this.interact(this.world.player.collisionBox);
  }
}