Implementing a speed-based turn counter system in a roguelike
Posted: Tue Sep 06, 2016 12:15 am
Never mind, I figured it out on my own. See my 2nd post for the solution.
Code: Select all
-- in "Mob.lua" module
function Mob:roll_call()
who_is_here[self.y][self.x] = self.is_monster -- puts true or false in the space
self.turn_counter = self.turn_counter * math.random(0.9, 1.1) -- randomizes turns a little
table.insert(allMobs, self)
end
function Mob:move(dx, dy)
-- update "who is here"
who_is_here[self.y][self.x] = nil -- erases the previous grid entry
self.x, self.y = self.x + dx, self.y + dy -- moves
who_is_here[self.y][self.x] = self.is_monster -- puts true or false in the space (for combat)
self.turn_counter = 1 * self.speed -- resets turn counter inside its own table
for k,v in ipairs(allMobs) do
if v == self then -- looks for itself in allMobs
v.turn_counter = self.turn_counter -- replaces its own turn_counter in the spot paired with whatever has that name with the fresh value
end
end
self.my_turn = false -- flags turn is over
turn_active = false -- flags no active turn
end
-- in "turn.lua" module
function turn.decrement_counters(dt) -- runs in Dungeon.update(dt) and subtracts from all turn_counters
if turn_active == false then
for k,v in ipairs(allMobs) do -- runs down each entry in allMobs table, k is the index, v is the table associated with it (the mob)
v.turn_counter = v.turn_counter - (10 * dt) -- v is the mob
if v.turn_counter < 0 then
v.turn_counter = 0 -- catches negative values and scoops em up to zero
turn_active = true -- flags that there's an active turn (halts decrementing)
whose_turn = v -- sends the mob that's in the #1 slot to a variable for outside use
whose_turn.my_turn = true
return
end
end
end
end