summaryrefslogtreecommitdiff
path: root/client/drawing.lua
blob: e3dc751a853ad6973596ba6249bf1af622f6a064 (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 coords=require"common.coords"
local Pos = coords.Pos
local chunk = require"common.chunk"
local CHUNK_SIZE = require"common.constants".CHUNK_SIZE

local tau=math.pi*2

local corners = {}
for i=0,5 do
	local angle = tau*(i+0.5)/6
	local x = math.cos(angle)
	local y = math.sin(angle)
	table.insert(corners,x)
	table.insert(corners,y)
end

-- local function draw_hex(cpos,color)
-- 	love.graphics.push()
-- 	love.graphics.setLineWidth(0.1)
-- 	love.graphics.translate(cpos.x,cpos.y)
-- 	if color then love.graphics.setColor(color) else
-- 		love.graphics.setColor(love.math.colorFromBytes(0xe7,0x9e,0))
-- 	end
-- 	love.graphics.polygon("fill",corners)
-- 	love.graphics.setColor(0,0,0)
-- 	love.graphics.polygon("line",corners)
-- 	love.graphics.pop()
-- end

local function c(...) return {love.math.colorFromBytes(...)} end
-- taken from breadquest
local colors = {
	c(255,64,64),	-- red
	c(255,128,0),	-- orange
	c(192,192,64),	-- yellow
	c(0,192,0),		-- green
	c(0,192,192),	-- teal
	c(64,64,255),	-- blue
	c(192,0,192),	-- purple
	c(128,128,128),	-- grey

	c(0xe7,0x9e,0), -- ubqorange
}
-- precompute sins and cosines
local _coss,_sins = {},{}
for i=0,5 do
	local angle = tau*(i+0.5)/6
	_coss[i+1] = math.cos(angle)
	_sins[i+1] = math.sin(angle)
end

local zthr0 = 2.7
local zthr1 = 6
local _corners = {}
local function draw_hex(cpos,color,zoom)
	local cx,cy = cpos.x,cpos.y
	for i=1,6 do
		local x = cx + _coss[i]
		local y = cy + _sins[i]
		_corners[2*i-1] = x
		_corners[2*i  ] = y
	end
	-- love.graphics.setColor(love.math.colorFromBytes(0xe7,0x9e,0))
	love.graphics.setColor(color)
	love.graphics.polygon("fill",_corners)
	if zoom > zthr0 then
		love.graphics.setLineWidth(0.1)
		love.graphics.setColor(0,0,0,zoom>zthr1 and 1 or (zoom-zthr0)/(zthr1-zthr0))
		love.graphics.polygon("line",_corners)
	end
end

local function draw_map(camera,map)
	local tl,br = camera:extents()
	local tlh,brh = tl:to_hex():round(), br:to_hex():round()
	local trh = coords.Pos:make(br.x,tl.y):to_hex():round()

	local _h = coords.Hex:new()
	local _p = coords.Pos:new()

	for r = tlh.r-1,brh.r+1 do
		local rowidx = r-tlh.r
		local minq = tlh.q - math.floor((rowidx+1)/2)
		local maxq = minq+(trh.q-tlh.q)+1
		for q = minq,maxq do
			-- local h = coords.Hex:make(q,r)
			_h:init(q,r)
			_h:to_pos(_p)
			local t = map:at(_h)
			if t ~= nil and t ~= 0 then
				draw_hex(_p,colors[t],camera.zoom)
			end
		end
	end

	if false and _G.debugmode then
		love.graphics.setColor(0,1,0)

		local function p(q,r) return coords.Hex:make(q,r):to_pos() end
		local h = CHUNK_SIZE-0.5
		local c00 = p(-0.5,-0.5)
		local c01 = p(-0.5,h)
		local c10 = p(h,-0.5)
		local c11 = p(h,h)

		love.graphics.polygon("line",
			c00.x,c00.y, c01.x,c01.y, c11.x,c11.y, c10.x, c10.y)
	end


end

return {draw_hex=draw_hex,draw_map=draw_map}