Mercurial > skaapsteker
annotate skaapsteker/sprites/player.py @ 296:15b2be883a40
Ancient and honorable tea ceremony.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Fri, 08 Apr 2011 22:29:49 +0200 |
parents | c68f2f3efc7f |
children | f29999d1bba6 |
rev | line source |
---|---|
90 | 1 """Class for dealing with the player""" |
2 | |
3 import pygame.transform | |
167
f7fb2ee24c70
Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
160
diff
changeset
|
4 import time |
90 | 5 |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
6 from ..sprites.base import find_sprite, TILE_SIZE, PC_LAYER, MONSTER_LAYER |
248 | 7 from ..physics import Sprite |
8 from ..constants import Layers | |
9 from ..data import get_files, load_image | |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
10 from ..engine import PlayerDied, ItemRepopulationEvent |
90 | 11 |
12 | |
13 class Player(Sprite): | |
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 |
271
56a529a69e97
Only backout / move-off "solid" collisions
Neil Muller <drnlmuller@gmail.com>
parents:
269
diff
changeset
|
19 block = True |
56a529a69e97
Only backout / move-off "solid" collisions
Neil Muller <drnlmuller@gmail.com>
parents:
269
diff
changeset
|
20 |
266 | 21 _max_sprint_time = 2 |
22 | |
263 | 23 def __init__(self, the_world, soundsystem): |
90 | 24 Sprite.__init__(self) |
25 self.image = None | |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
26 self.rect = None |
90 | 27 self._image_dict = {} |
263 | 28 self._soundsystem = soundsystem |
29 self._soundsystem.load_sound('yelp', 'sounds/yelp.ogg') | |
120 | 30 self._animation_frame = 0.0 |
167
f7fb2ee24c70
Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
160
diff
changeset
|
31 self._last_time = time.time() |
90 | 32 # State flags and such |
204
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
33 self.attacking = 0 |
90 | 34 self.running = False |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
35 self.sprinting = 0 |
90 | 36 self.jumping = False |
37 self.flying = False | |
38 self._load_images() | |
39 # 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
|
40 self.the_world = the_world |
90 | 41 self.set_facing('left') |
42 self.set_image() | |
43 self.set_pos((0, 0)) | |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
44 self._collisions_seen = 0 |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
45 self._last_collide = [] |
90 | 46 self._layer = Layers.PLAYER |
47 | |
48 def set_image(self): | |
206
e2acf4663065
Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
49 key = self._make_key(len(self.the_world.fox.tails)) |
120 | 50 images = self._image_dict[key] |
51 if self._animation_frame >= len(images): | |
52 self._animation_frame = 0.0 | |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
53 if self.rect: |
151
06be025c821c
Use midbottom, rather than bottomleft, when changing fox frames
Neil Muller <drnlmuller@gmail.com>
parents:
149
diff
changeset
|
54 cur_pos = self.collide_rect.midbottom |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
55 else: |
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
56 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
|
57 # 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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 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
|
63 cand_collide_rect.midbottom = cur_pos |
204
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
64 # We always allow the attacking animation frames |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
65 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
|
66 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
|
67 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
|
68 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
|
69 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
|
70 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
|
71 self.init_pos() |
90 | 72 |
116
69a97094417a
Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents:
115
diff
changeset
|
73 def update(self): |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
272
diff
changeset
|
74 self._touching_actionables = [] |
120 | 75 v_x, v_y = self.velocity |
167
f7fb2ee24c70
Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
160
diff
changeset
|
76 # 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
|
77 if self.attacking > 0: |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
78 if self._last_time: |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
79 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
|
80 self._animation_frame += 1 |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
81 self.attacking -= 1 |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
82 self._last_time = time.time() |
167
f7fb2ee24c70
Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
160
diff
changeset
|
83 else: |
f7fb2ee24c70
Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
160
diff
changeset
|
84 self._last_time = time.time() |
204
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
85 else: |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
86 old_frame = self._animation_frame |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
87 self._animation_frame += abs(v_x) / 300 |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
88 time_diff = time.time() - self._last_time |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
89 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
|
90 # Check time diff |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
91 if time_diff < 0.10: |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
92 # Delay animation frame jump |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
93 self._animation_frame -= abs(v_x) / 300 |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
94 else: |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
95 self._last_time = time.time() |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
96 elif time_diff > 0.20: |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
97 # Force animation frame jump |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
98 self._animation_frame = old_frame + 1 |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
99 self._last_time = time.time() |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
100 if self.sprinting > 0: |
266 | 101 if (time.time() - self._sprint_start_time) > self._max_sprint_time: |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
102 self.sprinting = 0 |
167
f7fb2ee24c70
Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
160
diff
changeset
|
103 if abs(v_x) < 80: |
148
0474df61d5b1
Clamp very slow horizontal motion
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
104 # 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
|
105 self.velocity = (0, v_y) |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
106 if self.sprinting == 1: |
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
107 self.sprinting = 0 |
160 | 108 self.running = not self.on_solid # if you're not on something you can't stand |
120 | 109 else: |
176
57a78f19c673
Hack player.py to have platformer physics
Neil Muller <drnlmuller@gmail.com>
parents:
172
diff
changeset
|
110 self.velocity = (0, v_y) # Standard platformer physics |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
111 if self.sprinting > 0: |
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
112 self.sprinting = 1 |
120 | 113 self.running = True |
114 self.set_image() | |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
115 if self._collisions_seen > 2 * len(self._last_collide): |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
116 # 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
|
117 # surface |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
118 best_move = (0, 0) |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
119 clip_area = 0 |
200
4e3f9cb49489
Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents:
192
diff
changeset
|
120 for obj in self._last_collide[:]: |
4e3f9cb49489
Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents:
192
diff
changeset
|
121 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
|
122 # Prune stale objects from the list |
4e3f9cb49489
Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents:
192
diff
changeset
|
123 self._last_collide.remove(obj) |
4e3f9cb49489
Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents:
192
diff
changeset
|
124 continue |
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(self.collide_rect) |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
126 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
|
127 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
|
128 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
|
129 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
|
130 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
|
131 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
|
132 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
|
133 self.init_pos() |
4e3f9cb49489
Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents:
192
diff
changeset
|
134 return # Jump out of this case |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
135 min_area = clip_area |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
136 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
|
137 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
|
138 for obj in self._last_collide: |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
139 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
|
140 clip = obj.collide_rect.clip(cand_rect) |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
141 clip_area += clip.width * clip.height |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
142 if clip_area < min_area: |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
143 min_area = clip_area |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
144 best_move = attempt |
253
dff6287b55b7
Prefer downward moves if all things are equal to reduce 'holding oneself against the wall in mid-air' options
Neil Muller <drnlmuller@gmail.com>
parents:
248
diff
changeset
|
145 elif clip_area == min_area and attempt[1] > best_move[1]: |
dff6287b55b7
Prefer downward moves if all things are equal to reduce 'holding oneself against the wall in mid-air' options
Neil Muller <drnlmuller@gmail.com>
parents:
248
diff
changeset
|
146 # Of equal choices, prefer that which moves us downwards |
dff6287b55b7
Prefer downward moves if all things are equal to reduce 'holding oneself against the wall in mid-air' options
Neil Muller <drnlmuller@gmail.com>
parents:
248
diff
changeset
|
147 best_move = attempt |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
148 self.collide_rect.move_ip(best_move) |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
149 self.rect.move_ip(best_move) |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
150 self.init_pos() |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
151 self._last_collide = [] |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
152 self._collisions_seen = 0 |
116
69a97094417a
Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents:
115
diff
changeset
|
153 |
90 | 154 def set_facing(self, new_facing): |
155 self.facing = new_facing | |
156 | |
149
5b4d73d301a1
Don't change image if it will introduce new collisions
Neil Muller <drnlmuller@gmail.com>
parents:
148
diff
changeset
|
157 def collided(self, other): |
210 | 158 if self.attacking and hasattr(other, 'damage'): |
159 # FIXME: Check if we're facing the right way | |
160 other.damage(5) | |
271
56a529a69e97
Only backout / move-off "solid" collisions
Neil Muller <drnlmuller@gmail.com>
parents:
269
diff
changeset
|
161 if other not in self._last_collide and (other.floor or other.block): |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
162 self._last_collide.append(other) |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
163 self._collide_pos = self.collide_rect.midbottom |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
164 self._collisions_seen = 0 |
271
56a529a69e97
Only backout / move-off "solid" collisions
Neil Muller <drnlmuller@gmail.com>
parents:
269
diff
changeset
|
165 elif other in self._last_collide: |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
166 self._collisions_seen += 1 |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
200
diff
changeset
|
167 if hasattr(other, 'collided_player'): |
208
c72d9bf911fb
Add start of player damage
Neil Muller <drnlmuller@gmail.com>
parents:
206
diff
changeset
|
168 other.collided_player(self) |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
212
diff
changeset
|
169 print 'Health', self.the_world.fox.cur_health |
208
c72d9bf911fb
Add start of player damage
Neil Muller <drnlmuller@gmail.com>
parents:
206
diff
changeset
|
170 |
209 | 171 |
208
c72d9bf911fb
Add start of player damage
Neil Muller <drnlmuller@gmail.com>
parents:
206
diff
changeset
|
172 def damage(self, damage): |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
212
diff
changeset
|
173 self.the_world.fox.cur_health -= damage |
263 | 174 self._soundsystem.play_sound('yelp') |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
212
diff
changeset
|
175 if self.the_world.fox.cur_health <= 0: |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
212
diff
changeset
|
176 PlayerDied.post() |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
177 |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
212
diff
changeset
|
178 def restore(self): |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
212
diff
changeset
|
179 """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
|
180 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
|
181 |
90 | 182 def set_pos(self, pos): |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
200
diff
changeset
|
183 self.rect.midbottom = pos[0] * TILE_SIZE[0] + self.rect_offset[0], (pos[1] + 1) * TILE_SIZE[1] + self.rect_offset[1] |
272 | 184 self.collide_rect.midbottom = pos[0] * TILE_SIZE[0], (pos[1] + 1) * TILE_SIZE[1] |
90 | 185 |
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 def action_left(self): |
115 | 187 if self.facing != 'left': |
188 self.facing = 'left' | |
189 self.set_image() | |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
190 if self.sprinting > 0: |
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
191 self.sprinting = 1 |
266 | 192 self.deltav((-900.0, 0.0)) |
193 else: | |
194 self.deltav((-450.0, 0.0)) | |
195 | |
196 def action_double_left(self): | |
197 # FIXME: Tie this to the tails | |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
198 if self.sprinting > 0: |
266 | 199 return |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
200 self.sprinting = 2 |
266 | 201 self._sprint_start_time = time.time() |
202 | |
203 def action_double_right(self): | |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
204 if self.sprinting > 0: |
266 | 205 return |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
206 self.sprinting = 2 |
266 | 207 self._sprint_start_time = time.time() |
90 | 208 |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
209 def action_double_up(self): |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
210 pass |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
211 |
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
|
212 def action_right(self): |
115 | 213 if self.facing != 'right': |
214 self.facing = 'right' | |
215 self.set_image() | |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
216 if self.sprinting > 0: |
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
217 self.sprinting = 1 # Flag so stopping works |
266 | 218 self.deltav((900.0, 0.0)) |
219 else: | |
220 self.deltav((450.0, 0.0)) | |
221 | |
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
|
222 |
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
|
223 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
|
224 if self.on_solid: |
177
88a78fb9fa82
Tweak some constants to make player motion more comfortable.
Jeremy Thurgood <firxen@gmail.com>
parents:
176
diff
changeset
|
225 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
|
226 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
|
227 |
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
|
228 def action_down(self): |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
272
diff
changeset
|
229 print self._touching_actionables |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
230 if self._touching_actionables: |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
231 self._touching_actionables[0].player_action(self) |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
232 elif self.the_world.fox.item is not None: |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
233 self.drop_item() |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
272
diff
changeset
|
234 |
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
|
235 |
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
|
236 def action_fire1(self): |
206
e2acf4663065
Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
237 # FIXME: Use the correct tail properties for this |
e2acf4663065
Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
238 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
|
239 # Only have a bite attack |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
240 print 'attacking' |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
241 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
|
242 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
|
243 |
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
|
244 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
|
245 print "F2" |
90 | 246 |
247 def _get_action(self): | |
204
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
248 if self.attacking: |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
249 return 'attacking' |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
250 if (self.sprinting > 0) and self.running: |
266 | 251 return 'sprinting' |
90 | 252 if self.running: |
253 return 'running' | |
254 if self.jumping: | |
255 return 'jumpin' | |
256 return 'standing' | |
257 | |
206
e2acf4663065
Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
258 def _make_key(self, tails, action=None): |
90 | 259 if action is None: |
260 action = self._get_action() | |
206
e2acf4663065
Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
261 if tails >= 4: |
90 | 262 tails = 4 |
206
e2acf4663065
Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
263 elif tails >= 2: |
90 | 264 tails = 2 |
265 return '%s %s %d' % (action, self.facing, tails) | |
266 | |
267 def _load_images(self): | |
158 | 268 for action in ['standing', 'running', 'jumping', 'attacking']: |
90 | 269 for tails in [0, 1, 2, 4]: |
206
e2acf4663065
Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
270 directory = 'sprites/kitsune_%s/kitsune_%s_%dtail' % (action, action, tails) |
90 | 271 for facing in ['left', 'right']: |
272 self.facing = facing | |
206
e2acf4663065
Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
273 key = self._make_key(tails, action) |
90 | 274 self._image_dict[key] = [] |
275 for image_file in get_files(directory): | |
276 if image_file.startswith('.'): | |
277 # Skip extra junk for now | |
278 continue | |
206
e2acf4663065
Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
279 image = load_image('%s/%s' % (directory, image_file)) |
266 | 280 if action == 'running': |
281 sprint_key = self._make_key(tails, 'sprinting') | |
282 if sprint_key not in self._image_dict: | |
283 self._image_dict[sprint_key] = [] | |
284 shockwave = load_image('sprites/kitsune_shockwave.png') | |
285 if facing == 'right': | |
286 shockwave = pygame.transform.flip(shockwave, True, False) | |
90 | 287 if facing == 'right': |
288 image = pygame.transform.flip(image, True, False) | |
289 self._image_dict[key].append(image) | |
266 | 290 if action == 'running': |
291 sprint_image = image.copy() | |
292 sprint_image.blit(shockwave, (0, 0)) | |
293 self._image_dict[sprint_key].append(sprint_image) | |
209 | 294 |
295 | |
296
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
296 |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
297 def discard_item(self): |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
298 self.the_world.fox.item = None |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
299 |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
300 |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
301 def drop_item(self): |
209 | 302 my_item = self.the_world.fox.item |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
303 if my_item is None: |
209 | 304 return |
296
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
305 self.discard_item() |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
306 world_item = getattr(self.the_world.items, my_item) |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
307 world_item.level = self.the_world.fox.level |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
308 world_item.pos = [a/b for a, b in zip(self.rect.center, TILE_SIZE)] |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
309 sprite_dict = world_item.copy() |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
310 sprite_dict.pop('level') |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
311 sprite_dict['name'] = my_item |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
312 sprite_dict['world'] = self.the_world |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
313 ItemRepopulationEvent.post(find_sprite(sprite_dict, 'items')) |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
314 |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
315 |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
316 def take_item(self, item): |
296
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
317 self.take_item_by_name(item.name) |
209 | 318 item.kill() |
319 print "took", item | |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
272
diff
changeset
|
320 |
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
272
diff
changeset
|
321 |
296
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
322 def take_item_by_name(self, item_name): |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
323 self.drop_item() |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
324 getattr(self.the_world.items, item_name).level = "_limbo" |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
325 self.the_world.fox.item = item_name |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
326 |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
327 |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
328 def has_item(self, item): |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
329 return self.the_world.fox.item == item |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
330 |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
331 |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
272
diff
changeset
|
332 def add_actionable(self, actionable): |
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
272
diff
changeset
|
333 self._touching_actionables.append(actionable) |