Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot get the pots to disappear after throwing them #2

Open
BGraw opened this issue Jun 14, 2020 · 0 comments
Open

cannot get the pots to disappear after throwing them #2

BGraw opened this issue Jun 14, 2020 · 0 comments

Comments

@BGraw
Copy link

BGraw commented Jun 14, 2020

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 :((((

@jwardlaw520 jwardlaw520 mentioned this issue Jul 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant