mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-22 05:13:48 +00:00
75 lines
1.9 KiB
TypeScript
75 lines
1.9 KiB
TypeScript
import { Execution, Game, Player, Unit, UnitType } from "../game/Game";
|
|
import { TileRef } from "../game/GameMap";
|
|
import { AirPathFinder } from "../pathfinding/PathFinding";
|
|
import { PseudoRandom } from "../PseudoRandom";
|
|
|
|
export class ShellExecution implements Execution {
|
|
private active = true;
|
|
private pathFinder: AirPathFinder;
|
|
private shell: Unit;
|
|
private mg: Game;
|
|
private destroyAtTick: number = -1;
|
|
|
|
constructor(
|
|
private spawn: TileRef,
|
|
private _owner: Player,
|
|
private ownerUnit: Unit,
|
|
private target: Unit,
|
|
) {}
|
|
|
|
init(mg: Game, ticks: number): void {
|
|
this.pathFinder = new AirPathFinder(mg, new PseudoRandom(mg.ticks()));
|
|
this.mg = mg;
|
|
}
|
|
|
|
tick(ticks: number): void {
|
|
if (this.shell == null) {
|
|
this.shell = this._owner.buildUnit(UnitType.Shell, 0, this.spawn);
|
|
}
|
|
if (!this.shell.isActive()) {
|
|
this.active = false;
|
|
return;
|
|
}
|
|
if (
|
|
!this.target.isActive() ||
|
|
this.target.owner() == this.shell.owner() ||
|
|
(this.destroyAtTick != -1 && this.mg.ticks() >= this.destroyAtTick)
|
|
) {
|
|
this.shell.delete(false);
|
|
this.active = false;
|
|
return;
|
|
}
|
|
|
|
if (this.destroyAtTick == -1 && !this.ownerUnit.isActive()) {
|
|
this.destroyAtTick = this.mg.ticks() + this.mg.config().shellLifetime();
|
|
}
|
|
|
|
for (let i = 0; i < 3; i++) {
|
|
const result = this.pathFinder.nextTile(
|
|
this.shell.tile(),
|
|
this.target.tile(),
|
|
);
|
|
if (result === true) {
|
|
this.active = false;
|
|
this.target.modifyHealth(-this.effectOnTarget());
|
|
this.shell.delete(false);
|
|
return;
|
|
} else {
|
|
this.shell.move(result);
|
|
}
|
|
}
|
|
}
|
|
|
|
private effectOnTarget(): number {
|
|
const baseDamage: number = this.mg.config().unitInfo(UnitType.Shell).damage;
|
|
return baseDamage;
|
|
}
|
|
|
|
isActive(): boolean {
|
|
return this.active;
|
|
}
|
|
activeDuringSpawnPhase(): boolean {
|
|
return false;
|
|
}
|
|
}
|