games50 / zelda

Lecture examples for Lecture 5: Legend of Zelda
35 stars 42 forks source link

cannot get the pots to disappear after throwing them #2

Open BGraw opened 4 years ago

BGraw commented 4 years ago

hey everyone, I'm currently working on the assignment for Zelda remake. I've got everything working fine but im stuck on how to get the pots to disappear after your throw them. I have been trying to add a table.remove(self.objects, k) in the room:update(dt) section of the logic. below is my latest attempt: (you can skip down to SEE BELOW FOR MY ATTEMPT)

function Room:update(dt) -- don't update anything if we are sliding to another room (we have offsets) if self.adjacentOffsetX ~= 0 or self.adjacentOffsetY ~= 0 then return end

self.player:update(dt)

for i = #self.entities, 1, -1 do
    local entity = self.entities[i]

    -- if entity is dead, skip the next code block
    if not entity.dead then
        -- if entity has been just defeated
        if entity.health <= 0 then
            -- mark entity as dead
            entity.dead = true
            -- spawn a heart with 10% probability
            if math.random(10) <= 7 then
                self:spawnHeart(entity.x, entity.y)
            end
        -- if entity has not been defeated yet
        else 
            entity:processAI({room = self}, dt)
            entity:update(dt)
        end

        -- collision between the player and entities in the room
        if self.player:collides(entity) and not self.player.invulnerable then
            gSounds['hit-player']:play()
            self.player:damage(1)
            self.player:goInvulnerable(1.5)

            if self.player.health == 0 then
                gStateMachine:change('game-over')
            end
        end
    end
end

----------------- SEE BELOW FOR MY ATTEMPT for k, object in pairs(self.objects) do object:update(dt) local distance = 3*16

    -- trigger collision callback on object
    if self.player:collides(object) then
        if object.type == 'heart' then
            Event.dispatch('heartCollected')
        end
        object:onCollide()
        if object.consumable then
            table.remove(self.objects, k)
        end
    end

    if object.type == 'pot' and object.state == 'thrown' then
        -- wall collision check
        if self:wallsCollideWith(object) then
            table.remove(self.objects, k)
        end

        -- for each entity (enemy)
        for i = #self.entities, 1, -1 do
            local entity = self.entities[i]
            -- if entity is dead, skip the next code block
            if not entity.dead then
                if entity:collides(object) then
                    -- pot collided with enemy, so deal damage
                    entity:damage(1)
                    -- and destroy the pot, removing it from the objects table
                    table.remove(self.objects, k)
                end
            end     
        end

        if object.type == 'pot' and object.state == 'idle' and distance == true then
            table.remove(self.objects, k)
        end
    end
end
Timer.update(dt)

end

any advice would be extremely helpful. Im sure im missing something small but i've spent the whole day trying to do this one part of the assignment :((((