summaryrefslogtreecommitdiff
path: root/main.lua
blob: 392fa05f4a3b3626366fefdf5408bac440f37f1b (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
local world = require "world"
local obj = require "obj"

obj.load_types()
line = love.graphics.line
set_color = love.graphics.setColor

line_width = 0.4
local cam = {
	x = 0, y = 0,
	scale = 1.1,
	panning = false,
}

for i = 1, 100 do
	obj.new("test", {math.random() * 1000, math.random() * 1000}, {
		avel = math.random() * 0.5 - 0.25,
		vel = {math.random() * 0.5 - 0.25, math.random() * 0.5 - 0.25}
	})
end

obj.new("test", {0, 0})

local function window_scale()
	local w, h = love.graphics.getDimensions()
	return 256 / math.min(w, h)
end

local function view_scale()
	return window_scale() / cam.scale
end

local function view_dimensions()
	local w, h = love.graphics.getDimensions()
	local scale = view_scale()
	return w * scale, h * scale
end

local function window_transform()
	local scale = window_scale()
	local trans = love.math.newTransform(0, 0, 0, 1/scale, 1/scale)
	return trans
end

local function view_transform()
	local w, h = view_dimensions()
	local trans = window_transform()
	trans:scale(cam.scale, cam.scale)
	trans:translate(-cam.x + w/2, -cam.y + h/2)
	return trans
end

function love.draw()
	love.graphics.clear(0, 0, 0)
	local w, h = view_dimensions()
	local cx1, cy1 = cam.x - w/2, cam.y - h/2
	local cx2, cy2 = cam.x + w/2, cam.y + h/2

	love.graphics.applyTransform(view_transform())
	-- the line thickness will scale according to the zoom amount. counteract this.
	love.graphics.setLineWidth(line_width / cam.scale)
	-- draw a grid
	set_color(0.1, 0.1, 0.1)
	for x = cx1 - cx1%64, cx2, 64 do
		for y = cy1 - cy1%64, cy2, 64 do
			line(x, y, x + world.chunk_size, y)
			line(x, y, x, y + world.chunk_size)
		end
	end
	-- draw all possibly visible objects
	for o in world.in_box(
			cx1 - obj.max_size, cy1 - obj.max_size,
			cx2 + obj.max_size, cy2 + obj.max_size) do
		set_color(1, 1, 1)
		o:draw()
	end
	love.graphics.origin()

	love.graphics.applyTransform(window_transform())
	love.graphics.setLineWidth(line_width)
	set_color(1, 1, 1)
end

function love.update()
	for _, o in pairs(world.objects) do
		o:tick()
	end
end

function love.mousepressed(_, _, button)
	if button == 2 then
		cam.panning = true
	end
end

function love.mousereleased(_, _, button)
	if button == 2 then
		cam.panning = false
	end
end

function love.mousemoved(_, _, dx, dy)
	if cam.panning then
		local scale = view_scale()
		dx, dy = dx * scale, dy * scale
		cam.x = cam.x - dx
		cam.y = cam.y - dy
	end
end

function love.wheelmoved(_, y)
	cam.scale = math.min(math.max(cam.scale + (y * 0.1), 0.25), 4)
end