Removed old file
[2dworld.git] / engine / actor.py
blobbb3bd566480fcba2b941537e8dacc96ab4dd27fa
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
4 import tiledtmxloader
5 import sys
6 import os
7 import math
8 import glob
9 import re
10 import pygame
12 CENTER = -1
13 NORTH = 0
14 NORTHEAST = 1
15 EAST = 2
16 SOUTHEAST = 3
17 SOUTH = 4
18 SOUTHWEST = 5
19 WEST = 6
20 NORTHWEST = 7
22 STAND = 0
23 WALK = 1
24 RUN = 2
25 JUMP = 3
26 CLIMB = 4
28 def get_files_natural_order(path):
29 file_list = []
30 for infile in glob.glob( path ):
31 if os.path.isfile(infile):
32 file_list.append(infile)
33 digits = re.compile(r'(\d+)')
34 def tokenize(filename):
35 return tuple(int(token) if match else token
36 for token, match in
37 ((fragment, digits.search(fragment))
38 for fragment in digits.split(filename)))
39 file_list.sort(key=tokenize)
40 return file_list
42 def get_images_from_files(file_list):
43 image_list = []
44 for filename in file_list:
45 try:
46 image = pygame.image.load(filename)
47 print 'Image: ', filename
48 image_list.append(image)
49 except pygame.error, message:
50 print 'Cannot load image: ', filename
51 return image_list
53 def create_image(width, height, red, green, blue, alpha):
54 image = pygame.Surface((width, height), pygame.SRCALPHA)
55 image.fill((red, green, blue, alpha))
56 return image
58 def get_images(directory, selection):
59 if isinstance(selection, (list, tuple)):
60 img = []
61 for element in selection:
62 img.extend(get_images(directory, element))
63 return img
64 else:
65 return get_images_from_files(get_files_natural_order(os.path.join(directory, selection)))
67 def get_mirror_images(source_images, flip_horizontal, flip_vertical):
68 destination_images = []
69 for image in source_images:
70 destination_images.append(pygame.transform.flip(image, flip_horizontal, flip_vertical))
71 return destination_images
73 def fill_images(img):
74 if not EAST in img and WEST in img:
75 img[EAST] = get_mirror_images(img[WEST], True, False)
76 if not WEST in img and not EAST in img and SOUTH in img:
77 img[EAST] = img[SOUTH]
78 img[WEST] = img[SOUTH]
79 if not WEST in img and not EAST in img and NORTH in img:
80 img[EAST] = img[NORTH]
81 img[WEST] = img[NORTH]
82 if not NORTH in img and SOUTH in img:
83 img[NORTH] = img[SOUTH]
84 if not SOUTH in img and NORTH in img:
85 img[SOUTH] = img[NORTH]
86 if not WEST in img and EAST in img:
87 img[WEST] = get_mirror_images(img[EAST], True, False)
88 if not SOUTHWEST in img and WEST in img:
89 img[SOUTHWEST] = img[WEST]
90 if not SOUTHEAST in img and EAST in img:
91 img[SOUTHEAST] = img[EAST]
92 if not NORTHWEST in img and WEST in img:
93 img[NORTHWEST] = img[WEST]
94 if not NORTHEAST in img and EAST in img:
95 img[NORTHEAST] = img[EAST]
96 for dir in [ NORTH, SOUTH, EAST, WEST, NORTHEAST, NORTHWEST, SOUTHEAST, SOUTHWEST ]:
97 if not dir in img:
98 if not 'fake_img' in locals():
99 fake_img = create_image(25, 45, 255, 0, 0, 150)
100 img[dir] = [ fake_img ]
103 class avatar(tiledtmxloader.helperspygame.SpriteLayer.Sprite):
104 def __init__(self, directory):
105 self.ch_speed = 100
106 self.load(directory)
107 try:
108 self.images_standing
109 except AttributeError:
110 self.images_standing = { }
111 fill_images(self.images_standing)
112 try:
113 fill_images(self.images_walking)
114 except AttributeError:
115 self.images_walking = self.images_standing
116 try:
117 fill_images(self.images_running)
118 except AttributeError:
119 self.images_running = self.images_walking
121 self.images = self.images_standing
122 try:
123 image = self.images[self.direction][self.frame_num]
124 rect = image.get_rect()
125 except:
126 image = None
127 rect = None
128 super(avatar, self).__init__(image, rect)
129 def load(self, directory):
130 self.images_standing = { }
131 self.images_walking = { }
132 self.images_running = { }
133 def lookat(self, direction):
134 midbottom = self.rect.midbottom
135 if (direction != CENTER):
136 self.direction = direction
137 if (self.frame_num >= len(self.images[self.direction])):
138 self.frame_num=0;
139 image = self.images[self.direction][self.frame_num]
140 rect = image.get_rect()
141 rect.midbottom = midbottom
142 self.image = image
143 self.rect = rect
144 def setaction(self, action):
145 self.images = self.images_standing
146 if (action == WALK):
147 self.images = self.images_walking
148 elif (action == RUN):
149 self.images = self.images_running
150 if (self.frame_num >= len(self.images[self.direction])):
151 self.frame_num=0;
152 def update(self, inc_x, inc_y, action_type):
153 direction = [NORTHWEST, NORTH, NORTHEAST, WEST, CENTER, EAST, SOUTHWEST, SOUTH, SOUTHEAST] \
154 [4 + cmp(inc_x, 0) + cmp(inc_y, 0) * 3]
155 self.ch_speed = 100
156 if action_type == STAND or direction == CENTER:
157 self.setaction(STAND)
158 elif action_type == RUN:
159 self.setaction(RUN)
160 self.ch_speed = 75
161 else:
162 self.setaction(WALK)
163 self.lookat(direction)
164 tick = pygame.time.get_ticks()
165 if (tick - self.last_tick > self.ch_speed):
166 self.last_tick = tick;
167 self.frame_num += 1;
168 if (self.frame_num >= len(self.images[self.direction])):
169 self.frame_num=0;
170 def moveto(self, pos_x, pos_y):
171 self.rect.midbottom = (pos_x, pos_y)