diff --git a/engine/components/collide_rect.py b/engine/components/collide_rect.py index bdc118e..83a636b 100644 --- a/engine/components/collide_rect.py +++ b/engine/components/collide_rect.py @@ -1,7 +1,18 @@ from .component import Component +from ..servers.physics import PhysicsServer class CollideRect(Component): - def __init__(self, rect): + def __init__(self, rect, static=True, solid=True): super().__init__() self.rect = rect + self.static = static + self.solid = solid + self.vx = 0 + self.vy = 0 + + def register(self): + PhysicsServer().register_component(self) + + def unregister(self): + PhysicsServer().register_component(self) diff --git a/engine/servers/physics.py b/engine/servers/physics.py index 0f1f234..b9953df 100644 --- a/engine/servers/physics.py +++ b/engine/servers/physics.py @@ -4,7 +4,47 @@ 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 step(self): - for component in self._components: - pass + 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 + 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 + d.vx = 0 + d.vy = 0 diff --git a/scenes/lvl0.py b/scenes/lvl0.py index 17e6815..fd3637d 100644 --- a/scenes/lvl0.py +++ b/scenes/lvl0.py @@ -37,8 +37,10 @@ class MovingEntity(Entity): super().__init__() sprite = Sprite(surf, 1) self.add(sprite) - self.add(CollideRect(Rect(self.x, self.y, - sprite.width, sprite.width))) + self.phys = CollideRect(Rect(self.x, self.y, + sprite.width, sprite.width), + static=False) + self.add(self.phys) class Ghost(MovingEntity): @@ -52,21 +54,34 @@ class Ghost(MovingEntity): class Lvl0(Scene): def load(self): - pac_surf = Surface((S, S)).convert() + T = 2 + v = 2 + + pac_surf = Surface((S//T, S//T)).convert() pac_surf.fill((0, 0, 0)) - draw.circle(pac_surf, (255, 255, 0), (S//2, S//2), S//2) + draw.circle(pac_surf, (255, 255, 0), (S//(2*T), S//(2*T)), S//(2*T)) pacman = MovingEntity(pac_surf) def pacman_script(entity): + p = pacman.phys inputs = key.get_pressed() if inputs[K_UP]: - entity.y -= 2 + # entity.y -= 2 + p.vy = -v if inputs[K_DOWN]: - entity.y += 2 + # entity.y += 2 + p.vy = v if inputs[K_LEFT]: - entity.x -= 2 + # entity.x -= 2 + p.vx = -v if inputs[K_RIGHT]: - entity.x += 2 + # entity.x += 2 + p.vx = v pacman.script = pacman_script + pacman.x = pacman.phys.rect.x = S*15 + pacman.y = pacman.phys.rect.y = S*15 + + pacman.phys.rect.w = S//T + pacman.phys.rect.h = S//T self.entities.append(pacman) self.entities.append(Level('lvl0.png'))