annotate skaapsteker/sprites/base.py @ 622:da331c80ec08

Clean up sprite inheritance hierarchy a bit.
author Jeremy Thurgood <firxen@gmail.com>
date Sat, 07 May 2011 13:42:27 +0200
parents 851c8726696c
children 65881746dc20
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
38d2f7c43d86 Sprite-based tiles. \o/
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
5
248
129afb4417cf Some light cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 242
diff changeset
6 from pygame import Rect
249
30ae3c681507 Doors and stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
7 import pygame.transform
248
129afb4417cf Some light cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 242
diff changeset
8
621
851c8726696c More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents: 592
diff changeset
9 from .. import data
851c8726696c More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents: 592
diff changeset
10 from .. import dialogue
851c8726696c More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents: 592
diff changeset
11 from .. import sound
248
129afb4417cf Some light cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 242
diff changeset
12 from ..physics import Sprite
129afb4417cf Some light cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 242
diff changeset
13 from ..constants import Layers
551
40a104ca0a69 Tear kimono
Neil Muller <drnlmuller@gmail.com>
parents: 549
diff changeset
14 from ..engine import OpenDialog, AddSpriteEvent, OpenNotification
621
851c8726696c More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents: 592
diff changeset
15 from ..utils import cadd, csub, cmul, cdiv
18
81974842b38e Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
16
81974842b38e Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
17
35
38d2f7c43d86 Sprite-based tiles. \o/
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
18 TILE_SIZE = (64, 64)
38d2f7c43d86 Sprite-based tiles. \o/
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
19
97
a1d95c6152a0 Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents: 88
diff changeset
20 # 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
21 PC_LAYER = 0
a1d95c6152a0 Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents: 88
diff changeset
22 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
23 NPC_LAYER = 2
362
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
24 PROJECTILE_LAYER = 3
35
38d2f7c43d86 Sprite-based tiles. \o/
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
25
551
40a104ca0a69 Tear kimono
Neil Muller <drnlmuller@gmail.com>
parents: 549
diff changeset
26 def notify(text):
40a104ca0a69 Tear kimono
Neil Muller <drnlmuller@gmail.com>
parents: 549
diff changeset
27 OpenNotification.post(text=text)
40a104ca0a69 Tear kimono
Neil Muller <drnlmuller@gmail.com>
parents: 549
diff changeset
28
202
a11325bc5ff0 Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 193
diff changeset
29
621
851c8726696c More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents: 592
diff changeset
30 def tile_midbottom(pos):
851c8726696c More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents: 592
diff changeset
31 return cadd(cmul(pos, TILE_SIZE), cdiv(TILE_SIZE, (2, 1)))
851c8726696c More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents: 592
diff changeset
32
851c8726696c More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents: 592
diff changeset
33
202
a11325bc5ff0 Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 193
diff changeset
34 class GameSprite(Sprite):
a11325bc5ff0 Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 193
diff changeset
35 image_dir = 'sprites/'
a11325bc5ff0 Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 193
diff changeset
36 image_file = None
622
da331c80ec08 Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 621
diff changeset
37 sprite_layer = None
97
a1d95c6152a0 Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents: 88
diff changeset
38
202
a11325bc5ff0 Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 193
diff changeset
39 def __init__(self, pos, **opts):
a11325bc5ff0 Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 193
diff changeset
40 Sprite.__init__(self)
622
da331c80ec08 Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 621
diff changeset
41 if self.sprite_layer is not None:
da331c80ec08 Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 621
diff changeset
42 # pygame's Sprite class clobbers self._layer in __init__(), so we need to thwart it.
da331c80ec08 Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 621
diff changeset
43 self._layer = self.sprite_layer
da331c80ec08 Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 621
diff changeset
44 self.setup_image_data(pos)
367
04b7d8724add crashing vase bugfix
Adrianna Pińska <adrianna.pinska@gmail.com>
parents: 362
diff changeset
45 self.setup(**opts)
264
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
46
342
8f578fe33fe7 Immolation fox.
Simon Cross <hodgestar@gmail.com>
parents: 336
diff changeset
47 def setup(self):
8f578fe33fe7 Immolation fox.
Simon Cross <hodgestar@gmail.com>
parents: 336
diff changeset
48 pass
264
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
49
622
da331c80ec08 Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 621
diff changeset
50 def get_tile_pos(self):
da331c80ec08 Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 621
diff changeset
51 return cdiv(self.rect.center, TILE_SIZE)
da331c80ec08 Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 621
diff changeset
52
264
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
53 def setup_image_data(self, pos):
202
a11325bc5ff0 Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 193
diff changeset
54 self.image = data.load_image(self.image_dir + self.image_file)
621
851c8726696c More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents: 592
diff changeset
55 self.rect = self.image.get_rect(midbottom=tile_midbottom(pos))
202
a11325bc5ff0 Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 193
diff changeset
56 self.collide_rect = self.rect.move(0, 0)
a11325bc5ff0 Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 193
diff changeset
57
a11325bc5ff0 Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 193
diff changeset
58
622
da331c80ec08 Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 621
diff changeset
59 class AnimatedGameSprite(GameSprite):
213
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
60 # first item is the starting animation
622
da331c80ec08 Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 621
diff changeset
61 animation_regexes = (
213
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
62 # 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
63 ("running", r"^.*_\d+.png$"),
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
64 ("standing", r"^.*_standing.png$"),
236
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
65 ("attacking", r"^.*_attacking.png$"),
622
da331c80ec08 Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 621
diff changeset
66 )
213
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
67
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
68 wants_updates = True
333
e499a10eb41f Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents: 302
diff changeset
69 frame_pause = 0.1 # default time between animation frames
622
da331c80ec08 Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 621
diff changeset
70 facings = None
333
e499a10eb41f Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents: 302
diff changeset
71
239
6a2f366d5e62 Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents: 236
diff changeset
72
622
da331c80ec08 Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 621
diff changeset
73 def setup_image_data(self, pos):
213
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
74 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
75 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
76 self._last_time = 0
213
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
77 self._animation = self.animation_regexes[0][0]
239
6a2f366d5e62 Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents: 236
diff changeset
78 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
79 self.facing = self.facings[self._animation][0][0]
6a2f366d5e62 Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents: 236
diff changeset
80 else:
6a2f366d5e62 Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents: 236
diff changeset
81 self.facing = None
213
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
82
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
83 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
84 for name, pattern in self.animation_regexes:
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
85 if re.match(pattern, image):
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
86 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
87 if self.facings and name in self.facings:
6a2f366d5e62 Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents: 236
diff changeset
88 if not self._animations[name]:
6a2f366d5e62 Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents: 236
diff changeset
89 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
90 for facing, transform in self.facings[name]:
6a2f366d5e62 Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents: 236
diff changeset
91 if transform:
6a2f366d5e62 Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents: 236
diff changeset
92 mod_img = transform(img)
6a2f366d5e62 Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents: 236
diff changeset
93 else:
6a2f366d5e62 Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents: 236
diff changeset
94 mod_img = img
6a2f366d5e62 Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents: 236
diff changeset
95 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
96 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
97 else:
6a2f366d5e62 Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents: 236
diff changeset
98 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
99 self._animations[name].append((img, collide_rect))
213
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
100
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
101 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
102 if isinstance(pos, pygame.Rect):
5bdb4677510a Have fireballs appear in the right place.
Simon Cross <hodgestar@gmail.com>
parents: 352
diff changeset
103 self.collide_rect.midbottom = pos.midbottom
5bdb4677510a Have fireballs appear in the right place.
Simon Cross <hodgestar@gmail.com>
parents: 352
diff changeset
104 else:
621
851c8726696c More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents: 592
diff changeset
105 self.collide_rect.midbottom = tile_midbottom(pos)
213
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
106 self._update_image()
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
107
342
8f578fe33fe7 Immolation fox.
Simon Cross <hodgestar@gmail.com>
parents: 336
diff changeset
108
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
109 def _update_image(self, force=False):
239
6a2f366d5e62 Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents: 236
diff changeset
110 if self.facing:
6a2f366d5e62 Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents: 236
diff changeset
111 images = self._animations[self._animation][self.facing]
6a2f366d5e62 Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents: 236
diff changeset
112 else:
6a2f366d5e62 Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents: 236
diff changeset
113 images = self._animations[self._animation]
213
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
114 if self._frame >= len(images):
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
115 self._frame = 0
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
116 cand_image, cand_collide_rect = images[self._frame]
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
117 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
118
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
119 cur_pos = self.collide_rect.midbottom
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
120
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
121 cand_rect = cand_image.get_rect()
621
851c8726696c More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents: 592
diff changeset
122 cand_rect_offset = csub(cand_rect.midbottom, cand_collide_rect.midbottom)
851c8726696c More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents: 592
diff changeset
123 cand_rect.midbottom = cadd(cur_pos, cand_rect_offset)
213
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
124 cand_collide_rect.midbottom = cur_pos
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
125
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
126 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
127 return
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
128
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
129 self.image = cand_image
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
130 self.collide_rect = cand_collide_rect
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
131 self.rect = cand_rect
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
132 self.rect_offset = cand_rect_offset
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
133 self.init_pos()
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
134
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
135 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
136 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
137 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
138 self._frame += 1
e499a10eb41f Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents: 302
diff changeset
139 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
140 force = False
e499a10eb41f Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents: 302
diff changeset
141 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
142 force = True
e499a10eb41f Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents: 302
diff changeset
143 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
144 else:
e499a10eb41f Time based animation for npcs and monsters. Make oni attack faster
Neil Muller <drnlmuller@gmail.com>
parents: 302
diff changeset
145 self._last_time = time.time()
213
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
146
202
a11325bc5ff0 Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 193
diff changeset
147
236
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
148 class Monster(AnimatedGameSprite):
202
a11325bc5ff0 Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 193
diff changeset
149
622
da331c80ec08 Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 621
diff changeset
150 sprite_layer = Layers.PLAYER
97
a1d95c6152a0 Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents: 88
diff changeset
151 collision_layer = MONSTER_LAYER
362
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
152 collides_with = set([PC_LAYER, PROJECTILE_LAYER])
47
215e2e74c244 Better dummy monster.
Jeremy Thurgood <firxen@gmail.com>
parents: 35
diff changeset
153
117
b361d9e17c26 Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents: 97
diff changeset
154 debug_color = (240, 120, 120)
b361d9e17c26 Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents: 97
diff changeset
155
152
60138b935bc0 Make enemies block by default, so we can jump off them
Neil Muller <drnlmuller@gmail.com>
parents: 144
diff changeset
156 block = True
60138b935bc0 Make enemies block by default, so we can jump off them
Neil Muller <drnlmuller@gmail.com>
parents: 144
diff changeset
157
622
da331c80ec08 Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 621
diff changeset
158 attack_frame = None # Mark a speficic frame in the animation as when the attack lands
236
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
159 attack_damage = 1
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
160
25
fe87d828d093 Very basic enemy support.
Jeremy Thurgood <firxen@gmail.com>
parents: 22
diff changeset
161 def __init__(self, pos, **opts):
236
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
162 AnimatedGameSprite.__init__(self, pos, **opts)
186
72e92893ccb8 Use layers for floor check
Neil Muller <drnlmuller@gmail.com>
parents: 174
diff changeset
163 self.floor_rect = Rect(self.collide_rect.topleft, (self.collide_rect.width, 2))
206
e2acf4663065 Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
164 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
165 self._done_attack = False
203
0a793c4ac341 Add default health values
Neil Muller <drnlmuller@gmail.com>
parents: 202
diff changeset
166 self.setup(**opts)
25
fe87d828d093 Very basic enemy support.
Jeremy Thurgood <firxen@gmail.com>
parents: 22
diff changeset
167
549
b7f912705adb Fishmonger now mongers fish.
Jeremy Thurgood <firxen@gmail.com>
parents: 538
diff changeset
168 def setup(self, fishmonger_count=False):
b7f912705adb Fishmonger now mongers fish.
Jeremy Thurgood <firxen@gmail.com>
parents: 538
diff changeset
169 self.fishmonger_count = fishmonger_count
b7f912705adb Fishmonger now mongers fish.
Jeremy Thurgood <firxen@gmail.com>
parents: 538
diff changeset
170
208
c72d9bf911fb Add start of player damage
Neil Muller <drnlmuller@gmail.com>
parents: 206
diff changeset
171 def collided_player(self, player):
236
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
172 self.start_attack(player)
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
173
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
174 def update(self):
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
175 AnimatedGameSprite.update(self)
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
176 if self._animation == 'attacking':
352
4e158a7bd119 Stop infinte attacks
Neil Muller <drnlmuller@gmail.com>
parents: 342
diff changeset
177 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
178 # 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
179 self._animation = self._old_state
239
6a2f366d5e62 Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents: 236
diff changeset
180 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
181 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
182 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
183 # Attack the player
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
184 self.do_attack()
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
185
376
4d6198b68cb9 Add projectile launching method to monsters.
Simon Cross <hodgestar@gmail.com>
parents: 374
diff changeset
186 def _launch_projectile(self, cls):
4d6198b68cb9 Add projectile launching method to monsters.
Simon Cross <hodgestar@gmail.com>
parents: 374
diff changeset
187 from .player import Player # avoid circular imports
4d6198b68cb9 Add projectile launching method to monsters.
Simon Cross <hodgestar@gmail.com>
parents: 374
diff changeset
188 if self.facing == 'left':
4d6198b68cb9 Add projectile launching method to monsters.
Simon Cross <hodgestar@gmail.com>
parents: 374
diff changeset
189 pos = pygame.Rect(self.rect.midleft, (0, 0))
4d6198b68cb9 Add projectile launching method to monsters.
Simon Cross <hodgestar@gmail.com>
parents: 374
diff changeset
190 else:
4d6198b68cb9 Add projectile launching method to monsters.
Simon Cross <hodgestar@gmail.com>
parents: 374
diff changeset
191 pos = pygame.Rect(self.rect.midright, (0, 0))
441
f3ccb00df6a4 Add life stealing.
Simon Cross <hodgestar@gmail.com>
parents: 434
diff changeset
192 projectile = cls(pos, direction=self.facing, hits=Player, source=self)
538
c1b0ad1c0932 Hook up projectile sounds.
Simon Cross <hodgestar@gmail.com>
parents: 537
diff changeset
193 projectile.launch()
376
4d6198b68cb9 Add projectile launching method to monsters.
Simon Cross <hodgestar@gmail.com>
parents: 374
diff changeset
194 AddSpriteEvent.post(projectile)
4d6198b68cb9 Add projectile launching method to monsters.
Simon Cross <hodgestar@gmail.com>
parents: 374
diff changeset
195
236
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
196 def do_attack(self):
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
197 """Overriden by monster classes"""
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
198 if self.check_collides(self._target):
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
199 self._target.damage(self.attack_damage)
352
4e158a7bd119 Stop infinte attacks
Neil Muller <drnlmuller@gmail.com>
parents: 342
diff changeset
200 self._done_attack = True
236
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
201
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
202 def start_attack(self, player):
410
115e738e209c Become visible when attacking. Enemies shouldn't attack invisible foxes.
Simon Cross <hodgestar@gmail.com>
parents: 409
diff changeset
203 if player.invisible:
115e738e209c Become visible when attacking. Enemies shouldn't attack invisible foxes.
Simon Cross <hodgestar@gmail.com>
parents: 409
diff changeset
204 return
236
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
205 if self._animation == 'attacking':
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
206 return # We're already attacking
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
207 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
208 self._done_attack = False
236
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
209 self._target = player
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
210 self._old_state = self._animation
239
6a2f366d5e62 Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents: 236
diff changeset
211 self._old_facing = self.facing
236
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
212 self._animation = 'attacking'
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
213 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
214 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
215 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
216 else:
9528c6fc7f75 Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents: 228
diff changeset
217 player.damage(1) # collision damage
18
81974842b38e Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
218
210
9a6c711e2fdf KILLhg statushg status
Neil Muller <drnlmuller@gmail.com>
parents: 209
diff changeset
219 def damage(self, damage):
9a6c711e2fdf KILLhg statushg status
Neil Muller <drnlmuller@gmail.com>
parents: 209
diff changeset
220 self.health -= damage
374
9530b8dbda5f 0 health is DEAD
Neil Muller <drnlmuller@gmail.com>
parents: 373
diff changeset
221 if self.health <= 0:
528
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
222 AddSpriteEvent.post(Skeleton(self.rect.midbottom))
210
9a6c711e2fdf KILLhg statushg status
Neil Muller <drnlmuller@gmail.com>
parents: 209
diff changeset
223 self.kill()
549
b7f912705adb Fishmonger now mongers fish.
Jeremy Thurgood <firxen@gmail.com>
parents: 538
diff changeset
224 if self.fishmonger_count:
b7f912705adb Fishmonger now mongers fish.
Jeremy Thurgood <firxen@gmail.com>
parents: 538
diff changeset
225 self.world.missions.fishmonger_demons_killed += 1
210
9a6c711e2fdf KILLhg statushg status
Neil Muller <drnlmuller@gmail.com>
parents: 209
diff changeset
226
373
a2efe5470b79 start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents: 370
diff changeset
227 class PatrollingMonster(Monster):
a2efe5470b79 start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents: 370
diff changeset
228 """Monster that collides with horizontal geography"""
a2efe5470b79 start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents: 370
diff changeset
229
a2efe5470b79 start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents: 370
diff changeset
230 debug_color = (120, 240, 120)
a2efe5470b79 start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents: 370
diff changeset
231
a2efe5470b79 start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents: 370
diff changeset
232 patrol_speed = (200, 0)
a2efe5470b79 start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents: 370
diff changeset
233
a2efe5470b79 start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents: 370
diff changeset
234 def update(self):
a2efe5470b79 start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents: 370
diff changeset
235 Monster.update(self)
a2efe5470b79 start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents: 370
diff changeset
236 if self._animation == 'running':
a2efe5470b79 start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents: 370
diff changeset
237 if self.facing == 'left':
a2efe5470b79 start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents: 370
diff changeset
238 self.velocity = (-self.patrol_speed[0], 0)
a2efe5470b79 start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents: 370
diff changeset
239 elif self.facing == 'right':
a2efe5470b79 start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents: 370
diff changeset
240 self.velocity = self.patrol_speed
a2efe5470b79 start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents: 370
diff changeset
241
a2efe5470b79 start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents: 370
diff changeset
242 def collided(self, other):
a2efe5470b79 start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents: 370
diff changeset
243 Monster.collided(self, other)
a2efe5470b79 start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents: 370
diff changeset
244 # Check if the object we've collided with is the same height our higher than us
399
488ab520408c Collide with floors
Neil Muller <drnlmuller@gmail.com>
parents: 380
diff changeset
245 if (other.block or other.floor) and other.collide_rect.bottom <= self.collide_rect.bottom:
373
a2efe5470b79 start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents: 370
diff changeset
246 # Change direction
409
ed26bbfec03a Add up-down 'Fying' Oni
Neil Muller <drnlmuller@gmail.com>
parents: 400
diff changeset
247 self.change_facing()
ed26bbfec03a Add up-down 'Fying' Oni
Neil Muller <drnlmuller@gmail.com>
parents: 400
diff changeset
248
ed26bbfec03a Add up-down 'Fying' Oni
Neil Muller <drnlmuller@gmail.com>
parents: 400
diff changeset
249 def change_facing(self):
ed26bbfec03a Add up-down 'Fying' Oni
Neil Muller <drnlmuller@gmail.com>
parents: 400
diff changeset
250 if self.facing == 'left':
ed26bbfec03a Add up-down 'Fying' Oni
Neil Muller <drnlmuller@gmail.com>
parents: 400
diff changeset
251 self.facing = 'right'
ed26bbfec03a Add up-down 'Fying' Oni
Neil Muller <drnlmuller@gmail.com>
parents: 400
diff changeset
252 self._update_image(True)
ed26bbfec03a Add up-down 'Fying' Oni
Neil Muller <drnlmuller@gmail.com>
parents: 400
diff changeset
253 else:
ed26bbfec03a Add up-down 'Fying' Oni
Neil Muller <drnlmuller@gmail.com>
parents: 400
diff changeset
254 self.facing = 'left'
ed26bbfec03a Add up-down 'Fying' Oni
Neil Muller <drnlmuller@gmail.com>
parents: 400
diff changeset
255 self._update_image(True)
373
a2efe5470b79 start of patrolling monsters
Neil Muller <drnlmuller@gmail.com>
parents: 370
diff changeset
256
400
1501d9b7f5dd Don't allow patrolling monsters to walk off edges
Neil Muller <drnlmuller@gmail.com>
parents: 399
diff changeset
257 def check_floors(self, floors):
1501d9b7f5dd Don't allow patrolling monsters to walk off edges
Neil Muller <drnlmuller@gmail.com>
parents: 399
diff changeset
258 """If we're only on 1 floor tile, and our centre is beyond half way,
1501d9b7f5dd Don't allow patrolling monsters to walk off edges
Neil Muller <drnlmuller@gmail.com>
parents: 399
diff changeset
259 turn back"""
592
1386cae4cc15 Remove don't crash if we're colliding with a player after being bounced off the floor
Neil Muller <drnlmuller@gmail.com>
parents: 584
diff changeset
260 if len(floors) != 1:
400
1501d9b7f5dd Don't allow patrolling monsters to walk off edges
Neil Muller <drnlmuller@gmail.com>
parents: 399
diff changeset
261 return
1501d9b7f5dd Don't allow patrolling monsters to walk off edges
Neil Muller <drnlmuller@gmail.com>
parents: 399
diff changeset
262
1501d9b7f5dd Don't allow patrolling monsters to walk off edges
Neil Muller <drnlmuller@gmail.com>
parents: 399
diff changeset
263 floor = floors[0]
1501d9b7f5dd Don't allow patrolling monsters to walk off edges
Neil Muller <drnlmuller@gmail.com>
parents: 399
diff changeset
264
1501d9b7f5dd Don't allow patrolling monsters to walk off edges
Neil Muller <drnlmuller@gmail.com>
parents: 399
diff changeset
265 if self.facing == 'left':
1501d9b7f5dd Don't allow patrolling monsters to walk off edges
Neil Muller <drnlmuller@gmail.com>
parents: 399
diff changeset
266 if self.collide_rect.centerx < floor.collide_rect.centerx:
409
ed26bbfec03a Add up-down 'Fying' Oni
Neil Muller <drnlmuller@gmail.com>
parents: 400
diff changeset
267 self.change_facing()
400
1501d9b7f5dd Don't allow patrolling monsters to walk off edges
Neil Muller <drnlmuller@gmail.com>
parents: 399
diff changeset
268 else:
1501d9b7f5dd Don't allow patrolling monsters to walk off edges
Neil Muller <drnlmuller@gmail.com>
parents: 399
diff changeset
269 if self.collide_rect.centerx > floor.collide_rect.centerx:
409
ed26bbfec03a Add up-down 'Fying' Oni
Neil Muller <drnlmuller@gmail.com>
parents: 400
diff changeset
270 self.change_facing()
18
81974842b38e Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
271
213
c5c4306593d8 Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents: 210
diff changeset
272 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
273
622
da331c80ec08 Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 621
diff changeset
274 sprite_layer = Layers.PLAYER
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
275 collision_layer = NPC_LAYER
362
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
276 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
277
9d08f99b5ddf Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents: 186
diff changeset
278 debug_color = (240, 240, 240)
271
56a529a69e97 Only backout / move-off "solid" collisions
Neil Muller <drnlmuller@gmail.com>
parents: 264
diff changeset
279 bounce_factor = (0, 0) # NPC's don't bounce by default
537
a7b7694644a5 Exclude NPC's from gravity
Neil Muller <drnlmuller@gmail.com>
parents: 534
diff changeset
280 gravitates = False
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
281
202
a11325bc5ff0 Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 193
diff changeset
282 block = False
273
95e2ef31e714 Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents: 271
diff changeset
283 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
284
484
b15b0bd933a5 Fix facing on NPCs and clean up debug prints.
Jeremy Thurgood <firxen@gmail.com>
parents: 475
diff changeset
285 def setup(self, name, world, dsm, state, facing=None):
302
78220c989e6a Add supporting for flicking between speaking NPCs.
Simon Cross <hodgestar@gmail.com>
parents: 296
diff changeset
286 self.name = name
549
b7f912705adb Fishmonger now mongers fish.
Jeremy Thurgood <firxen@gmail.com>
parents: 538
diff changeset
287 self.world = world
217
fcc5eca8eaca Add explicit starting state to avoid it magically appearing later.
Simon Cross <hodgestar@gmail.com>
parents: 214
diff changeset
288 self.dsm = dialogue.DSM(name, world, dsm, state)
461
c6d1165bb16f Can talk your way past the guard
Neil Muller <drnlmuller@gmail.com>
parents: 460
diff changeset
289 self._me = getattr(world.npcs, self.name)
475
18427edff33a Add facing support to npcs
Neil Muller <drnlmuller@gmail.com>
parents: 461
diff changeset
290 self.facing = facing
508
e4b1a79504f2 Force facing
Neil Muller <drnlmuller@gmail.com>
parents: 501
diff changeset
291 self._update_image(True) # Force things to the right image
18
81974842b38e Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
292
273
95e2ef31e714 Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents: 271
diff changeset
293 def player_action(self, player):
95e2ef31e714 Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents: 271
diff changeset
294 OpenDialog.post(self)
209
189f7f8ef714 Basic item interaction.
Jeremy Thurgood <firxen@gmail.com>
parents: 208
diff changeset
295
501
9a16483e49cb Remove 1st set of blocking samuri
Neil Muller <drnlmuller@gmail.com>
parents: 487
diff changeset
296 def remove(self):
9a16483e49cb Remove 1st set of blocking samuri
Neil Muller <drnlmuller@gmail.com>
parents: 487
diff changeset
297 self._me.level = '_limbo'
9a16483e49cb Remove 1st set of blocking samuri
Neil Muller <drnlmuller@gmail.com>
parents: 487
diff changeset
298 self.kill()
9a16483e49cb Remove 1st set of blocking samuri
Neil Muller <drnlmuller@gmail.com>
parents: 487
diff changeset
299
209
189f7f8ef714 Basic item interaction.
Jeremy Thurgood <firxen@gmail.com>
parents: 208
diff changeset
300
460
8b9b4706a4d6 Blocking NPC's block
Neil Muller <drnlmuller@gmail.com>
parents: 441
diff changeset
301 class BlockingNPC(NPC):
8b9b4706a4d6 Blocking NPC's block
Neil Muller <drnlmuller@gmail.com>
parents: 441
diff changeset
302
8b9b4706a4d6 Blocking NPC's block
Neil Muller <drnlmuller@gmail.com>
parents: 441
diff changeset
303 collides_with = set([PC_LAYER])
8b9b4706a4d6 Blocking NPC's block
Neil Muller <drnlmuller@gmail.com>
parents: 441
diff changeset
304 mobile = False
8b9b4706a4d6 Blocking NPC's block
Neil Muller <drnlmuller@gmail.com>
parents: 441
diff changeset
305 block = True
8b9b4706a4d6 Blocking NPC's block
Neil Muller <drnlmuller@gmail.com>
parents: 441
diff changeset
306
487
17e01e201f9b Add default facing to blocking NPCs
Neil Muller <drnlmuller@gmail.com>
parents: 484
diff changeset
307 def setup(self, name, world, dsm, state, block, facing=None):
475
18427edff33a Add facing support to npcs
Neil Muller <drnlmuller@gmail.com>
parents: 461
diff changeset
308 NPC.setup(self, name, world, dsm, state, facing)
460
8b9b4706a4d6 Blocking NPC's block
Neil Muller <drnlmuller@gmail.com>
parents: 441
diff changeset
309 self.block = block
8b9b4706a4d6 Blocking NPC's block
Neil Muller <drnlmuller@gmail.com>
parents: 441
diff changeset
310 self._animation = 'standing'
8b9b4706a4d6 Blocking NPC's block
Neil Muller <drnlmuller@gmail.com>
parents: 441
diff changeset
311
8b9b4706a4d6 Blocking NPC's block
Neil Muller <drnlmuller@gmail.com>
parents: 441
diff changeset
312
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
313 class Projectile(AnimatedGameSprite):
362
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
314
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
315 collision_layer = PROJECTILE_LAYER
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
316 collides_with = set()
538
c1b0ad1c0932 Hook up projectile sounds.
Simon Cross <hodgestar@gmail.com>
parents: 537
diff changeset
317 launch_sound = None, None
362
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
318
18
81974842b38e Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
319 gravitates = False
81974842b38e Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
320
362
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
321 DAMAGE = 10
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
322
366
249ba3bd6904 Very, very frightening.
Simon Cross <hodgestar@gmail.com>
parents: 362
diff changeset
323 PROJECTILE_SIZE = (0, 0) # pixels
249ba3bd6904 Very, very frightening.
Simon Cross <hodgestar@gmail.com>
parents: 362
diff changeset
324 VELOCITY = (10, 10) # pixels/s
249ba3bd6904 Very, very frightening.
Simon Cross <hodgestar@gmail.com>
parents: 362
diff changeset
325
441
f3ccb00df6a4 Add life stealing.
Simon Cross <hodgestar@gmail.com>
parents: 434
diff changeset
326 def setup(self, direction, hits, source, **opts):
366
249ba3bd6904 Very, very frightening.
Simon Cross <hodgestar@gmail.com>
parents: 362
diff changeset
327 super(Projectile, self).setup(**opts)
249ba3bd6904 Very, very frightening.
Simon Cross <hodgestar@gmail.com>
parents: 362
diff changeset
328 self.facing = direction
441
f3ccb00df6a4 Add life stealing.
Simon Cross <hodgestar@gmail.com>
parents: 434
diff changeset
329 self.source = source # source of the projectile (may be None)
380
a0afc7c1a4dc Force projectile image to have right facing
Neil Muller <drnlmuller@gmail.com>
parents: 376
diff changeset
330 self._update_image(True) # ensure we get the direction right
538
c1b0ad1c0932 Hook up projectile sounds.
Simon Cross <hodgestar@gmail.com>
parents: 537
diff changeset
331 if self.launch_sound[0]:
c1b0ad1c0932 Hook up projectile sounds.
Simon Cross <hodgestar@gmail.com>
parents: 537
diff changeset
332 sound.load_sound(self.launch_sound[0], self.launch_sound[0], self.launch_sound[1])
366
249ba3bd6904 Very, very frightening.
Simon Cross <hodgestar@gmail.com>
parents: 362
diff changeset
333
362
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
334 if isinstance(hits, tuple):
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
335 self.hits = hits + (Geography,)
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
336 else:
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
337 self.hits = (hits, Geography)
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
338
366
249ba3bd6904 Very, very frightening.
Simon Cross <hodgestar@gmail.com>
parents: 362
diff changeset
339 if self.facing == "left":
621
851c8726696c More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents: 592
diff changeset
340 shift = cdiv(self.PROJECTILE_SIZE, (-2, 1))
851c8726696c More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents: 592
diff changeset
341 dv = cmul(self.VELOCITY, (-1, 1))
366
249ba3bd6904 Very, very frightening.
Simon Cross <hodgestar@gmail.com>
parents: 362
diff changeset
342 else:
621
851c8726696c More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents: 592
diff changeset
343 shift = cdiv(self.PROJECTILE_SIZE, (2, 1))
851c8726696c More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents: 592
diff changeset
344 dv = cmul(self.VELOCITY, (1, 1))
366
249ba3bd6904 Very, very frightening.
Simon Cross <hodgestar@gmail.com>
parents: 362
diff changeset
345
249ba3bd6904 Very, very frightening.
Simon Cross <hodgestar@gmail.com>
parents: 362
diff changeset
346 self.rect.move_ip(shift)
249ba3bd6904 Very, very frightening.
Simon Cross <hodgestar@gmail.com>
parents: 362
diff changeset
347 self.collide_rect.move_ip(shift)
249ba3bd6904 Very, very frightening.
Simon Cross <hodgestar@gmail.com>
parents: 362
diff changeset
348 self.deltav(dv)
249ba3bd6904 Very, very frightening.
Simon Cross <hodgestar@gmail.com>
parents: 362
diff changeset
349
538
c1b0ad1c0932 Hook up projectile sounds.
Simon Cross <hodgestar@gmail.com>
parents: 537
diff changeset
350 def launch(self):
c1b0ad1c0932 Hook up projectile sounds.
Simon Cross <hodgestar@gmail.com>
parents: 537
diff changeset
351 if self.launch_sound[0]:
c1b0ad1c0932 Hook up projectile sounds.
Simon Cross <hodgestar@gmail.com>
parents: 537
diff changeset
352 sound.play_sound(self.launch_sound[0])
c1b0ad1c0932 Hook up projectile sounds.
Simon Cross <hodgestar@gmail.com>
parents: 537
diff changeset
353
362
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
354 def explode(self):
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
355 self.kill()
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
356
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
357 def collided(self, other):
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
358 if not isinstance(other, self.hits):
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
359 return
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
360 if hasattr(other, 'damage'):
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
361 other.damage(self.DAMAGE)
584
b8bed508036f Don't steal life from skulls
Stefano Rivera <stefano@rivera.za.net>
parents: 562
diff changeset
362 if hasattr(self.source, 'steal_life'):
b8bed508036f Don't steal life from skulls
Stefano Rivera <stefano@rivera.za.net>
parents: 562
diff changeset
363 self.source.steal_life(self.DAMAGE)
362
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
364 self.explode()
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
365
18
81974842b38e Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
366
202
a11325bc5ff0 Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 193
diff changeset
367 class Item(GameSprite):
18
81974842b38e Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
368 mobile = False
81974842b38e Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
369 gravitates = False
335
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 333
diff changeset
370 actionable = True
434
827c5d045cf5 Don't allow the cannon to be picked up
Neil Muller <drnlmuller@gmail.com>
parents: 412
diff changeset
371 liftable = True
18
81974842b38e Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
372
622
da331c80ec08 Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 621
diff changeset
373 sprite_layer = Layers.PLAYER
273
95e2ef31e714 Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents: 271
diff changeset
374 collision_layer = NPC_LAYER
127
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 122
diff changeset
375
335
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 333
diff changeset
376 debug_color = (240, 0, 240)
127
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 122
diff changeset
377
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 122
diff changeset
378
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
379 def setup(self, name, world):
209
189f7f8ef714 Basic item interaction.
Jeremy Thurgood <firxen@gmail.com>
parents: 208
diff changeset
380 self.name = name
189f7f8ef714 Basic item interaction.
Jeremy Thurgood <firxen@gmail.com>
parents: 208
diff changeset
381 self.world = world
335
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 333
diff changeset
382 self._me = getattr(self.world.items, self.name)
191
993f4f55eb93 Minor item changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 189
diff changeset
383
993f4f55eb93 Minor item changes.
Jeremy Thurgood <firxen@gmail.com>
parents: 189
diff changeset
384
273
95e2ef31e714 Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents: 271
diff changeset
385 def player_action(self, player):
434
827c5d045cf5 Don't allow the cannon to be picked up
Neil Muller <drnlmuller@gmail.com>
parents: 412
diff changeset
386 if self.liftable:
827c5d045cf5 Don't allow the cannon to be picked up
Neil Muller <drnlmuller@gmail.com>
parents: 412
diff changeset
387 player.take_item(self)
209
189f7f8ef714 Basic item interaction.
Jeremy Thurgood <firxen@gmail.com>
parents: 208
diff changeset
388
202
a11325bc5ff0 Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 193
diff changeset
389
335
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 333
diff changeset
390 def remove(self):
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 333
diff changeset
391 self._me.level = '_limbo'
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 333
diff changeset
392 self.kill()
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 333
diff changeset
393
202
a11325bc5ff0 Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 193
diff changeset
394
18
81974842b38e Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
395 class Geography(Sprite):
81974842b38e Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
396 mobile = False
81974842b38e Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
397 gravitates = False
362
02bf05964619 FIREBALLS!
Simon Cross <hodgestar@gmail.com>
parents: 355
diff changeset
398 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
399 is_ground = True
273
95e2ef31e714 Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents: 271
diff changeset
400 actionable = False
144
6b488e1351a5 Buggy ground implementation. Make the world less bouncy
Neil Muller <drnlmuller@gmail.com>
parents: 127
diff changeset
401 bounce_factor = (0.0, 0.0)
28
94d5ebaa912f Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
402
35
38d2f7c43d86 Sprite-based tiles. \o/
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
403 def __init__(self, pos, image):
38d2f7c43d86 Sprite-based tiles. \o/
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
404 Sprite.__init__(self)
38d2f7c43d86 Sprite-based tiles. \o/
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
405 self.tile_pos = pos
38d2f7c43d86 Sprite-based tiles. \o/
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
406 self.image = image
122
51bcc909873d Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents: 117
diff changeset
407 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
408 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
409 self.rect = self.image.get_rect()
621
851c8726696c More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents: 592
diff changeset
410 self.rect_offset = csub(self.collide_rect.topleft, self.rect.topleft)
851c8726696c More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents: 592
diff changeset
411 self.rect.topleft = cmul(pos, TILE_SIZE)
851c8726696c More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents: 592
diff changeset
412 self.collide_rect.topleft = cadd(self.rect.topleft, self.rect_offset)
851c8726696c More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents: 592
diff changeset
413 self.floor_rect.topleft = cadd(self.rect.topleft, self.rect_offset)
35
38d2f7c43d86 Sprite-based tiles. \o/
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
414
117
b361d9e17c26 Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents: 97
diff changeset
415 def get_debug_color(self):
b361d9e17c26 Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents: 97
diff changeset
416 if self.floor or self.block:
b361d9e17c26 Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents: 97
diff changeset
417 return (240, 240, 0)
b361d9e17c26 Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents: 97
diff changeset
418 return (0, 240, 0)
b361d9e17c26 Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents: 97
diff changeset
419
35
38d2f7c43d86 Sprite-based tiles. \o/
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
420
249
30ae3c681507 Doors and stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
421
30ae3c681507 Doors and stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
422 class Doorway(GameSprite):
30ae3c681507 Doors and stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
423 mobile = False
30ae3c681507 Doors and stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
424 gravitates = False
30ae3c681507 Doors and stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
425
271
56a529a69e97 Only backout / move-off "solid" collisions
Neil Muller <drnlmuller@gmail.com>
parents: 264
diff changeset
426 blocks = False
273
95e2ef31e714 Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents: 271
diff changeset
427 actionable = True
271
56a529a69e97 Only backout / move-off "solid" collisions
Neil Muller <drnlmuller@gmail.com>
parents: 264
diff changeset
428
249
30ae3c681507 Doors and stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
429 image_file = 'torii.png'
30ae3c681507 Doors and stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
430
264
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
431
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
432 def setup_image_data(self, pos):
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
433 super(Doorway, self).setup_image_data(pos)
249
30ae3c681507 Doors and stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
434 self.image = pygame.transform.scale(self.image, self.image.get_rect().center)
30ae3c681507 Doors and stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
435 self.rect = self.image.get_rect(midbottom=self.rect.midbottom)
30ae3c681507 Doors and stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
436 self.collide_rect = self.rect
30ae3c681507 Doors and stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
437
30ae3c681507 Doors and stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
438
264
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
439 def setup(self, facing, leadsto):
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
440 self.facing = facing
249
30ae3c681507 Doors and stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
441 self.leadsto = leadsto
30ae3c681507 Doors and stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
442
30ae3c681507 Doors and stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
443
273
95e2ef31e714 Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents: 271
diff changeset
444 def player_action(self, player):
264
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
445 from .. import engine, levelscene
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
446 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
447
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
448
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
449
562
91ad18a5acf1 Celestial Doorway now counts tails.
Jeremy Thurgood <firxen@gmail.com>
parents: 551
diff changeset
450 class CelestialDoorway(Doorway):
91ad18a5acf1 Celestial Doorway now counts tails.
Jeremy Thurgood <firxen@gmail.com>
parents: 551
diff changeset
451 def player_action(self, player):
91ad18a5acf1 Celestial Doorway now counts tails.
Jeremy Thurgood <firxen@gmail.com>
parents: 551
diff changeset
452 from .. import engine
91ad18a5acf1 Celestial Doorway now counts tails.
Jeremy Thurgood <firxen@gmail.com>
parents: 551
diff changeset
453 if len(self.world.fox.tails) < 8:
91ad18a5acf1 Celestial Doorway now counts tails.
Jeremy Thurgood <firxen@gmail.com>
parents: 551
diff changeset
454 engine.OpenNotification.post(text="You need eight tails to enter the Celestial Plane.")
91ad18a5acf1 Celestial Doorway now counts tails.
Jeremy Thurgood <firxen@gmail.com>
parents: 551
diff changeset
455 return
91ad18a5acf1 Celestial Doorway now counts tails.
Jeremy Thurgood <firxen@gmail.com>
parents: 551
diff changeset
456 super(CelestialDoorway, self).player_action(player)
91ad18a5acf1 Celestial Doorway now counts tails.
Jeremy Thurgood <firxen@gmail.com>
parents: 551
diff changeset
457
91ad18a5acf1 Celestial Doorway now counts tails.
Jeremy Thurgood <firxen@gmail.com>
parents: 551
diff changeset
458
91ad18a5acf1 Celestial Doorway now counts tails.
Jeremy Thurgood <firxen@gmail.com>
parents: 551
diff changeset
459
264
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
460 class StartingDoorway(Doorway):
273
95e2ef31e714 Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents: 271
diff changeset
461 actionable = False
264
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
462
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
463 def setup_image_data(self, pos):
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
464 self.image = pygame.Surface((0, 0))
621
851c8726696c More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents: 592
diff changeset
465 self.rect = self.image.get_rect(midbottom=tile_midbottom(pos))
264
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
466 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
467
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
468
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
469 def setup(self, facing):
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
470 Doorway.setup(self, facing, None)
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
471
afd9256ad682 Move between doorways. (Still with hacky collision limiting.)
Jeremy Thurgood <firxen@gmail.com>
parents: 261
diff changeset
472
528
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
473 class Skeleton(GameSprite):
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
474 mobile = False
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
475 gravitates = False
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
476 actionable = False
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
477 liftable = False
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
478 image_dir = 'sprites/skulls/'
622
da331c80ec08 Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 621
diff changeset
479 sprite_layer = Layers.BEHIND
528
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
480 debug_color = (255, 255, 0)
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
481
622
da331c80ec08 Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 621
diff changeset
482
528
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
483 def __init__(self, pos, player=False, **opts):
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
484 self._pos = pos
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
485 if player:
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
486 self.image_file = 'kitsune.png'
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
487 else:
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
488 self.image_file = 'monster.png'
622
da331c80ec08 Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 621
diff changeset
489 super(Skeleton, self).__init__(pos, **opts)
528
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
490
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
491 def setup_image_data(self, pos):
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
492 GameSprite.setup_image_data(self, pos)
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
493 # Pixel based rect, not tile:
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
494 self.rect = self.image.get_rect(midbottom=self._pos)
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
495 self.collide_rect = self.rect.move(0, 0)
52b38b803782 Skeleton for dead monsters
Stefano Rivera <stefano@rivera.za.net>
parents: 508
diff changeset
496
249
30ae3c681507 Doors and stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 248
diff changeset
497
127
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 122
diff changeset
498 def find_sprite(descr, mod_name=None):
28
94d5ebaa912f Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
499 """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
500 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
501 cls_name = descr.pop("type")
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 122
diff changeset
502 if mod_name is None:
e1dd3b785269 Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 122
diff changeset
503 mod_name, cls_name = cls_name.rsplit(".", 1)
28
94d5ebaa912f Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
504 mod_name = ".".join(["skaapsteker.sprites", mod_name])
94d5ebaa912f Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
505 mod = __import__(mod_name, fromlist=[cls_name])
94d5ebaa912f Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
506 cls = getattr(mod, cls_name)
94d5ebaa912f Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents: 25
diff changeset
507 return cls(**descr)
35
38d2f7c43d86 Sprite-based tiles. \o/
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
508