-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.lua
279 lines (235 loc) · 8.58 KB
/
user.lua
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
local console = require("lib.console")
local User = {}
User.__index = User
local AUTO_PROJECTILE_DELAY = 0.10
local function init(_self, payload)
local m0_x, m0_y = 0, 0
local m_x, m_y = love.mouse.getPosition()
local angle = lume.angle(m0_x, m0_y, m_x, m_y)
local distance = lume.distance(m0_x, m0_y, m_x, m_y)
local dx, dy = lume.vector(angle, distance)
local uuid = lume.uuid()
local d_width, d_height = love.window.getDesktopDimensions()
local w_width, w_height = love.graphics.getDimensions()
local self = {
debug = false,
fullscreen = false,
m_x = m_x,
m_y = m_y,
m_dx = dx,
m_dy = dy,
m_angle = angle,
m_distance = distance,
uuid = uuid,
is_bootstrapped = false,
client = nil,
payload = payload,
player = nil,
d_width = d_width,
d_height = d_height,
w_width = w_width,
w_height = w_height,
scores = {},
auto_projectile_timer = 0.0
}
setmetatable(self, User)
local on_input = function(txt)
if love.keyboard.isDown("lshift") then
console.eval(txt)
else
self.client:send_msg(txt)
end
end
console.initialize({on_input = on_input})
return self
end
setmetatable(User, {__call = init})
function User:update(dt)
if self.player:is_dead() or console.toggled() then return false end
local updates = {}
local dir = lfg.get_key_dir() -- FIXME: migrate logic to this module
local m0_x, m0_y = self.m_x, self.m_y
local m_x, m_y = love.mouse.getPosition()
if dir then
updates.cdir = lfg.ndirs[dir]
-- TODO: guarantee self.player is set
updates.state = self.player.STATES.run
end
if m0_x ~= m_x or m0_y ~= m_y then
local angle = lume.angle(m0_x, m0_y, m_x, m_y)
local distance = lume.distance(m0_x, m0_y, m_x, m_y)
local dx, dy = lume.vector(angle, distance)
-- TODO: should we send mouse movement to server?
-- at the very least need to use mouse dir to send dir player facing
--updated = true
--updates.m_x, updates.m_y = m_x, m_y
--updates.m_dx, updates.m_dy = dx, dy
-- TODO: move this into self.mouse = {...} ?
self.m_x = m_x
self.m_y = m_y
self.m_dx = dx
self.m_dy = dy
self.m_angle = angle
self.m_distance = distance
end
-- send player movement
if next(updates) ~= nil then
self.client:send_player_update(self, updates)
end
-- check if mouse is down and no button updates
self.auto_projectile_timer = self.auto_projectile_timer + dt
local mouse_down = love.mouse.isDown(1, 2)
if (self.auto_projectile_timer > AUTO_PROJECTILE_DELAY and
(mouse_down or self.mouse_updates)) then
-- don't do diff as we let this get big
self.auto_projectile_timer = 0.0
if (mouse_down and not (self.mouse_updates["1"]
or self.mouse_updates["2"])) then
if not self.mouse_updates then self.mouse_updates = {} end
local mx, my = love.mouse.getPosition()
self.mouse_updates["1"] = self:trigger_mouseaction(mx, my, nil)
end
-- send projectiles
if self.mouse_updates then
for button, m_info in pairs(self.mouse_updates) do
-- TODO: reenable melee attacks
-- however, one button mouse works well for trackpads
if false and button == "1" then
self.client:melee_attack(button, m_info)
elseif button == "1" or button == "2" then
m_info.spell_name = self.player.spell.name
self.client:create_projectile(m_info)
self.player:cast_spell(m_info)
end
end
self.mouse_updates = {}
end
end
end
function User:draw()
-- TODO: draw game UI
-- TODO: enable FPS and stats logic:
-- TODO: add bandwith numbers
-- TODO: add ping time
-- TODO: add server tps
-- TODO: add object stats for players/projectiles/collidables/etc
-- TODO: add toggle button for displaying these stats
if self.debug then
love.graphics.print("Current FPS: "..tostring(love.timer.getFPS()), 10, 10)
local tl_x, tl_y = lfg.map:convertPixelToTile(self:screen_x(), self:screen_y())
love.graphics.print(string.format("Current Pos: (%.2f, %.2f) <%.2f, %.2f>", self:screen_x(), self:screen_y(), tl_x, tl_y), 10, 30)
love.graphics.print(string.format("Mouse Pos: (%.2f, %.2f)", self.m_x, self.m_y), 10, 50)
local deg = (math.deg(self.m_angle) + 360) % 360
love.graphics.print(string.format("Angle[%.2f]: %.2f {%.2f} {[%i]}", self.m_distance, self.m_angle, math.deg(self.m_angle), deg), 10, 70)
end
if self.player:is_dead() then
-- TODO: properly fetch the respawn value timer
love.graphics.print(string.format("YOU HAVE DIED!!! Respawning in %i...", 7 - self.player.respawn_timer), 500, 500)
end
local count = 1
love.graphics.print("Scores:", 10, 100)
for _,score in ipairs(self.scores) do
love.graphics.print(string.format("%8.8s | %i", score.name, score.score ), 10, 100 + count * 20)
count = count + 1
end
console.draw(not console.toggled())
end
function User:bootstrap_player(player)
assert(player)
self.player = player
self.is_bootstrapped = true
end
function User:bootstrap(client)
log("BOOTSTRAPPING USER[%s]{%s}: %s", self.uuid, self.client, self.is_bootstrapped)
if not self.client then
self.client = client
client:create_player(self, self.payload)
end
return self.is_bootstrapped
end
function User:mousepressed(m_x, m_y, button)
if console.toggled() then
console.mousepressed(m_x, m_y, button)
else
button = tostring(button)
if not self.mouse_updates then self.mouse_updates = {} end
self.mouse_updates[button] = self:trigger_mouseaction(m_x, m_y, button)
end
end
function User:trigger_mouseaction(m_x, m_y, _button)
if(not self.player) then return end
local w_x = math.floor(love.graphics.getWidth() / 2)
local w_y = math.floor(love.graphics.getHeight() / 2)
local angle = lume.angle(w_x, w_y, m_x, m_y)
local r_angle = angle - math.pi / 4
local distance = lume.distance(w_x, w_y, m_x, m_y)
local dx, dy = lume.vector(r_angle, distance)
local n_dx = dx / distance
local n_dy = dy / distance
local dir = lfg.ndirs[lfg.angle_to_dir(angle)]
-- last update wins
return {
x = self:x(),
y = self:y(),
dx = n_dx,
dy = n_dy,
w_x = w_x,
w_y = w_y,
angle = angle,
distance = distance,
cdir = dir,
puid = self:puid(),
}
end
function User:keypressed(key, scancode, isrepeat)
if key == "q" and love.keyboard.isDown("lctrl", "rctrl", "capslock") then
love.event.quit()
end
if console.toggled() then
if key == "escape" then
console.off()
else
console.keypressed(key, scancode, isrepeat)
end
return
elseif key == "return" then
return console.on()
end
if isrepeat then return false end
if scancode == "f1" then self.debug = not self.debug end
if scancode == "f2" then
self.fullscreen = not self.fullscreen
local fst = {fullscreen=self.fullscreen, fullscreentype="desktop"}
--love.window.setFullscreen(self.fullscreen, "desktop")
if self.fullscreen then
love.window.setMode(self.d_width, self.d_height, fst)
lfg.map:resize(self.d_width, self.d_height)
else
love.window.setMode(self.w_width, self.w_height, fst)
lfg.map:resize(self.w_width, self.w_height)
end
end
if scancode == "f4" then
self.client:send_player_respawn(self)
end
end
function User.textinput(_self, ...)
if console.toggled() then console.textinput(...) end
end
function User:update_scores(scores, _tick)
self.scores = scores
end
function User.print(_self, ...)
console.print(...)
end
function User:x() return self.player.x end
function User:y() return self.player.y end
function User:screen_x() return self.player:screen_x() end
function User:screen_y() return self.player:screen_y() end
function User:ox() return self.player.ox end
function User:oy() return self.player.oy end
function User:w() return self.player.w end
function User:h() return self.player.h end
function User:cdir() return self.player.cdir end
function User:puid() return self.player.uuid end
return User