UNPKG

@occultus/entity-api

Version:

Star Tenon entity api and utils

92 lines (90 loc) 2.7 kB
import { Entity, EntitySpawnAfterEvent, Player, system, world, } from "@minecraft/server"; import { Boss } from "../Boss"; export class BossServer { private boss = new Map<string, Boss>(); private registriesBoss(entity: Entity) { if (!this.boss.get(entity.typeId)) return; const boss = this.boss.get(entity.typeId)!; entity.setDynamicProperty("occultus:start_skill", true); for (const skill of boss.skills) { const handle = system.runInterval(() => { skill.execute(entity); }, skill.cooldownTime); world.afterEvents.entityDie.subscribe((event) => { if (event.deadEntity.id === entity.id) { system.clearRun(handle); } }); world.afterEvents.entityRemove.subscribe((event) => { if (event.removedEntityId === entity.id) { system.clearRun(handle); } }); } } private registriesMusic(entity: Entity) { if (!this.boss.get(entity.typeId)) return; const boss = this.boss.get(entity.typeId)!; if (!boss.music) return; const music = boss.music; const players: Player[] = []; for (const aroundEntity of entity.dimension.getEntities({ location: entity.location, minDistance: 0, maxDistance: music.radius, })) { if (aroundEntity instanceof Player) { aroundEntity.playMusic(music.trackId, { loop: true }); players.push(aroundEntity); } } world.afterEvents.entityDie.subscribe((event) => { if (event.deadEntity.id === entity.id) { for (const player of players) { player.stopMusic(); } } }); world.afterEvents.entityRemove.subscribe((event) => { if (event.removedEntityId === entity.id) { for (const player of players) { player.stopMusic(); } } }); } private startUp() { world.afterEvents.entityDie.subscribe((event) => { const { deadEntity } = event; if (this.boss.has(deadEntity.id)) { const boss = this.boss.get(deadEntity.id)!; if (!boss.dieEvent) { return; } boss.dieEvent(event); } }); world.afterEvents.entitySpawn.subscribe((event) => { const entity = event.entity; this.registriesBoss(entity); this.registriesMusic(entity); }); world.afterEvents.entityLoad.subscribe((event) => { const entity = event.entity; this.registriesBoss(entity); this.registriesMusic(entity); }); } constructor() { this.startUp(); } addBoss(boss: Boss) { this.boss.set(boss.typeId, boss); } }