Browse Source

enlarge the hexes - shrink the map - make it easier to change map size

master
Nicholas Hayashi 4 years ago
parent
commit
ac134e83dd
  1. BIN
      res/thing.png
  2. 4
      src/entity.lua
  3. 6
      src/game.lua
  4. 31
      src/grid.lua
  5. 6
      src/mob.lua
  6. 4
      src/projectile.lua
  7. 12
      src/tower.lua

BIN
res/thing.png

Before

Width: 287  |  Height: 309  |  Size: 5.8 KiB

After

Width: 287  |  Height: 309  |  Size: 6.0 KiB

4
src/entity.lua

@ -26,10 +26,10 @@ function make_basic_entity(hex, node, update, position)
-- corresponds to the provided pixel position -- corresponds to the provided pixel position
if position then if position then
entity.position = position entity.position = position
entity.hex = pixel_to_hex(entity.position)
entity.hex = pixel_to_hex(entity.position, vec2(HEX_SIZE))
else else
entity.hex = hex entity.hex = hex
entity.position = hex_to_pixel(hex)
entity.position = hex_to_pixel(hex, vec2(HEX_SIZE))
end end
entity.update = update entity.update = update

6
src/game.lua

@ -166,8 +166,8 @@ local function game_action(scene)
end end
local mouse = WIN:mouse_position() local mouse = WIN:mouse_position()
local hex = pixel_to_hex(mouse - WORLDSPACE_COORDINATE_OFFSET)
local rounded_mouse = hex_to_pixel(hex) + WORLDSPACE_COORDINATE_OFFSET
local hex = pixel_to_hex(mouse - WORLDSPACE_COORDINATE_OFFSET, vec2(HEX_SIZE))
local rounded_mouse = hex_to_pixel(hex, vec2(HEX_SIZE)) + WORLDSPACE_COORDINATE_OFFSET
local evenq = hex_to_evenq(hex) local evenq = hex_to_evenq(hex)
local centered_evenq = evenq{ y = -evenq.y } - vec2(math.floor(HEX_GRID_WIDTH/2) local centered_evenq = evenq{ y = -evenq.y } - vec2(math.floor(HEX_GRID_WIDTH/2)
, math.floor(HEX_GRID_HEIGHT/2)) , math.floor(HEX_GRID_HEIGHT/2))
@ -423,7 +423,7 @@ function make_hex_cursor(radius, color_f, action_f)
local group = am.group() local group = am.group()
for _,h in pairs(map) do for _,h in pairs(map) do
local hexagon = am.circle(hex_to_pixel(h), HEX_SIZE, color or color_f(h), 6)
local hexagon = am.circle(hex_to_pixel(h, vec2(HEX_SIZE)), HEX_SIZE, color or color_f(h), 6)
group:append(hexagon) group:append(hexagon)
end end

31
src/grid.lua

