Browse Source

fix toolbelt button thing

master
Nicholas Hayashi 4 years ago
parent
commit
9cf99bd694
  1. 1
      color.lua
  2. BIN
      res/thing.aseprite
  3. BIN
      res/thing.png
  4. 40
      src/game.lua
  5. 28
      src/grid.lua
  6. 84
      src/tower.lua
  7. 2
      texture.lua

1
color.lua

@ -2,6 +2,7 @@
COLORS = { COLORS = {
TRANSPARENT = vec4(0.6), TRANSPARENT = vec4(0.6),
TRANSPARENT1 = vec4(0.4),
-- tones -- tones
WHITE = vec4(1, 1, 0.95, 1), WHITE = vec4(1, 1, 0.95, 1),

BIN
res/thing.aseprite

BIN
res/thing.png

After

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

40
src/game.lua

@ -83,6 +83,16 @@ local function get_top_right_display_text(hex, evenq, centered_evenq, display_ty
return str return str
end end
function alert(message)
WIN.scene:append(
am.scale(3) ^ am.text(message)
:action(coroutine.create(function(self)
am.wait(am.tween(self, 1, { color = vec4(0) }))
WIN.scene:remove(self)
end))
)
end
-- initialized later, as part of the init of the toolbelt -- initialized later, as part of the init of the toolbelt
local function select_tower_type(tower_type) end local function select_tower_type(tower_type) end
local function select_toolbelt_button(i) end local function select_toolbelt_button(i) end
@ -170,19 +180,21 @@ local function game_action(scene)
if WIN:mouse_pressed"left" then if WIN:mouse_pressed"left" then
if interactable then if interactable then
if buildable then if buildable then
local broken, flow_field = making_hex_unwalkable_breaks_flow_field(hex, tile)
local broken, flow_field = building_tower_breaks_flow_field(state.selected_tower_type, hex)
local cost = get_tower_cost(state.selected_tower_type) local cost = get_tower_cost(state.selected_tower_type)
if broken then if broken then
local node = WIN.scene("cursor"):child(2) local node = WIN.scene("cursor"):child(2)
node.color = COLORS.CLARET node.color = COLORS.CLARET
node:action(am.tween(0.1, { color = COLORS.TRANSPARENT })) node:action(am.tween(0.1, { color = COLORS.TRANSPARENT }))
alert("breaks flow field")
play_sfx(SOUNDS.BIRD2) play_sfx(SOUNDS.BIRD2)
elseif cost > state.money then elseif cost > state.money then
local node = WIN.scene("cursor"):child(2) local node = WIN.scene("cursor"):child(2)
node.color = COLORS.CLARET node.color = COLORS.CLARET
node:action(am.tween(0.1, { color = COLORS.TRANSPARENT })) node:action(am.tween(0.1, { color = COLORS.TRANSPARENT }))
alert("not enough $$$$")
play_sfx(SOUNDS.BIRD2) play_sfx(SOUNDS.BIRD2)
else else
@ -259,16 +271,7 @@ end
local function make_game_toolbelt() local function make_game_toolbelt()
local function toolbelt_button(size, half_size, tower_texture, padding, i, offset, key_name) local function toolbelt_button(size, half_size, tower_texture, padding, i, offset, key_name)
local x1 = (size + padding) * i + offset.x
local y1 = offset.y
local x2 = (size + padding) * i + offset.x + size
local y2 = offset.y + size
register_button_widget("toolbelt_tower_button" .. i
, am.rect(x1, y1, x2, y2)
, function() select_toolbelt_button(i) end)
return am.translate(vec2(size + padding, 0) * i + offset)
local button = am.translate(vec2(size + padding, 0) * i + offset)
^ am.group{ ^ am.group{
am.translate(0, half_size) am.translate(0, half_size)
^ pack_texture_into_sprite(TEXTURES.BUTTON1, size, size), ^ pack_texture_into_sprite(TEXTURES.BUTTON1, size, size),
@ -283,6 +286,20 @@ local function make_game_toolbelt()
^ am.text(key_name, COLORS.BLACK) ^ am.text(key_name, COLORS.BLACK)
} }
} }
local x1 = (size + padding) * i + offset.x - half_size
local y1 = offset.y
local x2 = (size + padding) * i + offset.x + size - half_size
local y2 = offset.y + size
local rect = { x1 = x1, y1 = y1, x2 = x2, y2 = y2 }
button:action(function(self)
if WIN:mouse_pressed"left" and point_in_rect(WIN:mouse_position(), rect) then
select_toolbelt_button(i)
end
end)
return button
end end
local toolbelt_height = hex_height(HEX_SIZE) * 2 local toolbelt_height = hex_height(HEX_SIZE) * 2
@ -362,6 +379,7 @@ local function make_game_toolbelt()
end end
select_tower_type = function(tower_type) select_tower_type = function(tower_type)
log(tower_type)
state.selected_tower_type = tower_type state.selected_tower_type = tower_type
if get_tower_spec(tower_type) then if get_tower_spec(tower_type) then

28
src/grid.lua

@ -50,6 +50,14 @@ function evenq_is_in_interactable_region(evenq)
}) })
end end
function is_water_elevation(elevation)
return elevation < -0.5
end
function is_mountain_elevation(elevation)
return elevation >= 0.5
end
function tile_is_medium_elevation(tile) function tile_is_medium_elevation(tile)
return tile.elevation >= -0.5 and tile.elevation < 0.5 return tile.elevation >= -0.5 and tile.elevation < 0.5
end end
@ -121,18 +129,30 @@ function apply_flow_field(map, flow_field, world)
end end
end end
function making_hex_unwalkable_breaks_flow_field(hex, tile)
if not mob_can_pass_through(nil, hex, tile) then
function building_tower_breaks_flow_field(tower_type, hex)
local hexes = spiral_map(hex, get_tower_size(tower_type))
local original_elevations = {}
for _,h in pairs(hexes) do
local tile = state.map.get(h.x, h.y)
if not mob_can_pass_through(nil, h, tile) then
return false return false
end end
local original_elevation = tile.elevation
table.insert(original_elevations, tile.elevation)
-- making the tile's elevation very large *should* make it unwalkable -- making the tile's elevation very large *should* make it unwalkable
tile.elevation = 999 tile.elevation = 999
end
local flow_field = generate_flow_field(state.map, HEX_GRID_CENTER) local flow_field = generate_flow_field(state.map, HEX_GRID_CENTER)
local result = not hex_map_get(flow_field, 0, 0) local result = not hex_map_get(flow_field, 0, 0)
tile.elevation = original_elevation
log(result)
for i,h in pairs(hexes) do
state.map.get(h.x, h.y).elevation = original_elevations[i]
end
return result, flow_field return result, flow_field
end end

