Compare commits

...

2 Commits

Author SHA1 Message Date
Juhani Krekelä f5ba1bdb17 Make keyrepeat work for autoexplore too 2019-06-30 18:46:09 +03:00
Juhani Krekelä a85b6de70a Finish autoexplore 2019-06-30 18:37:12 +03:00
1 changed files with 55 additions and 23 deletions

View File

@ -21,7 +21,6 @@ local flash_player_red_counter = 0
local slimes = {}
local last_key_pressed = nil
local last_direction_moved = nil
local move_repeat_counter = nil
local gamemodes = {normal = 0, won = 1, lost = 2, config = 3}
@ -44,8 +43,6 @@ local control_keys = {quit = 'q', restart = 'r', configure = 'c', autoexplore =
local win_image = nil
local distances = {} --debg
-- ------------------------------------------------------------------
-- Cavern generation
-- ------------------------------------------------------------------
@ -669,14 +666,6 @@ end
function step(direction)
if game_mode == gamemodes.normal then
if last_direction_moved == direction then
-- Repeat faster after the initial threshold for repetition has been met
move_repeat_counter = 0.1
else
last_direction_moved = direction
move_repeat_counter = 0.3
end
rememberVisible()
if direction ~= directions.inplace then
@ -724,8 +713,7 @@ function autoexplore()
-- then each known accessible space gets labelled with min(neighbours) + 1. Once we have
-- computed that table, we can then move towards closest unknown accessible space by
-- moving to our neighbouring space with lowest score
--local distances = {}
distances = {} --debg
local distances = {}
for x = 1, cavern.width do
local list = {}
for y = 1, cavern.height do
@ -772,7 +760,49 @@ function autoexplore()
queue = new_queue
until #queue == 0
step(distances[player_x][player_y].direction)
local direction = distances[player_x][player_y].direction
local body_x, body_y = getBodyLocation()
-- If we are backing up, consider feet
if player_direction == directions.up and direction == directions.down then
direction = distances[body_x][body_y].direction
if direction == directions.left then
direction = directions.downleft
elseif direction == directions.right then
direction = directions.downright
else
direction = directions.down
end
elseif player_direction == directions.left and direction == directions.right then
direction = distances[body_x][body_y].direction
if direction == directions.up then
direction = directions.upright
elseif direction == directions.down then
direction = directions.downright
else
direction = directions.right
end
elseif player_direction == directions.down and direction == directions.up then
direction = distances[body_x][body_y].direction
if direction == directions.left then
direction = directions.upleft
elseif direction == directions.right then
direction = directions.upright
else
direction = directions.up
end
elseif player_direction == directions.right and direction == directions.left then
direction = distances[body_x][body_y].direction
if direction == directions.up then
direction = directions.upleft
elseif direction == directions.down then
direction = directions.downleft
else
direction = directions.left
end
end
step(direction)
end
-- ------------------------------------------------------------------
@ -852,11 +882,6 @@ function drawCavern()
love.graphics.setColor(1, 1, 0)
love.graphics.rectangle('fill', x + 0.5 * scale, y + 0.5 * scale, scale/2, scale/2)
end
if distances[tile_x] ~= nil and distances[tile_x][tile_y].value ~= nil then --debg
love.graphics.setColor(0.5, 0.5, 0.5) --debg
love.graphics.print(distances[tile_x][tile_y].value, x, y) --debg
end --debg
end
end
end
@ -1035,8 +1060,8 @@ end
function love.update(dt)
if move_repeat_counter ~= nil and move_repeat_counter > 0 then
move_repeat_counter = move_repeat_counter - dt
if move_repeat_counter <= 0 then
step(last_direction_moved)
if move_repeat_counter <= 0 and last_key_pressed ~= nil then
love.keypressed(last_key_pressed)
end
end
@ -1072,7 +1097,12 @@ function love.keypressed(key)
game_mode = gamemodes.normal
end
else
last_key_pressed = key
if last_key_pressed == key then
-- Repeat faster after the initial threshold for repetition has been met
move_repeat_counter = 0.1
else
move_repeat_counter = 0.3
end
if key == control_keys.configure and game_mode == gamemodes.normal then
direction_keys = {}
@ -1084,8 +1114,10 @@ function love.keypressed(key)
newGame()
elseif key == control_keys.autoexplore then
autoexplore()
last_key_pressed = key
elseif direction_keys[key] ~= nil then
step(direction_keys[key])
last_key_pressed = key
elseif key == 'pause' then
debug = not debug
end
@ -1094,7 +1126,7 @@ end
function love.keyreleased(key)
if last_key_pressed == key then
last_direction_moved = nil
last_key_pressed = nil
move_repeat_counter = nil
end
end