Okay well it's all kind of referencing each other so here is the full code (it's really messy and the dialogue part is kind of broken right now)
Code: Select all
bump = require 'bump'
require 'drawWords'
local world = bump.newWorld()
local level = {elems = {}}
function level:addElem(x, y, w, h, drawLambda)
local t = {draw = drawLambda}
world:add(t, x, y, w, h)
table.insert(self.elems, t)
return t
end
function level:draw()
for _, v in pairs(self.elems) do
v:draw(world:getRect(v))
end
end
player_image = love.graphics.newImage("NonameSprite.png")
function love.load()
love.graphics.setDefaultFilter('nearest','nearest')
background_image = love.graphics.newImage("background.png")
collision = false
words = " "
dialogue = false
key = nil
wx = 0
wy = 0
updateWords()
dude_controller:spawnDude(200, 200, dude1)
dude_controller:spawnDude(200, 400, dude2)
for _, d in pairs(dude_controller.dudes) do
level:addElem(d.x, d.y, d.w, d.h,
function (self, x, y, w, h)
love.graphics.setColor(0, 255, 255)
love.graphics.rectangle('fill', x, y, w, h)
end)
end
player = level:addElem(10, 10, 64, 64,
function (self, x, y, w, h)
love.graphics.setColor(255, 255, 255)
love.graphics.draw(player_image, x, y-64, 0, 1, 1)
end)
end
function love.draw()
local sx = love.graphics.getWidth() / background_image:getWidth()
local sy = love.graphics.getHeight() / background_image:getHeight()
love.graphics.draw(background_image, 0, 0, 0, sx, sy)
level:draw()
drawWords(wx, wy)
end
function love.update(dt)
local x, y, _,_ = world:getRect(player)
function love.keyreleased(key)
if collision then
if key == "a" then
dialogue = true
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
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
break
end
end
end
end
if love.keyboard.isDown("left") then
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
break
end
end
end
if love.keyboard.isDown("up") then
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
break
end
end
end
if love.keyboard.isDown("down") then
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
break
end
end
end
end
Code: Select all
dude_controller = {}
dude_controller.dudes = {}
dude = {}
name = {}
function drawWords(x, y)
if dialogue then
love.graphics.setColor(255,255,255)
love.graphics.rectangle("fill", x, y, 64 - 20, 20)
for i,d in ipairs(dude_controller.dudes) do
if d.x == x and d.y == y then
words = d.words
break
end
end
love.graphics.setColor(0,0,0)
love.graphics.print(words, x, y)
end
end
function dude_controller:spawnDude(x, y, name)
dude = {}
dude.x = x
dude.y = y
dude.w = 64
dude.h = 128
dude.words = {}
dude.name = name
table.insert(self.dudes, dude)
end
function updateWords()
if dude.name == dude1 then
dude.words = 'Hi'
elseif dude.name == dude2 then
dude.words = 'What?'
end
end