mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-02 00:33:39 +00:00
implement destroyers
This commit is contained in:
@@ -12,6 +12,8 @@ export class DestroyerExecution implements Execution {
|
||||
private target: MutableUnit = null
|
||||
private pathfinder = new PathFinder()
|
||||
|
||||
private patrolTile: Tile;
|
||||
|
||||
// TODO: put in config
|
||||
private searchRange = 100
|
||||
|
||||
@@ -24,9 +26,11 @@ export class DestroyerExecution implements Execution {
|
||||
init(mg: MutableGame, ticks: number): void {
|
||||
this._owner = mg.player(this.playerID)
|
||||
this.mg = mg
|
||||
this.patrolTile = mg.tile(this.cell)
|
||||
}
|
||||
|
||||
tick(ticks: number): void {
|
||||
// TODO: remove gold from player
|
||||
if (this.destroyer == null) {
|
||||
this.destroyer = this._owner.addUnit(UnitType.Destroyer, 0, this.mg.tile(this.cell))
|
||||
return
|
||||
@@ -35,24 +39,46 @@ export class DestroyerExecution implements Execution {
|
||||
this.active = false
|
||||
return
|
||||
}
|
||||
if (this.target != null && !this.target.isActive()) {
|
||||
this.target = null
|
||||
}
|
||||
if (this.target == null) {
|
||||
const ships = this.mg.units(UnitType.TransportShip)
|
||||
.filter(u => manhattanDist(u.tile().cell(), this.destroyer.tile().cell()))
|
||||
.filter(u => u.owner() != this.destroyer.owner())
|
||||
.filter(u => manhattanDist(u.tile().cell(), this.destroyer.tile().cell()) < 100)
|
||||
// .filter(u => u.owner() != this.destroyer.owner())
|
||||
.filter(u => u != this.destroyer)
|
||||
.filter(u => !u.owner().isAlliedWith(this.destroyer.owner()))
|
||||
if (ships.length == 0) {
|
||||
if (manhattanDist(this.destroyer.tile().cell(), this.cell) > 5) {
|
||||
for (let i = 0; i < 1 + this.mg.ticks() % 2; i++) {
|
||||
const next = this.pathfinder.nextTile(this.destroyer.tile(), this.patrolTile)
|
||||
if (next == null) {
|
||||
this.target = null
|
||||
return
|
||||
}
|
||||
this.destroyer.move(next)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
// TODO: sort by distance
|
||||
this.target = ships[0]
|
||||
}
|
||||
const next = this.pathfinder.nextTile(this.destroyer.tile(), this.target.tile())
|
||||
if (next == null) {
|
||||
if (manhattanDist(this.destroyer.tile().cell(), this.target.tile().cell()) < 5) {
|
||||
this.target.delete()
|
||||
this.target = null
|
||||
return
|
||||
}
|
||||
for (let i = 0; i < 1 + this.mg.ticks() % 2; i++) {
|
||||
const next = this.pathfinder.nextTile(this.destroyer.tile(), this.target.tile())
|
||||
if (next == null) {
|
||||
this.target = null
|
||||
console.warn(`target not found`)
|
||||
return
|
||||
}
|
||||
this.destroyer.move(next)
|
||||
}
|
||||
|
||||
this.destroyer.move(next)
|
||||
}
|
||||
|
||||
owner(): MutablePlayer {
|
||||
|
||||
Reference in New Issue
Block a user