Changeset 336:1d487646a4d4


Ignore:
Timestamp:
Sep 6, 2013, 10:22:02 AM (7 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Message:

Better key handling, form change delay.

Location:
nagslang
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • nagslang/constants.py

    r333 r336  
    5555BULLET_DAMAGE = 10
    5656CLAW_DAMAGE = 5
     57
     58CMD_TOGGLE_FORM = 'toggle_form'
     59CMD_ATTACK = 'attack'
     60CMD_ACTION = 'action'
  • nagslang/protagonist.py

    r335 r336  
    66    COLLISION_TYPE_PLAYER, ZORDER_MID, WEREWOLF_SOAK_FACTOR,
    77    PROTAGONIST_HEALTH_MIN_LEVEL, PROTAGONIST_HEALTH_MAX_LEVEL,
    8     NON_GAME_OBJECT_COLLIDERS, BULLET_DAMAGE, CLAW_DAMAGE)
     8    NON_GAME_OBJECT_COLLIDERS, BULLET_DAMAGE, CLAW_DAMAGE,
     9    CMD_TOGGLE_FORM, CMD_ATTACK, CMD_ACTION)
    910from nagslang.events import FireEvent, ClawEvent
    1011from nagslang.game_object import GameObject, Physicser, make_body
     
    4546    WOLF_FORM = 'wolf'
    4647
     48    CHANGING_SEQUENCE = {
     49        # The key is the form we're changing *from*.
     50        HUMAN_FORM: (
     51            WOLF_FORM, HUMAN_FORM, HUMAN_FORM,
     52            WOLF_FORM, HUMAN_FORM, WOLF_FORM,
     53            HUMAN_FORM, WOLF_FORM, HUMAN_FORM,
     54            WOLF_FORM, HUMAN_FORM, WOLF_FORM,
     55            HUMAN_FORM, WOLF_FORM, WOLF_FORM,
     56        ),
     57        WOLF_FORM: (
     58            HUMAN_FORM, WOLF_FORM, WOLF_FORM,
     59            HUMAN_FORM, WOLF_FORM, HUMAN_FORM,
     60            WOLF_FORM, HUMAN_FORM, WOLF_FORM,
     61            HUMAN_FORM, WOLF_FORM, HUMAN_FORM,
     62            WOLF_FORM, HUMAN_FORM, HUMAN_FORM,
     63        ),
     64    }
     65
     66    zorder = ZORDER_MID
     67
    4768    def __init__(self, space, world, position):
    48         physicser = self._make_physics(space, position)
    49         renderer = self._make_renderer()
     69        self.form = self.HUMAN_FORM
     70        super(Protagonist, self).__init__(
     71            self._make_physics(space, position), self._make_renderer())
     72        self.world = world
    5073        self.inventory = {}
    51         self.form = self.HUMAN_FORM
     74        self.health_level = 100
     75
    5276        self.angle = 0
    5377        self.is_moving = False
    54         self.world = world
    55 
    56         super(Protagonist, self).__init__(physicser, renderer)
    57         self.zorder = ZORDER_MID
    58 
    59         self.health_level = 100
     78        self.changing_sequence = []
    6079
    6180        self.go_human()
     
    166185        return obj
    167186
     187    def handle_keypress(self, key_command):
     188        if self.changing_sequence:
     189            print "Changing, can't act."
     190            return
     191        if key_command == CMD_TOGGLE_FORM:
     192            self.world.transformations += 1
     193            self.toggle_form()
     194        if key_command == CMD_ATTACK:
     195            self.world.attacks += 1
     196            self.attack()
     197        if key_command == CMD_ACTION:
     198            self.perform_action()
     199
    168200    def get_render_angle(self):
    169201        # No image rotation when rendering, please.
     
    201233
    202234    def set_direction(self, dx, dy):
    203         if (dx, dy) == (0, 0):
     235        if (dx, dy) == (0, 0) or self.changing_sequence:
    204236            self.is_moving = False
    205237            return
     
    221253
    222254    def toggle_form(self):
    223         if self.form == self.WOLF_FORM:
     255        for form in self.CHANGING_SEQUENCE[self.form]:
     256            self.changing_sequence.extend([form] * 3)
     257
     258    def _go_to_next_form(self):
     259        if self.changing_sequence.pop(0) == self.WOLF_FORM:
     260            self.go_werewolf()
     261        else:
    224262            self.go_human()
    225         else:
    226             self.go_werewolf()
    227263
    228264    def get_current_interactible(self):
     
    306342
    307343    def update(self, dt):
     344        if self.changing_sequence:
     345            self._go_to_next_form()
    308346        if int(self.lifetime + dt) > int(self.lifetime):
    309347            if self.form == self.WOLF_FORM:
  • nagslang/screens/area.py

    r333 r336  
    77from nagslang.constants import (
    88    COLLISION_TYPE_WALL, COLLISION_TYPE_PLAYER, CALLBACK_COLLIDERS,
    9     COLLISION_TYPE_FURNITURE, COLLISION_TYPE_WEREWOLF_ATTACK)
     9    COLLISION_TYPE_FURNITURE, COLLISION_TYPE_WEREWOLF_ATTACK,
     10    CMD_TOGGLE_FORM, CMD_ATTACK, CMD_ACTION)
    1011from nagslang.enemies import DeadEnemy
    1112from nagslang.events import (
     
    2526    }
    2627
     28    command_keys = {
     29        pygame.locals.K_c: CMD_TOGGLE_FORM,
     30        pygame.locals.K_z: CMD_ATTACK,
     31        pygame.locals.K_LCTRL: CMD_ATTACK,
     32        pygame.locals.K_SPACE: CMD_ACTION,
     33    }
     34
    2735    def __init__(self):
    2836        self.keys_down = set()
     
    4755                dy += ty
    4856        return (dx, dy)
     57
     58    def get_command_key(self, key):
     59        return self.command_keys.get(key, None)
    4960
    5061
     
    152163            if ev.key == pygame.locals.K_ESCAPE:
    153164                ScreenChange.post('menu')
    154             if ev.key == pygame.locals.K_c:
    155                 self.protagonist.toggle_form()
    156                 self.world.transformations += 1
    157             if ev.key in (pygame.locals.K_z, pygame.locals.K_LCTRL):
    158                 self.world.attacks += 1
    159                 self.protagonist.attack()
    160             if ev.key == pygame.locals.K_SPACE:
    161                 self.protagonist.perform_action()
     165            cmd_key = self.keys.get_command_key(ev.key)
     166            if cmd_key is not None:
     167                self.protagonist.handle_keypress(cmd_key)
    162168        elif DoorEvent.matches(ev):
    163169            self.protagonist.set_position(ev.dest_pos)
Note: See TracChangeset for help on using the changeset viewer.