Changeset 371:21c1c329f8e3 for nagslang


Ignore:
Timestamp:
Sep 6, 2013, 7:56:57 PM (7 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Message:

Automatic weapons.

Location:
nagslang
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • nagslang/constants.py

    r361 r371  
    6161
    6262CMD_TOGGLE_FORM = 'toggle_form'
    63 CMD_ATTACK = 'attack'
    6463CMD_ACTION = 'action'
  • nagslang/game_object.py

    r370 r371  
    127127            self.interactible.set_game_object(self)
    128128        self.remove = False  # If true, will be removed from drawables
     129        self._timers = {}
     130        self._active_timers = {}
     131
     132    def add_timer(self, name, secs):
     133        self._timers[name] = secs
     134
     135    def start_timer(self, name, secs=None):
     136        if secs is None:
     137            secs = self._timers[name]
     138        self._active_timers[name] = secs
     139
     140    def check_timer(self, name):
     141        return name in self._active_timers
    129142
    130143    def set_stored_state_dict(self, stored_state):
     
    160173    def update(self, dt):
    161174        self.lifetime += dt
     175        for timer in self._active_timers.keys():
     176            self._active_timers[timer] -= dt
     177            if self._active_timers[timer] <= 0:
     178                self._active_timers.pop(timer)
    162179        self.renderer.update(dt)
    163180
  • nagslang/protagonist.py

    r370 r371  
    99    PROTAGONIST_HEALTH_MIN_LEVEL, PROTAGONIST_HEALTH_MAX_LEVEL,
    1010    NON_GAME_OBJECT_COLLIDERS, BULLET_DAMAGE, BULLET_SPEED, CLAW_DAMAGE,
    11     CMD_TOGGLE_FORM, CMD_ATTACK, CMD_ACTION)
     11    CMD_TOGGLE_FORM, CMD_ACTION)
    1212from nagslang.events import FireEvent, ClawEvent
    1313from nagslang.game_object import GameObject, Physicser, make_body
     
    7979        self.is_moving = False
    8080        self.changing_sequence = []
    81         self.change_delay = 0
     81        self.add_timer('attack_cooldown', 0.2)
     82        self.add_timer('change_delay', 0.1)
    8283
    8384        self.go_human()
     
    188189        return obj
    189190
     191    def handle_attack_key_down(self):
     192        if self.changing_sequence or self.check_timer('attack_cooldown'):
     193            return
     194        self.start_timer('attack_cooldown')
     195        self.world.attacks += 1
     196        self.attack()
     197
    190198    def handle_keypress(self, key_command):
    191199        if self.changing_sequence:
    192             print "Changing, can't act."
    193200            return
    194201        if key_command == CMD_TOGGLE_FORM:
    195202            self.world.transformations += 1
    196203            self.toggle_form()
    197         if key_command == CMD_ATTACK:
    198             self.world.attacks += 1
    199             self.attack()
    200204        if key_command == CMD_ACTION:
    201205            self.perform_action()
     
    256260
    257261    def toggle_form(self):
    258         if self.change_delay:
     262        if self.check_timer('change_delay'):
    259263            return
    260264        self.changing_sequence.extend(self.CHANGING_SEQUENCE[self.form])
     
    353357            self.health_level = PROTAGONIST_HEALTH_MAX_LEVEL
    354358
     359    def _decrement_timer(self, timer, dt):
     360        if self._timers[timer] > 0:
     361            self._timers[timer] -= dt
     362        if self._timers[timer] < 0:
     363            self._timers[timer] = 0
     364
    355365    def update(self, dt):
    356         if self.change_delay > 0:
    357             self.change_delay -= 1
    358366        if self.changing_sequence:
    359367            self._go_to_next_form()
     
    366374        if self.in_human_form() and not self.changing_sequence:
    367375            self.toggle_form()
    368         self.change_delay = 2
     376        self.start_timer('change_delay')
  • nagslang/screens/area.py

    r365 r371  
    88    COLLISION_TYPE_WALL, COLLISION_TYPE_PLAYER, CALLBACK_COLLIDERS,
    99    COLLISION_TYPE_FURNITURE, COLLISION_TYPE_WEREWOLF_ATTACK,
    10     CMD_TOGGLE_FORM, CMD_ATTACK, CMD_ACTION)
     10    CMD_TOGGLE_FORM, CMD_ACTION)
    1111from nagslang.enemies import DeadEnemy
    1212from nagslang.events import (
     
    2626    }
    2727
     28    attack_keys = set([pygame.locals.K_z, pygame.locals.K_LCTRL])
     29
    2830    command_keys = {
    2931        pygame.locals.K_c: CMD_TOGGLE_FORM,
    30         pygame.locals.K_z: CMD_ATTACK,
    31         pygame.locals.K_LCTRL: CMD_ATTACK,
    3232        pygame.locals.K_SPACE: CMD_ACTION,
    3333    }
     
    5555                dy += ty
    5656        return (dx, dy)
     57
     58    def is_attacking(self):
     59        return bool(self.keys_down & self.attack_keys)
    5760
    5861    def get_command_key(self, key):
     
    234237        dx, dy = self.keys.get_direction()
    235238        self.protagonist.set_direction(dx, dy)
     239        if self.keys.is_attacking():
     240            self.protagonist.handle_attack_key_down()
    236241
    237242    def tick(self, seconds):
Note: See TracChangeset for help on using the changeset viewer.