Problem destroying entities with HUMP
Posted: Mon Mar 06, 2017 12:59 pm
Super beginner here, please excuse the absolute mess of my code. I'm trying to use HUMP to switch rooms(maps) but quickly ran into the problem that objects stick around in the new map. So I need to use leave() to erase the objects, right? Only when I do remove them from the bump.lua world, it is still trying to getRect on something and can't. If I don't remove them from the bump world, it says I am adding my room change collision object twice. I'm not really clear on what's happening here. Again, sorry about the code there are a lot of placeholders/unfinished stuff and I clearly have no idea what I'm doing. Have a feeling part of this has to do with me not understanding HUMP's init and enter functions properly. Anyway, here it is:
Code: Select all
local overworld = {}
bump = require 'bump'
sti = require 'sti'
require 'drawWords'
require "AnimatedSprite"
local world = bump.newWorld()
local level = {}
level.players = {}
level.elems = {}
function overworld:init()
-- Grab window size
windowWidth = love.graphics.getWidth()
windowHeight = love.graphics.getHeight()
-- Set world meter size (in pixels)
love.physics.setMeter(32)
dialogueFont = love.graphics.newFont('lunchds.ttf', 32)
local draw_x = 0
local draw_y = 0
local next_animation = 2
love.graphics.setDefaultFilter('nearest','nearest')
Noname = GetInstance ("NonameSprite.lua")
player_image = love.graphics.newImage("NonameSprite.png")
function level:addElem(x, y, w, h, image, drawLambda)
local t = {draw = drawLambda}
world:add(t, x, y, w, h)
table.insert(self.elems, t)
return t
end
function level:addPlayer(x, y, w, h, image, drawLambda)
local t = {draw = drawLambda}
world:add(t, x, y, w, h)
table.insert(self.players, t)
return t
end
function level:draw()
for _, v in pairs(self.elems) do
v:draw(world:getRect(v))
end
for _, v in pairs(self.players) do
v:draw(world:getRect(v))
end
end
player = level:addPlayer(10, 10, 40, 50, _,
function (self, x, y, w, h)
DrawInstance (Noname, x-10, y-74)
end)
end
function overworld:enter(overworld, levelMap)
-- Load a map exported to Lua from Tiled
map = sti(levelMap, { "bump" })
map:bump_init(world)
collision = false
words = " "
dialogue = false
key = nil
wx = 0
wy = 0
for _, object in pairs(map.objects) do
if object.properties.room then
world:add(object.name, object.x, object.y, object.width, object.height)
else
dude_controller:spawnDude(object.x, object.y, object.name, object.name .. ".png")
end
end
for _, d in pairs(dude_controller.dudes) do
level:addElem(d.x, d.y, d.w, d.h, d.image,
function (self, x, y, w, h, image)
love.graphics.setColor(255, 255, 255)
love.graphics.draw(d.image, x, y, 0, 1, 1)
end)
end
end
function overworld:draw()
local x, y, _,_ = world:getRect(player)
love.graphics.translate(-x + windowWidth/2, -y + windowHeight/2)
map:draw()
level:draw()
drawWords(wx, wy)
end
function overworld:update(dt)
map:update(dt)
UpdateInstance(Noname, dt)
local x, y, _,_ = world:getRect(player)
function love.keyreleased(key)
if collision then
for _, d in pairs(dude_controller.dudes) do
if key == "a" and d.x == wx and d.y == wy then
dialogue = true
end
end
for _, object in pairs(map.objects) do
if object.properties.room and
wx >= object.x and
wx <= object.x + object.width then
collision = false
roomName = object.name
return Gamestate.switch(overworld, roomName .. ".lua")
end
end
end
end
if dialogue then
function love.keyreleased(key)
if key == "a" then
dialogue = false
collision = false
end
end
end
if dialogue == false then
if love.keyboard.isDown("right") then
Noname.curr_anim = "right"
local actualX, actualY, cols, len = world:move(player, x + 5, y)
if len > 0 then
for i=1,len do
collision = true
wx, wy = cols[i].otherRect.x, cols[i].otherRect.y
wordsPosition(wx, wy)
break
end
end
elseif love.keyboard.isDown("left") then
Noname.curr_anim = "left"
local actualX, actualY, cols, len = world:move(player, x - 5, y)
if len > 0 then
for i=1,len do
collision = true
wx, wy = cols[i].otherRect.x, cols[i].otherRect.y
wordsPosition(wx, wy)
break
end
end
elseif love.keyboard.isDown("up") then
Noname.curr_anim = "up"
local actualX, actualY, cols, len = world:move(player, x, y - 5)
if len > 0 then
for i=1,len do
collision = true
wx, wy = cols[i].otherRect.x, cols[i].otherRect.y
wordsPosition(wx, wy)
break
end
end
elseif love.keyboard.isDown("down") then
Noname.curr_anim = "down"
local actualX, actualY, cols, len = world:move(player, x, y + 5)
if len > 0 then
for i=1,len do
collision = true
wx, wy = cols[i].otherRect.x, cols[i].otherRect.y
wordsPosition(wx, wy)
break
end
end
else
Noname.curr_anim = "idle"
end
end
end
function overworld:leave()
elemsRemoved = true
for _, d in pairs(dude_controller.dudes) do
table.remove(d, self.dudes)
end
for _, elem in pairs(level.elems) do
table.remove(elem, self.elems)
end
for _, object in pairs(map.objects) do
world:remove(object)
end
end
return overworld