| 1 | local blueprints = require 'blueprints' |
| 2 | |
| 3 | local bullet_speed = 20 |
| 4 | local shot_cooldown_time = 6 |
| 5 | local shot_capacity = 5 |
| 6 | local shot_reload_rate = 2/60 |
| 7 | |
| 8 | local shots = shot_capacity |
| 9 | local cooldown = 0 |
| 10 | |
| 11 | function is_empty() |
| 12 | return shots < 1 |
| 13 | end |
| 14 | |
| 15 | function is_reloaded() |
| 16 | return shots == shot_capacity |
| 17 | end |
| 18 | |
| 19 | function is_cooled_down() |
| 20 | return cooldown == 0 |
| 21 | end |
| 22 | |
| 23 | function is_ready_to_shoot() |
| 24 | return is_cooled_down() and not is_empty() |
| 25 | end |
| 26 | |
| 27 | function update() |
| 28 | cooldown = math.max(0, cooldown - 1) |
| 29 | shots = math.min(shots + shot_reload_rate, shot_capacity) |
| 30 | end |
| 31 | |
| 32 | function shoot() |
| 33 | if cooldown == 0 and shots >= 1 then |
| 34 | shots = shots - 1 |
| 35 | cooldown = shot_cooldown_time |
| 36 | |
| 37 | local start_vel = bullet_speed * self.transform.facing + self.ship.velocity |
| 38 | game.actors.new(blueprints.laser, |
| 39 | {'transform', pos=self.transform.pos, facing=self.transform.facing}, |
| 40 | {'bullet', player=self.ship.player, velocity=start_vel}) |
| 41 | end |
| 42 | end |