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

56 lines
1.7 KiB
Python
Raw Permalink Normal View History

2019-11-23 10:46:25 +01:00
from .server import Server
class PhysicsServer(Server):
def __init__(self):
super().__init__()
2019-12-05 11:31:53 +01:00
self._static = []
self._dynamic = []
def register_component(self, component):
2019-12-08 23:47:32 +01:00
if component.static:
self._static.append(component)
2019-12-05 11:31:53 +01:00
else:
2019-12-08 23:47:32 +01:00
self._dynamic.append(component)
2019-12-05 11:31:53 +01:00
def unregister_component(self, component):
2019-12-08 23:47:32 +01:00
if component in self._static:
self._static.remove(component)
if component in self._dynamic:
self._dynamic.remove(component)
2019-11-23 10:46:25 +01:00
2019-12-08 23:47:32 +01:00
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
2019-11-23 10:46:25 +01:00
def step(self):
2019-12-05 11:31:53 +01:00
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)):
2019-12-09 09:57:53 +01:00
d.parent.x += x_step
2019-12-08 23:47:32 +01:00
c = self._check_collide(d)
if c is not None:
if c.cb:
2019-12-09 12:36:42 +01:00
c.cb(d)
if d.cb:
d.cb(c)
2019-12-08 23:47:32 +01:00
if c.solid:
d.parent.x -= x_step
2019-12-09 20:51:45 +01:00
d.vx = -d.vx * d.friction
2019-12-08 23:47:32 +01:00
break
for i in range(abs(d.vy)):
2019-12-09 09:57:53 +01:00
d.parent.y += y_step
2019-12-08 23:47:32 +01:00
c = self._check_collide(d)
if c is not None:
if c.cb:
2019-12-09 12:36:42 +01:00
c.cb(d)
if d.cb:
d.cb(c)
2019-12-08 23:47:32 +01:00
if c.solid:
d.parent.y -= y_step
2019-12-09 20:51:45 +01:00
d.vy = -d.vy * d.friction
2019-12-08 23:47:32 +01:00
break