|
|
@ -30,6 +30,15 @@ function mob_on_hex(hex) |
|
|
|
end) |
|
|
|
end |
|
|
|
|
|
|
|
function mob_can_pass_through(mob, hex) |
|
|
|
local tile = HEX_MAP.get(hex.x, hex.y) |
|
|
|
if tile and tile.elevation < -0.5 or tile.elevation >= 0.5 then |
|
|
|
return false |
|
|
|
else |
|
|
|
return true |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
function mob_die(mob, mob_index) |
|
|
|
WORLD:action(vplay_sound(SOUNDS.EXPLOSION1)) |
|
|
|
delete_entity(MOBS, mob_index) |
|
|
@ -61,13 +70,11 @@ end |
|
|
|
-- try reducing map size by identifying key nodes (inflection points) |
|
|
|
-- there are performance hits everytime we spawn a mob and it's Astar's fault |
|
|
|
function get_mob_path(mob, map, start, goal) |
|
|
|
--return Astar(map, goal, start, grid_heuristic, grid_cost) |
|
|
|
return Astar(map, goal, start, grid_heuristic, grid_cost) |
|
|
|
end |
|
|
|
|
|
|
|
-- @FIXME there's a bug here where the position of the spawn hex is sometimes 1 closer to the center than we want |
|
|
|
local function get_spawn_hex() |
|
|
|
local spawn_hex |
|
|
|
repeat |
|
|
|
-- ensure we spawn on an random tile along the map's edges |
|
|
|
local roll = math.random(HEX_GRID_WIDTH * 2 + HEX_GRID_HEIGHT * 2) - 1 |
|
|
|
local x, y |
|
|
@ -86,12 +93,7 @@ local function get_spawn_hex() |
|
|
|
end |
|
|
|
|
|
|
|
-- @NOTE negate 'y' because hexyz algorithms assume south is positive, in amulet north is positive |
|
|
|
spawn_hex = evenq_to_hex(vec2(x, -y)) |
|
|
|
local tile = HEX_MAP[spawn_hex.x][spawn_hex.y] |
|
|
|
|
|
|
|
until is_passable(tile) |
|
|
|
|
|
|
|
return spawn_hex |
|
|
|
return evenq_to_hex(vec2(x, -y)) |
|
|
|
end |
|
|
|
|
|
|
|
local function mob_update(mob, mob_index) |
|
|
@ -104,8 +106,13 @@ local function mob_update(mob, mob_index) |
|
|
|
return true |
|
|
|
end |
|
|
|
|
|
|
|
--local frame_target = mob.path[mob.hex.x] and mob.path[mob.hex.x][mob.hex.y] |
|
|
|
local frame_target = nil |
|
|
|
if mob.path then |
|
|
|
-- we have an explicitly stored hex that this mob wants to move towards. |
|
|
|
frame_target = mob.path[mob.hex.x] and mob.path[mob.hex.x][mob.hex.y] |
|
|
|
|
|
|
|
else |
|
|
|
-- make a dumb guess where we should go. |
|
|
|
local neighbours = HEX_MAP.neighbours(mob.hex) |
|
|
|
if #neighbours ~= 0 then |
|
|
|
local first_entry = HEX_MAP.get(neighbours[1].x, neighbours[1].y) |
|
|
@ -114,7 +121,6 @@ local function mob_update(mob, mob_index) |
|
|
|
local best_cost = first_entry and first_entry.priority or HEX_MAP.get(last_frame_hex.x, last_frame_hex.y).priority |
|
|
|
|
|
|
|
for _,h in pairs(neighbours) do |
|
|
|
--if h ~= last_frame_hex then |
|
|
|
local map_entry = HEX_MAP.get(h.x, h.y) |
|
|
|
local cost = map_entry.priority |
|
|
|
|
|
|
@ -122,10 +128,16 @@ local function mob_update(mob, mob_index) |
|
|
|
best_cost = cost |
|
|
|
best_hex = h |
|
|
|
end |
|
|
|
--end |
|
|
|
end |
|
|
|
frame_target = best_hex |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
if frame_target == last_frame_hex or not mob_can_pass_through(mob, frame_target) then |
|
|
|
-- we are trying to go somewhere dumb. make a better path. |
|
|
|
mob.path = get_mob_path(mob, HEX_MAP, mob.hex, HEX_GRID_CENTER) |
|
|
|
return -- don't move this frame. too much time thinking |
|
|
|
end |
|
|
|
|
|
|
|
if frame_target then |
|
|
|
mob.position = mob.position + math.normalize(hex_to_pixel(frame_target) - mob.position) * mob.speed |
|
|
@ -150,7 +162,7 @@ local function make_and_register_mob() |
|
|
|
mob_update |
|
|
|
) |
|
|
|
|
|
|
|
mob.path = get_mob_path(mob, HEX_MAP, mob.hex, HEX_GRID_CENTER) |
|
|
|
mob.path = false --get_mob_path(mob, HEX_MAP, mob.hex, HEX_GRID_CENTER) |
|
|
|
mob.health = 10 |
|
|
|
mob.speed = 1 |
|
|
|
mob.bounty = 5 |
|
|
|