Keeping track of time

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Trevor
Prole
Posts: 30
Joined: Thu Mar 31, 2011 4:14 pm

Keeping track of time

Post by Trevor »

I'd like each enemy wave to appear after a certain amount of time has passed. I'd also like to be able to pause the game. However, the time between each wave is based on a getTime() value calculated when the level is loaded. If the game has been paused long enough by the time I resume and call getTime() again the value is so large multiple enemy waves are then drawn simultaneously. I call getTime() regularly as it is used to track the delay between player and enemy shots.

Here is the relevant code:
FirstLevel:

Code: Select all

function SecondLevel:load()
    Level:load()
    SecondLevel.Waves = {
        Formations:ByOnes(Raider, 0, 4),
        Formations:ByOnes(Raider, 128, 4),
        Formations:ByOnes(Raider, 288, 4)
    }
    -- getTime() determined when level loads. Pausing breaks this.
    SecondLevel.WaveTimes = {lt.getTime() + 3, lt.getTime() + 6, lt.getTime() + 9}
end
Utils:

Code: Select all

function Utils:togglePause()
    timeElapsed = love.timer.getTime()
    paused = not paused
end
Level:

Code: Select all

function Level:update(dt, te, w, wt)

    if not paused then
    
    -- Move enemy ships
    for i, v in ipairs(w) do
        if te > wt[i] then
            Manager:update(dt, v)
        end
    end
    ...
ActionSprite:

Code: Select all

function ActionSprite:shoot(dt, te)
    if (te - self.lastFired >= self.Weapon.rateOfFire) --[[and self.alive]] then
        -- Switch + / - for enemy / player repectively
        if self.isEnemy then
            table.insert(EnemyWeaponManager, self.Weapon:new{
            x = self.x, y = self.y + self.Weapon.image:getHeight() })
        elseif self.name == "P1" then
            table.insert(P1WeaponManager, self.Weapon:new{
            x = self.x, y = self.y - self.Weapon.image:getHeight() })
        elseif self.name == "P2" then
            table.insert(P2WeaponManager, self.Weapon:new{
            x = self.x, y = self.y - self.Weapon.image:getHeight() })
        end
        self.lastFired = love.timer.getTime()
    end
end
The full source code is here:
http://www.trevorhrenko.ca/jelaga/Jelaga.love

Thank you.
Trevor
Prole
Posts: 30
Joined: Thu Mar 31, 2011 4:14 pm

Re: Keeping track of time

Post by Trevor »

Consider this one solved. I started tracking the amount of time spent paused then added that value to the time until the next wave to compensate.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests