view skaapsteker/sprites/player.py @ 100:3eafceff6293

Have monsters also collide with players as an interim solution while I relax, eat dinner and think about the best way to use the new collision layers.
author Simon Cross <hodgestar@gmail.com>
date Mon, 04 Apr 2011 19:20:05 +0200
parents a1d95c6152a0
children 2e913a89e69d
line wrap: on
line source

"""Class for dealing with the player"""

import pygame.transform
import os

from skaapsteker.sprites.base import TILE_SIZE, PC_LAYER, MONSTER_LAYER
from skaapsteker.physics import Sprite
from skaapsteker.constants import Layers
from skaapsteker.data import get_files, load_image


class Player(Sprite):

    collision_layer = PC_LAYER
    collides_with = set([MONSTER_LAYER])

    def __init__(self):
        Sprite.__init__(self)
        self.image = None
        self._image_dict = {}
        # State flags and such
        self.running = False
        self.jumping = False
        self.flying = False
        self._load_images()
        # We muck with these in load for convience, so ensure they're right
        self.tails = 0
        self.set_facing('left')
        self.set_image()
        self.set_pos((0, 0))
        self._layer = Layers.PLAYER

    def set_image(self):
        key = self._make_key()
        # TODO: handle animations
        self.image = self._image_dict[key][0]

    def set_facing(self, new_facing):
        self.facing = new_facing

    def set_pos(self, pos):
        self.starting_tile_pos = pos
        self.rect = self.image.get_rect(topleft=(pos[0]*TILE_SIZE[0], pos[1]*TILE_SIZE[1]))


    def dispatch(self, ev):
        pass

    def _get_action(self):
        if self.running:
            return 'running'
        if self.jumping:
            return 'jumpin'
        return 'standing'


    def _make_key(self, action=None):
        if action is None:
            action = self._get_action()
        tails = self.tails
        if self.tails >= 4:
            tails = 4
        elif self.tails >= 2:
            tails = 2
        return '%s %s %d' % (action, self.facing, tails)

    def _load_images(self):
        for action in ['standing', 'running', 'jumping']:
            for tails in [0, 1, 2, 4]:
                self.tails = tails
                directory = os.path.join('sprites',
                        'kitsune_%s' % action,
                        'kitsune_%s_%dtail' % (action, tails))
                for facing in ['left', 'right']:
                    self.facing = facing
                    key = self._make_key(action)
                    self._image_dict[key] = []
                    for image_file in get_files(directory):
                        if image_file.startswith('.'):
                            # Skip extra junk for now
                            continue
                        image = load_image(os.path.join(directory, image_file))
                        if facing == 'right':
                            image = pygame.transform.flip(image, True, False)
                        self._image_dict[key].append(image)