Changeset 308:3dee86b6c216


Ignore:
Timestamp:
Sep 5, 2013, 10:36:14 PM (7 years ago)
Author:
Stefano Rivera <stefano@…>
Branch:
default
Message:

Enemies leave corpses

Location:
nagslang
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • nagslang/enemies.py

    r307 r308  
    55
    66from nagslang import render
    7 from nagslang.constants import COLLISION_TYPE_ENEMY, ZORDER_MID
     7from nagslang.constants import (COLLISION_TYPE_ENEMY, COLLISION_TYPE_BOX,
     8                                ZORDER_MID)
     9from nagslang.events import EnemyDeathEvent
    810from nagslang.game_object import GameObject, SingleShapePhysicser, make_body
    911from nagslang.mutators import FLIP_H
     
    2224class Enemy(GameObject):
    2325    """A base class for mobile enemies"""
     26    enemy_type = None  # Image to use for dead Enemy
    2427
    2528    def __init__(self, space, world, position):
     
    5255        self.health -= amount
    5356        if self.health < 0:
    54             self.world.kills += 1
    5557            self.physicser.remove_from_space()
    5658            self.remove = True
     59            EnemyDeathEvent.post(self.physicser.position, 'A')
     60
     61
     62class DeadEnemy(GameObject):
     63    def __init__(self, space, world, position, enemy_type='A'):
     64        body = make_body(10, 10000, position, damping=0.5)
     65        self.shape = pymunk.Poly(
     66            body, [(-20, -20), (20, -20), (20, 20), (-20, 20)])
     67        self.shape.friction = 0.5
     68        self.shape.collision_type = COLLISION_TYPE_BOX
     69        super(DeadEnemy, self).__init__(
     70            SingleShapePhysicser(space, self.shape),
     71            render.ImageRenderer(resources.get_image(
     72                'creatures', 'alien_%s_dead.png' % enemy_type)),
     73        )
     74
     75    @classmethod
     76    def requires(cls):
     77        return [("name", "string"), ("position", "coordinates")]
    5778
    5879
    5980class PatrollingAlien(Enemy):
    6081    is_moving = True  # Always walking.
     82    enemy_type = 'A'
    6183
    6284    def __init__(self, space, world, position, end_position):
     
    151173    # Simplistic charging of the protagonist
    152174    is_moving = False
     175    enemy_type = 'B'
    153176
    154177    def __init__(self, space, world, position, attack_range=100):
  • nagslang/events.py

    r305 r308  
    5353                                   damage=damage,
    5454                                   source_collision_type=source_collision_type)
     55
     56
     57class EnemyDeathEvent(UserEvent):
     58    @classmethod
     59    def post(cls, position, enemy_type):
     60        super(EnemyDeathEvent, cls).post(position=position,
     61                                         enemy_type=enemy_type)
  • nagslang/screens/area.py

    r307 r308  
    77from nagslang.constants import (
    88    COLLISION_TYPE_WALL, COLLISION_TYPE_PLAYER, CALLBACK_COLLIDERS)
    9 from nagslang.events import ScreenChange, DoorEvent, FireEvent
     9from nagslang.enemies import DeadEnemy
     10from nagslang.events import ScreenChange, DoorEvent, FireEvent, EnemyDeathEvent
    1011from nagslang.level import Level
    1112from nagslang.screens.base import Screen
     
    151152                            ev.source_collision_type)
    152153            self._drawables.add(bullet)
     154        elif EnemyDeathEvent.matches(ev):
     155            self.world.kills += 1
     156            dead_enemy = DeadEnemy(self.space, self.world, ev.position,
     157                                   ev.enemy_type)
     158            self._drawables.add(dead_enemy)
    153159        self.keys.handle_event(ev)
    154160
Note: See TracChangeset for help on using the changeset viewer.