summaryrefslogtreecommitdiff
path: root/main.lua
blob: 2540c016b6250dc6c9b68b8390a814ea451d8512 (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
local game = require 'game'
local physics = require 'physics'
local Bolvis = require 'Bolvis'
local stuff = require 'stuff'

local camera

function love.load()
	love.mouse.setRelativeMode(true)

	local bolvis = Bolvis({960, 540})
	camera = bolvis.camera
	
	stuff.Teapot({400, 540}, 0)
	stuff.Apioform({600, 540}, 0)
	stuff.TestPlatform({960, 300})
	stuff.TestPlatform({960, 700})
	stuff.TestPlatform({430, 700})
	stuff.TestPlatform({1490, 700})
end

local function screen_transform()
	local ww, wh = love.graphics.getDimensions()
	local sw, sh = 1920, 1080
	local scalex, scaley = ww / sw, wh / sh
	local scale
	if scalex * sh < wh then
		scale = scalex
	else
		scale = scaley
	end
	local dimx, dimy = sw * scale, sh * scale
	local x, y = ww / 2 - dimx / 2, wh / 2 - dimy / 2
	return love.math.newTransform(x, y, 0, scale, scale)
end

local to_draw
local events = {}
local handlers = {}

local function event(name)
	love[name] = function(...)
		table.insert(events, {type = name, ...})
	end
end

function love.update(dt)
	to_draw = {}
	physics.world:update(dt, 20, 20)
	for _, e in ipairs(events) do
		if handlers[e.type] then
			handlers[e.type](unpack(e))
		end
	end
	for o in pairs(game.all_objects) do
		for _, e in ipairs(events) do
			if o[e.type] then
				o[e.type](o, unpack(e))
			end
		end
		o:update(dt)
		if o:visible() then
			table.insert(to_draw, o)
		end
	end
	events = {}
end

function love.draw()
	love.graphics.applyTransform(screen_transform())
	camera:use()
	table.sort(to_draw, function(a, b) return a.z < b.z end)
	for _, o in ipairs(to_draw) do
		o:draw()
	end
end

event "mousemoved"
event "mousepressed"
event "mousereleased"
event "keypressed"
event "keyreleased"

function handlers.keypressed(key)
	if key == 'tab' and
		(love.keyboard.isDown 'ralt' or love.keyboard.isDown 'lalt') then
		love.mouse.setRelativeMode(false)
	end
end

function handlers.mousepressed()
	love.mouse.setRelativeMode(true)
end