summaryrefslogtreecommitdiff
path: root/game.lua
blob: 1146f1fcebea76138b5b5f1c0791043ff7e13254 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
-- 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