84
src/tower.lua

@ -26,6 +26,7 @@ TOWER_SPECS = {
range = 0, range = 0,
fire_rate = 2, fire_rate = 2,
size = 0, size = 0,
height = 1,
}, },
[TOWER_TYPE.HOWITZER] = { [TOWER_TYPE.HOWITZER] = {
name = "Howitzer", name = "Howitzer",
@ -37,6 +38,7 @@ TOWER_SPECS = {
range = 10, range = 10,
fire_rate = 4, fire_rate = 4,
size = 1, size = 1,
height = 1,
}, },
[TOWER_TYPE.REDEYE] = { [TOWER_TYPE.REDEYE] = {
name = "Redeye", name = "Redeye",
@ -48,6 +50,7 @@ TOWER_SPECS = {
range = 12, range = 12,
fire_rate = 1, fire_rate = 1,
size = 1, size = 1,
height = 1,
}, },
[TOWER_TYPE.MOAT] = { [TOWER_TYPE.MOAT] = {
name = "Moat", name = "Moat",
@ -59,6 +62,7 @@ TOWER_SPECS = {
range = 0, range = 0,
fire_rate = 2, fire_rate = 2,
size = 0, size = 0,
height = -1,
}, },
[TOWER_TYPE.RADAR] = { [TOWER_TYPE.RADAR] = {
name = "Radar", name = "Radar",
@ -70,6 +74,7 @@ TOWER_SPECS = {
range = 0, range = 0,
fire_rate = 1, fire_rate = 1,
size = 1, size = 1,
height = 1,
}, },
[TOWER_TYPE.LIGHTHOUSE] = { [TOWER_TYPE.LIGHTHOUSE] = {
name = "Lighthouse", name = "Lighthouse",
@ -81,6 +86,7 @@ TOWER_SPECS = {
range = 8, range = 8,
fire_rate = 1, fire_rate = 1,
size = 1, size = 1,
height = 1,
}, },
} }
@ -100,7 +106,7 @@ function get_tower_texture(tower_type)
return TOWER_SPECS[tower_type].texture return TOWER_SPECS[tower_type].texture
end end
function get_tower_icon_texture(tower_type) function get_tower_icon_texture(tower_type)
return TOWER_SPECS[tower_type] and TOWER_SPECS[tower_type].icon_texture
return TOWER_SPECS[tower_type].icon_texture
end end
function get_tower_cost(tower_type) function get_tower_cost(tower_type)
return TOWER_SPECS[tower_type].cost return TOWER_SPECS[tower_type].cost
@ -119,14 +125,15 @@ local function make_tower_sprite(tower_type)
return pack_texture_into_sprite(get_tower_texture(tower_type), HEX_PIXEL_WIDTH, HEX_PIXEL_HEIGHT) return pack_texture_into_sprite(get_tower_texture(tower_type), HEX_PIXEL_WIDTH, HEX_PIXEL_HEIGHT)
end end
local HEX_FLOWER_DIMENSIONS = vec2(115, 125)
local function make_tower_node(tower_type) local function make_tower_node(tower_type)
if tower_type == TOWER_TYPE.REDEYE then if tower_type == TOWER_TYPE.REDEYE then
return make_tower_sprite(tower_type) return make_tower_sprite(tower_type)
elseif tower_type == TOWER_TYPE.HOWITZER then elseif tower_type == TOWER_TYPE.HOWITZER then
return am.group{ return am.group{
make_tower_sprite(tower_type),
am.rotate(0) ^ am.sprite("res/cannon1.png")
pack_texture_into_sprite(TEXTURES.HEX_FLOWER, HEX_FLOWER_DIMENSIONS.x, HEX_FLOWER_DIMENSIONS.y),
am.rotate(0) ^ pack_texture_into_sprite(TEXTURES.CANNON1, 100, 100)
} }
elseif tower_type == TOWER_TYPE.LIGHTHOUSE then elseif tower_type == TOWER_TYPE.LIGHTHOUSE then
return am.group{ return am.group{
@ -168,7 +175,7 @@ end
do do
local tower_cursors = {} local tower_cursors = {}
for _,i in pairs(TOWER_TYPE) do for _,i in pairs(TOWER_TYPE) do
local tower_sprite = make_tower_sprite(i)
local tower_sprite = make_tower_node(i)
tower_sprite.color = COLORS.TRANSPARENT tower_sprite.color = COLORS.TRANSPARENT
local coroutine_ = coroutine.create(function(node) local coroutine_ = coroutine.create(function(node)
@ -240,23 +247,37 @@ function tower_type_is_buildable_on(hex, tile, tower_type)
local blocking_towers = {} local blocking_towers = {}
local blocking_mobs = {} local blocking_mobs = {}
local has_water = false
local has_mountain = false
local has_ground = false
for _,h in pairs(spiral_map(hex, get_tower_size(tower_type))) do for _,h in pairs(spiral_map(hex, get_tower_size(tower_type))) do
table.append(blocking_towers, towers_on_hex(hex))
table.append(blocking_mobs, mobs_on_hex(hex))
end
table.merge(blocking_towers, towers_on_hex(h))
table.merge(blocking_mobs, mobs_on_hex(h))
local tile = state.map.get(h.x, h.y)
-- this should always be true, unless it is possible to place a tower
-- where part of the tower overflows the edge of the map
if tile then
if is_water_elevation(tile.elevation) then
has_water = true
elseif is_mountain_elevation(tile.elevation) then
has_mountain = true
if WIN:key_down"space" then
log(table.tostring(blocking_towers))
else
has_ground = true
end
end
end end
local towers_blocking = table.count(blocking_towers) ~= 0 local towers_blocking = table.count(blocking_towers) ~= 0
local mobs_blocking = table.count(blocking_mobs) ~= 0 local mobs_blocking = table.count(blocking_mobs) ~= 0
local blocked = mobs_blocking or towers_blocking local blocked = mobs_blocking or towers_blocking
if tower_type == TOWER_TYPE.HOWITZER then if tower_type == TOWER_TYPE.HOWITZER then
if not mobs_blocking and towers_blocking then if not mobs_blocking and towers_blocking then
-- you can build howitzers on top of walls.
blocked = false blocked = false
for _,tower in pairs(blocking_towers) do for _,tower in pairs(blocking_towers) do
if tower.type ~= TOWER_TYPE.WALL then if tower.type ~= TOWER_TYPE.WALL then
@ -265,10 +286,11 @@ function tower_type_is_buildable_on(hex, tile, tower_type)
end end
end end
end end
return not blocked and tile.elevation >= -0.5
return not (blocked or has_water)
elseif tower_type == TOWER_TYPE.REDEYE then elseif tower_type == TOWER_TYPE.REDEYE then
if not mobs_blocking and towers_blocking then if not mobs_blocking and towers_blocking then
-- you can build redeyes on top of walls
blocked = false blocked = false
for _,tower in pairs(blocking_towers) do for _,tower in pairs(blocking_towers) do
if tower.type ~= TOWER_TYPE.WALL then if tower.type ~= TOWER_TYPE.WALL then
@ -277,7 +299,10 @@ function tower_type_is_buildable_on(hex, tile, tower_type)
end end
end end
end end
return not blocked and tile.elevation > 0.5
return not blocked
and not has_water
and not has_ground
and has_mountain
elseif tower_type == TOWER_TYPE.LIGHTHOUSE then elseif tower_type == TOWER_TYPE.LIGHTHOUSE then
local has_water_neighbour = false local has_water_neighbour = false
@ -289,7 +314,10 @@ function tower_type_is_buildable_on(hex, tile, tower_type)
break break
end end
end end
return not blocked and tile_is_medium_elevation(tile) and has_water_neighbour
return not blocked
and not has_mountain
and not has_water
and has_water_neighbour
elseif tower_type == TOWER_TYPE.WALL then elseif tower_type == TOWER_TYPE.WALL then
return not blocked and tile_is_medium_elevation(tile) return not blocked and tile_is_medium_elevation(tile)
@ -406,7 +434,6 @@ function update_tower_lighthouse(tower, tower_index)
end end
end end
local TOWER_HEIGHT = 1
function make_and_register_tower(hex, tower_type) function make_and_register_tower(hex, tower_type)
local tower = make_basic_entity( local tower = make_basic_entity(
hex, hex,
@ -423,30 +450,15 @@ function make_and_register_tower(hex, tower_type)
tower.last_shot_time = -spec.fire_rate tower.last_shot_time = -spec.fire_rate
tower.size = spec.size tower.size = spec.size
tower.hexes = spiral_map(tower.hex, tower.size) tower.hexes = spiral_map(tower.hex, tower.size)
tower.height = spec.height
if tower_type == TOWER_TYPE.REDEYE then
local tile = state.map.get(hex.x, hex.y)
tile.elevation = tile.elevation + TOWER_HEIGHT
elseif tower_type == TOWER_TYPE.HOWITZER then
local tile = state.map.get(hex.x, hex.y)
tile.elevation = tile.elevation + TOWER_HEIGHT
tower.props.z = tile.elevation
elseif tower_type == TOWER_TYPE.LIGHTHOUSE then
tower.perimeter = ring_map(tower.hex, tower.range)
local tile = state.map.get(hex.x, hex.y)
tile.elevation = tile.elevation + TOWER_HEIGHT
elseif tower_type == TOWER_TYPE.WALL then
state.map.get(hex.x, hex.y).elevation = TOWER_HEIGHT
elseif tower_type == TOWER_TYPE.MOAT then
state.map.get(hex.x, hex.y).elevation = -TOWER_HEIGHT
for _,h in pairs(tower.hexes) do
local tile = state.map.get(h.x, h.y)
tile.elevation = tile.elevation + tower.height
end
elseif tower_type == TOWER_TYPE.RADAR then
local tile = state.map.get(hex.x, hex.y)
tile.elevation = tile.elevation + TOWER_HEIGHT
if tower.type == TOWER_TYPE.HOWITZER then
tower.props.z = tower.height
end end
register_entity(TOWERS, tower) register_entity(TOWERS, tower)

2
texture.lua

@ -33,6 +33,8 @@ TEXTURES = {
TOWER_LIGHTHOUSE = load_texture("res/tower_lighthouse.png"), TOWER_LIGHTHOUSE = load_texture("res/tower_lighthouse.png"),
TOWER_LIGHTHOUSE_ICON = load_texture("res/tower_lighthouse_icon.png"), TOWER_LIGHTHOUSE_ICON = load_texture("res/tower_lighthouse_icon.png"),
HEX_FLOWER = load_texture("res/thing.png"),
-- mob stuff -- mob stuff
MOB_BEEPER = load_texture("res/mob_beeper.png"), MOB_BEEPER = load_texture("res/mob_beeper.png"),
MOB_SPOODER = load_texture("res/mob_spooder.png"), MOB_SPOODER = load_texture("res/mob_spooder.png"),

Loading…
Cancel
Save