2022-03-03 20:24:10 +01:00
|
|
|
local Game = {}
|
|
|
|
|
|
|
|
local insert = table.insert
|
|
|
|
|
|
|
|
function Game:reinit()
|
|
|
|
self.gameloop = {}
|
|
|
|
self.objects = {}
|
2022-03-04 14:03:06 +01:00
|
|
|
self.scenes = require "scenes"
|
2022-03-03 20:24:10 +01:00
|
|
|
self.objects.base = require "objects/base"
|
|
|
|
self.objects.grid = require "objects/grid"
|
|
|
|
self.objects.circle = require "objects/circle"
|
2022-03-04 14:34:54 +01:00
|
|
|
self.objects.ending = require "objects/end"
|
2022-03-04 14:57:05 +01:00
|
|
|
self.objects.background = require "objects/background"
|
2022-03-03 20:24:10 +01:00
|
|
|
self.rect = require "lib/rect"
|
|
|
|
self.maxobjects = 0
|
|
|
|
self.camerax = 0
|
2022-03-04 15:19:16 +01:00
|
|
|
self.debug = true
|
2022-03-03 20:24:10 +01:00
|
|
|
self.cameray = 0
|
|
|
|
self.floatcamerax = 0
|
|
|
|
self.floatcameray = 0
|
|
|
|
self.assets = {} -- Caching loaded images
|
|
|
|
self:initInputs()
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:changesize(width,height)
|
|
|
|
self.WIDTH = width
|
|
|
|
self.HEIGHT = height
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:movecamera(movex,movey)
|
|
|
|
self.floatcamerax = self.floatcamerax + movex
|
|
|
|
self.floatcameray = self.floatcameray + movey
|
|
|
|
self.camerax = math.floor(self.floatcamerax+0.5)
|
|
|
|
self.cameray = math.floor(self.floatcameray+0.5)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:setcamera(x,y)
|
|
|
|
self.floatcamerax = x
|
|
|
|
self.floatcameray = y
|
|
|
|
self.camerax = math.floor(self.floatcamerax+0.5)
|
|
|
|
self.cameray = math.floor(self.floatcameray+0.5)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:initInputs()
|
|
|
|
self.inputs = {}
|
|
|
|
self.inputs.deadzone = 0.4
|
|
|
|
self.inputs.keys = {}
|
|
|
|
self.inputs.buttons = {}
|
|
|
|
self.inputs.axis= {}
|
|
|
|
self.inputs.actions = {}
|
|
|
|
for i,v in pairs({"up","down","left","right","menu","valid"}) do
|
|
|
|
local action = {}
|
|
|
|
action.keys = {}
|
|
|
|
action.buttons = {}
|
|
|
|
action.axis = {}
|
|
|
|
self.inputs.actions[v] = action
|
|
|
|
action.timer = 0
|
|
|
|
action.lastpress = 99
|
|
|
|
end
|
|
|
|
self.inputs.actions.valid.keys = {"space"}
|
|
|
|
self.inputs.actions.valid.buttons = {"a"}
|
|
|
|
self.inputs.actions.up.keys = {"up","w"}
|
|
|
|
self.inputs.actions.up.buttons = {"dpup"}
|
|
|
|
self.inputs.actions.up.axis = {{"lefty",-1}}
|
|
|
|
self.inputs.actions.down.keys = {"down","s"}
|
|
|
|
self.inputs.actions.down.buttons = {"dpdown"}
|
|
|
|
self.inputs.actions.down.axis = {{"lefty",1}}
|
|
|
|
self.inputs.actions.left.keys = {"left","a"}
|
|
|
|
self.inputs.actions.left.buttons = {"dpleft"}
|
|
|
|
self.inputs.actions.left.axis = {{"leftx",-1}}
|
|
|
|
self.inputs.actions.right.keys = {"right","d"}
|
|
|
|
self.inputs.actions.right.buttons = {"dpright"}
|
|
|
|
self.inputs.actions.right.axis = {{"leftx",1}}
|
|
|
|
self.inputs.actions.menu.keys = {"escape","return"}
|
|
|
|
self.inputs.actions.menu.buttons = {"start","back"}
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:updateInputs(dt)
|
|
|
|
for i,action in pairs(self.inputs.actions) do
|
|
|
|
local pressed = false
|
|
|
|
for _,key in pairs(action.keys) do
|
|
|
|
if self.inputs.keys[key] then pressed = true end
|
|
|
|
end
|
|
|
|
for _,button in pairs(action.buttons) do
|
|
|
|
if self.inputs.buttons[button] then pressed = true end
|
|
|
|
end
|
|
|
|
for _,axis in pairs(action.axis) do
|
|
|
|
if self.inputs.axis[axis[1]]~=nil then
|
|
|
|
if self.inputs.axis[axis[1]]*axis[2]>self.inputs.deadzone then
|
|
|
|
pressed = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if pressed then
|
|
|
|
action.timer = action.timer+1
|
|
|
|
if action.timer == 1 then
|
|
|
|
action.lastpress = 0
|
|
|
|
end
|
|
|
|
else
|
|
|
|
action.timer = 0
|
|
|
|
end
|
|
|
|
action.lastpress = action.lastpress + dt
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:summon(obj)
|
|
|
|
obj.game = self
|
|
|
|
obj.id = self.maxobjects
|
|
|
|
self.maxobjects = self.maxobjects + 1
|
|
|
|
insert(self.gameloop,obj)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:findName(askedname)
|
|
|
|
local result = {}
|
|
|
|
for i,v in ipairs(self.gameloop) do
|
|
|
|
if v~=nil then
|
|
|
|
for j,name in pairs(v.classes) do
|
|
|
|
if name==askedname then
|
|
|
|
insert(result,v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:delid(objid)
|
|
|
|
self.gameloop[objid] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:delname(objName)
|
|
|
|
for i,v in ipairs(Game:findName(objName)) do
|
|
|
|
if v~=nil then
|
|
|
|
self:delid(v.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:step(dt)
|
|
|
|
self:updateInputs(dt)
|
|
|
|
for i,v in ipairs(self.gameloop) do
|
|
|
|
--Processing all the objects
|
|
|
|
if v~=nil then v:step(dt) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:newImage(path)
|
|
|
|
local image = nil
|
|
|
|
if self.assets[path]~=nil then -- If already loaded
|
|
|
|
image = self.assets[path]
|
|
|
|
else
|
|
|
|
image = love.graphics.newImage("assets/"..path) -- Load it and save it
|
|
|
|
self.assets[path] = image
|
|
|
|
end
|
|
|
|
return image
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:getSpriteDir(path,ext,func,folder)
|
|
|
|
if not ext then ext = ".png" end
|
|
|
|
if not func then func = self.newImage end
|
|
|
|
if not folder then folder = "assets/" end
|
|
|
|
local counter = 0
|
|
|
|
local result = {}
|
|
|
|
while love.filesystem.getInfo(folder..path..counter..ext) do
|
|
|
|
insert(result,func(self,path..counter..ext))
|
|
|
|
counter = counter + 1
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:getLevel(i)
|
|
|
|
local level = nil
|
|
|
|
if self.levels[i]~=nil then
|
|
|
|
level = self.levels[i]
|
|
|
|
else
|
|
|
|
level = {}
|
|
|
|
level.folder = "maps/Level "..i.."/"
|
|
|
|
local module = level.folder.."data"
|
|
|
|
level.data = require(module)
|
|
|
|
level.backgrounds = {}
|
|
|
|
level.bgsizes = {}
|
|
|
|
-- Get backgrounds
|
|
|
|
local counter = 0
|
|
|
|
while love.filesystem.getInfo(level.folder.."background"..counter..".png") do
|
|
|
|
img = love.graphics.newImage(level.folder.."background"..counter..".png")
|
|
|
|
table.insert(level.backgrounds,img)
|
|
|
|
table.insert(level.bgsizes,{img:getWidth(),img:getHeight()})
|
|
|
|
counter = counter + 1
|
|
|
|
end
|
|
|
|
self.levels[i] = level
|
|
|
|
end
|
|
|
|
return level
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:draw(screen)
|
|
|
|
draworder = {}
|
|
|
|
for i,v in ipairs(self.gameloop) do
|
|
|
|
if v~=nil then
|
|
|
|
table.insert(draworder,v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.sort(draworder, function(a,b) return a.depth< b.depth end)
|
|
|
|
for i,v in ipairs(draworder) do
|
|
|
|
--Processing all the objects
|
|
|
|
if v~=nil then
|
|
|
|
if screen~= "bottom" then
|
|
|
|
if v.draw ~= nil then
|
|
|
|
local offset = 0
|
|
|
|
if screen=="left" then offset = -love.graphics.get3DDepth() end
|
|
|
|
if screen=="right" then offset = love.graphics.get3DDepth() end
|
|
|
|
self.camerax = self.camerax + offset
|
|
|
|
v:draw()
|
|
|
|
self.camerax = self.camerax - offset
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if v.bottomdraw~=nil then v.bottomdraw() end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-03-04 15:19:16 +01:00
|
|
|
if self.debug then lg.print("OSTYPE: "..self.OSTYPE.."; ".."OS: "..self.OS.."; ".."FPS: "..tostring(love.timer.getFPS( )), 10, 10) end
|
2022-03-03 20:24:10 +01:00
|
|
|
end
|
|
|
|
|
2022-03-04 10:39:02 +01:00
|
|
|
function Game:MouseCallback(x,y,presses)
|
|
|
|
local i
|
|
|
|
for i=1,#self.gameloop do
|
|
|
|
self.gameloop[i]:MouseCallback(x,y,presses)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-03 20:24:10 +01:00
|
|
|
function Game:bint(bool) -- Convert Boolean to Integer
|
|
|
|
local result = 0
|
|
|
|
if bool then result = 1 end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
function Game:Timer(time)
|
|
|
|
local Timer = {}
|
|
|
|
Timer.maxcount = time
|
|
|
|
Timer.timer = time
|
|
|
|
Timer.loops = 0
|
|
|
|
|
|
|
|
function Timer:tick(advancement)
|
|
|
|
local result = false
|
|
|
|
self.timer = self.timer - advancement
|
|
|
|
if self.timer<0 then
|
|
|
|
result = true
|
|
|
|
self.timer = self.timer + self.maxcount
|
|
|
|
self.loops = self.loops + 1
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
function Timer:getratio()
|
|
|
|
local result = self.timer/self.maxcount
|
|
|
|
if result<0 then result = 0 end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
function Timer:reset()
|
|
|
|
self.timer = self.maxcount
|
|
|
|
self.loops = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
return Timer
|
2022-03-04 10:39:02 +01:00
|
|
|
|
2022-03-03 20:24:10 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
Game:reinit()
|
|
|
|
|
|
|
|
return Game
|