| 1 |
local v2 = require 'dokidoki.v2' |
| 2 |
|
| 3 |
local stopping = false |
| 4 |
local target_fuzzy_pos |
| 5 |
|
| 6 |
local function retarget () |
| 7 |
target = game.targeting.get_nearest_of_type(self, 'fighter') or |
| 8 |
game.targeting.get_nearest_of_type(self, 'frigate') or |
| 9 |
game.targeting.get_nearest_of_type(self, 'factory') |
| 10 |
|
| 11 |
if target then |
| 12 |
target_fuzzy_pos = target.transform.pos + v2.random() * 250 |
| 13 |
end |
| 14 |
end |
| 15 |
|
| 16 |
function update() |
| 17 |
|
| 18 |
if not target or target.dead or math.random() < 0.001 then |
| 19 |
retarget() |
| 20 |
end |
| 21 |
|
| 22 |
if target then |
| 23 |
target_distance = v2.mag(target.transform.pos - self.transform.pos) |
| 24 |
speed_square = v2.sqrmag(self.ship.velocity) |
| 25 |
|
| 26 |
if self.frigate_shooting.is_ready_to_shoot() and speed_square > 0 then |
| 27 |
stopping = true |
| 28 |
elseif self.frigate_shooting.is_empty() then |
| 29 |
stopping = false |
| 30 |
end |
| 31 |
|
| 32 |
if not stopping then |
| 33 |
if target_distance < 200 then |
| 34 |
|
| 35 |
self.ship.go_away(target_fuzzy_pos, true) |
| 36 |
else |
| 37 |
self.ship.go_towards(target_fuzzy_pos, true) |
| 38 |
end |
| 39 |
end |
| 40 |
|
| 41 |
|
| 42 |
if not self.frigate_shooting.is_empty() and speed_square < 0.01 then |
| 43 |
self.frigate_shooting.shoot() |
| 44 |
end |
| 45 |
end |
| 46 |
end |