Changeset 366:9898fa231c4b


Ignore:
Timestamp:
Sep 6, 2013, 7:07:24 PM (7 years ago)
Author:
Stefano Rivera <stefano@…>
Branch:
default
rebase_source:
bb4c130834e9014faa1ed6b82a4c00c9f8c67104
Message:

Move ranged attack code to Enemy. Don't shoot through solid objects

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nagslang/enemies.py

    r363 r366  
    33import pymunk
    44import pymunk.pygame_util
     5from pymunk.vec2d import Vec2d
    56
    67from nagslang import render
     
    4142        self.world = world
    4243        self.angle = 0
     44        self._last_fired = 0
    4345
    4446    def make_physics(self, space, position):
     
    8385    def collide_with_claw_attack(self, claw_attack):
    8486        self.lose_health(claw_attack.damage)
     87
     88    def ranged_attack(self, range_, speed, damage, type_, reload_time):
     89        pos = self.physicser.position
     90        target = self.world.protagonist.get_shape().body.position
     91
     92        r = self.get_space().segment_query(pos, target)
     93        for collision in r:
     94            shape = collision.shape
     95            if (shape in (self.get_shape(), self.world.protagonist.get_shape())
     96                    or shape.sensor):
     97                continue
     98            return
     99
     100        if self.lifetime - self._last_fired >= reload_time:
     101            vec = Vec2d((target.x - pos.x, target.y - pos.y))
     102            if vec.length < range_:
     103                vec.length = speed
     104                FireEvent.post(pos, vec, damage, type_,
     105                               COLLISION_TYPE_ENEMY)
     106                self._last_fired = self.lifetime
    85107
    86108
     
    165187            self._switch_direction()
    166188        self.set_direction(x_step, y_step)
     189        self.ranged_attack(300, ACID_SPEED, ACID_DAMAGE, 'acid', 0.2)
    167190        super(PatrollingAlien, self).update(dt)
    168191
     
    180203    enemy_damage = 20
    181204    impulse_factor = 300
    182     reload_time = 0.2
    183205
    184206    def __init__(self, space, world, position, attack_range=100):
    185207        super(ChargingAlien, self).__init__(space, world, position)
    186208        self._range = attack_range
    187         self._last_fired = 0
    188209
    189210    def make_physics(self, space, position):
     
    209230        # Calculate the step every frame
    210231        # Distance to the protagonist
     232        self.ranged_attack(300, ACID_SPEED, ACID_DAMAGE, 'acid', 0.2)
    211233        pos = self.physicser.position
    212234        target = self.world.protagonist.get_shape().body.position
     
    219241        dy = target.y - pos.y
    220242        self.set_direction(dx, dy)
    221         if self.lifetime - self._last_fired >= self.reload_time:
    222             FireEvent.post(pos, vec_with_length((dx, dy), ACID_SPEED),
    223                            ACID_DAMAGE, 'acid', COLLISION_TYPE_ENEMY)
    224             self._last_fired = self.lifetime
    225243        super(ChargingAlien, self).update(dt)
    226244
Note: See TracChangeset for help on using the changeset viewer.