Mercurial > skaapsteker
annotate skaapsteker/sprites/base.py @ 249:30ae3c681507
Doors and stuff.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Thu, 07 Apr 2011 22:20:58 +0200 |
parents | 129afb4417cf |
children | 8d7edd77bfbf |
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 |
35 | 4 |
248 | 5 from pygame import Rect |
249 | 6 import pygame.transform |
248 | 7 |
8 from ..physics import Sprite | |
9 from ..constants import Layers | |
10 from .. import data | |
11 from .. import dialogue | |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
12 |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
13 |
35 | 14 TILE_SIZE = (64, 64) |
15 | |
97
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
16 # 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
|
17 PC_LAYER = 0 |
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
18 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
|
19 NPC_LAYER = 2 |
97
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
20 |
35 | 21 |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
22 |
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
23 class GameSprite(Sprite): |
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
24 image_dir = 'sprites/' |
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
25 image_file = None |
97
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
26 |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
27 def __init__(self, pos, **opts): |
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
28 Sprite.__init__(self) |
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
29 self.image = data.load_image(self.image_dir + self.image_file) |
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
30 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
|
31 self.collide_rect = self.rect.move(0, 0) |
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
32 self.setup(**opts) |
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
33 |
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
34 |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
35 class AnimatedGameSprite(Sprite): |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
36 # folder for animation files, e.g. sprites/foo |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
37 image_dir = None |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
38 |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
39 # first item is the starting animation |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
40 animation_regexes = [ |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
41 # 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
|
42 ("running", r"^.*_\d+.png$"), |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
43 ("standing", r"^.*_standing.png$"), |
236
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
44 ("attacking", r"^.*_attacking.png$"), |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
45 ] |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
46 |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
47 wants_updates = True |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
48 |
239
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
49 facings = {} |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
50 |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
51 def __init__(self, pos, **opts): |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
52 Sprite.__init__(self) |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
53 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
|
54 self._frame = 0 |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
55 self._tick = 0 # TODO: hack to show some animation; kill shortly |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
56 self._animation = self.animation_regexes[0][0] |
239
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
57 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
|
58 self.facing = self.facings[self._animation][0][0] |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
59 else: |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
60 self.facing = None |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
61 |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
62 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
|
63 for name, pattern in self.animation_regexes: |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
64 if re.match(pattern, image): |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
65 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
|
66 if self.facings and name in self.facings: |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
67 if not self._animations[name]: |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
68 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
|
69 for facing, transform in self.facings[name]: |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
70 if transform: |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
71 mod_img = transform(img) |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
72 else: |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
73 mod_img = img |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
74 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
|
75 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
|
76 else: |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
77 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
|
78 self._animations[name].append((img, collide_rect)) |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
79 |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
80 self.collide_rect = Rect((0, 0), (2, 2)) |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
81 self.collide_rect.midbottom = (pos[0]*TILE_SIZE[0]+TILE_SIZE[0]/2, (pos[1]+1)*TILE_SIZE[1]) |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
82 self._update_image() |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
83 self.setup(**opts) |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
84 |
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
|
85 def _update_image(self, force=False): |
239
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
86 if self.facing: |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
87 images = self._animations[self._animation][self.facing] |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
88 else: |
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
89 images = self._animations[self._animation] |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
90 if self._frame >= len(images): |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
91 self._frame = 0 |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
92 cand_image, cand_collide_rect = images[self._frame] |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
93 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
|
94 |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
95 cur_pos = self.collide_rect.midbottom |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
96 |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
97 cand_rect = cand_image.get_rect() |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
98 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
|
99 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
|
100 cand_collide_rect.midbottom = cur_pos |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
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 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
|
103 return |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
104 |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
105 self.image = cand_image |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
106 self.collide_rect = cand_collide_rect |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
107 self.rect = cand_rect |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
108 self.rect_offset = cand_rect_offset |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
109 self.init_pos() |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
110 |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
111 def update(self): |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
112 if self._tick > 10: |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
113 self._tick = 0 |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
114 self._frame += 1 |
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
|
115 force = False |
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
|
116 if self._animation == 'attacking': |
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
|
117 force = True |
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
|
118 self._update_image(force) |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
119 self._tick += 1 |
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
120 |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
121 |
236
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
122 class Monster(AnimatedGameSprite): |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
123 |
97
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
124 collision_layer = MONSTER_LAYER |
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
88
diff
changeset
|
125 collides_with = set([PC_LAYER]) |
47 | 126 |
117
b361d9e17c26
Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
127 debug_color = (240, 120, 120) |
b361d9e17c26
Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
128 |
152
60138b935bc0
Make enemies block by default, so we can jump off them
Neil Muller <drnlmuller@gmail.com>
parents:
144
diff
changeset
|
129 block = True |
60138b935bc0
Make enemies block by default, so we can jump off them
Neil Muller <drnlmuller@gmail.com>
parents:
144
diff
changeset
|
130 |
236
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
131 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
|
132 attack_damage = 1 |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
133 |
25
fe87d828d093
Very basic enemy support.
Jeremy Thurgood <firxen@gmail.com>
parents:
22
diff
changeset
|
134 def __init__(self, pos, **opts): |
236
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
135 AnimatedGameSprite.__init__(self, pos, **opts) |
186
72e92893ccb8
Use layers for floor check
Neil Muller <drnlmuller@gmail.com>
parents:
174
diff
changeset
|
136 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
|
137 self._layer = Layers.PLAYER |
206
e2acf4663065
Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
138 self.health = 10 |
203
0a793c4ac341
Add default health values
Neil Muller <drnlmuller@gmail.com>
parents:
202
diff
changeset
|
139 self.setup(**opts) |
25
fe87d828d093
Very basic enemy support.
Jeremy Thurgood <firxen@gmail.com>
parents:
22
diff
changeset
|
140 |
fe87d828d093
Very basic enemy support.
Jeremy Thurgood <firxen@gmail.com>
parents:
22
diff
changeset
|
141 |
208
c72d9bf911fb
Add start of player damage
Neil Muller <drnlmuller@gmail.com>
parents:
206
diff
changeset
|
142 def collided_player(self, player): |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
143 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
|
144 self.start_attack(player) |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
145 |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
146 def update(self): |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
147 AnimatedGameSprite.update(self) |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
148 if self._animation == 'attacking': |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
149 if self._frame == 0 and self._tick == 1: |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
150 # FIXME: This will need to change when AnimatedGameSprite changes |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
151 # 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
|
152 self._animation = self._old_state |
239
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
153 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
|
154 self._update_image(True) |
236
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
155 elif self._frame == self.attack_frame and self._tick == 5: |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
156 # Attack the player |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
157 self.do_attack() |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
158 |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
159 def do_attack(self): |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
160 """Overriden by monster classes""" |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
161 if self.check_collides(self._target): |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
162 self._target.damage(self.attack_damage) |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
163 |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
164 def start_attack(self, player): |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
165 if self._animation == 'attacking': |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
166 return # We're already attacking |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
167 elif self.attack_frame is not None: |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
168 self._target = player |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
169 self._old_state = self._animation |
239
6a2f366d5e62
Add facing support for attack animations
Neil Muller <drnlmuller@gmail.com>
parents:
236
diff
changeset
|
170 self._old_facing = self.facing |
236
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
171 self._animation = 'attacking' |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
172 self._tick = 1 |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
173 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
|
174 self._update_image(True) |
236
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
175 else: |
9528c6fc7f75
Hook up attack anaimation (needs facing support still)
Neil Muller <drnlmuller@gmail.com>
parents:
228
diff
changeset
|
176 player.damage(1) # collision damage |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
177 |
210 | 178 def damage(self, damage): |
179 print 'Damaged by ', damage | |
180 self.health -= damage | |
181 print 'Monster health', self.health | |
182 if self.health < 0: | |
183 self.kill() | |
184 | |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
185 |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
186 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
|
187 |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
188 collision_layer = NPC_LAYER |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
189 collides_with = set([PC_LAYER]) |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
190 |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
191 debug_color = (240, 240, 240) |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
192 |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
193 block = 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
|
194 |
9d08f99b5ddf
Add npcs to gamestate. Update gamestate. Pass world around a bit. Some other stuff.
Simon Cross <hodgestar@gmail.com>
parents:
186
diff
changeset
|
195 def __init__(self, pos, **opts): |
213
c5c4306593d8
Attempt to animate NPCs a bit.
Simon Cross <hodgestar@gmail.com>
parents:
210
diff
changeset
|
196 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
|
197 self._layer = Layers.PLAYER |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
198 |
217
fcc5eca8eaca
Add explicit starting state to avoid it magically appearing later.
Simon Cross <hodgestar@gmail.com>
parents:
214
diff
changeset
|
199 def setup(self, name, world, dsm, state): |
fcc5eca8eaca
Add explicit starting state to avoid it magically appearing later.
Simon Cross <hodgestar@gmail.com>
parents:
214
diff
changeset
|
200 self.dsm = dialogue.DSM(name, world, dsm, state) |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
201 |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
202 |
209 | 203 def collided_player(self, player): |
204 print "%s is ignoring player" % self | |
205 | |
206 | |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
207 class Projectile(GameSprite): |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
208 gravitates = False |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
209 |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
210 |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
211 class Item(GameSprite): |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
212 mobile = False |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
213 gravitates = False |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
214 |
127
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
215 collision_layer = MONSTER_LAYER |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
216 collides_with = set([PC_LAYER]) |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
217 |
191 | 218 portable = True |
127
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
219 |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
220 def __init__(self, pos, **opts): |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
221 GameSprite.__init__(self, pos, **opts) |
127
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
222 self._layer = Layers.PLAYER |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
223 |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
224 |
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
|
225 def setup(self, name, world): |
209 | 226 self.name = name |
227 self.world = world | |
127
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
228 |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
229 |
191 | 230 def get_debug_color(self): |
231 if self.portable: | |
232 return (240, 0, 240) | |
233 return (0, 0, 240) | |
234 | |
235 | |
209 | 236 def collided_player(self, player): |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
237 print "Player touched %s" % self |
209 | 238 player.take_item(self) |
239 | |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
240 |
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
193
diff
changeset
|
241 |
18
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
242 class Geography(Sprite): |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
243 mobile = False |
81974842b38e
Add skeleton for physics and sprites.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
244 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
|
245 collides_with = set([PC_LAYER, MONSTER_LAYER, NPC_LAYER]) |
144
6b488e1351a5
Buggy ground implementation. Make the world less bouncy
Neil Muller <drnlmuller@gmail.com>
parents:
127
diff
changeset
|
246 is_ground = True |
6b488e1351a5
Buggy ground implementation. Make the world less bouncy
Neil Muller <drnlmuller@gmail.com>
parents:
127
diff
changeset
|
247 bounce_factor = (0.0, 0.0) |
28
94d5ebaa912f
Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
248 |
35 | 249 def __init__(self, pos, image): |
250 Sprite.__init__(self) | |
251 self.tile_pos = pos | |
252 self.image = image | |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
253 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
|
254 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
|
255 self.rect = self.image.get_rect() |
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
117
diff
changeset
|
256 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
|
257 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
|
258 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
|
259 self.rect.topleft = pos[0] * TILE_SIZE[0], pos[1] * TILE_SIZE[1] |
35 | 260 |
117
b361d9e17c26
Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
261 def get_debug_color(self): |
b361d9e17c26
Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
262 if self.floor or self.block: |
b361d9e17c26
Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
263 return (240, 240, 0) |
b361d9e17c26
Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
264 return (0, 240, 0) |
b361d9e17c26
Angry fruit salad debug boxes.
Jeremy Thurgood <firxen@gmail.com>
parents:
97
diff
changeset
|
265 |
35 | 266 |
249 | 267 |
268 class Doorway(GameSprite): | |
269 mobile = False | |
270 gravitates = False | |
271 collides_with = set([PC_LAYER]) | |
272 | |
273 image_file = 'torii.png' | |
274 | |
275 debug_color = (120, 240, 120) | |
276 | |
277 def __init__(self, pos, **opts): | |
278 GameSprite.__init__(self, pos, **opts) | |
279 self._layer = Layers.PLAYER | |
280 self.image = pygame.transform.scale(self.image, self.image.get_rect().center) | |
281 self.rect = self.image.get_rect(midbottom=self.rect.midbottom) | |
282 self.collide_rect = self.rect | |
283 | |
284 | |
285 def setup(self, leadsto): | |
286 self.leadsto = leadsto | |
287 print leadsto | |
288 | |
289 | |
290 def collided_player(self, player): | |
291 print "Player touched %s" % self | |
292 from .. import engine, levelscene | |
293 engine.ChangeScene.post((levelscene.LevelScene, self.leadsto)) | |
294 | |
295 | |
296 | |
127
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
297 def find_sprite(descr, mod_name=None): |
28
94d5ebaa912f
Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
298 """Create a sprite object from a dictionary describing it.""" |
94d5ebaa912f
Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
299 descr = descr.copy() |
127
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
300 cls_name = descr.pop("type") |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
301 if mod_name is None: |
e1dd3b785269
Initial game state stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
122
diff
changeset
|
302 mod_name, cls_name = cls_name.rsplit(".", 1) |
28
94d5ebaa912f
Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
303 mod_name = ".".join(["skaapsteker.sprites", mod_name]) |
94d5ebaa912f
Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
304 mod = __import__(mod_name, fromlist=[cls_name]) |
94d5ebaa912f
Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
305 cls = getattr(mod, cls_name) |
94d5ebaa912f
Example for global sprite importing.
Simon Cross <hodgestar@gmail.com>
parents:
25
diff
changeset
|
306 return cls(**descr) |
35 | 307 |