Thanks pgimeno , I checked
https://ironypolicy.wordpress.com/2014/ ... oguelikes/ and solved it:
First, I added "energy" and "energygen" field for each entities, and changed "movedelay" to "movecost".
"energy" represents how much energy entity has, "energygen" represents how much energy will generated by each turn, and "movecost" is how much energy needed to move.
"energygen" is not shared among entities because if entity move faster or slower somehow(like "haste" or "slow" effect), modifying energygen value will be easier than modifying every movecost/attackcost/castcost...(if I add those later.)
Before:
Code: Select all
entities = {
{type = "player", x = 2, y = 2, movedelay = 100}, -- player is always entity #1
{type = "goblin", x = 14, y = 9, movedelay = 100},
{type = "snake", x = 2, y = 9, movedelay = 80},
{type = "troll", x = 14, y = 2, movedelay = 120}
}
After:
Code: Select all
entities = {
{type = "player", x = 2, y = 2, energy = 0, energygen = 100, movecost = 100}, -- player is always entity #1
{type = "goblin", x = 14, y = 9, energy = 0, energygen = 100, movecost = 100},
{type = "snake", x = 2, y = 9, energy = 0, energygen = 100, movecost = 80},
{type = "troll", x = 14, y = 2, energy = 0, energygen = 100, movecost = 120}
}
Then, I fixed love.keypressed callback function - if player has less than 0 energy, turn(s) will be processed until player regenerate enough amount of energy to act.
Before:
Code: Select all
function love.keypressed(key)
local playertookaction = false
-- h(←) j(↓) k(↑) l(→) y(↖) u(↗) b(↙) n(↘) p(perform pathfinding to mouse cursor)
if key == "h" or key == "left" then -- move left
playertookaction = move(player(), -1, 0)
elseif key == "j" or key == "down" then -- move down
playertookaction = move(player(), 0, 1)
elseif key == "k" or key == "up" then -- move up
playertookaction = move(player(), 0, -1)
elseif key == "l" or key == "right" then -- move right
playertookaction = move(player(), 1, 0)
elseif key == "y" then -- move left-up
playertookaction = move(player(), -1, -1)
elseif key == "u" then -- move right-up
playertookaction = move(player(), 1, -1)
elseif key == "b" then -- move left-down
playertookaction = move(player(), -1, 1)
elseif key == "n" then -- move right-down
playertookaction = move(player(), 1, 1)
elseif key == "." then
playertookaction = true
elseif key == "r" then
reset()
elseif key == "p" then -- perform pathfinding to mouse pointer
end
if playertookaction == true then
tick()
end
end
After:
Code: Select all
function love.keypressed(key)
local playertookaction = false
-- h(←) j(↓) k(↑) l(→) y(↖) u(↗) b(↙) n(↘) p(perform pathfinding to mouse cursor)
if key == "h" or key == "left" then -- move left
playertookaction = move(player(), -1, 0)
elseif key == "j" or key == "down" then -- move down
playertookaction = move(player(), 0, 1)
elseif key == "k" or key == "up" then -- move up
playertookaction = move(player(), 0, -1)
elseif key == "l" or key == "right" then -- move right
playertookaction = move(player(), 1, 0)
elseif key == "y" then -- move left-up
playertookaction = move(player(), -1, -1)
elseif key == "u" then -- move right-up
playertookaction = move(player(), 1, -1)
elseif key == "b" then -- move left-down
playertookaction = move(player(), -1, 1)
elseif key == "n" then -- move right-down
playertookaction = move(player(), 1, 1)
elseif key == "." then
playertookaction = true
elseif key == "r" then
reset()
elseif key == "p" then -- perform pathfinding to mouse pointer
end
if playertookaction == true then
player().energy = player().energy - player().movecost
while player().energy < 0 do
player().energy = player().energy + player().energygen
tick()
end
end
end
Finally, I added while loop in tick() function - if entity has enough energy to move, move entity while it has enough energy to move.
Before:
Code: Select all
function tick()
-- process turn and move another monsters
turn = turn + 1
-- make non-player creatures move
local k, v, path
for k, v in pairs(entities) do
if v.type ~= "player" then
-- perform pathfinding and track player
path = pathfind(v, player())
if path[1] ~= nil then
move(v, path[1].x, path[1].y)
end
end
end
end
After:
Code: Select all
function tick()
-- process turn and move another monsters
turn = turn + 1
-- make non-player creatures move
local k, v, path
local creature_moved
for k, v in pairs(entities) do
if v.type ~= "player" then
-- perform pathfinding and track player
v.energy = v.energy + v.energygen
while(v.energy >= v.movecost) do
path = pathfind(v, player())
if path[1] ~= nil then
move(v, path[1].x, path[1].y)
end
v.energy = v.energy - v.movecost
end
end
end
end
Final product is working well(at least I think). I attach modified .love file here.