annotate skaapsteker/sprites/player.py @ 244:5bbf90e6a94b

Add minimal support for player death
author Neil Muller <drnlmuller@gmail.com>
date Thu, 07 Apr 2011 18:13:51 +0200
parents d5a1471a38a7
children 129afb4417cf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
1 """Class for dealing with the player"""
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
2
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
3 import pygame.transform
167
f7fb2ee24c70 Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 160
diff changeset
4 import time
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
5
100
3eafceff6293 Have monsters also collide with players as an interim solution while I relax, eat dinner and think about the best way to use the new collision layers.
Simon Cross <hodgestar@gmail.com>
parents: 97
diff changeset
6 from skaapsteker.sprites.base import TILE_SIZE, PC_LAYER, MONSTER_LAYER
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
7 from skaapsteker.physics import Sprite
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
8 from skaapsteker.constants import Layers
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
9 from skaapsteker.data import get_files, load_image
244
5bbf90e6a94b Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents: 212
diff changeset
10 from skaapsteker.engine import PlayerDied
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
11
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
12
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
13 class Player(Sprite):
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
14
97
a1d95c6152a0 Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents: 90
diff changeset
15 collision_layer = PC_LAYER
100
3eafceff6293 Have monsters also collide with players as an interim solution while I relax, eat dinner and think about the best way to use the new collision layers.
Simon Cross <hodgestar@gmail.com>
parents: 97
diff changeset
16 collides_with = set([MONSTER_LAYER])
116
69a97094417a Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents: 115
diff changeset
17 wants_updates = True
97
a1d95c6152a0 Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents: 90
diff changeset
18
206
e2acf4663065 Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents: 204
diff changeset
19 def __init__(self, the_world):
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
20 Sprite.__init__(self)
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
21 self.image = None
122
51bcc909873d Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents: 120
diff changeset
22 self.rect = None
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
23 self._image_dict = {}
120
9b08afeadf06 Animated running.
Simon Cross <hodgestar@gmail.com>
parents: 116
diff changeset
24 self._animation_frame = 0.0
167
f7fb2ee24c70 Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 160
diff changeset
25 self._last_time = time.time()
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
26 # State flags and such
204
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
27 self.attacking = 0
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
28 self.running = False
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
29 self.jumping = False
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
30 self.flying = False
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
31 self._load_images()
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
32 # We muck with these in load for convience, so ensure they're right
206
e2acf4663065 Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents: 204
diff changeset
33 self.the_world = the_world
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
34 self.set_facing('left')
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
35 self.set_image()
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
36 self.set_pos((0, 0))
187
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
37 self._collisions_seen = 0
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
38 self._last_collide = []
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
39 self._layer = Layers.PLAYER
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
40
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
41 def set_image(self):
206
e2acf4663065 Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents: 204
diff changeset
42 key = self._make_key(len(self.the_world.fox.tails))
120
9b08afeadf06 Animated running.
Simon Cross <hodgestar@gmail.com>
parents: 116
diff changeset
43 images = self._image_dict[key]
9b08afeadf06 Animated running.
Simon Cross <hodgestar@gmail.com>
parents: 116
diff changeset
44 if self._animation_frame >= len(images):
9b08afeadf06 Animated running.
Simon Cross <hodgestar@gmail.com>
parents: 116
diff changeset
45 self._animation_frame = 0.0
122
51bcc909873d Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents: 120
diff changeset
46 if self.rect:
151
06be025c821c Use midbottom, rather than bottomleft, when changing fox frames
Neil Muller <drnlmuller@gmail.com>
parents: 149
diff changeset
47 cur_pos = self.collide_rect.midbottom
122
51bcc909873d Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents: 120
diff changeset
48 else:
51bcc909873d Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents: 120
diff changeset
49 cur_pos = (0, 0)
172
bf144d817113 if year in range(1980, 1990): continue # how can we sleep while the kitsune is stuck to the floor?
Simon Cross <hodgestar@gmail.com>
parents: 167
diff changeset
50 # TODO: can save a lot of calculation here by caching collision rects
149
5b4d73d301a1 Don't change image if it will introduce new collisions
Neil Muller <drnlmuller@gmail.com>
parents: 148
diff changeset
51 cand_image = images[int(self._animation_frame)]
172
bf144d817113 if year in range(1980, 1990): continue # how can we sleep while the kitsune is stuck to the floor?
Simon Cross <hodgestar@gmail.com>
parents: 167
diff changeset
52 cand_collide_rect = cand_image.get_bounding_rect(1).inflate(-2,-2)
bf144d817113 if year in range(1980, 1990): continue # how can we sleep while the kitsune is stuck to the floor?
Simon Cross <hodgestar@gmail.com>
parents: 167
diff changeset
53 cand_rect = cand_image.get_rect()
bf144d817113 if year in range(1980, 1990): continue # how can we sleep while the kitsune is stuck to the floor?
Simon Cross <hodgestar@gmail.com>
parents: 167
diff changeset
54 cand_rect_offset = cand_rect.centerx - cand_collide_rect.centerx, cand_rect.bottom - cand_collide_rect.bottom
bf144d817113 if year in range(1980, 1990): continue # how can we sleep while the kitsune is stuck to the floor?
Simon Cross <hodgestar@gmail.com>
parents: 167
diff changeset
55 cand_rect.midbottom = cur_pos[0] + cand_rect_offset[0], cur_pos[1] + cand_rect_offset[1]
151
06be025c821c Use midbottom, rather than bottomleft, when changing fox frames
Neil Muller <drnlmuller@gmail.com>
parents: 149
diff changeset
56 cand_collide_rect.midbottom = cur_pos
204
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
57 # We always allow the attacking animation frames
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
58 if not self.check_collide_rect(cand_collide_rect, cand_rect, cand_image) and not self.attacking:
149
5b4d73d301a1 Don't change image if it will introduce new collisions
Neil Muller <drnlmuller@gmail.com>
parents: 148
diff changeset
59 return
172
bf144d817113 if year in range(1980, 1990): continue # how can we sleep while the kitsune is stuck to the floor?
Simon Cross <hodgestar@gmail.com>
parents: 167
diff changeset
60 self.image = cand_image
bf144d817113 if year in range(1980, 1990): continue # how can we sleep while the kitsune is stuck to the floor?
Simon Cross <hodgestar@gmail.com>
parents: 167
diff changeset
61 self.collide_rect = cand_collide_rect
bf144d817113 if year in range(1980, 1990): continue # how can we sleep while the kitsune is stuck to the floor?
Simon Cross <hodgestar@gmail.com>
parents: 167
diff changeset
62 self.rect = cand_rect
bf144d817113 if year in range(1980, 1990): continue # how can we sleep while the kitsune is stuck to the floor?
Simon Cross <hodgestar@gmail.com>
parents: 167
diff changeset
63 self.rect_offset = cand_rect_offset
144
6b488e1351a5 Buggy ground implementation. Make the world less bouncy
Neil Muller <drnlmuller@gmail.com>
parents: 122
diff changeset
64 self.init_pos()
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
65
116
69a97094417a Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents: 115
diff changeset
66 def update(self):
120
9b08afeadf06 Animated running.
Simon Cross <hodgestar@gmail.com>
parents: 116
diff changeset
67 v_x, v_y = self.velocity
167
f7fb2ee24c70 Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 160
diff changeset
68 # Never animate slower than !7 fps, never faster than ~15 fps
204
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
69 if self.attacking > 0:
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
70 if self._last_time:
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
71 if time.time() - self._last_time > 0.15:
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
72 self._animation_frame += 1
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
73 self.attacking -= 1
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
74 self._last_time = time.time()
167
f7fb2ee24c70 Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 160
diff changeset
75 else:
f7fb2ee24c70 Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 160
diff changeset
76 self._last_time = time.time()
204
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
77 else:
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
78 old_frame = self._animation_frame
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
79 self._animation_frame += abs(v_x) / 300
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
80 time_diff = time.time() - self._last_time
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
81 if int(self._animation_frame) - int(old_frame) > 0:
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
82 # Check time diff
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
83 if time_diff < 0.10:
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
84 # Delay animation frame jump
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
85 self._animation_frame -= abs(v_x) / 300
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
86 else:
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
87 self._last_time = time.time()
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
88 elif time_diff > 0.20:
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
89 # Force animation frame jump
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
90 self._animation_frame = old_frame + 1
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
91 self._last_time = time.time()
167
f7fb2ee24c70 Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 160
diff changeset
92 if abs(v_x) < 80:
148
0474df61d5b1 Clamp very slow horizontal motion
Neil Muller <drnlmuller@gmail.com>
parents: 147
diff changeset
93 # Clamp when we're not moving at least 5 pixel / s
0474df61d5b1 Clamp very slow horizontal motion
Neil Muller <drnlmuller@gmail.com>
parents: 147
diff changeset
94 self.velocity = (0, v_y)
160
d44a66ca3fd9 Don't stand in the air.
Simon Cross <hodgestar@gmail.com>
parents: 158
diff changeset
95 self.running = not self.on_solid # if you're not on something you can't stand
120
9b08afeadf06 Animated running.
Simon Cross <hodgestar@gmail.com>
parents: 116
diff changeset
96 else:
176
57a78f19c673 Hack player.py to have platformer physics
Neil Muller <drnlmuller@gmail.com>
parents: 172
diff changeset
97 self.velocity = (0, v_y) # Standard platformer physics
120
9b08afeadf06 Animated running.
Simon Cross <hodgestar@gmail.com>
parents: 116
diff changeset
98 self.running = True
9b08afeadf06 Animated running.
Simon Cross <hodgestar@gmail.com>
parents: 116
diff changeset
99 self.set_image()
187
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
100 if self._collisions_seen > 2 * len(self._last_collide):
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
101 # Can we find a position "nearby" that reduces the collision
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
102 # surface
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
103 best_move = (0, 0)
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
104 clip_area = 0
200
4e3f9cb49489 Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents: 192
diff changeset
105 for obj in self._last_collide[:]:
4e3f9cb49489 Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents: 192
diff changeset
106 if not obj.collide_rect.colliderect(self.collide_rect):
4e3f9cb49489 Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents: 192
diff changeset
107 # Prune stale objects from the list
4e3f9cb49489 Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents: 192
diff changeset
108 self._last_collide.remove(obj)
4e3f9cb49489 Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents: 192
diff changeset
109 continue
188
30a5f7cf670a Hack'ish logic to move out of colliding with floors better
Neil Muller <drnlmuller@gmail.com>
parents: 187
diff changeset
110 clip = obj.collide_rect.clip(self.collide_rect)
187
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
111 clip_area += clip.width * clip.height
188
30a5f7cf670a Hack'ish logic to move out of colliding with floors better
Neil Muller <drnlmuller@gmail.com>
parents: 187
diff changeset
112 if (obj.floor or obj.block) and \
200
4e3f9cb49489 Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents: 192
diff changeset
113 clip.width > TILE_SIZE[0] / 2 and \
4e3f9cb49489 Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents: 192
diff changeset
114 self.collide_rect.bottom < obj.collide_rect.top + TILE_SIZE[1] / 3:
188
30a5f7cf670a Hack'ish logic to move out of colliding with floors better
Neil Muller <drnlmuller@gmail.com>
parents: 187
diff changeset
115 delta = self.rect.bottom - self.collide_rect.bottom
30a5f7cf670a Hack'ish logic to move out of colliding with floors better
Neil Muller <drnlmuller@gmail.com>
parents: 187
diff changeset
116 self.collide_rect.bottom = obj.collide_rect.top - 1
30a5f7cf670a Hack'ish logic to move out of colliding with floors better
Neil Muller <drnlmuller@gmail.com>
parents: 187
diff changeset
117 self.rect.bottom = self.collide_rect.bottom + delta
200
4e3f9cb49489 Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents: 192
diff changeset
118 self.init_pos()
4e3f9cb49489 Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents: 192
diff changeset
119 return # Jump out of this case
187
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
120 min_area = clip_area
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
121 for attempt in [(0, 2), (2, 0), (-2, 0), (2, 2), (-2, 2)]:
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
122 clip_area = 0
188
30a5f7cf670a Hack'ish logic to move out of colliding with floors better
Neil Muller <drnlmuller@gmail.com>
parents: 187
diff changeset
123 for obj in self._last_collide:
187
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
124 cand_rect = self.collide_rect.move(attempt)
188
30a5f7cf670a Hack'ish logic to move out of colliding with floors better
Neil Muller <drnlmuller@gmail.com>
parents: 187
diff changeset
125 clip = obj.collide_rect.clip(cand_rect)
187
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
126 clip_area += clip.width * clip.height
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
127 if clip_area < min_area:
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
128 min_area = clip_area
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
129 best_move = attempt
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
130 self.collide_rect.move_ip(best_move)
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
131 self.rect.move_ip(best_move)
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
132 self.init_pos()
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
133 self._last_collide = []
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
134 self._collisions_seen = 0
116
69a97094417a Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents: 115
diff changeset
135
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
136 def set_facing(self, new_facing):
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
137 self.facing = new_facing
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
138
149
5b4d73d301a1 Don't change image if it will introduce new collisions
Neil Muller <drnlmuller@gmail.com>
parents: 148
diff changeset
139 def collided(self, other):
210
9a6c711e2fdf KILLhg statushg status
Neil Muller <drnlmuller@gmail.com>
parents: 209
diff changeset
140 if self.attacking and hasattr(other, 'damage'):
9a6c711e2fdf KILLhg statushg status
Neil Muller <drnlmuller@gmail.com>
parents: 209
diff changeset
141 # FIXME: Check if we're facing the right way
9a6c711e2fdf KILLhg statushg status
Neil Muller <drnlmuller@gmail.com>
parents: 209
diff changeset
142 other.damage(5)
187
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
143 if other not in self._last_collide:
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
144 self._last_collide.append(other)
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
145 self._collide_pos = self.collide_rect.midbottom
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
146 self._collisions_seen = 0
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
147 else:
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
148 self._collisions_seen += 1
202
a11325bc5ff0 Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 200
diff changeset
149 if hasattr(other, 'collided_player'):
208
c72d9bf911fb Add start of player damage
Neil Muller <drnlmuller@gmail.com>
parents: 206
diff changeset
150 other.collided_player(self)
244
5bbf90e6a94b Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents: 212
diff changeset
151 print 'Health', self.the_world.fox.cur_health
208
c72d9bf911fb Add start of player damage
Neil Muller <drnlmuller@gmail.com>
parents: 206
diff changeset
152
209
189f7f8ef714 Basic item interaction.
Jeremy Thurgood <firxen@gmail.com>
parents: 208
diff changeset
153
208
c72d9bf911fb Add start of player damage
Neil Muller <drnlmuller@gmail.com>
parents: 206
diff changeset
154 def damage(self, damage):
244
5bbf90e6a94b Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents: 212
diff changeset
155 self.the_world.fox.cur_health -= damage
5bbf90e6a94b Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents: 212
diff changeset
156 if self.the_world.fox.cur_health <= 0:
5bbf90e6a94b Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents: 212
diff changeset
157 PlayerDied.post()
187
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
158
244
5bbf90e6a94b Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents: 212
diff changeset
159 def restore(self):
5bbf90e6a94b Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents: 212
diff changeset
160 """Restore player to max health (for restarting levels, etc.)"""
5bbf90e6a94b Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents: 212
diff changeset
161 self.the_world.fox.cur_health = self.the_world.fox.max_health
122
51bcc909873d Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents: 120
diff changeset
162
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
163 def set_pos(self, pos):
202
a11325bc5ff0 Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 200
diff changeset
164 self.rect.midbottom = pos[0] * TILE_SIZE[0] + self.rect_offset[0], (pos[1] + 1) * TILE_SIZE[1] + self.rect_offset[1]
152
60138b935bc0 Make enemies block by default, so we can jump off them
Neil Muller <drnlmuller@gmail.com>
parents: 151
diff changeset
165 self.collide_rect.midbottom = pos[0] * TILE_SIZE[0], pos[1] * TILE_SIZE[1]
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
166
102
2e913a89e69d Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents: 100
diff changeset
167 def action_left(self):
115
b73724bb93f8 Fix fox facing
Neil Muller <drnlmuller@gmail.com>
parents: 113
diff changeset
168 if self.facing != 'left':
b73724bb93f8 Fix fox facing
Neil Muller <drnlmuller@gmail.com>
parents: 113
diff changeset
169 self.facing = 'left'
b73724bb93f8 Fix fox facing
Neil Muller <drnlmuller@gmail.com>
parents: 113
diff changeset
170 self.set_image()
177
88a78fb9fa82 Tweak some constants to make player motion more comfortable.
Jeremy Thurgood <firxen@gmail.com>
parents: 176
diff changeset
171 self.deltav((-450.0, 0.0))
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
172
102
2e913a89e69d Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents: 100
diff changeset
173 def action_right(self):
115
b73724bb93f8 Fix fox facing
Neil Muller <drnlmuller@gmail.com>
parents: 113
diff changeset
174 if self.facing != 'right':
b73724bb93f8 Fix fox facing
Neil Muller <drnlmuller@gmail.com>
parents: 113
diff changeset
175 self.facing = 'right'
b73724bb93f8 Fix fox facing
Neil Muller <drnlmuller@gmail.com>
parents: 113
diff changeset
176 self.set_image()
177
88a78fb9fa82 Tweak some constants to make player motion more comfortable.
Jeremy Thurgood <firxen@gmail.com>
parents: 176
diff changeset
177 self.deltav((450.0, 0.0))
102
2e913a89e69d Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents: 100
diff changeset
178
2e913a89e69d Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents: 100
diff changeset
179 def action_up(self):
152
60138b935bc0 Make enemies block by default, so we can jump off them
Neil Muller <drnlmuller@gmail.com>
parents: 151
diff changeset
180 if self.on_solid:
177
88a78fb9fa82 Tweak some constants to make player motion more comfortable.
Jeremy Thurgood <firxen@gmail.com>
parents: 176
diff changeset
181 self.deltav((0.0, -self.terminal_velocity[1]))
152
60138b935bc0 Make enemies block by default, so we can jump off them
Neil Muller <drnlmuller@gmail.com>
parents: 151
diff changeset
182 self.on_solid = False
102
2e913a89e69d Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents: 100
diff changeset
183
2e913a89e69d Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents: 100
diff changeset
184 def action_down(self):
103
aaef228b6358 Run kitsune, run! And by the running of the kitsune gravity is restored to normal.
Simon Cross <hodgestar@gmail.com>
parents: 102
diff changeset
185 self.deltav((0.0, 100.0))
102
2e913a89e69d Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents: 100
diff changeset
186
2e913a89e69d Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents: 100
diff changeset
187 def action_fire1(self):
206
e2acf4663065 Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents: 204
diff changeset
188 # FIXME: Use the correct tail properties for this
e2acf4663065 Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents: 204
diff changeset
189 if len(self.the_world.fox.tails) < 2:
204
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
190 # Only have a bite attack
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
191 print 'attacking'
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
192 self.attacking = 2
102
2e913a89e69d Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents: 100
diff changeset
193 print "F1"
2e913a89e69d Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents: 100
diff changeset
194
2e913a89e69d Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents: 100
diff changeset
195 def action_fire2(self):
2e913a89e69d Link up key presses to calls to action functions on player and to quitting level.
Simon Cross <hodgestar@gmail.com>
parents: 100
diff changeset
196 print "F2"
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
197
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
198 def _get_action(self):
204
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
199 if self.attacking:
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
200 return 'attacking'
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
201 if self.running:
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
202 return 'running'
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
203 if self.jumping:
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
204 return 'jumpin'
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
205 return 'standing'
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
206
206
e2acf4663065 Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents: 204
diff changeset
207 def _make_key(self, tails, action=None):
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
208 if action is None:
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
209 action = self._get_action()
206
e2acf4663065 Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents: 204
diff changeset
210 if tails >= 4:
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
211 tails = 4
206
e2acf4663065 Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents: 204
diff changeset
212 elif tails >= 2:
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
213 tails = 2
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
214 return '%s %s %d' % (action, self.facing, tails)
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
215
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
216 def _load_images(self):
158
794565c8f9f4 Load attacking images
Neil Muller <drnlmuller@gmail.com>
parents: 152
diff changeset
217 for action in ['standing', 'running', 'jumping', 'attacking']:
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
218 for tails in [0, 1, 2, 4]:
206
e2acf4663065 Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents: 204
diff changeset
219 directory = 'sprites/kitsune_%s/kitsune_%s_%dtail' % (action, action, tails)
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
220 for facing in ['left', 'right']:
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
221 self.facing = facing
206
e2acf4663065 Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents: 204
diff changeset
222 key = self._make_key(tails, action)
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
223 self._image_dict[key] = []
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
224 for image_file in get_files(directory):
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
225 if image_file.startswith('.'):
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
226 # Skip extra junk for now
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
227 continue
206
e2acf4663065 Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents: 204
diff changeset
228 image = load_image('%s/%s' % (directory, image_file))
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
229 if facing == 'right':
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
230 image = pygame.transform.flip(image, True, False)
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
231 self._image_dict[key].append(image)
209
189f7f8ef714 Basic item interaction.
Jeremy Thurgood <firxen@gmail.com>
parents: 208
diff changeset
232
189f7f8ef714 Basic item interaction.
Jeremy Thurgood <firxen@gmail.com>
parents: 208
diff changeset
233
189f7f8ef714 Basic item interaction.
Jeremy Thurgood <firxen@gmail.com>
parents: 208
diff changeset
234 def take_item(self, item):
189f7f8ef714 Basic item interaction.
Jeremy Thurgood <firxen@gmail.com>
parents: 208
diff changeset
235 my_item = self.the_world.fox.item
189f7f8ef714 Basic item interaction.
Jeremy Thurgood <firxen@gmail.com>
parents: 208
diff changeset
236 if my_item is not None:
189f7f8ef714 Basic item interaction.
Jeremy Thurgood <firxen@gmail.com>
parents: 208
diff changeset
237 print "I already have", my_item
189f7f8ef714 Basic item interaction.
Jeremy Thurgood <firxen@gmail.com>
parents: 208
diff changeset
238 return
189f7f8ef714 Basic item interaction.
Jeremy Thurgood <firxen@gmail.com>
parents: 208
diff changeset
239 getattr(self.the_world.items, item.name).level = "gone"
189f7f8ef714 Basic item interaction.
Jeremy Thurgood <firxen@gmail.com>
parents: 208
diff changeset
240 self.the_world.fox.item = item.name
189f7f8ef714 Basic item interaction.
Jeremy Thurgood <firxen@gmail.com>
parents: 208
diff changeset
241 item.kill()
189f7f8ef714 Basic item interaction.
Jeremy Thurgood <firxen@gmail.com>
parents: 208
diff changeset
242 print "took", item