diff --git a/engine/components/collide_rect.py b/engine/components/collide_rect.py index d4fc82a..3ca7d8d 100644 --- a/engine/components/collide_rect.py +++ b/engine/components/collide_rect.py @@ -3,7 +3,7 @@ from ..servers.physics import PhysicsServer class CollideRect(Component): - def __init__(self, parent, rect, static=True, solid=True): + def __init__(self, parent, rect, static=True, solid=True, cb=None): super().__init__(parent) self.rect = rect def x_change_cb(x): @@ -14,6 +14,7 @@ class CollideRect(Component): parent.subscribe('y', y_change_cb) self.static = static self.solid = solid + self.cb = cb self.vx = 0 self.vy = 0 @@ -21,4 +22,4 @@ class CollideRect(Component): PhysicsServer().register_component(self) def unregister(self): - PhysicsServer().register_component(self) + PhysicsServer().unregister_component(self) diff --git a/engine/components/sprite.py b/engine/components/sprite.py index 6f1d72f..0fbfca4 100644 --- a/engine/components/sprite.py +++ b/engine/components/sprite.py @@ -13,4 +13,4 @@ class Sprite(Component): GraphicsServer().register_component(self) def unregister(self): - GraphicsServer().register_component(self) + GraphicsServer().unregister_component(self) diff --git a/engine/servers/physics.py b/engine/servers/physics.py index 4390f0e..38a57e7 100644 --- a/engine/servers/physics.py +++ b/engine/servers/physics.py @@ -8,39 +8,46 @@ class PhysicsServer(Server): self._dynamic = [] def register_component(self, component): - if component.solid: - if component.static: - self._static.append(component) - else: - self._dynamic.append(component) + if component.static: + self._static.append(component) else: - self._area.append(component) + self._dynamic.append(component) def unregister_component(self, component): - self._static.remove(component) - self._dynamic.remove(component) + if component in self._static: + self._static.remove(component) + if component in self._dynamic: + self._dynamic.remove(component) - def _check_collide(self, rect): - for s in self._static: - if s.rect.colliderect(rect): - return True - return False + 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)): - if self._check_collide(d.rect): - d.parent.x -= x_step - d.vx = 0 - break + 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 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 + 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 d.parent.y += y_step # d.vx = 0 # d.vy = 0 diff --git a/res/lvl0.png b/res/lvl0.png index 503e9b9..aa7e2ea 100644 Binary files a/res/lvl0.png and b/res/lvl0.png differ diff --git a/scenes/lvl0.py b/scenes/lvl0.py index 25b51e0..a65da24 100644 --- a/scenes/lvl0.py +++ b/scenes/lvl0.py @@ -15,6 +15,24 @@ from itertools import product S=20 + +entities=[] + + +class PacDot(Entity): + s = Surface((S, S)) + draw.circle(s, (255, 255, 0), (S//2, S//2), S//4) + + def __init__(self): + super().__init__() + CollideRect(self, Rect(0, 0, S, S), + static=True, solid=False, cb=self.cb) + Sprite(self, PacDot.s, 1) + + def cb(self): + self.unregister() + + class Level(Entity): def __init__(self, path): super().__init__() @@ -26,16 +44,22 @@ class Level(Entity): wall = Surface((S, S)).convert() wall.fill((0, 0, 255)) for x, y in product(range(w), range(h)): - if desc.get_at((x, y)) == (0, 0, 255): + col = desc.get_at((x, y)) + if col == (0, 0, 255): CollideRect(self, Rect(x * S, y * S, S, S)) self.surf.blit(wall, (x * S, y * S)) + elif col == (139, 139, 139): + pc = PacDot() + pc.x = x * S + pc.y = y * S + entities.append(pc) Sprite(self, self.surf, 0) class MovingEntity(Entity): def __init__(self, surf): super().__init__() - sprite = Sprite(self, surf, 1) + sprite = Sprite(self, surf, 2) self.phys = CollideRect( self, Rect(self.x, self.y, sprite.width, sprite.width), @@ -81,8 +105,10 @@ class Lvl0(Scene): pacman.phys.rect.w = S//T pacman.phys.rect.h = S//T - self.entities.append(pacman) self.entities.append(Level('lvl0.png')) + for entity in entities: + self.entities.append(entity) + self.entities.append(pacman) super().load()