From: Simon Cross Date: Sat, 5 Mar 2016 20:45:16 +0000 (+0200) Subject: Melee weapons now do damage. X-Git-Url: https://git.ctpug.org.za/?a=commitdiff_plain;h=e8bb29ef4dd7789bf9c20aebfb87f3911bb0d7b9;p=koperkapel.git Melee weapons now do damage. --- diff --git a/koperkapel/gamelib/enemy_roach.py b/koperkapel/gamelib/enemy_roach.py index 95b3e19..e36b176 100644 --- a/koperkapel/gamelib/enemy_roach.py +++ b/koperkapel/gamelib/enemy_roach.py @@ -8,4 +8,5 @@ def get_enemy_roach(): roach = t32_roaches.assemble(WorldRoach(), color=(255, 0, 0, 255)) roach.anchor = (0, 0) roach.game_pos = (0, 0) + roach.health = 5 return roach diff --git a/koperkapel/gamelib/level.py b/koperkapel/gamelib/level.py index 23701b9..aed4ccc 100644 --- a/koperkapel/gamelib/level.py +++ b/koperkapel/gamelib/level.py @@ -99,6 +99,9 @@ class Level(object): return enemy return None + def remove_enemy(self, enemy): + self.enemies.remove(enemy) + def add_enemy(self, enemy, x, y): """Add an enemy to an empty floor space near x, y""" added = False @@ -112,4 +115,3 @@ class Level(object): added = True x += random.randint(-1, 1) y += random.randint(-1, 1) - diff --git a/koperkapel/scenes/level.py b/koperkapel/scenes/level.py index 7978a73..50ec808 100644 --- a/koperkapel/scenes/level.py +++ b/koperkapel/scenes/level.py @@ -10,6 +10,7 @@ from ..constants import TILE_SIZE, WIDTH, HEIGHT from ..gamelib.items import clone_old_item from ..roaches import build_roach from ..vehicles.base import Vehicle +from ..weapons import weapon_by_name class BaseLevelScene(Scene): @@ -99,7 +100,8 @@ class GameLevelScene(BaseLevelScene): self._enemies = self.actors.add_layer("enemies", level=7) self._vehicle = Vehicle.current(world) self._mode = 'walk' - self._angle = 0 + self._angle = 0 # up + self._angle_dp = (0, -1) # up self._init_items() self._init_friendly_roaches() self._init_generators() @@ -154,8 +156,9 @@ class GameLevelScene(BaseLevelScene): elif self._mode == 'crawl': return self._level.can_crawl(x, y, self._level_layer) - def _set_angle(self, angle): + def _set_angle(self, angle, dp): self._angle = angle + self._angle_dp = dp self._avatar.angle = angle @defer_to_update @@ -165,7 +168,7 @@ class GameLevelScene(BaseLevelScene): self._avatar = self._vehicle.get_avatar(world) self._avatar.pos = (WIDTH // 2, HEIGHT // 2) self._roaches.add(self._avatar) - self._set_angle(self._angle) + self._set_angle(self._angle, self._angle_dp) @defer_to_update def _add_roach(self, world): @@ -185,6 +188,26 @@ class GameLevelScene(BaseLevelScene): self._items.add(clone) self._vehicle_changed() + def _hit_enemy(self, enemy, weapon): + enemy.health -= weapon.damage + if enemy.health <= 0: + self._level.remove_enemy(enemy) + self._enemies.remove(enemy) + + @defer_to_update + def _fire_weapon(self, world): + weapon = weapon_by_name(world.weapons.current) + if weapon.bullet_range > 0: + # ranged, fire bullet + print("Pew.") + else: + # melee + pos, dp = self._player_pos, self._angle_dp + pos = (pos[0] + dp[0], pos[1] + dp[1]) + enemy = self._level.get_enemy(pos[0], pos[1]) + if enemy: + self._hit_enemy(enemy, weapon) + @defer_to_update def _change_vehicle(self, world): vehicle = Vehicle.random() @@ -239,12 +262,12 @@ class GameLevelScene(BaseLevelScene): if self._can_move(nx, ny): self._set_pos(nx, ny) offset = (TILE_SIZE * dp[0], TILE_SIZE * dp[1]) - self._set_angle(angle) + self._set_angle(angle, dp) self._last_key_down = 0 return [MoveViewportEvent(offset)] else: # just turn - self._set_angle(angle) + self._set_angle(angle, dp) def _activate_key(self): x, y = self._player_pos @@ -279,7 +302,7 @@ class GameLevelScene(BaseLevelScene): def _fire_key(self, dt): if self._last_key_down > self._key_rate: self._last_key_down = 0 - print('Boom') + self._fire_weapon() def _vehicle_management_key(self): from .roach_management import RoachesScene diff --git a/koperkapel/weapons.py b/koperkapel/weapons.py index 3962ecc..3ef9879 100644 --- a/koperkapel/weapons.py +++ b/koperkapel/weapons.py @@ -28,6 +28,10 @@ WEAPONS = [ WEAPON_LOOKUP = {w.name: w for w in WEAPONS} +def weapon_by_name(weapon_name): + return WEAPON_LOOKUP[weapon_name] + + class WeaponActor(AnimatedSurfActor): def __init__(self, weapon, **kw): super().__init__(**kw) @@ -38,7 +42,7 @@ class WeaponFactory: def assemble_frame(self, suffix, weapon, tape): surf = images.load(safepath("weapons/%s%s") - % (weapon.image_name, suffix)) + % (weapon.image_name, suffix)) frame = surf.copy() if tape: tape_surf = images.load(safepath("weapons/tape")) @@ -46,7 +50,7 @@ class WeaponFactory: return frame def assemble(self, weapon_name, tape=False): - weapon = WEAPON_LOOKUP[weapon_name] + weapon = weapon_by_name(weapon_name) tape = tape and weapon.can_tape frames = [ self.assemble_frame(suffix, weapon, tape)