annotate skaapsteker/sprites/player.py @ 346:077f43eb4145

Hook up smaller fireball images. Set fireball facing.
author Simon Cross <hodgestar@gmail.com>
date Sat, 09 Apr 2011 13:30:26 +0200
parents 8f578fe33fe7
children 7d4738347a9c
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
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
342
8f578fe33fe7 Immolation fox.
Simon Cross <hodgestar@gmail.com>
parents: 340
diff changeset
7 from ..sprites.projectiles import Fireball
248
129afb4417cf Some light cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 244
diff changeset
8 from ..physics import Sprite
129afb4417cf Some light cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 244
diff changeset
9 from ..constants import Layers
129afb4417cf Some light cleanup.
Jeremy Thurgood <firxen@gmail.com>
parents: 244
diff changeset
10 from ..data import get_files, load_image
339
a9d760134706 Rename ItemRepopulationEvent to AddSprite before repurposing it for fireballs.
Simon Cross <hodgestar@gmail.com>
parents: 336
diff changeset
11 from ..engine import PlayerDied, AddSpriteEvent
90
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
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
14 class Player(Sprite):
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
15
97
a1d95c6152a0 Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents: 90
diff changeset
16 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
17 collides_with = set([MONSTER_LAYER])
116
69a97094417a Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents: 115
diff changeset
18 wants_updates = True
97
a1d95c6152a0 Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents: 90
diff changeset
19
271
56a529a69e97 Only backout / move-off "solid" collisions
Neil Muller <drnlmuller@gmail.com>
parents: 269
diff changeset
20 block = True
56a529a69e97 Only backout / move-off "solid" collisions
Neil Muller <drnlmuller@gmail.com>
parents: 269
diff changeset
21
266
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
22 _max_sprint_time = 2
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
23
263
44cd7cfd2de3 Yelp when hit
Neil Muller <drnlmuller@gmail.com>
parents: 253
diff changeset
24 def __init__(self, the_world, soundsystem):
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
25 Sprite.__init__(self)
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
26 self.image = None
122
51bcc909873d Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents: 120
diff changeset
27 self.rect = None
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
28 self._image_dict = {}
263
44cd7cfd2de3 Yelp when hit
Neil Muller <drnlmuller@gmail.com>
parents: 253
diff changeset
29 self._soundsystem = soundsystem
44cd7cfd2de3 Yelp when hit
Neil Muller <drnlmuller@gmail.com>
parents: 253
diff changeset
30 self._soundsystem.load_sound('yelp', 'sounds/yelp.ogg')
120
9b08afeadf06 Animated running.
Simon Cross <hodgestar@gmail.com>
parents: 116
diff changeset
31 self._animation_frame = 0.0
167
f7fb2ee24c70 Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 160
diff changeset
32 self._last_time = time.time()
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
33 # State flags and such
204
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
34 self.attacking = 0
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
35 self.running = False
269
c24588c7ba09 Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents: 266
diff changeset
36 self.sprinting = 0
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
37 self.jumping = False
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
38 self.flying = False
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
39 self._load_images()
315
0fc2b9d1a9cb Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents: 314
diff changeset
40 self.inventory_image = None
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
41 # 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
42 self.the_world = the_world
335
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 332
diff changeset
43 self._me = the_world.fox
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
44 self.set_facing('left')
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
45 self.set_image()
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
46 self.set_pos((0, 0))
187
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
47 self._collisions_seen = 0
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
48 self._last_collide = []
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
49 self._layer = Layers.PLAYER
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
50
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
51 def set_image(self):
335
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 332
diff changeset
52 key = self._make_key(len(self._me.tails))
120
9b08afeadf06 Animated running.
Simon Cross <hodgestar@gmail.com>
parents: 116
diff changeset
53 images = self._image_dict[key]
9b08afeadf06 Animated running.
Simon Cross <hodgestar@gmail.com>
parents: 116
diff changeset
54 if self._animation_frame >= len(images):
9b08afeadf06 Animated running.
Simon Cross <hodgestar@gmail.com>
parents: 116
diff changeset
55 self._animation_frame = 0.0
122
51bcc909873d Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents: 120
diff changeset
56 if self.rect:
151
06be025c821c Use midbottom, rather than bottomleft, when changing fox frames
Neil Muller <drnlmuller@gmail.com>
parents: 149
diff changeset
57 cur_pos = self.collide_rect.midbottom
122
51bcc909873d Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents: 120
diff changeset
58 else:
51bcc909873d Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents: 120
diff changeset
59 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
60 # 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
61 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
62 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
63 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
64 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
65 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
66 cand_collide_rect.midbottom = cur_pos
204
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
67 # We always allow the attacking animation frames
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
68 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
69 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
70 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
71 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
72 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
73 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
74 self.init_pos()
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
75
116
69a97094417a Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents: 115
diff changeset
76 def update(self):
273
95e2ef31e714 Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents: 272
diff changeset
77 self._touching_actionables = []
120
9b08afeadf06 Animated running.
Simon Cross <hodgestar@gmail.com>
parents: 116
diff changeset
78 v_x, v_y = self.velocity
167
f7fb2ee24c70 Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 160
diff changeset
79 # 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
80 if self.attacking > 0:
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
81 if self._last_time:
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
82 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
83 self._animation_frame += 1
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
84 self.attacking -= 1
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
85 self._last_time = time.time()
167
f7fb2ee24c70 Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 160
diff changeset
86 else:
f7fb2ee24c70 Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 160
diff changeset
87 self._last_time = time.time()
204
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
88 else:
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
89 old_frame = self._animation_frame
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
90 self._animation_frame += abs(v_x) / 300
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
91 time_diff = time.time() - self._last_time
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
92 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
93 # Check time diff
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
94 if time_diff < 0.10:
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
95 # Delay animation frame jump
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
96 self._animation_frame -= abs(v_x) / 300
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
97 else:
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
98 self._last_time = time.time()
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
99 elif time_diff > 0.20:
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
100 # Force animation frame jump
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
101 self._animation_frame = old_frame + 1
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
102 self._last_time = time.time()
269
c24588c7ba09 Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents: 266
diff changeset
103 if self.sprinting > 0:
266
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
104 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
105 self.sprinting = 0
167
f7fb2ee24c70 Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents: 160
diff changeset
106 if abs(v_x) < 80:
148
0474df61d5b1 Clamp very slow horizontal motion
Neil Muller <drnlmuller@gmail.com>
parents: 147
diff changeset
107 # 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
108 self.velocity = (0, v_y)
269
c24588c7ba09 Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents: 266
diff changeset
109 if self.sprinting == 1:
c24588c7ba09 Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents: 266
diff changeset
110 self.sprinting = 0
160
d44a66ca3fd9 Don't stand in the air.
Simon Cross <hodgestar@gmail.com>
parents: 158
diff changeset
111 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
112 else:
176
57a78f19c673 Hack player.py to have platformer physics
Neil Muller <drnlmuller@gmail.com>
parents: 172
diff changeset
113 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
114 if self.sprinting > 0:
c24588c7ba09 Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents: 266
diff changeset
115 self.sprinting = 1
120
9b08afeadf06 Animated running.
Simon Cross <hodgestar@gmail.com>
parents: 116
diff changeset
116 self.running = True
9b08afeadf06 Animated running.
Simon Cross <hodgestar@gmail.com>
parents: 116
diff changeset
117 self.set_image()
187
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
118 if self._collisions_seen > 2 * len(self._last_collide):
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
119 # 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
120 # surface
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
121 best_move = (0, 0)
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
122 clip_area = 0
200
4e3f9cb49489 Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents: 192
diff changeset
123 for obj in self._last_collide[:]:
4e3f9cb49489 Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents: 192
diff changeset
124 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
125 # Prune stale objects from the list
4e3f9cb49489 Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents: 192
diff changeset
126 self._last_collide.remove(obj)
4e3f9cb49489 Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents: 192
diff changeset
127 continue
188
30a5f7cf670a Hack'ish logic to move out of colliding with floors better
Neil Muller <drnlmuller@gmail.com>
parents: 187
diff changeset
128 clip = obj.collide_rect.clip(self.collide_rect)
187
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
129 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
130 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
131 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
132 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
133 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
134 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
135 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
136 self.init_pos()
4e3f9cb49489 Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents: 192
diff changeset
137 return # Jump out of this case
187
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
138 min_area = clip_area
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
139 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
140 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
141 for obj in self._last_collide:
187
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
142 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
143 clip = obj.collide_rect.clip(cand_rect)
187
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
144 clip_area += clip.width * clip.height
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
145 if clip_area < min_area:
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
146 min_area = clip_area
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
147 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
148 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
149 # 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
150 best_move = attempt
187
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
151 self.collide_rect.move_ip(best_move)
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
152 self.rect.move_ip(best_move)
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
153 self.init_pos()
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
154 self._last_collide = []
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
155 self._collisions_seen = 0
116
69a97094417a Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents: 115
diff changeset
156
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
157 def set_facing(self, new_facing):
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
158 self.facing = new_facing
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
159
149
5b4d73d301a1 Don't change image if it will introduce new collisions
Neil Muller <drnlmuller@gmail.com>
parents: 148
diff changeset
160 def collided(self, other):
210
9a6c711e2fdf KILLhg statushg status
Neil Muller <drnlmuller@gmail.com>
parents: 209
diff changeset
161 if self.attacking and hasattr(other, 'damage'):
9a6c711e2fdf KILLhg statushg status
Neil Muller <drnlmuller@gmail.com>
parents: 209
diff changeset
162 # FIXME: Check if we're facing the right way
9a6c711e2fdf KILLhg statushg status
Neil Muller <drnlmuller@gmail.com>
parents: 209
diff changeset
163 other.damage(5)
271
56a529a69e97 Only backout / move-off "solid" collisions
Neil Muller <drnlmuller@gmail.com>
parents: 269
diff changeset
164 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
165 self._last_collide.append(other)
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
166 self._collide_pos = self.collide_rect.midbottom
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
167 self._collisions_seen = 0
271
56a529a69e97 Only backout / move-off "solid" collisions
Neil Muller <drnlmuller@gmail.com>
parents: 269
diff changeset
168 elif other in self._last_collide:
187
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
169 self._collisions_seen += 1
202
a11325bc5ff0 Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 200
diff changeset
170 if hasattr(other, 'collided_player'):
208
c72d9bf911fb Add start of player damage
Neil Muller <drnlmuller@gmail.com>
parents: 206
diff changeset
171 other.collided_player(self)
335
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 332
diff changeset
172 print 'Health', self._me.cur_health
208
c72d9bf911fb Add start of player damage
Neil Muller <drnlmuller@gmail.com>
parents: 206
diff changeset
173
209
189f7f8ef714 Basic item interaction.
Jeremy Thurgood <firxen@gmail.com>
parents: 208
diff changeset
174
208
c72d9bf911fb Add start of player damage
Neil Muller <drnlmuller@gmail.com>
parents: 206
diff changeset
175 def damage(self, damage):
335
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 332
diff changeset
176 self._me.cur_health -= damage
263
44cd7cfd2de3 Yelp when hit
Neil Muller <drnlmuller@gmail.com>
parents: 253
diff changeset
177 self._soundsystem.play_sound('yelp')
335
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 332
diff changeset
178 if self._me.cur_health <= 0:
244
5bbf90e6a94b Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents: 212
diff changeset
179 PlayerDied.post()
187
92ab784ecf5a Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents: 177
diff changeset
180
244
5bbf90e6a94b Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents: 212
diff changeset
181 def restore(self):
5bbf90e6a94b Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents: 212
diff changeset
182 """Restore player to max health (for restarting levels, etc.)"""
335
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 332
diff changeset
183 self._me.cur_health = self._me.max_health
122
51bcc909873d Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents: 120
diff changeset
184
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
185 def set_pos(self, pos):
202
a11325bc5ff0 Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 200
diff changeset
186 self.rect.midbottom = pos[0] * TILE_SIZE[0] + self.rect_offset[0], (pos[1] + 1) * TILE_SIZE[1] + self.rect_offset[1]
272
630ebb87b38a Remove debugging print
Neil Muller <drnlmuller@gmail.com>
parents: 271
diff changeset
187 self.collide_rect.midbottom = pos[0] * TILE_SIZE[0], (pos[1] + 1) * TILE_SIZE[1]
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
188
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
189 def action_left(self):
115
b73724bb93f8 Fix fox facing
Neil Muller <drnlmuller@gmail.com>
parents: 113
diff changeset
190 if self.facing != 'left':
b73724bb93f8 Fix fox facing
Neil Muller <drnlmuller@gmail.com>
parents: 113
diff changeset
191 self.facing = 'left'
b73724bb93f8 Fix fox facing
Neil Muller <drnlmuller@gmail.com>
parents: 113
diff changeset
192 self.set_image()
269
c24588c7ba09 Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents: 266
diff changeset
193 if self.sprinting > 0:
c24588c7ba09 Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents: 266
diff changeset
194 self.sprinting = 1
266
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
195 self.deltav((-900.0, 0.0))
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
196 else:
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
197 self.deltav((-450.0, 0.0))
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
198
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
199 def action_double_left(self):
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
200 # FIXME: Tie this to the tails
269
c24588c7ba09 Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents: 266
diff changeset
201 if self.sprinting > 0:
266
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
202 return
269
c24588c7ba09 Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents: 266
diff changeset
203 self.sprinting = 2
266
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
204 self._sprint_start_time = time.time()
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
205
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
206 def action_double_right(self):
269
c24588c7ba09 Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents: 266
diff changeset
207 if self.sprinting > 0:
266
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
208 return
269
c24588c7ba09 Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents: 266
diff changeset
209 self.sprinting = 2
266
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
210 self._sprint_start_time = time.time()
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
211
290
c68f2f3efc7f Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents: 284
diff changeset
212 def action_double_up(self):
c68f2f3efc7f Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents: 284
diff changeset
213 pass
c68f2f3efc7f Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents: 284
diff changeset
214
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
215 def action_right(self):
115
b73724bb93f8 Fix fox facing
Neil Muller <drnlmuller@gmail.com>
parents: 113
diff changeset
216 if self.facing != 'right':
b73724bb93f8 Fix fox facing
Neil Muller <drnlmuller@gmail.com>
parents: 113
diff changeset
217 self.facing = 'right'
b73724bb93f8 Fix fox facing
Neil Muller <drnlmuller@gmail.com>
parents: 113
diff changeset
218 self.set_image()
269
c24588c7ba09 Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents: 266
diff changeset
219 if self.sprinting > 0:
c24588c7ba09 Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents: 266
diff changeset
220 self.sprinting = 1 # Flag so stopping works
266
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
221 self.deltav((900.0, 0.0))
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
222 else:
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
223 self.deltav((450.0, 0.0))
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
224
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
225
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
226 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
227 if self.on_solid:
177
88a78fb9fa82 Tweak some constants to make player motion more comfortable.
Jeremy Thurgood <firxen@gmail.com>
parents: 176
diff changeset
228 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
229 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
230
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
231 def action_down(self):
273
95e2ef31e714 Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents: 272
diff changeset
232 print self._touching_actionables
290
c68f2f3efc7f Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents: 284
diff changeset
233 if self._touching_actionables:
c68f2f3efc7f Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents: 284
diff changeset
234 self._touching_actionables[0].player_action(self)
335
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 332
diff changeset
235 elif self._me.item is not None:
290
c68f2f3efc7f Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents: 284
diff changeset
236 self.drop_item()
273
95e2ef31e714 Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents: 272
diff changeset
237
336
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
238 def _bite_attack(self):
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
239 print 'ninja bite attack'
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
240 self.attacking = 2
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
241 self._last_time = time.time() # Reset the animation clock
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
242
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
243 def _fireball_attack(self):
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
244 print 'ninja fireball attack attack attack'
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
245 self.attacking = 2
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
246 self._last_time = time.time() # Reset the animation clock
346
077f43eb4145 Hook up smaller fireball images. Set fireball facing.
Simon Cross <hodgestar@gmail.com>
parents: 342
diff changeset
247 fireball = Fireball(self.get_tile_pos())
077f43eb4145 Hook up smaller fireball images. Set fireball facing.
Simon Cross <hodgestar@gmail.com>
parents: 342
diff changeset
248 fireball.facing = self.facing
077f43eb4145 Hook up smaller fireball images. Set fireball facing.
Simon Cross <hodgestar@gmail.com>
parents: 342
diff changeset
249 AddSpriteEvent.post(fireball)
336
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
250
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
251 def _lightning_attack(self):
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
252 print 'thunderbolts and lightning'
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
253 self.attacking = 2
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
254 self._last_time = time.time() # Reset the animation clock
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
255
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
256 def action_fire1(self):
336
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
257 if "fireball" not in self._me.tails:
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
258 self._bite_attack()
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
259 else:
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
260 self._fireball_attack()
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
261
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
262 def action_fire2(self):
336
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
263 if "lightning" not in self._me.tails:
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
264 self._bite_attack()
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
265 else:
c8fd82ff0c71 Hook up fire buttons to attack functions. Use correct check for tails. Add skeleton for projectiles.
Simon Cross <hodgestar@gmail.com>
parents: 335
diff changeset
266 self._lightning_attack()
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
267
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
268 def _get_action(self):
204
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
269 if self.attacking:
0f9715a2f07b Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents: 203
diff changeset
270 return 'attacking'
269
c24588c7ba09 Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents: 266
diff changeset
271 if (self.sprinting > 0) and self.running:
266
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
272 return 'sprinting'
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
273 if self.running:
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
274 return 'running'
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
275 if self.jumping:
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
276 return 'jumpin'
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
277 return 'standing'
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
278
206
e2acf4663065 Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents: 204
diff changeset
279 def _make_key(self, tails, action=None):
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
280 if action is None:
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
281 action = self._get_action()
206
e2acf4663065 Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents: 204
diff changeset
282 if tails >= 4:
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
283 tails = 4
206
e2acf4663065 Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents: 204
diff changeset
284 elif tails >= 2:
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
285 tails = 2
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
286 return '%s %s %d' % (action, self.facing, tails)
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
287
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
288 def _load_images(self):
158
794565c8f9f4 Load attacking images
Neil Muller <drnlmuller@gmail.com>
parents: 152
diff changeset
289 for action in ['standing', 'running', 'jumping', 'attacking']:
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
290 for tails in [0, 1, 2, 4]:
206
e2acf4663065 Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents: 204
diff changeset
291 directory = 'sprites/kitsune_%s/kitsune_%s_%dtail' % (action, action, tails)
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
292 for facing in ['left', 'right']:
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
293 self.facing = facing
206
e2acf4663065 Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents: 204
diff changeset
294 key = self._make_key(tails, action)
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
295 self._image_dict[key] = []
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
296 for image_file in get_files(directory):
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
297 if image_file.startswith('.'):
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
298 # Skip extra junk for now
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
299 continue
206
e2acf4663065 Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents: 204
diff changeset
300 image = load_image('%s/%s' % (directory, image_file))
266
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
301 if action == 'running':
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
302 sprint_key = self._make_key(tails, 'sprinting')
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
303 if sprint_key not in self._image_dict:
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
304 self._image_dict[sprint_key] = []
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
305 shockwave = load_image('sprites/kitsune_shockwave.png')
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
306 if facing == 'right':
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
307 shockwave = pygame.transform.flip(shockwave, True, False)
90
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
308 if facing == 'right':
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
309 image = pygame.transform.flip(image, True, False)
96ba88d66e41 Add forgetten player.py
Neil Muller <drnlmuller@gmail.com>
parents:
diff changeset
310 self._image_dict[key].append(image)
266
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
311 if action == 'running':
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
312 sprint_image = image.copy()
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
313 sprint_image.blit(shockwave, (0, 0))
be516ca5e3b8 Add sprinting
Neil Muller <drnlmuller@gmail.com>
parents: 263
diff changeset
314 self._image_dict[sprint_key].append(sprint_image)
209
189f7f8ef714 Basic item interaction.
Jeremy Thurgood <firxen@gmail.com>
parents: 208
diff changeset
315
189f7f8ef714 Basic item interaction.
Jeremy Thurgood <firxen@gmail.com>
parents: 208
diff changeset
316
296
15b2be883a40 Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents: 290
diff changeset
317
15b2be883a40 Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents: 290
diff changeset
318 def discard_item(self):
335
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 332
diff changeset
319 self._me.item = None
296
15b2be883a40 Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents: 290
diff changeset
320
15b2be883a40 Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents: 290
diff changeset
321
342
8f578fe33fe7 Immolation fox.
Simon Cross <hodgestar@gmail.com>
parents: 340
diff changeset
322 def get_tile_pos(self):
8f578fe33fe7 Immolation fox.
Simon Cross <hodgestar@gmail.com>
parents: 340
diff changeset
323 return [a/b for a, b in zip(self.rect.center, TILE_SIZE)]
8f578fe33fe7 Immolation fox.
Simon Cross <hodgestar@gmail.com>
parents: 340
diff changeset
324
332
971c1726c530 don't set position and level when creating a sprite just for the inventory image
Neil Muller <drnlmuller@gmail.com>
parents: 328
diff changeset
325 def get_sprite(self, set_level):
335
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 332
diff changeset
326 my_item = self._me.item
290
c68f2f3efc7f Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents: 284
diff changeset
327 if my_item is None:
315
0fc2b9d1a9cb Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents: 314
diff changeset
328 return None
290
c68f2f3efc7f Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents: 284
diff changeset
329 world_item = getattr(self.the_world.items, my_item)
332
971c1726c530 don't set position and level when creating a sprite just for the inventory image
Neil Muller <drnlmuller@gmail.com>
parents: 328
diff changeset
330 if set_level:
335
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 332
diff changeset
331 world_item.level = self._me.level
342
8f578fe33fe7 Immolation fox.
Simon Cross <hodgestar@gmail.com>
parents: 340
diff changeset
332 world_item.pos = self.get_tile_pos()
290
c68f2f3efc7f Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents: 284
diff changeset
333 sprite_dict = world_item.copy()
c68f2f3efc7f Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents: 284
diff changeset
334 sprite_dict.pop('level')
c68f2f3efc7f Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents: 284
diff changeset
335 sprite_dict['name'] = my_item
c68f2f3efc7f Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents: 284
diff changeset
336 sprite_dict['world'] = self.the_world
315
0fc2b9d1a9cb Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents: 314
diff changeset
337 return find_sprite(sprite_dict, 'items')
0fc2b9d1a9cb Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents: 314
diff changeset
338
0fc2b9d1a9cb Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents: 314
diff changeset
339
0fc2b9d1a9cb Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents: 314
diff changeset
340 def drop_item(self):
332
971c1726c530 don't set position and level when creating a sprite just for the inventory image
Neil Muller <drnlmuller@gmail.com>
parents: 328
diff changeset
341 sprite = self.get_sprite(True)
315
0fc2b9d1a9cb Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents: 314
diff changeset
342 if sprite is None:
0fc2b9d1a9cb Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents: 314
diff changeset
343 return
0fc2b9d1a9cb Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents: 314
diff changeset
344 self.discard_item()
339
a9d760134706 Rename ItemRepopulationEvent to AddSprite before repurposing it for fireballs.
Simon Cross <hodgestar@gmail.com>
parents: 336
diff changeset
345 AddSpriteEvent.post(sprite)
290
c68f2f3efc7f Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents: 284
diff changeset
346
c68f2f3efc7f Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents: 284
diff changeset
347
c68f2f3efc7f Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents: 284
diff changeset
348 def take_item(self, item):
296
15b2be883a40 Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents: 290
diff changeset
349 self.take_item_by_name(item.name)
314
f29999d1bba6 Add inventory drawing
Neil Muller <drnlmuller@gmail.com>
parents: 296
diff changeset
350 # We create a scaled version of the image for the inventory display
335
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 332
diff changeset
351 item.remove()
315
0fc2b9d1a9cb Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents: 314
diff changeset
352 print "took", item
0fc2b9d1a9cb Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents: 314
diff changeset
353
0fc2b9d1a9cb Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents: 314
diff changeset
354
0fc2b9d1a9cb Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents: 314
diff changeset
355 def make_inventory_image(self):
332
971c1726c530 don't set position and level when creating a sprite just for the inventory image
Neil Muller <drnlmuller@gmail.com>
parents: 328
diff changeset
356 sprite = self.get_sprite(False)
315
0fc2b9d1a9cb Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents: 314
diff changeset
357 if sprite is None:
0fc2b9d1a9cb Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents: 314
diff changeset
358 self.inventory_image = None
0fc2b9d1a9cb Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents: 314
diff changeset
359 image = sprite.image
314
f29999d1bba6 Add inventory drawing
Neil Muller <drnlmuller@gmail.com>
parents: 296
diff changeset
360 if image.get_width() > image.get_height():
f29999d1bba6 Add inventory drawing
Neil Muller <drnlmuller@gmail.com>
parents: 296
diff changeset
361 new_width = 48
f29999d1bba6 Add inventory drawing
Neil Muller <drnlmuller@gmail.com>
parents: 296
diff changeset
362 new_height = int(image.get_height() * (48.0 / image.get_width()))
f29999d1bba6 Add inventory drawing
Neil Muller <drnlmuller@gmail.com>
parents: 296
diff changeset
363 else:
f29999d1bba6 Add inventory drawing
Neil Muller <drnlmuller@gmail.com>
parents: 296
diff changeset
364 new_height = 48
f29999d1bba6 Add inventory drawing
Neil Muller <drnlmuller@gmail.com>
parents: 296
diff changeset
365 new_width = int(image.get_width() * (48.0 / image.get_height()))
f29999d1bba6 Add inventory drawing
Neil Muller <drnlmuller@gmail.com>
parents: 296
diff changeset
366 self.inventory_image = pygame.transform.scale(image, (new_width, new_height))
332
971c1726c530 don't set position and level when creating a sprite just for the inventory image
Neil Muller <drnlmuller@gmail.com>
parents: 328
diff changeset
367 sprite.kill() # ensure we don't leak into the scene at any point
273
95e2ef31e714 Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents: 272
diff changeset
368
95e2ef31e714 Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents: 272
diff changeset
369
296
15b2be883a40 Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents: 290
diff changeset
370 def take_item_by_name(self, item_name):
15b2be883a40 Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents: 290
diff changeset
371 self.drop_item()
15b2be883a40 Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents: 290
diff changeset
372 getattr(self.the_world.items, item_name).level = "_limbo"
335
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 332
diff changeset
373 self._me.item = item_name
320
0dc80b153580 Refresh inventory image on all inventory changes
Neil Muller <drnlmuller@gmail.com>
parents: 315
diff changeset
374 self.make_inventory_image()
296
15b2be883a40 Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents: 290
diff changeset
375
15b2be883a40 Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents: 290
diff changeset
376
15b2be883a40 Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents: 290
diff changeset
377 def has_item(self, item):
335
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 332
diff changeset
378 return self._me.item == item
296
15b2be883a40 Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents: 290
diff changeset
379
15b2be883a40 Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents: 290
diff changeset
380
273
95e2ef31e714 Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents: 272
diff changeset
381 def add_actionable(self, actionable):
95e2ef31e714 Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents: 272
diff changeset
382 self._touching_actionables.append(actionable)
335
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 332
diff changeset
383
340
f870e3122ac7 Collect haiku scrolls.
Jeremy Thurgood <firxen@gmail.com>
parents: 339
diff changeset
384
335
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 332
diff changeset
385 def eat_aburage(self):
c6552e9fc2e1 Added aburage.
Jeremy Thurgood <firxen@gmail.com>
parents: 332
diff changeset
386 self._me.tofu += 1
340
f870e3122ac7 Collect haiku scrolls.
Jeremy Thurgood <firxen@gmail.com>
parents: 339
diff changeset
387
f870e3122ac7 Collect haiku scrolls.
Jeremy Thurgood <firxen@gmail.com>
parents: 339
diff changeset
388
f870e3122ac7 Collect haiku scrolls.
Jeremy Thurgood <firxen@gmail.com>
parents: 339
diff changeset
389 def collect_scroll(self, scroll):
f870e3122ac7 Collect haiku scrolls.
Jeremy Thurgood <firxen@gmail.com>
parents: 339
diff changeset
390 self._me.scrolls.append(scroll.text)
f870e3122ac7 Collect haiku scrolls.
Jeremy Thurgood <firxen@gmail.com>
parents: 339
diff changeset
391