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