52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
from .server import Server
|
|
|
|
|
|
class PhysicsServer(Server):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self._static = []
|
|
self._dynamic = []
|
|
|
|
def register_component(self, component):
|
|
if component.static:
|
|
self._static.append(component)
|
|
else:
|
|
self._dynamic.append(component)
|
|
|
|
def unregister_component(self, component):
|
|
if component in self._static:
|
|
self._static.remove(component)
|
|
if component in self._dynamic:
|
|
self._dynamic.remove(component)
|
|
|
|
def _check_collide(self, dyn):
|
|
for s in self._static + self._dynamic:
|
|
if s != dyn and s.rect.colliderect(dyn.rect):
|
|
return s
|
|
return None
|
|
|
|
def step(self):
|
|
for d in self._dynamic:
|
|
x_step = -1 if d.vx < 0 else 1
|
|
y_step = -1 if d.vy < 0 else 1
|
|
for i in range(abs(d.vx)):
|
|
d.parent.x += x_step
|
|
c = self._check_collide(d)
|
|
if c is not None:
|
|
if c.cb:
|
|
c.cb()
|
|
if c.solid:
|
|
d.parent.x -= x_step
|
|
d.vx = 0
|
|
break
|
|
for i in range(abs(d.vy)):
|
|
d.parent.y += y_step
|
|
c = self._check_collide(d)
|
|
if c is not None:
|
|
if c.cb:
|
|
c.cb()
|
|
if c.solid:
|
|
d.parent.y -= y_step
|
|
d.vy = 0
|
|
break
|