-- Namespace. local Game = {} -- Import. Game.newRoom = require "room" Game.newPlayer = require "player" Game.newProp = require "prop" Game.newMessage = require "message" Game.quads = require "quads" -- Various children. Game.sprites = {} Game.rooms = {} Game.props = {} Game.player = nil Game.message = nil Game.delay = 0.08 Game.initial = Game.delay -- Aliases for window size getters. Game.getWidth = love.graphics.getWidth Game.getHeight = love.graphics.getHeight -- Loads sprites atlases. function Game.load() -- Font Game.font = love.graphics.newImageFont("assets/font.png", " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.,:;-_/\\!@#$%^&*?=+~`|'\"()[]{}<>", 1) Game.font:setLineHeight(0.875) love.graphics.setFont(Game.font) -- Spritesheets Game.sprites.player = love.graphics.newImage("assets/player.png") Game.sprites.room = love.graphics.newImage("assets/rooms.png") Game.sprites.door = love.graphics.newImage("assets/door.png") Game.sprites.console = love.graphics.newImage("assets/console.png") Game.sprites.fridge = love.graphics.newImage("assets/fridge.png") Game.sprites.outside = love.graphics.newImage("assets/outside.png") Game.sprites.wand = love.graphics.newImage("assets/wand.png") -- Create new player Game.player = Game.newPlayer() end -- Based on current window sizes returns proper scale for elements. function Game.getScale() local w,h = Game.getWidth(), Game.getHeight() local scale_w = math.ceil(w / 100) local scale_h = math.ceil(h / 75) return math.max(scale_w, scale_h) end -- Get current offsets. function Game.getOffsets() local scale, width, height, mouse_x, mouse_y, player_x, offset_x, offset_y scale = Game.getScale() width, height = Game.getWidth(), Game.getHeight() mouse_x, mouse_y = love.mouse.getPosition() player_x = Game.player.x * scale mouse_x = (mouse_x - width / 2)*.02 mouse_y = (mouse_y - height / 2)*.01 offset_x = width/2 - player_x - mouse_x offset_y = (height - (32 * scale))/2 - mouse_y return offset_x, offset_y end -- Returns mouse position in game world. function Game.getMousePosition(x, y) local scale = Game.getScale() if not x and not y then local x, y = love.mouse.getPosition() end local ox, oy = Game.getOffsets() x = math.floor((x - ox)/scale) y = math.floor((y - oy)/scale) return x, y end -- Returns double true if given position is inside any Room. function Game.inRange(x, y) local horizontal, vertical = false, false if y > 0 and y < 32 then vertical = true end local width = 0 for _,room in pairs(Game.rooms) do if room.discovered then width = width + room:getDimensions() end end if x > 2 and x < width-1 then horizontal = true end return horizontal, vertical end -- Tests if given position is inside any Prop. function Game.testPosition(x, y) for _,prop in pairs(Game.props) do local test = prop:testPosition(x, y) if test then return test end end end -- Returns player. The only one. function Game.getPlayer() return Game.player end -- Returns current message, beep. function Game.getMessage() return Game.message end -- Inserts new Room. function Game.insertRoom(width, height) local x, y local last = Game.rooms[#Game.rooms] if last then local lw, lh = last:getDimensions() local lx, ly = last:getPosition() x, y = lx + lw, ly end local room = Game.newRoom():setDimensions(width, height):setPosition(x, y) table.insert(Game.rooms, room) return room end function Game.insertDoor(room) local x, y = room:getPosition() local w, h = room:getDimensions() local door = Game.insertProp():setPosition(x+w-5,10):setDimensions(10,18) return door end -- Inserts new Prop. function Game.insertProp(prop) local prop = prop if not prop then prop = Game.newProp() end table.insert(Game.props, prop) return prop end -- Sets given or empty message as current message. function Game.setMessage(message) local message = message if not message then message = Game.newMessage() end Game.message = message return message end return Game