-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayerNetworkClass.lua
More file actions
245 lines (208 loc) · 6.04 KB
/
playerNetworkClass.lua
File metadata and controls
245 lines (208 loc) · 6.04 KB
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
require("animationDynamicClass")
require("common")
Player = Animation:new()
function Player:new(id, x, y, shape, width, height, baseSpeed, maxSpeed, angle, action, obstacles)
local o = Animation:new(id, x, y, shape, width, height, baseSpeed, maxSpeed, angle, action, obstacles)
setmetatable(o, self)
self.__index = self
self.opponents = {}
self.socket = require("socket")
self.client = nil
self.udp = nil
self.address = nil
self.port = nil
self.updateRate = 0.1
self.t = 0
o.type = "player"
o:setIsMovable(true) -- players are movable by default
self.isHurt = false
self.shoot = false
self.projAngle = 0
self.projStartCoords = {0, 0}
return o
end
function Player:storeProj(angle, x, y)
self.shoot = true
self.projAngle = angle
self.projStartCoords = {x, y}
end
function Player:connect(address, port)
self.address = address
self.port = port
local osString = love.system.getOS()
-- print(osString)
local timeStamp = tostring(os.time())
local dg = string.format("%s %d %s %s", 'init', timeStamp, 'test_match', self.id)
local udp = self.socket.udp()
udp:settimeout(5)
if osString == "Linux" then
-- local ip = assert(self.socket.dns.toip(self.address)) -- TODO: this is not needed for non localhost ip! WTF?
udp:setsockname("*", 0) -- bind on any availible port and local(?) ip address.
-- udp:sendto(dg, ip, self.port)
udp:sendto(dg, self.address, self.port)
else
udp:setpeername(self.address, self.port)
udp:send(dg)
udp:setpeername("*")
end
self.udp = udp
end
function Player:networkUpdate(dt)
if not self.isDead then
self.t = self.t + dt
if not self.client then
data, fromIp, fromPort = self.udp:receivefrom()
self.udp:setpeername(fromIp, fromPort)
self.client = self.udp
self.client:settimeout(0)
elseif self.t > self.updateRate then
self:handleSelfUpdate()
else
local update = self.client:receive()
local opponentUpdate = {}
if update then
for w in update:gmatch("%S+") do
table.insert(opponentUpdate, w)
end
self:handleOpponentUpdate(opponentUpdate)
end
end
return 2
else
return 1
end
end
function Player:update(dt, obstacles, direction)
-- if self.statusB == 1
-- and self.platform.y ~= nil
-- then
-- if self.y + self.height + 7.5 > self.platform.y then
-- self.y = self.y - 0.1
-- end
-- end
if self.isHurt == true then
self:setAnimation(3)
self.animation.currentTime = self.animation.currentTime + dt
if self.animation.currentTime >= self.animation.duration then
self.animation.currentTime = self.animation.currentTime - self.animation.duration
self.isDead = true
end
end
Animation.update(self, dt, obstacles, direction, callbacks)
end
function Player:test()
print("player network test")
end
function Player:updateOpponents(opponents)
self.opponents = opponents or {}
end
function Player:setUpdateData(
x,
y,
width,
height,
baseSpeed,
maxSpeed,
action,
angle,
time,
fixX,
fixY,
throwAngleTimeMultiplier,
statusL,
statusT,
statusR,
statusB,
direction,
isJump,
shoot,
projAngle,
projStartCoordsX,
projStartCoordsY,
platformX,
platformY,
platformWidth,
platformHeight
)
self.shoot = numToBool(tonumber(shoot))
self.projAngle = tonumber(projAngle)
self.projStartCoords = {tonumber(projStartCoordsX), tonumber(projStartCoordsY)}
-- TODO: for some reason I can't use ":" without self here (WTF?)
Animation.setUpdateData(self, x, y, width, height, baseSpeed, maxSpeed, action, angle, time, fixX, fixY, throwAngleTimeMultiplier, statusL, statusT, statusR, statusB, direction, isJump, platformX, platformY, platformWidth, platformHeight)
end
function Player:handleSelfUpdate()
local timeStamp = tostring(os.time())
local dg = string.format("%s %d %f %f %f %f %f %f %s %f %f %f %f %f %f %f %f %f %s %f %f %f %f %f", 'move', timeStamp,
-- self.id, -- TODO: handle me
self.x,
self.y,
self.width,
self.height,
self.baseSpeed,
self.maxSpeed,
self.action,
self.angle,
self.time,
self.fixX,
self.fixY,
self.throwAngleTimeMultiplier,
self.statusL,
self.statusT,
self.statusR,
self.statusB,
self.direction,
boolToNum(self.isJump),
boolToNum(self.shoot),
self.projAngle,
self.projStartCoords[1],
self.projStartCoords[2]
)
dg = dg .. platformToDg(self.platform)
self.client:send(dg)
self.t = self.t - self.updateRate
self.shoot = false
end
function Player:handleOpponentUpdate(update)
self.opponents[update[2]]:setUpdateData(
update[3],
update[4],
update[5],
update[6],
update[7],
update[8],
update[9],
update[10],
update[11],
update[12],
update[13],
update[14],
update[15],
update[16],
update[17],
update[18],
update[19],
update[20],
update[21],
update[22],
update[23],
update[24],
update[25],
update[26],
update[27],
update[28],
update[29]
)
end
function Player:handleProj()
self.isHurt = true
end
function platformToDg(platform)
local string = ""
if platform.x ~= nil and platform.y ~= nil and platform.width ~= nil and platform.height ~= nil then
string = string .. string.format(" %f", platform.x)
string = string .. string.format(" %f", platform.y)
string = string .. string.format(" %f", platform.width)
string = string .. string.format(" %f", platform.height)
end
return string
end