- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphysics.lua
More file actions
Latest commit
139 lines (123 loc) · 4.81 KB
/
Copy pathphysics.lua
File metadata and controls
139 lines (123 loc) · 4.81 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
physics= {
server= {},
client= {}
}
love.physics.setMeter(16)
-- categories: 1=player, 2=projectile, 3=enemy, 4=obstacle
localdefaults= {server={}, client={}}
for_, scinipairs{'server', 'client'} do
physics[sc].newDefaults=function()
return {
postUpdateQueue= {}
}
end
physics[sc].new=function(self, o)
o=oor {}
fork, vinpairs(self.newDefaults()) do
ifo[k] ==niltheno[k] =vend
end
setmetatable(o, self)
self.__index=self
returno
end
physics[sc].update=function(self, dt)
self.world:update(dt)
fori, vinpairs(self.postUpdateQueue) do
v()
self.postUpdateQueue[i] =nil
end
end
-- can't do stuff like destroying fixtures in callbacks - queue for update
physics[sc].postUpdatePush=function(self, f)
table.insert(self.postUpdateQueue, f)
end
physics[sc].draw=function(self)
ifsc=='server' then
love.graphics.setColor(1, 0, 0, 0.4)
elseifsc=='client' then
love.graphics.setColor(0, 1, 0, 0.4)
end
for_, bodyinpairs(self.world:getBodies()) do
for_, fixtureinpairs(body:getFixtures()) do
localshape=fixture:getShape()
ifshape:typeOf('CircleShape') then
localcx, cy=body:getWorldPoints(shape:getPoint())
love.graphics.circle('fill', cx, cy, shape:getRadius())
elseifshape:typeOf('PolygonShape') then
love.graphics.polygon('fill', body:getWorldPoints(shape:getPoints()))
else
love.graphics.line(body:getWorldPoints(shape:getPoints()))
end
end
end
end
end
functionphysics.server:load()
self.beginContact=function(a, b, coll)
for_, vinipairs{{a, b}, {b, a}} do
localfixa=v[1]
localfixb=v[2]
localuda=fixa:getUserData() or {}
localudb=fixb:getUserData() or {}
ifuda.type=='playerSwing' then
ifudb.enemythen
self:postUpdatePush(function()
localswing=projectiles.server.container[uda.id]
localenemy=udb
ifswingandenemyandnotenemy.hitBy[swing.id] then
server.nutServer:sendRPC('damageText', bitser.dumps{
v=swing.damage, x=enemy.x, y=enemy.y-26})
enemy.hitBy[swing.id] =true
enemy:damage(swing.damage, swing.playerId)
swing.pierce=swing.pierce-1
ifswing.pierce<=0then
projectiles.server.destroy(swing.id)
end
end
end)
end
elseifuda.type=='playerSwingObstacleCollider' then
-- check if first category is obstacle
iffixb:getCategory() ==4then
self:postUpdatePush(function()
localswing=projectiles.server.container[uda.data.id]
ifswingthen
projectiles.server.destroy(swing.id)
end
end)
end
end
end
end
self.endContact=function(a, b, coll) end
self.preSolve=function(a, b, coll) end
self.postSolve=function(a, b, coll, normalImpulse, tangentImpulse) end
ifself.worldthenself.world:destroy() end
self.world=love.physics.newWorld(0, 0, true)
self.world:setCallbacks(self.beginContact,
self.endContact, self.preSolve, self.postSolve)
end
functionphysics.client:load()
self.beginContact=function(a, b, coll)
for_, vinipairs{{a, b}, {b, a}} do
localfixa=v[1]
localfixb=v[2]
localuda=fixa:getUserData() or {}
localudb=fixb:getUserData() or {}
ifuda.type=='slimeBall' andudb.id==playerController.player.idthen
self:postUpdatePush(function()
damageText.add{v=uda.damage, x=udb.x, y=udb.y-24}
udb:damage(uda.damage)
slimeBalls.destroy(uda.id)
end)
end
end
end
self.endContact=function(a, b, coll) end
self.preSolve=function(a, b, coll) end
self.postSolve=function(a, b, coll, normalImpulse, tangentImpulse) end
ifself.worldthenself.world:destroy() end
self.world=love.physics.newWorld(0, 0, true)
self.world:setCallbacks(self.beginContact,
self.endContact, self.preSolve, self.postSolve)
end