| 1 |
local gl = require 'gl' |
| 2 |
local blueprints = require 'blueprints' |
| 3 |
local v2 = require 'dokidoki.v2' |
| 4 |
|
| 5 |
BUILDING_SPEED = 3 |
| 6 |
|
| 7 |
local SEGMENTS = 32 |
| 8 |
local RADIUS = 32 |
| 9 |
local DRAW_OFFSET = -4 |
| 10 |
local SPAWN_OFFSET = 47 |
| 11 |
|
| 12 |
UNIT_COSTS = { fighter = 50, bomber = 170, frigate = 360, upgrade = 1080 } |
| 13 |
|
| 14 |
local potential_cost = 0 |
| 15 |
local needle_angle = 0 |
| 16 |
local needle_velocity = 0 |
| 17 |
|
| 18 |
local texcoord_scroller = -0.25 |
| 19 |
|
| 20 |
button_held = false |
| 21 |
halt_production = false |
| 22 |
|
| 23 |
local function spawn(unit_type) |
| 24 |
self.resources.amount = self.resources.amount - UNIT_COSTS[unit_type] |
| 25 |
|
| 26 |
if (unit_type == 'upgrade') then |
| 27 |
self.resources.harvest_rate = self.resources.harvest_rate + 0.25 |
| 28 |
else |
| 29 |
local spawn_pos = self.transform.pos + SPAWN_OFFSET * self.transform.facing |
| 30 |
game.log.record_spawn(blueprints[unit_type]) |
| 31 |
game.actors.new(blueprints[unit_type], |
| 32 |
{'transform', pos=spawn_pos, facing=self.transform.facing}, |
| 33 |
{'ship', player=self.ship.player}) |
| 34 |
end |
| 35 |
end |
| 36 |
|
| 37 |
function update() |
| 38 |
if button_held then |
| 39 |
if (potential_cost == 0 and self.resources.amount >= potential_cost) then |
| 40 |
potential_cost = UNIT_COSTS.fighter |
| 41 |
end |
| 42 |
if (self.resources.amount > potential_cost + BUILDING_SPEED-1) then |
| 43 |
potential_cost = potential_cost + BUILDING_SPEED |
| 44 |
end |
| 45 |
else |
| 46 |
if potential_cost > UNIT_COSTS.upgrade then |
| 47 |
spawn('upgrade') |
| 48 |
elseif potential_cost > UNIT_COSTS.frigate then |
| 49 |
spawn('frigate') |
| 50 |
elseif potential_cost > UNIT_COSTS.bomber then |
| 51 |
spawn('bomber') |
| 52 |
elseif potential_cost > UNIT_COSTS.fighter then |
| 53 |
spawn('fighter') |
| 54 |
end |
| 55 |
potential_cost = 0 |
| 56 |
end |
| 57 |
|
| 58 |
|
| 59 |
local key_z = game.debug_keys.key_pressed(string.byte('Z')) |
| 60 |
local key_x = game.debug_keys.key_pressed(string.byte('X')) |
| 61 |
local key_c = game.debug_keys.key_pressed(string.byte('C')) |
| 62 |
local key_v = game.debug_keys.key_pressed(string.byte('V')) |
| 63 |
|
| 64 |
if key_z then spawn('fighter') end |
| 65 |
if key_x then spawn('bomber') end |
| 66 |
if key_c then spawn('frigate') end |
| 67 |
if key_v then spawn('upgrade') end |
| 68 |
end |
| 69 |
|
| 70 |
local function scale_angle(frames) |
| 71 |
local angle = 0 |
| 72 |
if (frames < UNIT_COSTS.fighter) then |
| 73 |
angle = 0.25 |
| 74 |
elseif (frames < UNIT_COSTS.bomber) then |
| 75 |
angle = (frames - UNIT_COSTS.fighter) / (UNIT_COSTS.bomber - UNIT_COSTS.fighter) * 0.25 + 0.25 |
| 76 |
elseif (frames < UNIT_COSTS.frigate) then |
| 77 |
angle = (frames - UNIT_COSTS.bomber) / (UNIT_COSTS.frigate - UNIT_COSTS.bomber) * 0.25 + 0.5 |
| 78 |
else |
| 79 |
angle = (frames - UNIT_COSTS.frigate) / (UNIT_COSTS.upgrade - UNIT_COSTS.frigate) * 0.25 + 0.75 |
| 80 |
end |
| 81 |
return math.min(angle - 0.25, 1) |
| 82 |
end |
| 83 |
|
| 84 |
local function get_resources_spent(frames) |
| 85 |
local spent |
| 86 |
if (frames < UNIT_COSTS.fighter) then |
| 87 |
spent = 0 |
| 88 |
elseif (frames < UNIT_COSTS.bomber) then |
| 89 |
spent = UNIT_COSTS.fighter |
| 90 |
elseif (frames < UNIT_COSTS.frigate) then |
| 91 |
spent = UNIT_COSTS.bomber |
| 92 |
elseif (frames < UNIT_COSTS.upgrade) then |
| 93 |
spent = UNIT_COSTS.frigate |
| 94 |
else |
| 95 |
spent = UNIT_COSTS.upgrade |
| 96 |
end |
| 97 |
return spent |
| 98 |
end |
| 99 |
|
| 100 |
function draw() |
| 101 |
if halt_production then |
| 102 |
return |
| 103 |
end |
| 104 |
|
| 105 |
local pos = self.transform.pos; |
| 106 |
local offset = DRAW_OFFSET * self.transform.facing |
| 107 |
|
| 108 |
gl.glPushMatrix() |
| 109 |
gl.glTranslated(pos.x + offset.x, pos.y + offset.y, 0) |
| 110 |
|
| 111 |
game.resources.production_layer_1:draw() |
| 112 |
|
| 113 |
|
| 114 |
gl.glBegin(gl.GL_TRIANGLE_FAN) |
| 115 |
local angle = scale_angle(self.resources.amount) * math.pi * 2 |
| 116 |
local filled_angle = math.pi * 2 - angle |
| 117 |
gl.glColor4d(0, 0, 0, 0.6) |
| 118 |
gl.glVertex2d(0, 0) |
| 119 |
for point = 0,SEGMENTS do |
| 120 |
local vert = v2.unit(math.pi/2 - angle - point / SEGMENTS * filled_angle) * RADIUS |
| 121 |
gl.glVertex2d(vert.x, vert.y) |
| 122 |
end |
| 123 |
gl.glVertex2d(0, 0) |
| 124 |
gl.glEnd() |
| 125 |
|
| 126 |
|
| 127 |
if self.resources.amount > UNIT_COSTS.fighter and button_held then |
| 128 |
gl.glBegin(gl.GL_TRIANGLE_FAN) |
| 129 |
local cost = get_resources_spent(potential_cost) |
| 130 |
local bottom_highlight_angle = scale_angle(cost) * math.pi * 2 |
| 131 |
gl.glColor4d(0.5, 1, 1, 1) |
| 132 |
gl.glVertex2d(0, 0) |
| 133 |
for point = 0,SEGMENTS do |
| 134 |
gl.glVertex2d(math.sin(point / SEGMENTS * math.pi * 0.5 + bottom_highlight_angle) * RADIUS, math.cos(point / SEGMENTS * math.pi * 0.5 + bottom_highlight_angle) * RADIUS) |
| 135 |
end |
| 136 |
gl.glEnd() |
| 137 |
else |
| 138 |
|
| 139 |
end |
| 140 |
|
| 141 |
|
| 142 |
local angle = scale_angle(potential_cost) |
| 143 |
if angle == 0 then |
| 144 |
if needle_angle > 0 or needle_velocity < 0 then |
| 145 |
needle_velocity = math.min(needle_velocity + 0.002, 0.025) |
| 146 |
needle_angle = math.max(needle_angle - needle_velocity, 0) |
| 147 |
if (needle_angle == 0) then needle_velocity = needle_velocity * -0.475 end |
| 148 |
end |
| 149 |
else |
| 150 |
needle_velocity = 0 |
| 151 |
needle_angle = angle |
| 152 |
end |
| 153 |
gl.glColor3d(1, 1, 1) |
| 154 |
gl.glPushMatrix() |
| 155 |
gl.glScaled(0.5, 0.5, 1) |
| 156 |
gl.glRotated(-needle_angle * 360, 0, 0, 1) |
| 157 |
game.resources.needle_sprite:draw() |
| 158 |
gl.glPopMatrix() |
| 159 |
|
| 160 |
game.resources.production_layer_2:draw() |
| 161 |
game.resources.production_layer_3:draw() |
| 162 |
|
| 163 |
|
| 164 |
if button_held then |
| 165 |
if potential_cost > UNIT_COSTS.upgrade then |
| 166 |
game.resources.upgrade_preview_sprite:draw() |
| 167 |
elseif potential_cost > UNIT_COSTS.frigate then |
| 168 |
game.resources.frigate_preview_sprite:draw() |
| 169 |
elseif potential_cost > UNIT_COSTS.bomber then |
| 170 |
game.resources.bomber_preview_sprite:draw() |
| 171 |
elseif potential_cost > UNIT_COSTS.fighter then |
| 172 |
game.resources.fighter_preview_sprite:draw() |
| 173 |
end |
| 174 |
else |
| 175 |
texcoord_scroller = texcoord_scroller + 0.01 |
| 176 |
if texcoord_scroller > 0.25 then texcoord_scroller = texcoord_scroller - 0.5 end |
| 177 |
gl.glMatrixMode(gl.GL_TEXTURE) |
| 178 |
gl.glTranslated(texcoord_scroller, 0,0) |
| 179 |
gl.glMatrixMode(gl.GL_MODELVIEW) |
| 180 |
gl.glPushMatrix() |
| 181 |
gl.glScaled(1.20, 1, 1) |
| 182 |
|
| 183 |
local health = self.ship.health_percentage() |
| 184 |
if health < game.constants.low_health_threshold then |
| 185 |
local factor = health / game.constants.low_health_threshold |
| 186 |
gl.glScaled(1, math.max(0.5, factor), 1) |
| 187 |
gl.glColor3d(1, factor*0.3, factor*0.3) |
| 188 |
game.resources.health_none:draw() |
| 189 |
elseif health < game.constants.high_health_threshold then |
| 190 |
local factor = (health - game.constants.low_health_threshold) / (game.constants.high_health_threshold - game.constants.low_health_threshold) |
| 191 |
gl.glScaled(1, factor * 0.5 + 0.5, 1) |
| 192 |
gl.glColor3d(1, factor * 0.7 + 0.3, factor * 0.2 + 0.3) |
| 193 |
game.resources.health_some:draw() |
| 194 |
else |
| 195 |
local factor = (health - game.constants.high_health_threshold) / (1 - game.constants.high_health_threshold) |
| 196 |
gl.glScaled(1, health, 1) |
| 197 |
gl.glColor3d((1-factor)*0.3+0.7, 1, factor * 0.4 + 0.6) |
| 198 |
game.resources.health_full:draw() |
| 199 |
end |
| 200 |
|
| 201 |
gl.glPopMatrix() |
| 202 |
gl.glMatrixMode(gl.GL_TEXTURE) |
| 203 |
gl.glLoadIdentity() |
| 204 |
gl.glMatrixMode(gl.GL_MODELVIEW) |
| 205 |
end |
| 206 |
|
| 207 |
gl.glColor3d(1, 1, 1) |
| 208 |
|
| 209 |
gl.glPopMatrix() |
| 210 |
end |