Mercurial > skaapsteker
annotate skaapsteker/sprites/base.py @ 374:9530b8dbda5f
0 health is DEAD
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Sat, 09 Apr 2011 15:02:44 +0200 |
parents | a2efe5470b79 |
children | 4d6198b68cb9 |
rev | line source |
---|---|
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
1 """Basic sprite classes.""" |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
2 |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
3 import re |
333
e499a10eb41f
Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents:
302
diff
changeset
|
4 import time |
35 | 5 |
248 | 6 from pygame import Rect |
249 | 7 import pygame.transform |
248 | 8 |
9 from ..physics import Sprite | |
10 from ..constants import Layers | |
250
8d7edd77bfbf
Start hooking up NPC interactions.
Simon Cross <hodgestar@gmail.com>
parents:
249
diff
changeset
|
11 from ..engine import OpenDialog |
248 | 12 from .. import data |
13 from .. import dialogue | |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
14 |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
15 |
35 | 16 TILE_SIZE = (64, 64) |
17 | |
97
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
18 # Collision Layers (values are ids not numbers) |
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
19 PC_LAYER = 0 |
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
20 MONSTER_LAYER = 1 |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
21 NPC_LAYER = 2 |
362 | 22 PROJECTILE_LAYER = 3 |
35 | 23 |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
24 |
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
25 class GameSprite(Sprite): |
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
26 image_dir = 'sprites/' |
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
27 image_file = None |
97
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
28 |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
29 def __init__(self, pos, **opts): |
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
30 Sprite.__init__(self) |
264
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
31 self._starting_tile_pos = pos |
367
04b7d8724add
crashing vase bugfix
Adrianna Pińska <adrianna.pinska@gmail.com>
parents:
362
diff
changeset
|
32 self.setup(**opts) |
264
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
33 self.setup_image_data(pos) |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
34 |
342 | 35 def setup(self): |
36 pass | |
264
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
37 |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
38 def setup_image_data(self, pos): |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
39 self.image = data.load_image(self.image_dir + self.image_file) |
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
40 self.rect = self.image.get_rect(midbottom=(pos[0]*TILE_SIZE[0]+TILE_SIZE[0]/2, (pos[1]+1)*TILE_SIZE[1])) |
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
41 self.collide_rect = self.rect.move(0, 0) |
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
42 |
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
43 |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
44 class AnimatedGameSprite(Sprite): |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
45 # folder for animation files, e.g. sprites/foo |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
46 image_dir = None |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
47 |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
48 # first item is the starting animation |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
49 animation_regexes = [ |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
50 # TODO: swap back once we know how to swap |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
51 ("running", r"^.*_\d+.png$"), |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
52 ("standing", r"^.*_standing.png$"), |
236
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
53 ("attacking", r"^.*_attacking.png$"), |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
54 ] |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
55 |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
56 wants_updates = True |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
57 |
333
e499a10eb41f
Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents:
302
diff
changeset
|
58 frame_pause = 0.1 # default time between animation frames |
e499a10eb41f
Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents:
302
diff
changeset
|
59 |
239
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
60 facings = {} |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
61 |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
62 def __init__(self, pos, **opts): |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
63 Sprite.__init__(self) |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
64 self._animations = dict((k, []) for k, r in self.animation_regexes) |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
65 self._frame = 0 |
333
e499a10eb41f
Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents:
302
diff
changeset
|
66 self._last_time = 0 |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
67 self._animation = self.animation_regexes[0][0] |
239
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
68 if self.facings and self._animation in self.facings: |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
69 self.facing = self.facings[self._animation][0][0] |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
70 else: |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
71 self.facing = None |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
72 |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
73 for image in data.get_files(self.image_dir): |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
74 for name, pattern in self.animation_regexes: |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
75 if re.match(pattern, image): |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
76 img = data.load_image("%s/%s" % (self.image_dir, image)) |
239
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
77 if self.facings and name in self.facings: |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
78 if not self._animations[name]: |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
79 self._animations[name] = dict((k, []) for k, t in self.facings[name]) |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
80 for facing, transform in self.facings[name]: |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
81 if transform: |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
82 mod_img = transform(img) |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
83 else: |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
84 mod_img = img |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
85 collide_rect = mod_img.get_bounding_rect(1).inflate(-2,-2) |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
86 self._animations[name][facing].append((mod_img, collide_rect)) |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
87 else: |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
88 collide_rect = img.get_bounding_rect(1).inflate(-2,-2) |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
89 self._animations[name].append((img, collide_rect)) |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
90 |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
91 self.collide_rect = Rect((0, 0), (2, 2)) |
355
5bdb4677510a
Have fireballs appear in the right place.
Simon Cross <hodgestar@gmail.com>
parents:
352
diff
changeset
|
92 if isinstance(pos, pygame.Rect): |
5bdb4677510a
Have fireballs appear in the right place.
Simon Cross <hodgestar@gmail.com>
parents:
352
diff
changeset
|
93 self.collide_rect.midbottom = pos.midbottom |
5bdb4677510a
Have fireballs appear in the right place.
Simon Cross <hodgestar@gmail.com>
parents:
352
diff
changeset
|
94 else: |
5bdb4677510a
Have fireballs appear in the right place.
Simon Cross <hodgestar@gmail.com>
parents:
352
diff
changeset
|
95 self.collide_rect.midbottom = (pos[0]*TILE_SIZE[0]+TILE_SIZE[0]/2, (pos[1]+1)*TILE_SIZE[1]) |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
96 self._update_image() |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
97 self.setup(**opts) |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
98 |
342 | 99 def setup(self): |
100 pass | |
101 | |
242
c30fcf903d29
Add update force parameter, so we can transition out of attacking animations even if it introduce a new collision
Neil Muller <drnlmuller@gmail.com>
parents:
241
diff
changeset
|
102 def _update_image(self, force=False): |
239
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
103 if self.facing: |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
104 images = self._animations[self._animation][self.facing] |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
105 else: |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
106 images = self._animations[self._animation] |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
107 if self._frame >= len(images): |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
108 self._frame = 0 |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
109 cand_image, cand_collide_rect = images[self._frame] |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
110 cand_collide_rect = cand_collide_rect.move(0, 0) # copy collide rect before we move it |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
111 |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
112 cur_pos = self.collide_rect.midbottom |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
113 |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
114 cand_rect = cand_image.get_rect() |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
115 cand_rect_offset = cand_rect.centerx - cand_collide_rect.centerx, cand_rect.bottom - cand_collide_rect.bottom |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
116 cand_rect.midbottom = cur_pos[0] + cand_rect_offset[0], cur_pos[1] + cand_rect_offset[1] |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
117 cand_collide_rect.midbottom = cur_pos |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
118 |
242
c30fcf903d29
Add update force parameter, so we can transition out of attacking animations even if it introduce a new collision
Neil Muller <drnlmuller@gmail.com>
parents:
241
diff
changeset
|
119 if not self.check_collide_rect(cand_collide_rect, cand_rect, cand_image) and not force: |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
120 return |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
121 |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
122 self.image = cand_image |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
123 self.collide_rect = cand_collide_rect |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
124 self.rect = cand_rect |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
125 self.rect_offset = cand_rect_offset |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
126 self.init_pos() |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
127 |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
128 def update(self): |
333
e499a10eb41f
Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents:
302
diff
changeset
|
129 if self._last_time is not None: |
e499a10eb41f
Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents:
302
diff
changeset
|
130 if time.time() - self._last_time > self.frame_pause: |
e499a10eb41f
Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents:
302
diff
changeset
|
131 self._frame += 1 |
e499a10eb41f
Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents:
302
diff
changeset
|
132 self._last_time = time.time() |
e499a10eb41f
Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents:
302
diff
changeset
|
133 force = False |
e499a10eb41f
Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents:
302
diff
changeset
|
134 if self._animation == 'attacking': |
e499a10eb41f
Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents:
302
diff
changeset
|
135 force = True |
e499a10eb41f
Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents:
302
diff
changeset
|
136 self._update_image(force) |
e499a10eb41f
Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents:
302
diff
changeset
|
137 else: |
e499a10eb41f
Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents:
302
diff
changeset
|
138 self._last_time = time.time() |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
139 |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
140 |
236
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
141 class Monster(AnimatedGameSprite): |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
142 |
97
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
143 collision_layer = MONSTER_LAYER |
362 | 144 collides_with = set([PC_LAYER, PROJECTILE_LAYER]) |
47 | 145 |
117
b361d9e17c26
Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
146 debug_color = (240, 120, 120) |
b361d9e17c26
Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
147 |
152
60138b935bc0
Make enemies block by default, so we can jump off them
Neil Muller <drnlmuller@gmail.com>
parents:
144
diff
changeset
|
148 block = True |
60138b935bc0
Make enemies block by default, so we can jump off them
Neil Muller <drnlmuller@gmail.com>
parents:
144
diff
changeset
|
149 |
236
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
150 attack_frame = None # Mark a spefici frame in the animatio n as when the attack lands |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
151 attack_damage = 1 |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
152 |
25
fe87d828d093
Very basic enemy support.
Jeremy Thurgood <firxen@gmail.com>
parents:
22
diff
changeset
|
153 def __init__(self, pos, **opts): |
236
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
154 AnimatedGameSprite.__init__(self, pos, **opts) |
186
72e92893ccb8
Use layers for floor check
Neil Muller <drnlmuller@gmail.com>
parents:
174
diff
changeset
|
155 self.floor_rect = Rect(self.collide_rect.topleft, (self.collide_rect.width, 2)) |
59
1be1ca704346
Add Layers constants. Set Monsters to player's layer by default
Neil Muller <drnlmuller@gmail.com>
parents:
47
diff
changeset
|
156 self._layer = Layers.PLAYER |
206
e2acf4663065
Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
157 self.health = 10 |
333
e499a10eb41f
Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents:
302
diff
changeset
|
158 self._done_attack = False |
203
0a793c4ac341
Add default health values
Neil Muller <drnlmuller@gmail.com>
parents:
202
diff
changeset
|
159 self.setup(**opts) |
25
fe87d828d093
Very basic enemy support.
Jeremy Thurgood <firxen@gmail.com>
parents:
22
diff
changeset
|
160 |
208
c72d9bf911fb
Add start of player damage
Neil Muller <drnlmuller@gmail.com>
parents:
206
diff
changeset
|
161 def collided_player(self, player): |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
162 print "%s collided with player" % self |
236
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
163 self.start_attack(player) |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
164 |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
165 def update(self): |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
166 AnimatedGameSprite.update(self) |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
167 if self._animation == 'attacking': |
352 | 168 if self._frame == 0 and self._done_attack: |
236
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
169 # We've just looped through the animation sequence |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
170 self._animation = self._old_state |
239
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
171 self.facing = self._old_facing |
242
c30fcf903d29
Add update force parameter, so we can transition out of attacking animations even if it introduce a new collision
Neil Muller <drnlmuller@gmail.com>
parents:
241
diff
changeset
|
172 self._update_image(True) |
333
e499a10eb41f
Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents:
302
diff
changeset
|
173 elif self._frame == self.attack_frame and not self._done_attack: |
236
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
174 # Attack the player |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
175 self.do_attack() |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
176 |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
177 def do_attack(self): |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
178 """Overriden by monster classes""" |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
179 if self.check_collides(self._target): |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
180 self._target.damage(self.attack_damage) |
352 | 181 self._done_attack = True |
236
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
182 |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
183 def start_attack(self, player): |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
184 if self._animation == 'attacking': |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
185 return # We're already attacking |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
186 elif self.attack_frame is not None: |
333
e499a10eb41f
Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents:
302
diff
changeset
|
187 self._done_attack = False |
236
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
188 self._target = player |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
189 self._old_state = self._animation |
239
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
190 self._old_facing = self.facing |
236
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
191 self._animation = 'attacking' |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
192 self._frame = 0 # Start the attack from the beginning |
242
c30fcf903d29
Add update force parameter, so we can transition out of attacking animations even if it introduce a new collision
Neil Muller <drnlmuller@gmail.com>
parents:
241
diff
changeset
|
193 self._update_image(True) |
333
e499a10eb41f
Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents:
302
diff
changeset
|
194 self._last_time = self._start_attack_time = time.time() |
236
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
195 else: |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
196 player.damage(1) # collision damage |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
197 |
210 | 198 def damage(self, damage): |
199 print 'Damaged by ', damage | |
200 self.health -= damage | |
201 print 'Monster health', self.health | |
374 | 202 if self.health <= 0: |
210 | 203 self.kill() |
204 | |
373
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
205 class PatrollingMonster(Monster): |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
206 """Monster that collides with horizontal geography""" |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
207 |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
208 debug_color = (120, 240, 120) |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
209 |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
210 patrol_speed = (200, 0) |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
211 |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
212 def update(self): |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
213 Monster.update(self) |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
214 if self._animation == 'running': |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
215 if self.facing == 'left': |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
216 self.velocity = (-self.patrol_speed[0], 0) |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
217 elif self.facing == 'right': |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
218 self.velocity = self.patrol_speed |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
219 |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
220 def collided(self, other): |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
221 Monster.collided(self, other) |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
222 # Check if the object we've collided with is the same height our higher than us |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
223 if other.block and other.collide_rect.bottom <= self.collide_rect.bottom: |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
224 # Change direction |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
225 if self.facing == 'left': |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
226 self.facing = 'right' |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
227 else: |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
228 self.facing = 'left' |
a2efe5470b79
start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents:
370
diff
changeset
|
229 |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
230 |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
231 class NPC(AnimatedGameSprite): |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
232 |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
233 collision_layer = NPC_LAYER |
362 | 234 collides_with = set([]) |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
235 |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
236 debug_color = (240, 240, 240) |
271
56a529a69e97
Only backout / move-off "solid" collisions
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
237 bounce_factor = (0, 0) # NPC's don't bounce by default |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
238 |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
239 block = False |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
271
diff
changeset
|
240 actionable = True |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
241 |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
242 def __init__(self, pos, **opts): |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
243 AnimatedGameSprite.__init__(self, pos, **opts) |
189
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
244 self._layer = Layers.PLAYER |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
245 |
217
fcc5eca8eaca
Add explicit starting state to avoid it magically appearing later.
Simon Cross <hodgestar@gmail.com>
parents:
214
diff
changeset
|
246 def setup(self, name, world, dsm, state): |
302
78220c989e6a
Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents:
296
diff
changeset
|
247 self.name = name |
217
fcc5eca8eaca
Add explicit starting state to avoid it magically appearing later.
Simon Cross <hodgestar@gmail.com>
parents:
214
diff
changeset
|
248 self.dsm = dialogue.DSM(name, world, dsm, state) |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
249 |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
271
diff
changeset
|
250 def player_action(self, player): |
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
271
diff
changeset
|
251 OpenDialog.post(self) |
209 | 252 |
253 | |
336
c8fd82ff0c71
Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents:
335
diff
changeset
|
254 class Projectile(AnimatedGameSprite): |
362 | 255 |
256 collision_layer = PROJECTILE_LAYER | |
257 collides_with = set() | |
258 | |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
259 gravitates = False |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
260 |
362 | 261 DAMAGE = 10 |
262 | |
366 | 263 PROJECTILE_SIZE = (0, 0) # pixels |
264 VELOCITY = (10, 10) # pixels/s | |
265 | |
266 def setup(self, direction, hits, **opts): | |
267 super(Projectile, self).setup(**opts) | |
268 self.facing = direction | |
269 | |
362 | 270 if isinstance(hits, tuple): |
271 self.hits = hits + (Geography,) | |
272 else: | |
273 self.hits = (hits, Geography) | |
274 | |
366 | 275 if self.facing == "left": |
276 shift = (-self.PROJECTILE_SIZE[0] / 2, self.PROJECTILE_SIZE[1]) | |
277 dv = (-self.VELOCITY[0], self.VELOCITY[1]) | |
278 else: | |
279 shift = (self.PROJECTILE_SIZE[0] / 2, self.PROJECTILE_SIZE[1]) | |
280 dv = (self.VELOCITY[0], self.VELOCITY[1]) | |
281 | |
282 self.rect.move_ip(shift) | |
283 self.collide_rect.move_ip(shift) | |
284 self.deltav(dv) | |
285 | |
362 | 286 def explode(self): |
287 self.kill() | |
288 | |
289 def collided(self, other): | |
290 if not isinstance(other, self.hits): | |
291 return | |
292 if hasattr(other, 'damage'): | |
293 other.damage(self.DAMAGE) | |
294 self.explode() | |
295 | |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
296 |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
297 class Item(GameSprite): |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
298 mobile = False |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
299 gravitates = False |
335 | 300 actionable = True |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
301 |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
271
diff
changeset
|
302 collision_layer = NPC_LAYER |
127
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
303 |
335 | 304 debug_color = (240, 0, 240) |
127
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
305 |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
306 def __init__(self, pos, **opts): |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
307 GameSprite.__init__(self, pos, **opts) |
127
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
308 self._layer = Layers.PLAYER |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
309 |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
310 |
193
897eec397cbb
Fix state checks for hattori, ichiro, kaneda and kumiko. In the process provide easier access to other npcs.
Simon Cross <hodgestar@gmail.com>
parents:
191
diff
changeset
|
311 def setup(self, name, world): |
209 | 312 self.name = name |
313 self.world = world | |
335 | 314 self._me = getattr(self.world.items, self.name) |
191 | 315 |
316 | |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
271
diff
changeset
|
317 def player_action(self, player): |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
318 print "Player touched %s" % self |
209 | 319 player.take_item(self) |
320 | |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
321 |
335 | 322 def remove(self): |
323 self._me.level = '_limbo' | |
324 self.kill() | |
325 | |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
326 |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
327 class Geography(Sprite): |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
328 mobile = False |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
329 gravitates = False |
362 | 330 collides_with = set([PC_LAYER, MONSTER_LAYER, NPC_LAYER, PROJECTILE_LAYER]) |
144
6b488e1351a5
Buggy ground implementation. Make the world less bouncy
Neil Muller <drnlmuller@gmail.com>
parents:
127
diff
changeset
|
331 is_ground = True |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
271
diff
changeset
|
332 actionable = False |
144
6b488e1351a5
Buggy ground implementation. Make the world less bouncy
Neil Muller <drnlmuller@gmail.com>
parents:
127
diff
changeset
|
333 bounce_factor = (0.0, 0.0) |
28
94d5ebaa912f
Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
334 |
35 | 335 def __init__(self, pos, image): |
336 Sprite.__init__(self) | |
337 self.tile_pos = pos | |
338 self.image = image | |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
339 self.collide_rect = self.image.get_bounding_rect(1) |
186
72e92893ccb8
Use layers for floor check
Neil Muller <drnlmuller@gmail.com>
parents:
174
diff
changeset
|
340 self.floor_rect = Rect(self.collide_rect.topleft, (self.collide_rect.width, 2)) |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
341 self.rect = self.image.get_rect() |
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
342 self.rect_offset = self.collide_rect.left - self.rect.left, self.rect.top - self.rect.top |
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
343 self.collide_rect.topleft = pos[0] * TILE_SIZE[0] + self.rect_offset[0], pos[1] * TILE_SIZE[1] + self.rect_offset[1] |
186
72e92893ccb8
Use layers for floor check
Neil Muller <drnlmuller@gmail.com>
parents:
174
diff
changeset
|
344 self.floor_rect.topleft = pos[0] * TILE_SIZE[0] + self.rect_offset[0], pos[1] * TILE_SIZE[1] + self.rect_offset[1] |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
345 self.rect.topleft = pos[0] * TILE_SIZE[0], pos[1] * TILE_SIZE[1] |
35 | 346 |
117
b361d9e17c26
Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
347 def get_debug_color(self): |
b361d9e17c26
Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
348 if self.floor or self.block: |
b361d9e17c26
Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
349 return (240, 240, 0) |
b361d9e17c26
Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
350 return (0, 240, 0) |
b361d9e17c26
Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
351 |
35 | 352 |
249 | 353 |
354 class Doorway(GameSprite): | |
355 mobile = False | |
356 gravitates = False | |
357 | |
271
56a529a69e97
Only backout / move-off "solid" collisions
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
358 blocks = False |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
271
diff
changeset
|
359 actionable = True |
271
56a529a69e97
Only backout / move-off "solid" collisions
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
360 |
249 | 361 image_file = 'torii.png' |
362 | |
264
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
363 |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
364 def setup_image_data(self, pos): |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
365 super(Doorway, self).setup_image_data(pos) |
249 | 366 self.image = pygame.transform.scale(self.image, self.image.get_rect().center) |
367 self.rect = self.image.get_rect(midbottom=self.rect.midbottom) | |
368 self.collide_rect = self.rect | |
369 | |
370 | |
264
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
371 def setup(self, facing, leadsto): |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
372 self.facing = facing |
249 | 373 self.leadsto = leadsto |
374 print leadsto | |
375 | |
376 | |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
271
diff
changeset
|
377 def player_action(self, player): |
264
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
378 print "Player touched %s" % self |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
379 from .. import engine, levelscene |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
380 engine.ChangeScene.post((levelscene.LevelScene, self.leadsto)) |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
381 |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
382 |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
383 |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
384 class StartingDoorway(Doorway): |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
271
diff
changeset
|
385 actionable = False |
264
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
386 |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
387 def setup_image_data(self, pos): |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
388 self.image = pygame.Surface((0, 0)) |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
389 self.rect = self.image.get_rect(midbottom=(pos[0]*TILE_SIZE[0]+TILE_SIZE[0]/2, (pos[1]+1)*TILE_SIZE[1])) |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
390 self.collide_rect = self.rect.move(0, 0) |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
391 |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
392 |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
393 def setup(self, facing): |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
394 Doorway.setup(self, facing, None) |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
395 |
afd9256ad682
Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents:
261
diff
changeset
|
396 |
249 | 397 |
127
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
398 def find_sprite(descr, mod_name=None): |
28
94d5ebaa912f
Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
399 """Create a sprite object from a dictionary describing it.""" |
261
7668243695f4
Trial patch for dealing with unicode dict key issue on Windows.
Simon Cross <hodgestar@gmail.com>
parents:
254
diff
changeset
|
400 descr = dict((str(k), v) for k, v in descr.items()) # convert unicode keys |
127
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
401 cls_name = descr.pop("type") |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
402 if mod_name is None: |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
403 mod_name, cls_name = cls_name.rsplit(".", 1) |
28
94d5ebaa912f
Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
404 mod_name = ".".join(["skaapsteker.sprites", mod_name]) |
94d5ebaa912f
Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
405 mod = __import__(mod_name, fromlist=[cls_name]) |
94d5ebaa912f
Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
406 cls = getattr(mod, cls_name) |
94d5ebaa912f
Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
407 return cls(**descr) |
35 | 408 |