2019-03-16 12:27:38 +01:00
|
|
|
-- scenes.lua :: the scene object, that aim to give a better control to the engine
|
|
|
|
-- to the different scene, without having to call too much boilerplate
|
|
|
|
|
|
|
|
--[[
|
|
|
|
Copyright © 2019 Kazhnuz
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
]]
|
|
|
|
|
2019-03-16 14:56:46 +01:00
|
|
|
local cwd = (...):gsub('%.scenes$', '') .. "."
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
local Scene = Object:extend()
|
2019-04-11 17:03:53 +02:00
|
|
|
|
|
|
|
local Assets = require(cwd .. "assets")
|
|
|
|
local MenuSystem = require(cwd .. "menusystem")
|
|
|
|
|
|
|
|
-- INIT FUNCTIONS
|
|
|
|
-- Initialize and configure the scene
|
2019-03-16 12:27:38 +01:00
|
|
|
|
|
|
|
function Scene:new()
|
|
|
|
self.mouse = {}
|
|
|
|
self.mouse.x, self.mouse.y = core.screen:getMousePosition()
|
|
|
|
|
|
|
|
self.assets = Assets()
|
2019-04-20 17:54:01 +02:00
|
|
|
self.menusystem = MenuSystem(self)
|
2019-04-07 17:51:37 +02:00
|
|
|
self.sources = core.input:getSources()
|
2019-03-16 15:03:35 +01:00
|
|
|
|
2019-09-07 21:51:20 +02:00
|
|
|
self.inputLocked = true
|
|
|
|
self.inputLockedTimer = 2
|
|
|
|
self:flushKeys()
|
2019-04-01 08:25:02 +02:00
|
|
|
|
2019-05-28 19:28:05 +02:00
|
|
|
self:initWorld()
|
|
|
|
|
2019-03-16 15:03:35 +01:00
|
|
|
self:register()
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function Scene:register()
|
2019-03-16 15:04:55 +01:00
|
|
|
core.scenemanager:setScene(self)
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
2019-04-11 17:03:53 +02:00
|
|
|
function Scene:clear()
|
|
|
|
-- TODO: send automatic cleanups to the different elements of the scene
|
|
|
|
end
|
|
|
|
|
|
|
|
-- UPDATE FUNCTIONS
|
|
|
|
-- Handle stuff that happens every steps
|
|
|
|
|
2019-06-13 18:33:29 +02:00
|
|
|
function Scene:updateStart(dt)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
function Scene:update(dt)
|
|
|
|
-- Empty function, is just here to avoid crash
|
|
|
|
end
|
|
|
|
|
2019-06-13 18:33:29 +02:00
|
|
|
function Scene:updateEnd(dt)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-05-30 13:27:41 +02:00
|
|
|
function Scene:updateWorld(dt)
|
2019-05-30 13:37:55 +02:00
|
|
|
if (self.world ~= nil) and (self.world.isActive) then
|
2019-05-30 13:27:41 +02:00
|
|
|
self.world:update(dt)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-11 17:03:53 +02:00
|
|
|
-- MOUSE FUNCTIONS
|
|
|
|
-- Make the scene support the pointer
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
function Scene:mousemoved(x, y, dx, dy)
|
|
|
|
-- Empty function, is just here to avoid crash
|
|
|
|
end
|
|
|
|
|
|
|
|
function Scene:mousepressed( x, y, button, istouch )
|
|
|
|
-- Empty function, is just here to avoid crash
|
|
|
|
end
|
|
|
|
|
2019-05-28 19:28:05 +02:00
|
|
|
-- WORLD FUNCTIONS
|
|
|
|
-- Basic functions to manage the world
|
|
|
|
|
|
|
|
function Scene:initWorld()
|
|
|
|
self.world = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
function Scene:registerWorld(world)
|
|
|
|
self.world = world
|
|
|
|
end
|
|
|
|
|
2019-04-14 19:45:52 +02:00
|
|
|
-- KEYBOARD FUNCTIONS
|
|
|
|
-- Add send keys functions to the scene
|
|
|
|
|
2019-04-14 19:46:39 +02:00
|
|
|
function Scene:keypressed( key, scancode, isrepeat )
|
2019-04-14 19:45:52 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-04-14 19:46:39 +02:00
|
|
|
function Scene:keyreleased( key )
|
2019-04-14 19:45:52 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-04-11 17:03:53 +02:00
|
|
|
-- DRAW FUNCTIONS
|
|
|
|
-- Draw the scene and its content
|
|
|
|
|
2019-06-13 18:33:29 +02:00
|
|
|
function Scene:drawStart()
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
function Scene:draw()
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-06-13 18:33:29 +02:00
|
|
|
function Scene:drawEnd()
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-05-30 13:27:41 +02:00
|
|
|
function Scene:drawWorld(dt)
|
|
|
|
if (self.world ~= nil) then
|
|
|
|
self.world:draw()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-14 19:45:52 +02:00
|
|
|
-- INPUT FUNCTIONS
|
2019-04-11 17:03:53 +02:00
|
|
|
-- Handle inputs from keyboard/controllers
|
2019-03-16 12:27:38 +01:00
|
|
|
|
2019-04-01 08:25:02 +02:00
|
|
|
function Scene:setKeys()
|
|
|
|
if (self.inputLocked) then
|
|
|
|
self.inputLockedTimer = self.inputLockedTimer - 1
|
|
|
|
if (self.inputLockedTimer <= 0 ) then
|
2019-04-01 08:27:18 +02:00
|
|
|
self.inputLocked = false
|
2019-04-01 08:25:02 +02:00
|
|
|
end
|
2019-09-07 21:51:20 +02:00
|
|
|
self.menusystem.keys = self:getKeys(1)
|
2019-04-01 08:25:02 +02:00
|
|
|
else
|
2019-04-07 17:51:37 +02:00
|
|
|
self.sources = core.input.sources
|
2019-09-07 21:51:20 +02:00
|
|
|
self.menusystem.keys = self:getKeys(1)
|
2019-04-01 08:25:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-07 17:42:46 +02:00
|
|
|
function Scene:getKeys(sourceid)
|
2019-04-07 17:51:37 +02:00
|
|
|
if sourceid == nil then
|
2019-07-24 11:21:32 +02:00
|
|
|
core.debug:warning("scene", "no sourceid detected, will default to 1")
|
2019-04-07 17:51:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local sourceid = sourceid or 1
|
2019-04-01 08:25:02 +02:00
|
|
|
if (self.inputLocked) then
|
2019-09-07 21:51:20 +02:00
|
|
|
core.debug:print("scene", "inputs are currently locked")
|
2019-04-01 08:25:02 +02:00
|
|
|
return core.input.fakekeys
|
2019-09-07 21:51:20 +02:00
|
|
|
else
|
|
|
|
return self.sources[sourceid].keys
|
2019-04-01 08:25:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-07 21:51:20 +02:00
|
|
|
function Scene:flushKeys(timer)
|
2019-05-30 13:49:19 +02:00
|
|
|
core.input:flushKeys()
|
2019-09-07 21:51:20 +02:00
|
|
|
self.sources = core.input.sources
|
|
|
|
self.inputLockedTimer = timer or 2
|
2019-04-01 08:25:02 +02:00
|
|
|
self.inputLocked = true
|
2019-03-16 12:27:38 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return Scene
|