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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| enum BossPhase { PHASE_1, PHASE_2, PHASE_3 }
class BossBlackboard extends Blackboard { currentPhase: BossPhase = BossPhase.PHASE_1; maxHealth: number; minions: Enemy[] = []; lastSummonTime: number = 0; summonCooldown: number = 10;
constructor(boss: Enemy, target: Player) { super(boss, target); this.maxHealth = boss.health; }
updatePhase(): void { const healthPercent = (this.enemy.health / this.maxHealth) * 100; if (healthPercent <= 40) { this.currentPhase = BossPhase.PHASE_3; } else if (healthPercent <= 70) { this.currentPhase = BossPhase.PHASE_2; } }
getDamageMultiplier(): number { switch (this.currentPhase) { case BossPhase.PHASE_1: return 1.0; case BossPhase.PHASE_2: return 1.5; case BossPhase.PHASE_3: return 2.0; default: return 1.0; } } }
class CheckPhaseTransition extends BTNode { private targetPhase: BossPhase;
constructor(blackboard: BossBlackboard, targetPhase: BossPhase) { super(blackboard); this.targetPhase = targetPhase; }
tick(): NodeStatus { return (this.blackboard as BossBlackboard).currentPhase === this.targetPhase ? NodeStatus.SUCCESS : NodeStatus.FAILURE; } }
class SummonMinions extends BTNode { private minionCount: number; private minionType: string;
constructor(blackboard: BossBlackboard, count: number, type: string) { super(blackboard); this.minionCount = count; this.minionType = type; }
tick(): NodeStatus { const bb = this.blackboard as BossBlackboard; const currentTime = performance.now() / 1000;
if (currentTime - bb.lastSummonTime < bb.summonCooldown) { return NodeStatus.FAILURE; }
bb.minions = bb.minions.filter(minion => minion.health > 0);
if (bb.minions.length < this.minionCount) { const boss = bb.enemy; const angle = (Math.PI * 2) / this.minionCount; for (let i = bb.minions.length; i < this.minionCount; i++) { const x = boss.x + Math.cos(angle * i) * 100; const y = boss.y + Math.sin(angle * i) * 100; const minion = new Enemy(x, y); minion.health = 50; minion.damage = 10; minion.type = this.minionType; bb.minions.push(minion); } bb.lastSummonTime = currentTime; return NodeStatus.SUCCESS; }
return NodeStatus.FAILURE; } }
class Enrage extends BTNode { tick(): NodeStatus { const bb = this.blackboard as BossBlackboard; const boss = bb.enemy; boss.speed *= 1.5; boss.damage *= bb.getDamageMultiplier(); return NodeStatus.SUCCESS; } }
class BossBehaviorTree extends EnemyBehaviorTree { constructor(boss: Enemy, target: Player) { super(boss, target); }
protected buildTree(): BTNode { const bb = this.blackboard as BossBlackboard; const root = new Selector(bb);
const phase1Sequence = new Sequence(bb); phase1Sequence.addChild(new CheckPhaseTransition(bb, BossPhase.PHASE_1)); phase1Sequence.addChild(new Selector(bb, [ new Sequence(bb, [ new CheckHealthCondition(bb, 90), new RangedAttack(bb, 15, 2) ]), new ChaseTarget(bb, 80) ]));
const phase2Sequence = new Sequence(bb); phase2Sequence.addChild(new CheckPhaseTransition(bb, BossPhase.PHASE_2)); phase2Sequence.addChild(new Selector(bb, [ new SummonMinions(bb, 3, "skeleton"), new Sequence(bb, [ new CheckDistanceCondition(bb, 150), new RangedAttack(bb, 25, 1.5) ]), new ChaseTarget(bb, 100) ]));
const phase3Sequence = new Sequence(bb); phase3Sequence.addChild(new CheckPhaseTransition(bb, BossPhase.PHASE_3)); phase3Sequence.addChild(new Enrage(bb)); phase3Sequence.addChild(new Selector(bb, [ new SummonMinions(bb, 5, "demon"), new Sequence(bb, [ new CheckDistanceCondition(bb, 100), new RangedAttack(bb, 40, 1) ]), new ChaseTarget(bb, 120) ]));
root.addChild(phase1Sequence); root.addChild(phase2Sequence); root.addChild(phase3Sequence);
return root; }
update(deltaTime: number): void { (this.blackboard as BossBlackboard).updatePhase(); super.update(deltaTime); } }
|