@ -1,23 +1,34 @@
-- distance from hex centerpoint to any vertex -- distance from hex centerpoint to any vertex
HEX_SIZE = 20
HEX_SIZE = 24
HEX_PIXEL_WIDTH = hex_width(HEX_SIZE, ORIENTATION.FLAT) HEX_PIXEL_WIDTH = hex_width(HEX_SIZE, ORIENTATION.FLAT)
HEX_PIXEL_HEIGHT = hex_height(HEX_SIZE, ORIENTATION.FLAT) HEX_PIXEL_HEIGHT = hex_height(HEX_SIZE, ORIENTATION.FLAT)
HEX_PIXEL_DIMENSIONS = vec2(HEX_PIXEL_WIDTH, HEX_PIXEL_HEIGHT) HEX_PIXEL_DIMENSIONS = vec2(HEX_PIXEL_WIDTH, HEX_PIXEL_HEIGHT)
-- with 1920x1080, the minimal dimensions to cover the screen is 65x33
-- added 2 cell padding, because we terraform the very outer edge and it looks ugly, so hide it
-- odd numbers are important because we want a 'true' center -- odd numbers are important because we want a 'true' center
HEX_GRID_WIDTH = 67
HEX_GRID_HEIGHT = 35
HEX_GRID_DIMENSIONS = vec2(HEX_GRID_WIDTH, HEX_GRID_HEIGHT)
-- added 2 cell padding, because we terraform the very outer edge and it looks ugly, so hide it
do
-- padding should be an even number if the result of the below calculation returns an odd number,
-- and vice-versa
local padding = 2
-- leaving y == 0 makes this the center in hex coordinates
HEX_GRID_CENTER = vec2(math.floor(HEX_GRID_WIDTH/2)
HEX_GRID_WIDTH = math.floor(WIN.width / (HEX_PIXEL_WIDTH + HEX_SIZE) * 2) + padding
HEX_GRID_HEIGHT = math.floor(WIN.height / HEX_PIXEL_HEIGHT) + padding
HEX_GRID_DIMENSIONS = vec2(HEX_GRID_WIDTH, HEX_GRID_HEIGHT)
log(HEX_GRID_DIMENSIONS)
assert(HEX_GRID_WIDTH % 2 == 1
and HEX_GRID_HEIGHT % 2 == 1
, string.format("grid dimensions aren't both odd numbers - %s", tostring(HEX_GRID_DIMENSIONS)))
-- leaving y == 0 makes this the center in hex coordinates, assuming that our dimensions are correct
HEX_GRID_CENTER = vec2(math.floor(HEX_GRID_WIDTH/2)
, 0) , 0)
-- , math.floor(HEX_GRID_HEIGHT/2)) -- , math.floor(HEX_GRID_HEIGHT/2))
end
HEX_GRID_MINIMUM_ELEVATION = -1 HEX_GRID_MINIMUM_ELEVATION = -1
HEX_GRID_MAXIMUM_ELEVATION = 1 HEX_GRID_MAXIMUM_ELEVATION = 1
@ -115,7 +126,7 @@ function apply_flow_field(map, flow_field, world)
if flow then if flow then
map[i][j].priority = flow.priority map[i][j].priority = flow.priority
overlay_group:append(am.translate(hex_to_pixel(vec2(i, j)))
overlay_group:append(am.translate(hex_to_pixel(vec2(i, j), vec2(HEX_SIZE)))
^ am.text(string.format("%.1f", flow.priority * 10))) ^ am.text(string.format("%.1f", flow.priority * 10)))
else else
map[i][j].priority = nil map[i][j].priority = nil
@ -212,7 +223,7 @@ function random_map(seed)
, ((-evenq.y - HEX_GRID_HEIGHT/2) / HEX_GRID_HEIGHT) ^ 2)) , ((-evenq.y - HEX_GRID_HEIGHT/2) / HEX_GRID_HEIGHT) ^ 2))
local color = color_at(noise) - mask local color = color_at(noise) - mask
local node = am.translate(hex_to_pixel(vec2(i, j)))
local node = am.translate(hex_to_pixel(vec2(i, j), vec2(HEX_SIZE)))
^ am.circle(vec2(0), HEX_SIZE, color, 6) ^ am.circle(vec2(0), HEX_SIZE, color, 6)
map.set(i, j, { map.set(i, j, {

6
src/mob.lua

@ -145,7 +145,7 @@ end
local function resolve_frame_target_for_mob(mob, mob_index) local function resolve_frame_target_for_mob(mob, mob_index)
local last_frame_hex = mob.hex local last_frame_hex = mob.hex
mob.hex = pixel_to_hex(mob.position)
mob.hex = pixel_to_hex(mob.position, vec2(HEX_SIZE))
if mob.hex == HEX_GRID_CENTER then if mob.hex == HEX_GRID_CENTER then
update_score(-mob.health) update_score(-mob.health)
@ -223,7 +223,7 @@ local function update_mob_spooder(mob, mob_index)
local to = state.map.get(mob.frame_target.x, mob.frame_target.y) local to = state.map.get(mob.frame_target.x, mob.frame_target.y)
local rate = (math.abs(from.elevation - to.elevation) * 100) * mob.speed * am.delta_time local rate = (math.abs(from.elevation - to.elevation) * 100) * mob.speed * am.delta_time
mob.position = mob.position + math.normalize(hex_to_pixel(mob.frame_target) - mob.position) * rate
mob.position = mob.position + math.normalize(hex_to_pixel(mob.frame_target, vec2(HEX_SIZE)) - mob.position) * rate
mob.node.position2d = mob.position mob.node.position2d = mob.position
else else
mob.frame_target = false mob.frame_target = false
@ -251,7 +251,7 @@ local function update_mob_beeper(mob, mob_index)
local to = state.map.get(mob.frame_target.x, mob.frame_target.y) local to = state.map.get(mob.frame_target.x, mob.frame_target.y)
local rate = (4 * mob.speed - math.abs(to.elevation - from.elevation)) * am.delta_time local rate = (4 * mob.speed - math.abs(to.elevation - from.elevation)) * am.delta_time
mob.position = mob.position + math.normalize(hex_to_pixel(mob.frame_target) - mob.position) * rate
mob.position = mob.position + math.normalize(hex_to_pixel(mob.frame_target, vec2(HEX_SIZE)) - mob.position) * rate
mob.node.position2d = mob.position mob.node.position2d = mob.position
else else
mob.frame_target = false mob.frame_target = false

4
src/projectile.lua

@ -77,7 +77,7 @@ local function update_projectile_shell(projectile, projectile_index)
end end
projectile.node.position2d = projectile.position projectile.node.position2d = projectile.position
projectile.hex = pixel_to_hex(projectile.position)
projectile.hex = pixel_to_hex(projectile.position, vec2(HEX_SIZE))
projectile.props.z = projectile.props.z - SHELL_GRAVITY * am.delta_time projectile.props.z = projectile.props.z - SHELL_GRAVITY * am.delta_time
-- check if we hit something -- check if we hit something
@ -137,7 +137,7 @@ local function update_projectile_laser(projectile, projectile_index)
end end
projectile.node.position2d = projectile.position projectile.node.position2d = projectile.position
projectile.hex = pixel_to_hex(projectile.position)
projectile.hex = pixel_to_hex(projectile.position, vec2(HEX_SIZE))
-- check if we hit something -- check if we hit something
-- get a list of hexes that could have something we could hit on them -- get a list of hexes that could have something we could hit on them

12
src/tower.lua

@ -37,7 +37,7 @@ TOWER_SPECS = {
cost = 20, cost = 20,
range = 10, range = 10,
fire_rate = 4, fire_rate = 4,
size = 1,
size = 0,
height = 1, height = 1,
}, },
[TOWER_TYPE.REDEYE] = { [TOWER_TYPE.REDEYE] = {
@ -49,7 +49,7 @@ TOWER_SPECS = {
cost = 20, cost = 20,
range = 12, range = 12,
fire_rate = 1, fire_rate = 1,
size = 1,
size = 0,
height = 1, height = 1,
}, },
[TOWER_TYPE.MOAT] = { [TOWER_TYPE.MOAT] = {
@ -85,7 +85,7 @@ TOWER_SPECS = {
cost = 20, cost = 20,
range = 8, range = 8,
fire_rate = 1, fire_rate = 1,
size = 1,
size = 0,
height = 1, height = 1,
}, },
} }
@ -132,9 +132,9 @@ local function make_tower_node(tower_type)
elseif tower_type == TOWER_TYPE.HOWITZER then elseif tower_type == TOWER_TYPE.HOWITZER then
return am.group{ return am.group{
pack_texture_into_sprite(TEXTURES.HEX_FLOWER, HEX_FLOWER_DIMENSIONS.x, HEX_FLOWER_DIMENSIONS.y),
pack_texture_into_sprite(TEXTURES.HEX_FLOWER, HEX_PIXEL_WIDTH, HEX_PIXEL_HEIGHT),
am.rotate(state.time or 0) ^ am.group{ am.rotate(state.time or 0) ^ am.group{
pack_texture_into_sprite(TEXTURES.CANNON1, 100, 100)
pack_texture_into_sprite(TEXTURES.CANNON1, 50, 50)
} }
} }
elseif tower_type == TOWER_TYPE.LIGHTHOUSE then elseif tower_type == TOWER_TYPE.LIGHTHOUSE then
@ -371,7 +371,7 @@ function update_tower_howitzer(tower, tower_index)
end end
end end
end end
tower.node("rotate").angle = math.wrapf(tower.node("rotate").angle + 0.2 * am.delta_time, math.pi*2)
tower.node("rotate").angle = math.wrapf(tower.node("rotate").angle + 0.1 * am.delta_time, math.pi*2)
else else
-- we should have a target -- we should have a target
if MOBS[tower.target_index] == false then if MOBS[tower.target_index] == false then

Loading…
Cancel
Save