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

51 lines
1.6 KiB
Python
Raw 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):
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)
2019-11-23 10:46:25 +01:00
def step(self):
2019-12-05 11:31:53 +01:00
for d in self._dynamic:
for x in range(abs(d.vx)):
stop = False
for s in self._static:
if s.rect.colliderect(d.rect):
stop = True
break
if stop:
d.parent.x += -1 if d.vx > 0 else 1
d.rect.x = d.parent.x
break
d.parent.x += 1 if d.vx > 0 else -1
# d.rect.x = d.parent.x
2019-12-05 11:31:53 +01:00
for y in range(abs(d.vy)):
stop = False
for s in self._static:
if s.rect.colliderect(d.rect):
stop = True
break
if stop:
d.parent.y += -1 if d.vy > 0 else 1
d.rect.y = d.parent.y
break
d.parent.y += 1 if d.vy > 0 else -1
# d.rect.y = d.parent.y
2019-12-05 11:31:53 +01:00
d.vx = 0
d.vy = 0