Changeset 409:180c27514619 for nagslang


Ignore:
Timestamp:
Sep 7, 2013, 9:21:54 AM (7 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
rebase_source:
4b28c28c2284f08707bce0b432dd418827bfa0f3
Message:

Better protagonist finding for enemies.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nagslang/enemies.py

    r406 r409  
    44import pymunk
    55import pymunk.pygame_util
    6 from pymunk.vec2d import Vec2d
    76
    87from nagslang import render
     
    8584        self.lose_health(claw_attack.damage)
    8685
     86    def range_to_visible_protagonist(self):
     87        """Get a vector to the protagonist if there are no barriers in between.
     88
     89        Returns a vector to the protagonist if she is within line of sight,
     90        otherwise `None`
     91        """
     92
     93        pos = self.physicser.position
     94        target = self.world.protagonist.get_shape().body.position
     95
     96        for collision in self.get_space().segment_query(pos, target):
     97            shape = collision.shape
     98            if (shape in (self.get_shape(), self.world.protagonist.get_shape())
     99                    or shape.sensor):
     100                continue
     101            return None
     102        return target - pos
     103
    87104    def ranged_attack(self, range_, speed, damage, type_, reload_time,
    88105                      result=None):
     
    90107            result = Result()
    91108
    92         pos = self.physicser.position
    93         target = self.world.protagonist.get_shape().body.position
    94 
    95         r = self.get_space().segment_query(pos, target)
    96         for collision in r:
    97             shape = collision.shape
    98             if (shape in (self.get_shape(), self.world.protagonist.get_shape())
    99                     or shape.sensor):
    100                 continue
     109        vec = self.range_to_visible_protagonist()
     110        if vec is None:
    101111            return result
    102112
    103113        if not self.check_timer('reload_time'):
    104114            self.start_timer('reload_time', reload_time)
    105             vec = Vec2d((target.x - pos.x, target.y - pos.y))
    106115            if vec.length < range_:
    107116                vec.length = speed
    108                 result.add += (Bullet(self.get_space(), pos, vec, damage,
    109                                       type_, COLLISION_TYPE_ENEMY),)
     117                result.add += (Bullet(
     118                    self.get_space(), self.physicser.position, vec, damage,
     119                    type_, COLLISION_TYPE_ENEMY),)
    110120        return result
    111121
Note: See TracChangeset for help on using the changeset viewer.