Browse Source

stop you from placing towers that ruin the flow field

master
Nicholas Hayashi 4 years ago
parent
commit
a0feeec2e1
  1. 4
      color.lua
  2. 20
      src/game.lua
  3. 35
      src/grid.lua

4
color.lua

@ -4,9 +4,9 @@ COLORS = {
TRANSPARENT = vec4(0.6),
-- tones
WHITE = vec4(0.8, 0.8, 0.7, 1),
WHITE = vec4(1, 1, 0.95, 1),
PALE_SILVER = vec4(193/255, 178/255, 171/255, 1),
BLACK = vec4(0, 0, 0.02, 1),
BLACK = vec4(0, 0, 0.05, 1),
VERY_DARK_GRAY = vec4(35/255, 35/255, 25/255, 1),
TRUE_BLACK = vec4(0, 0, 0, 1),

20
src/game.lua

@ -1,13 +1,9 @@
state = {}
local function get_initial_game_state(seed)
local STARTING_MONEY = 50
local map, world = random_map(seed)
-- top right display types
TRDTS = {
local TRDTS = {
NOTHING = 0,
CENTERED_EVENQ = 1,
EVENQ = 2,
@ -18,6 +14,10 @@ TRDTS = {
TILE = 7,
}
local function get_initial_game_state(seed)
local STARTING_MONEY = 50
local map, world = random_map(seed)
return {
map = map, -- map of hex coords map[x][y] to some stuff at that location
world = world, -- the root scene graph node for the game 'world'
@ -68,6 +68,10 @@ local function get_top_right_display_text(display_type)
end
local function can_do_build(hex, tile, tower_type)
if making_hex_unwalkable_breaks_flow_field(hex, tile) then
return false
end
if not can_afford_tower(state.money, tower_type) then
return false
end
@ -157,7 +161,7 @@ local function game_action(scene)
state.top_right_display_type = (state.top_right_display_type + 1) % #table.keys(TRDTS)
elseif WIN:key_pressed"f2" then
WORLD"flow_field".hidden = not WORLD"flow_field".hidden
state.world"flow_field".hidden = not state.world"flow_field".hidden
elseif WIN:key_pressed"tab" then
if WIN:key_down"lshift" then
@ -321,6 +325,8 @@ function make_hex_cursor(position, radius, color_f)
end
function game_scene()
local score = am.translate(WIN.left + 10, WIN.top - 20) ^ am.text("", "left"):tag"score"
local money = am.translate(WIN.left + 10, WIN.top - 40) ^ am.text("", "left"):tag"money"
local top_right_display = am.translate(WIN.right - 10, WIN.top - 20) ^ am.text("", "right", "top"):tag"top_right_display"

35
src/grid.lua

@ -73,6 +73,17 @@ function grid_heuristic(source, target)
return math.distance(source, target)
end
function making_hex_unwalkable_breaks_flow_field(hex, tile)
local original_elevation = tile.elevation
-- making the tile's elevation very large *should* make it unwalkable
tile.elevation = 10
local flow_field = generate_flow_field(state.map, HEX_GRID_CENTER)
local result = not hex_map_get(flow_field, 0, 0)
tile.elevation = original_elevation
return result
end
function grid_cost(map, from, to)
local t1, t2 = map.get(from.x, from.y), map.get(to.x, to.y)
@ -86,8 +97,12 @@ function grid_cost(map, from, to)
return epsilon - cost
end
function generate_flow_field(map, start)
return dijkstra(map, start, nil, grid_cost)
end
function generate_and_apply_flow_field(map, start, world)
local flow = dijkstra(map, start, nil, grid_cost)
local flow_field = generate_flow_field(map, start)
local flow_field_hidden = world and world"flow_field" and world"flow_field".hidden or true
if world and world"flow_field" then
@ -95,14 +110,18 @@ function generate_and_apply_flow_field(map, start, world)
end
local overlay_group = am.group():tag"flow_field"
for i,_ in pairs(flow) do
for j,f in pairs(flow[i]) do
if f then
map[i][j].priority = f.priority
for i,_ in pairs(map) do
for j,f in pairs(map[i]) do
local flow = hex_map_get(flow_field, i, j)
if flow then
map[i][j].priority = flow.priority
overlay_group:append(am.translate(hex_to_pixel(vec2(i, j)))
^ am.text(string.format("%.1f", f.priority * 10)))
^ am.text(string.format("%.1f", flow.priority * 10)))
else
map[i][j].priority = nil
log('fire')
-- should fire exactly once
end
end
@ -128,6 +147,7 @@ function random_map(seed)
local neg_mask = am.rect(0, 0, HEX_GRID_PIXEL_WIDTH, HEX_GRID_PIXEL_HEIGHT, COLORS.TRUE_BLACK):tag"negative_mask"
local world = am.group(neg_mask):tag"world"
local edge_nodes = {}
for i,_ in pairs(map) do
for j,noise in pairs(map[i]) do
local evenq = hex_to_evenq(vec2(i, j))
@ -137,6 +157,9 @@ function random_map(seed)
-- if we're on an edge -- terraform edges to be passable
noise = 0
-- it's nice to also store the coords for edge nodes in a separate table for later
table.insert(edge_nodes, vec2(i, j))
elseif j == HEX_GRID_CENTER.y and i == HEX_GRID_CENTER.x then
-- also terraform the center of the grid to be passable
-- very infrequently, but still sometimes it is not medium elevation

Loading…
Cancel
Save