Changeset 333:3dd32686dbc3


Ignore:
Timestamp:
Sep 6, 2013, 8:49:19 AM (7 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Message:

Better wolf claw attack.

Location:
nagslang
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • nagslang/constants.py

    r318 r333  
    5252PROTAGONIST_HEALTH_MAX_LEVEL = 100
    5353PROTAGONIST_HEALTH_MIN_LEVEL = 0
     54
     55BULLET_DAMAGE = 10
     56CLAW_DAMAGE = 5
  • nagslang/enemies.py

    r318 r333  
    2525    """A base class for mobile enemies"""
    2626    enemy_type = None  # Image to use for dead Enemy
     27    enemy_damage = None
    2728
    2829    def __init__(self, space, world, position):
     
    6162            self.remove = True
    6263            EnemyDeathEvent.post(self.physicser.position, self.enemy_type)
     64
     65    def collide_with_protagonist(self, protagonist):
     66        if self.enemy_damage is not None:
     67            protagonist.lose_health(self.enemy_damage)
     68
     69    def collide_with_claw_attack(self, claw_attack):
     70        self.lose_health(claw_attack.damage)
    6371
    6472
     
    136144            (dx * self.impulse_factor, dy * self.impulse_factor))
    137145
    138     def update(self, seconds):
     146    def update(self, dt):
    139147        # Calculate the step every frame
    140148        if self._direction == 'away':
     
    159167            self._switch_direction()
    160168        self.set_direction(x_step, y_step)
    161         super(PatrollingAlien, self).update(seconds)
    162 
    163     def collide_with_protagonist(self, protagonist):
    164         protagonist.lose_health(15)
     169        super(PatrollingAlien, self).update(dt)
    165170
    166171    @classmethod
     
    217222            (dx * self.impulse_factor, dy * self.impulse_factor))
    218223
    219     def update(self, seconds):
     224    def update(self, dt):
    220225        # Calculate the step every frame
    221226        # Distance to the protagonist
     
    242247            y_step = 0
    243248        self.set_direction(x_step, y_step)
    244         super(ChargingAlien, self).update(seconds)
    245 
    246     def collide_with_protagonist(self, protagonist):
    247         protagonist.lose_health(20)
     249        super(ChargingAlien, self).update(dt)
    248250
    249251    @classmethod
  • nagslang/events.py

    r313 r333  
    6464class ClawEvent(UserEvent):
    6565    @classmethod
    66     def post(cls, source, vector, source_collision_type):
    67         super(ClawEvent, cls).post(source=source, vector=vector,
    68                                    source_collision_type=source_collision_type)
     66    def post(cls, source, vector, damage):
     67        super(ClawEvent, cls).post(source=source, vector=vector, damage=damage)
  • nagslang/game_object.py

    r332 r333  
    33
    44import math
    5 import time
    65
    76from nagslang import environment
     
    112111    def __init__(self, physicser, renderer, puzzler=None, overlay=None,
    113112                 interactible=None):
     113        self.lifetime = 0
    114114        self.physicser = physicser
    115115        physicser.set_game_object(self)
     
    148148        return self.renderer.render(surface)
    149149
    150     def update(self, seconds):
    151         self.renderer.update(seconds)
     150    def update(self, dt):
     151        self.lifetime += dt
     152        self.renderer.update(dt)
    152153
    153154    def hit(self, weapon):
     
    164165
    165166    def collide_with_furniture(self, furniture):
     167        return True
     168
     169    def collide_with_claw_attack(self, claw_attack):
    166170        return True
    167171
     
    365369        self.physicser.apply_impulse(impulse)
    366370
    367     def update(self, seconds):
    368         super(Bullet, self).update(seconds)
     371    def update(self, dt):
     372        super(Bullet, self).update(dt)
    369373        position = (self.physicser.position.x, self.physicser.position.y)
    370374        r = self.get_space().segment_query(self.last_position, position)
     
    415419
    416420class ClawAttack(GameObject):
    417     def __init__(self, space, position, vector, source_collision_type):
    418         body = make_body(1, pymunk.inf, position)
    419         self.last_position = position
     421    def __init__(self, space, position, vector, damage):
     422        body = make_body(1, pymunk.inf, position + vector)
     423        body.angle = vector.angle
    420424        self.shape = pymunk.Circle(body, 30)
    421425        self.shape.sensor = True
    422426        self.shape.collision_type = COLLISION_TYPE_WEREWOLF_ATTACK
    423         self.source_collision_type = source_collision_type
     427        self.damage = damage
    424428        super(ClawAttack, self).__init__(
    425429            SingleShapePhysicser(space, self.shape),
    426             render.ImageRenderer(resources.get_image
    427                                 ('objects', 'werewolf_SW_claw_attack.png')),
    428         )
    429         self.time_created = time.time()
    430 
    431     def update(self, seconds):
    432         super(ClawAttack, self).update(seconds)
    433         position = (self.physicser.position.x, self.physicser.position.y)
    434         r = self.get_space().segment_query(self.last_position, position)
    435         self.last_position = position
    436         for collision in r:
    437             shape = collision.shape
    438             if (shape.collision_type == self.source_collision_type
    439                     or shape == self.physicser.get_shape()
    440                     or shape.sensor):
    441                 continue
    442             if hasattr(shape, 'physicser'):
    443                 shape.physicser.game_object.hit(self)
    444         if time.time() - self.time_created > 0.2:
     430            render.ImageRenderer(resources.get_image(
     431                'objects', 'werewolf_SW_claw_attack.png',
     432                transforms=(FLIP_H,))),
     433        )
     434
     435    def update(self, dt):
     436        super(ClawAttack, self).update(dt)
     437        if self.lifetime > 0.2:
    445438            self.physicser.remove_from_space()
    446439            self.remove = True
  • nagslang/protagonist.py

    r313 r333  
    77    COLLISION_TYPE_PLAYER, ZORDER_MID, WEREWOLF_SOAK_FACTOR,
    88    PROTAGONIST_HEALTH_MIN_LEVEL, PROTAGONIST_HEALTH_MAX_LEVEL,
    9     NON_GAME_OBJECT_COLLIDERS, COLLISION_TYPE_WEREWOLF_ATTACK)
     9    NON_GAME_OBJECT_COLLIDERS, BULLET_DAMAGE, CLAW_DAMAGE)
    1010from nagslang.events import FireEvent, ClawEvent
    1111from nagslang.game_object import GameObject, Physicser, make_body
     
    6060
    6161        self.go_human()
    62         self._time = 0
    6362
    6463    def _make_physics(self, space, position):
     
    263262        vec.angle = self.angle
    264263        vec.length = 1000
    265         FireEvent.post(self.physicser.position, vec, 10, COLLISION_TYPE_PLAYER)
     264        FireEvent.post(
     265            self.physicser.position, vec, BULLET_DAMAGE, COLLISION_TYPE_PLAYER)
    266266
    267267    def claw(self):
    268268        vec = Vec2d.unit()
    269269        vec.angle = self.angle
    270         vec.length = 100
    271         ClawEvent.post(self.physicser.position, vec,
    272                        COLLISION_TYPE_WEREWOLF_ATTACK)
    273         print "Claw", self.physicser.position, vec
     270        vec.length = 30
     271        ClawEvent.post(self.physicser.position, vec, CLAW_DAMAGE)
    274272
    275273    def in_wolf_form(self):
     
    311309
    312310    def update(self, dt):
    313         last_secs = int(self._time)
    314         self._time += dt
    315         secs = int(self._time)
    316         if self.form == self.WOLF_FORM and secs > last_secs:
    317             self.gain_health(1)
     311        if int(self.lifetime + dt) > int(self.lifetime):
     312            if self.form == self.WOLF_FORM:
     313                self.gain_health(1)
    318314        super(Protagonist, self).update(dt)
  • nagslang/screens/area.py

    r321 r333  
    77from nagslang.constants import (
    88    COLLISION_TYPE_WALL, COLLISION_TYPE_PLAYER, CALLBACK_COLLIDERS,
    9     COLLISION_TYPE_FURNITURE)
     9    COLLISION_TYPE_FURNITURE, COLLISION_TYPE_WEREWOLF_ATTACK)
    1010from nagslang.enemies import DeadEnemy
    11 from nagslang.events import ScreenChange, DoorEvent, FireEvent, \
    12     EnemyDeathEvent, ClawEvent
     11from nagslang.events import (
     12    ScreenChange, DoorEvent, FireEvent, EnemyDeathEvent, ClawEvent)
    1313from nagslang.level import Level
    1414from nagslang.screens.base import Screen
     
    9494        # accidentally reject collisions from handlers that return `None`, so
    9595        # we explicitly check for `False` and treate everything else as `True`.
    96         if result is False:
    97             return False
    98         return True
     96        return result is not False
     97
     98    def _claw_attack_collision_pre_solve_handler(self, space, arbiter):
     99        claw = arbiter.shapes[0].physicser.game_object
     100        gobj = arbiter.shapes[1].physicser.game_object
     101        result = gobj.collide_with_claw_attack(claw)
     102        return result is not False
    99103
    100104    def _furniture_collision_pre_solve_handler(self, space, arbiter):
     
    112116                COLLISION_TYPE_FURNITURE, collision_type,
    113117                pre_solve=self._furniture_collision_pre_solve_handler)
     118            self.space.add_collision_handler(
     119                COLLISION_TYPE_WEREWOLF_ATTACK, collision_type,
     120                pre_solve=self._claw_attack_collision_pre_solve_handler)
    114121
    115122    def add_walls(self):
     
    174181        elif ClawEvent.matches(ev):
    175182            claw_attack = ClawAttack(self.space, ev.source, ev.vector,
    176                                      ev.source_collision_type)
     183                                     ev.damage)
    177184            self._drawables.add(claw_attack)
    178185        self.keys.handle_event(ev)
     
    235242        else:
    236243            health_box_colour = pygame.color.THECOLORS['white']
    237         pygame.draw.rect(surface,  health_box_colour, rect, 0)
     244        pygame.draw.rect(surface, health_box_colour, rect, 0)
    238245        if self.protagonist.in_human_form():
    239246            health_colour = pygame.color.THECOLORS['red']
     
    241248            health_colour = pygame.color.THECOLORS['purple']
    242249        rect = pygame.Rect(55, 505, self.protagonist.get_health_level(), 40)
    243         pygame.draw.rect(surface,  health_colour,
    244                          rect, 0)
     250        pygame.draw.rect(surface, health_colour, rect, 0)
Note: See TracChangeset for help on using the changeset viewer.