summaryrefslogtreecommitdiff
path: root/client/main.lua
blob: 769fccf7234811b36708b42307647128041789ca (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
local enet = require"enet"
local json = require"common.dkjson"

-- local SERVER_HOSTNAME = "ubq323.website"
local SERVER_HOSTNAME = "localhost"
local PLAYER_SIZE = 0.7

local local_player = nil

local drawing = require"drawing"
local coords = require"common.coords"
local Pos = coords.Pos
local camera = require"camera".Camera:make()
local Chunk = require"common.chunk".Chunk
local util = require"util"
local Map = require"common.map".Map

-- local pprint=require"common.pprint"
-- pprint.setup{show_all=true}


math.randomseed(os.time())

local map = Map:make()

local host,peer

_G.debugmode = false

function love.keypressed(key,scancode,isrepeat)
	if scancode == "f3" then _G.debugmode = not _G.debugmode end
end

local function draw_player(pl,islocal)
	local hplsz = PLAYER_SIZE/2
	love.graphics.setColor(pl.color)
	love.graphics.rectangle("fill",pl.pos.x-hplsz,pl.pos.y-hplsz,PLAYER_SIZE,PLAYER_SIZE)
	-- love.graphics.print(tostring(pl.id),pl.pos.x,pl.pos.y)
	if islocal then
		love.graphics.setLineWidth(0.01)
		love.graphics.setColor(0.5,0,0)
		love.graphics.rectangle("line",pl.pos.x-hplsz,pl.pos.y-hplsz,PLAYER_SIZE,PLAYER_SIZE)
	end
end

local remote_players = {}

local function update_local_player(pl,dt)
	local SPEED = 10 -- units/sec
	if love.keyboard.isScancodeDown("lshift") then SPEED = 100 end
	local function kd(code)
		if love.keyboard.isScancodeDown(code) then return 1 else return 0 end
	end
	local dx = kd"d"-kd"a"
	local dy = kd"s"-kd"w"

	if dx == 0 and dy == 0 then
		pl.pos_dirty = false
		return
	end

	if dx ~= 0 and dy ~= 0 then
		dx = dx * 0.5
		dy = dy * (math.sqrt(3)/2)
	end

	pl.pos.x = pl.pos.x + SPEED * dt * dx
	pl.pos.y = pl.pos.y + SPEED * dt * dy
	pl.pos_dirty = true


end

local function sync_local_player(pl)
	-- send updated info about local player to server
	if pl.pos_dirty then
		peer:send(json.encode{t="ppos",x=pl.pos.x,y=pl.pos.y})
	end
end

local function send_settile(hpos,tile)
	peer:send(json.encode{t="settile",q=hpos.q,r=hpos.r,tile=tile})
end


function love.update(dt)
	if local_player then
		update_local_player(local_player,dt)
		if love.keyboard.isScancodeDown"q" then camera.zoom = camera.zoom*1.05 end
		if love.keyboard.isScancodeDown"e" then camera.zoom = camera.zoom/1.05 end
		camera.zoom = math.max(2.25,math.min(50,camera.zoom))
		sync_local_player(local_player)
	end

	local mh = camera:screen_to_world(Pos:make(love.mouse.getPosition())):to_hex():round()
	if false== map:at(mh) and love.mouse.isDown(1) then
		print("place at",mh)
		map:set_at(mh,true)
		-- print(mh,true)
		send_settile(mh,true)
	elseif map:at(mh) and love.mouse.isDown(2) then
		map:set_at(mh,false)
		-- print(mh,false)
		send_settile(mh,false)
	end

	-- load chunks near player
	if local_player then
		local player_cp = local_player.pos:to_hex():containing_chunk()
		for du = -1,1 do
			for dv = -1,1 do
				local cp = player_cp+coords.ChunkPos:make(du,dv)
				if map:chunk(cp) == nil then
					print("sending chunk request",cp)
					map:mark_chunk_loading(cp)
					peer:send(json.encode{t="reqchunk",u=cp.u,v=cp.v})
				end
			end
		end
	end
	

	repeat
		local ev = host:service()
		if ev and ev.type == "receive" then
			-- print(ev.data)
			local j = json.decode(ev.data)
			local op = j.t
			if op == "join" then
				local pl = j.pl
				remote_players[pl.id] = {pos=coords.Pos:make(pl.x,pl.y),color=pl.color,id=pl.id}
			elseif op == "leave" then
				local id = j.id
				remote_players[id]=nil
			elseif op == "move" then
				local id,x,y = j.id,j.x,j.y
				assert(remote_players[id],"wheeze "..id)
				remote_players[id].pos.x = x
				remote_players[id].pos.y = y
			elseif op == "you" then
				local pl = j.pl
				local_player = {pos=coords.Pos:make(pl.x,pl.y),color=pl.color,id=pl.id}
			elseif op == "chunk" then
				local ch = Chunk.from_packet_data(j)
				map:add_chunk(ch)
			elseif op == "settile" then
				local h = coords.Hex:make(j.q,j.r)
				map:set_at(h,j.tile)
			end
		end
	until not ev
end

function love.draw()
	love.graphics.clear(1,1,1)
	love.graphics.origin()
	if local_player then
		camera.pos = local_player.pos
	end
	camera:apply_trans()

	drawing.draw_map(camera,map)

	if local_player then
		draw_player(local_player,true)
	end
	for _,pl in pairs(remote_players) do
		draw_player(pl)
	end

	love.graphics.setColor(1,0,0)
	love.graphics.rectangle("fill",0,0,1,1)

	local sm = Pos:make(love.mouse.getPosition())
	local wm = camera:screen_to_world(sm)
	local hm = wm:to_hex()

	love.graphics.origin()
	if _G.debugmode and local_player then
		util.print_good({
			 "ms "..tostring(sm),
			 "mw "..tostring(wm),
			 "mh "..tostring(hm).." "..tostring(hm:round()),
			 "-",
			 "pw "..tostring(local_player.pos),
			 "ph "..tostring(local_player.pos:to_hex()).." "..tostring(local_player.pos:to_hex():round()),
			 "-",
			 "voob "..tostring(camera.zoom),
			 "-",
			 "fps "..tostring(love.timer.getFPS()),
		 },10,10)
	 end
end

function love.load()
	require"profile".start(10,io.open("./trace","w"))
	host = enet.host_create()
	peer = host:connect(SERVER_HOSTNAME..":8473")
end

function love.quit()
	require"profile".stop()
	peer:disconnect()
	host:flush()
end