| 1 |
local v2 = require 'dokidoki.v2' |
| 2 |
|
| 3 |
assert(player, 'missing player argument') |
| 4 |
assert(turn_speed, 'missing turn_speed argument') |
| 5 |
assert(accel, 'missing accel argument') |
| 6 |
assert(hit_points, 'missing hit_points argument') |
| 7 |
|
| 8 |
local max_hit_points = hit_points |
| 9 |
self.sprite.color = {1.0, 1.0, 1.0} |
| 10 |
|
| 11 |
if sprites_table then |
| 12 |
self.sprite.image = assert(game.resources[sprites_table][player]) |
| 13 |
end |
| 14 |
|
| 15 |
velocity = v2(0, 0) |
| 16 |
|
| 17 |
|
| 18 |
function turn(direction, amount) |
| 19 |
amount = amount or 1 |
| 20 |
self.transform.facing = |
| 21 |
v2.norm(v2.rotate(self.transform.facing, turn_speed * direction * amount)) |
| 22 |
end |
| 23 |
|
| 24 |
function thrust(amount) |
| 25 |
amount = amount or 1 |
| 26 |
velocity = velocity + self.transform.facing * accel * amount |
| 27 |
end |
| 28 |
|
| 29 |
local function random_point_on_ship() |
| 30 |
|
| 31 |
local a = self.collision.poly.vertices[1] |
| 32 |
local ab = self.collision.poly.vertices[2] - a |
| 33 |
local ac = self.collision.poly.vertices[4] - a |
| 34 |
return self.transform.pos + |
| 35 |
v2.rotate_to(self.transform.facing, |
| 36 |
a + ab * math.random() + ac * math.random()) |
| 37 |
end |
| 38 |
|
| 39 |
function update() |
| 40 |
velocity = velocity * 0.97 |
| 41 |
self.transform.pos = self.transform.pos + velocity |
| 42 |
if hit_points <= 0 then destruct() end |
| 43 |
|
| 44 |
if math.random() < v2.mag(velocity) / 10 then |
| 45 |
game.particles.add_bubble(random_point_on_ship()) |
| 46 |
end |
| 47 |
|
| 48 |
|
| 49 |
game.tracing.trace_bar(self.transform.pos + v2(0, 10), |
| 50 |
hit_points / max_hit_points) |
| 51 |
end |
| 52 |
|
| 53 |
local function go_towards_or_away(target_pos, force_thrust, is_away) |
| 54 |
local target_direction = target_pos - self.transform.pos |
| 55 |
if is_away then target_direction = -target_direction end |
| 56 |
|
| 57 |
if v2.cross(self.transform.facing, target_direction) > 0 then |
| 58 |
turn(1) |
| 59 |
else |
| 60 |
turn(-1) |
| 61 |
end |
| 62 |
if force_thrust or v2.dot(self.transform.facing, target_direction) > 0 then |
| 63 |
thrust() |
| 64 |
end |
| 65 |
end |
| 66 |
|
| 67 |
function health_percentage() |
| 68 |
return math.max(hit_points / max_hit_points, 0) |
| 69 |
end |
| 70 |
|
| 71 |
function damage(amount) |
| 72 |
hit_points = math.max(hit_points - amount, 0) |
| 73 |
end |
| 74 |
|
| 75 |
function destruct() |
| 76 |
if self.factory_ai then |
| 77 |
factory_destruct() |
| 78 |
else |
| 79 |
game.particles.explode(self) |
| 80 |
game.log.record_death(self.blueprint) |
| 81 |
self.dead = true |
| 82 |
end |
| 83 |
end |
| 84 |
|
| 85 |
local death_counter = 100 |
| 86 |
local next_explosion = 10 |
| 87 |
local opacity = 0.6 |
| 88 |
|
| 89 |
function factory_destruct() |
| 90 |
if death_counter > 0 then |
| 91 |
self.production.halt_production = true |
| 92 |
self.sprite.color = {self.sprite.color[1], self.sprite.color[2], self.sprite.color[3], math.max(0, opacity)} |
| 93 |
opacity = opacity - 0.006 |
| 94 |
if death_counter % next_explosion == 0 then |
| 95 |
game.particles.explode(self, random_point_on_ship()) |
| 96 |
next_explosion = math.random(6,15) |
| 97 |
end |
| 98 |
death_counter = death_counter - 1 |
| 99 |
else |
| 100 |
for i = 1,5 do |
| 101 |
random = (v2.random() + v2.random()) * 20 |
| 102 |
game.particles.explode(self, self.transform.pos + random) |
| 103 |
end |
| 104 |
game.log.record_death(self.blueprint) |
| 105 |
self.dead = true |
| 106 |
end |
| 107 |
end |
| 108 |
|
| 109 |
|
| 110 |
function go_towards(target_pos, force_thrust) |
| 111 |
go_towards_or_away(target_pos, force_thrust, false) |
| 112 |
end |
| 113 |
|
| 114 |
function go_away(target_pos, force_thrust) |
| 115 |
go_towards_or_away(target_pos, force_thrust, true) |
| 116 |
end |