comparison nagslang/enemies.py @ 361:534eac55a178

ChargingEnemy spits acid
author Stefano Rivera <stefano@rivera.za.net>
date Fri, 06 Sep 2013 20:10:46 +0200
parents 911547a1c378
children 3dd08e18580f
comparison
equal deleted inserted replaced
360:ba7a5159a69b 361:534eac55a178
3 import pymunk 3 import pymunk
4 import pymunk.pygame_util 4 import pymunk.pygame_util
5 5
6 from nagslang import render 6 from nagslang import render
7 from nagslang.constants import (COLLISION_TYPE_ENEMY, COLLISION_TYPE_FURNITURE, 7 from nagslang.constants import (COLLISION_TYPE_ENEMY, COLLISION_TYPE_FURNITURE,
8 ZORDER_MID) 8 ACID_SPEED, ACID_DAMAGE, ZORDER_MID)
9 from nagslang.events import EnemyDeathEvent 9 from nagslang.events import EnemyDeathEvent, FireEvent
10 from nagslang.game_object import GameObject, SingleShapePhysicser, make_body 10 from nagslang.game_object import GameObject, SingleShapePhysicser, make_body
11 from nagslang.mutators import FLIP_H 11 from nagslang.mutators import FLIP_H
12 from nagslang.resources import resources 12 from nagslang.resources import resources
13 from nagslang.utils import vec_with_length 13 from nagslang.utils import vec_with_length
14 14
177 is_moving = False 177 is_moving = False
178 enemy_type = 'B' 178 enemy_type = 'B'
179 health = 42 179 health = 42
180 enemy_damage = 20 180 enemy_damage = 20
181 impulse_factor = 300 181 impulse_factor = 300
182 reload_time = 0.2
182 183
183 def __init__(self, space, world, position, attack_range=100): 184 def __init__(self, space, world, position, attack_range=100):
184 super(ChargingAlien, self).__init__(space, world, position) 185 super(ChargingAlien, self).__init__(space, world, position)
185 self._range = attack_range 186 self._range = attack_range
187 self._last_fired = 0
186 188
187 def make_physics(self, space, position): 189 def make_physics(self, space, position):
188 body = make_body(100, pymunk.inf, position, 0.8) 190 body = make_body(100, pymunk.inf, position, 0.8)
189 shape = pymunk.Circle(body, 30) 191 shape = pymunk.Circle(body, 30)
190 shape.elasticity = 1.0 192 shape.elasticity = 1.0
211 if pos.get_distance(target) > self._range: 213 if pos.get_distance(target) > self._range:
212 # stop 214 # stop
213 self.is_moving = False 215 self.is_moving = False
214 return 216 return
215 self.is_moving = True 217 self.is_moving = True
216 self.set_direction(target.x - pos.x, target.y - pos.y) 218 dx = target.x - pos.x
219 dy = target.y - pos.y
220 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, COLLISION_TYPE_ENEMY)
224 self._last_fired = self.lifetime
217 super(ChargingAlien, self).update(dt) 225 super(ChargingAlien, self).update(dt)
218 226
219 @classmethod 227 @classmethod
220 def requires(cls): 228 def requires(cls):
221 return [("name", "string"), ("position", "coordinates"), 229 return [("name", "string"), ("position", "coordinates"),