This repository has been archived on 2019-12-09. You can view files and clone it, but cannot push or open issues or pull requests.
pacman2/engine/servers/physics.py

47 lines
1.3 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.solid:
if component.static:
self._static.append(component)
else:
self._dynamic.append(component)
else:
self._area.append(component)
def unregister_component(self, component):
self._static.remove(component)
self._dynamic.remove(component)
def _check_collide(self, rect):
for s in self._static:
if s.rect.colliderect(rect):
return True
return False
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)):
if self._check_collide(d.rect):
d.parent.x -= x_step
d.vx = 0
break
d.parent.x += x_step
for i in range(abs(d.vy)):
if self._check_collide(d.rect):
d.parent.y -= y_step
d.vy = 0
break
d.parent.y += y_step
# d.vx = 0
# d.vy = 0