Mercurial > skaapsteker
annotate skaapsteker/sprites/player.py @ 362:02bf05964619
FIREBALLS!
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Sat, 09 Apr 2011 14:25:45 +0200 |
parents | eacb3e1bc1d1 |
children | 249ba3bd6904 |
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 |
362 | 6 from ..sprites.base import find_sprite, Monster, TILE_SIZE, PC_LAYER, MONSTER_LAYER, PROJECTILE_LAYER |
342 | 7 from ..sprites.projectiles import Fireball |
248 | 8 from ..physics import Sprite |
357
742731e6e6fd
Centre small inventory items rather than resizing
Stefano Rivera <stefano@rivera.za.net>
parents:
355
diff
changeset
|
9 from ..constants import Layers, FoxHud |
248 | 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 | 12 |
13 | |
14 class Player(Sprite): | |
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 |
362 | 17 collides_with = set([MONSTER_LAYER, PROJECTILE_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 | 22 _max_sprint_time = 2 |
23 | |
263 | 24 def __init__(self, the_world, soundsystem): |
90 | 25 Sprite.__init__(self) |
26 self.image = None | |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
27 self.rect = None |
90 | 28 self._image_dict = {} |
263 | 29 self._soundsystem = soundsystem |
30 self._soundsystem.load_sound('yelp', 'sounds/yelp.ogg') | |
120 | 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 | 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 | 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 | 37 self.jumping = False |
38 self.flying = False | |
348 | 39 self.shape = 'fox' # Needed so load image does the right thing |
90 | 40 self._load_images() |
315
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
41 self.inventory_image = None |
90 | 42 # 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
|
43 self.the_world = the_world |
348 | 44 self.shape = the_world.fox.shape |
335 | 45 self._me = the_world.fox |
90 | 46 self.set_facing('left') |
47 self.set_image() | |
48 self.set_pos((0, 0)) | |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
49 self._collisions_seen = 0 |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
50 self._last_collide = [] |
90 | 51 self._layer = Layers.PLAYER |
52 | |
53 def set_image(self): | |
335 | 54 key = self._make_key(len(self._me.tails)) |
120 | 55 images = self._image_dict[key] |
56 if self._animation_frame >= len(images): | |
57 self._animation_frame = 0.0 | |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
58 if self.rect: |
151
06be025c821c
Use midbottom, rather than bottomleft, when changing fox frames
Neil Muller <drnlmuller@gmail.com>
parents:
149
diff
changeset
|
59 cur_pos = self.collide_rect.midbottom |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
60 else: |
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
61 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
|
62 # 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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 cand_collide_rect.midbottom = cur_pos |
204
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
69 # We always allow the attacking animation frames |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
70 if not self.check_collide_rect(cand_collide_rect, cand_rect, cand_image) and not self.attacking: |
353
3ac03073218f
Don't allow tranformations if they'll get us stuck
Neil Muller <drnlmuller@gmail.com>
parents:
348
diff
changeset
|
71 return False |
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
|
72 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
|
73 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
|
74 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
|
75 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
|
76 self.init_pos() |
353
3ac03073218f
Don't allow tranformations if they'll get us stuck
Neil Muller <drnlmuller@gmail.com>
parents:
348
diff
changeset
|
77 return True |
90 | 78 |
116
69a97094417a
Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents:
115
diff
changeset
|
79 def update(self): |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
272
diff
changeset
|
80 self._touching_actionables = [] |
120 | 81 v_x, v_y = self.velocity |
167
f7fb2ee24c70
Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
160
diff
changeset
|
82 # 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
|
83 if self.attacking > 0: |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
84 if self._last_time: |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
85 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
|
86 self._animation_frame += 1 |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
87 self.attacking -= 1 |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
88 self._last_time = time.time() |
167
f7fb2ee24c70
Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
160
diff
changeset
|
89 else: |
f7fb2ee24c70
Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
160
diff
changeset
|
90 self._last_time = time.time() |
204
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
91 else: |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
92 old_frame = self._animation_frame |
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 time_diff = time.time() - self._last_time |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
95 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
|
96 # Check time diff |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
97 if time_diff < 0.10: |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
98 # Delay animation frame jump |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
99 self._animation_frame -= abs(v_x) / 300 |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
100 else: |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
101 self._last_time = time.time() |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
102 elif time_diff > 0.20: |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
103 # Force animation frame jump |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
104 self._animation_frame = old_frame + 1 |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
105 self._last_time = time.time() |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
106 if self.sprinting > 0: |
266 | 107 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
|
108 self.sprinting = 0 |
167
f7fb2ee24c70
Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
160
diff
changeset
|
109 if abs(v_x) < 80: |
148
0474df61d5b1
Clamp very slow horizontal motion
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
110 # 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
|
111 self.velocity = (0, v_y) |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
112 if self.sprinting == 1: |
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
113 self.sprinting = 0 |
160 | 114 self.running = not self.on_solid # if you're not on something you can't stand |
120 | 115 else: |
176
57a78f19c673
Hack player.py to have platformer physics
Neil Muller <drnlmuller@gmail.com>
parents:
172
diff
changeset
|
116 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
|
117 if self.sprinting > 0: |
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
118 self.sprinting = 1 |
120 | 119 self.running = True |
120 self.set_image() | |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
121 if self._collisions_seen > 2 * len(self._last_collide): |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
122 # 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
|
123 # surface |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
124 best_move = (0, 0) |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
125 clip_area = 0 |
200
4e3f9cb49489
Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents:
192
diff
changeset
|
126 for obj in self._last_collide[:]: |
4e3f9cb49489
Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents:
192
diff
changeset
|
127 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
|
128 # Prune stale objects from the list |
4e3f9cb49489
Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents:
192
diff
changeset
|
129 self._last_collide.remove(obj) |
4e3f9cb49489
Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents:
192
diff
changeset
|
130 continue |
188
30a5f7cf670a
Hack'ish logic to move out of colliding with floors better
Neil Muller <drnlmuller@gmail.com>
parents:
187
diff
changeset
|
131 clip = obj.collide_rect.clip(self.collide_rect) |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
132 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
|
133 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
|
134 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
|
135 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
|
136 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
|
137 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
|
138 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
|
139 self.init_pos() |
4e3f9cb49489
Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents:
192
diff
changeset
|
140 return # Jump out of this case |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
141 min_area = clip_area |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
142 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
|
143 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
|
144 for obj in self._last_collide: |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
145 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
|
146 clip = obj.collide_rect.clip(cand_rect) |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
147 clip_area += clip.width * clip.height |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
148 if clip_area < min_area: |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
149 min_area = clip_area |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
150 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
|
151 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
|
152 # 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
|
153 best_move = attempt |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
154 self.collide_rect.move_ip(best_move) |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
155 self.rect.move_ip(best_move) |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
156 self.init_pos() |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
157 self._last_collide = [] |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
158 self._collisions_seen = 0 |
116
69a97094417a
Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents:
115
diff
changeset
|
159 |
90 | 160 def set_facing(self, new_facing): |
161 self.facing = new_facing | |
162 | |
149
5b4d73d301a1
Don't change image if it will introduce new collisions
Neil Muller <drnlmuller@gmail.com>
parents:
148
diff
changeset
|
163 def collided(self, other): |
210 | 164 if self.attacking and hasattr(other, 'damage'): |
165 # FIXME: Check if we're facing the right way | |
166 other.damage(5) | |
271
56a529a69e97
Only backout / move-off "solid" collisions
Neil Muller <drnlmuller@gmail.com>
parents:
269
diff
changeset
|
167 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
|
168 self._last_collide.append(other) |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
169 self._collide_pos = self.collide_rect.midbottom |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
170 self._collisions_seen = 0 |
271
56a529a69e97
Only backout / move-off "solid" collisions
Neil Muller <drnlmuller@gmail.com>
parents:
269
diff
changeset
|
171 elif other in self._last_collide: |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
172 self._collisions_seen += 1 |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
200
diff
changeset
|
173 if hasattr(other, 'collided_player'): |
208
c72d9bf911fb
Add start of player damage
Neil Muller <drnlmuller@gmail.com>
parents:
206
diff
changeset
|
174 other.collided_player(self) |
335 | 175 print 'Health', self._me.cur_health |
208
c72d9bf911fb
Add start of player damage
Neil Muller <drnlmuller@gmail.com>
parents:
206
diff
changeset
|
176 |
209 | 177 |
208
c72d9bf911fb
Add start of player damage
Neil Muller <drnlmuller@gmail.com>
parents:
206
diff
changeset
|
178 def damage(self, damage): |
335 | 179 self._me.cur_health -= damage |
263 | 180 self._soundsystem.play_sound('yelp') |
335 | 181 if self._me.cur_health <= 0: |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
212
diff
changeset
|
182 PlayerDied.post() |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
183 |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
212
diff
changeset
|
184 def restore(self): |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
212
diff
changeset
|
185 """Restore player to max health (for restarting levels, etc.)""" |
335 | 186 self._me.cur_health = self._me.max_health |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
187 |
90 | 188 def set_pos(self, pos): |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
200
diff
changeset
|
189 self.rect.midbottom = pos[0] * TILE_SIZE[0] + self.rect_offset[0], (pos[1] + 1) * TILE_SIZE[1] + self.rect_offset[1] |
272 | 190 self.collide_rect.midbottom = pos[0] * TILE_SIZE[0], (pos[1] + 1) * TILE_SIZE[1] |
90 | 191 |
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
|
192 def action_left(self): |
115 | 193 if self.facing != 'left': |
194 self.facing = 'left' | |
195 self.set_image() | |
348 | 196 if self.shape != 'fox': |
197 self.deltav((-300.0, 0.0)) | |
198 elif self.sprinting > 0: | |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
199 self.sprinting = 1 |
266 | 200 self.deltav((-900.0, 0.0)) |
201 else: | |
202 self.deltav((-450.0, 0.0)) | |
203 | |
204 def action_double_left(self): | |
205 # FIXME: Tie this to the tails | |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
206 if self.sprinting > 0: |
266 | 207 return |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
208 self.sprinting = 2 |
266 | 209 self._sprint_start_time = time.time() |
210 | |
211 def action_double_right(self): | |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
212 if self.sprinting > 0: |
266 | 213 return |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
214 self.sprinting = 2 |
266 | 215 self._sprint_start_time = time.time() |
90 | 216 |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
217 def action_double_up(self): |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
218 pass |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
219 |
348 | 220 def action_transform(self): |
221 """Transform the fox""" | |
222 if not self.on_solid: | |
223 return | |
224 if 'shapeshift' not in self.the_world.fox.tails: | |
225 return | |
226 if self.shape == 'fox': | |
227 # Become human | |
228 if self.the_world.fox.has_fan: | |
353
3ac03073218f
Don't allow tranformations if they'll get us stuck
Neil Muller <drnlmuller@gmail.com>
parents:
348
diff
changeset
|
229 self.shape = 'human_with_fan' |
348 | 230 else: |
353
3ac03073218f
Don't allow tranformations if they'll get us stuck
Neil Muller <drnlmuller@gmail.com>
parents:
348
diff
changeset
|
231 self.shape = 'human' |
348 | 232 else: |
353
3ac03073218f
Don't allow tranformations if they'll get us stuck
Neil Muller <drnlmuller@gmail.com>
parents:
348
diff
changeset
|
233 self.shape = 'fox' |
3ac03073218f
Don't allow tranformations if they'll get us stuck
Neil Muller <drnlmuller@gmail.com>
parents:
348
diff
changeset
|
234 # Check the transformation is feasible |
3ac03073218f
Don't allow tranformations if they'll get us stuck
Neil Muller <drnlmuller@gmail.com>
parents:
348
diff
changeset
|
235 if self.set_image(): |
3ac03073218f
Don't allow tranformations if they'll get us stuck
Neil Muller <drnlmuller@gmail.com>
parents:
348
diff
changeset
|
236 # Transformation succeeded |
3ac03073218f
Don't allow tranformations if they'll get us stuck
Neil Muller <drnlmuller@gmail.com>
parents:
348
diff
changeset
|
237 self.the_world.fox.shape = self.shape |
3ac03073218f
Don't allow tranformations if they'll get us stuck
Neil Muller <drnlmuller@gmail.com>
parents:
348
diff
changeset
|
238 else: |
3ac03073218f
Don't allow tranformations if they'll get us stuck
Neil Muller <drnlmuller@gmail.com>
parents:
348
diff
changeset
|
239 # Back out of transformation |
3ac03073218f
Don't allow tranformations if they'll get us stuck
Neil Muller <drnlmuller@gmail.com>
parents:
348
diff
changeset
|
240 self.shape = self.the_world.fox.shape |
348 | 241 |
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 def action_right(self): |
115 | 243 if self.facing != 'right': |
244 self.facing = 'right' | |
245 self.set_image() | |
348 | 246 if self.shape != 'fox': |
247 self.deltav((300.0, 0.0)) | |
248 elif self.sprinting > 0: | |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
249 self.sprinting = 1 # Flag so stopping works |
266 | 250 self.deltav((900.0, 0.0)) |
251 else: | |
252 self.deltav((450.0, 0.0)) | |
253 | |
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
|
254 |
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 def action_up(self): |
348 | 256 if self.on_solid and self.shape == 'fox': |
177
88a78fb9fa82
Tweak some constants to make player motion more comfortable.
Jeremy Thurgood <firxen@gmail.com>
parents:
176
diff
changeset
|
257 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
|
258 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
|
259 |
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
|
260 def action_down(self): |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
272
diff
changeset
|
261 print self._touching_actionables |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
262 if self._touching_actionables: |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
263 self._touching_actionables[0].player_action(self) |
359
eacb3e1bc1d1
Only drop items while standing on ground.
Jeremy Thurgood <firxen@gmail.com>
parents:
357
diff
changeset
|
264 elif self._me.item is not None and self.on_solid: |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
265 self.drop_item() |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
272
diff
changeset
|
266 |
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
|
267 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
|
268 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
|
269 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
|
270 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
|
271 |
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
|
272 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
|
273 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
|
274 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
|
275 self._last_time = time.time() # Reset the animation clock |
355
5bdb4677510a
Have fireballs appear in the right place.
Simon Cross <hodgestar@gmail.com>
parents:
353
diff
changeset
|
276 if self.facing == 'left': |
5bdb4677510a
Have fireballs appear in the right place.
Simon Cross <hodgestar@gmail.com>
parents:
353
diff
changeset
|
277 pos = pygame.Rect(self.rect.midleft, (0, 0)) |
5bdb4677510a
Have fireballs appear in the right place.
Simon Cross <hodgestar@gmail.com>
parents:
353
diff
changeset
|
278 else: |
5bdb4677510a
Have fireballs appear in the right place.
Simon Cross <hodgestar@gmail.com>
parents:
353
diff
changeset
|
279 pos = pygame.Rect(self.rect.midright, (0, 0)) |
362 | 280 fireball = Fireball(pos, direction=self.facing, hits=Monster) |
346
077f43eb4145
Hook up smaller fireball images. Set fireball facing.
Simon Cross <hodgestar@gmail.com>
parents:
342
diff
changeset
|
281 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
|
282 |
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
|
283 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
|
284 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
|
285 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
|
286 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
|
287 |
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
|
288 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
|
289 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
|
290 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
|
291 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
|
292 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
|
293 |
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
|
294 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
|
295 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
|
296 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
|
297 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
|
298 self._lightning_attack() |
90 | 299 |
300 def _get_action(self): | |
204
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
301 if self.attacking: |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
302 return 'attacking' |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
303 if (self.sprinting > 0) and self.running: |
266 | 304 return 'sprinting' |
90 | 305 if self.running: |
306 return 'running' | |
307 if self.jumping: | |
308 return 'jumpin' | |
309 return 'standing' | |
310 | |
206
e2acf4663065
Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
311 def _make_key(self, tails, action=None): |
348 | 312 if self.shape != 'fox': |
313 # special logic for human shapes | |
314 if self.running: | |
315 return '%s_running_%s' % (self.shape, self.facing) | |
316 else: | |
317 return '%s_standing_%s' % (self.shape, self.facing) | |
90 | 318 if action is None: |
319 action = self._get_action() | |
206
e2acf4663065
Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
320 if tails >= 4: |
90 | 321 tails = 4 |
206
e2acf4663065
Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
322 elif tails >= 2: |
90 | 323 tails = 2 |
324 return '%s %s %d' % (action, self.facing, tails) | |
325 | |
326 def _load_images(self): | |
158 | 327 for action in ['standing', 'running', 'jumping', 'attacking']: |
90 | 328 for tails in [0, 1, 2, 4]: |
206
e2acf4663065
Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
329 directory = 'sprites/kitsune_%s/kitsune_%s_%dtail' % (action, action, tails) |
90 | 330 for facing in ['left', 'right']: |
331 self.facing = facing | |
206
e2acf4663065
Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
332 key = self._make_key(tails, action) |
90 | 333 self._image_dict[key] = [] |
334 for image_file in get_files(directory): | |
335 if image_file.startswith('.'): | |
336 # Skip extra junk for now | |
337 continue | |
206
e2acf4663065
Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
338 image = load_image('%s/%s' % (directory, image_file)) |
266 | 339 if action == 'running': |
340 sprint_key = self._make_key(tails, 'sprinting') | |
341 if sprint_key not in self._image_dict: | |
342 self._image_dict[sprint_key] = [] | |
343 shockwave = load_image('sprites/kitsune_shockwave.png') | |
344 if facing == 'right': | |
345 shockwave = pygame.transform.flip(shockwave, True, False) | |
90 | 346 if facing == 'right': |
347 image = pygame.transform.flip(image, True, False) | |
348 self._image_dict[key].append(image) | |
266 | 349 if action == 'running': |
350 sprint_image = image.copy() | |
351 sprint_image.blit(shockwave, (0, 0)) | |
352 self._image_dict[sprint_key].append(sprint_image) | |
348 | 353 for shape, name in [('human', 'disguise_'), ('human_with_fan', 'disguise-fan_')]: |
354 directory = 'sprites/kitsune_disguise' | |
355 for facing in ['left', 'right']: | |
356 key = '%s_running_%s' % (shape, facing) | |
357 standing_key = '%s_standing_%s' % (shape, facing) | |
358 self._image_dict[key] = [] | |
359 for image_file in get_files(directory): | |
360 if name not in image_file: | |
361 continue | |
362 image = load_image('%s/%s' % (directory, image_file)) | |
363 if facing == 'right': | |
364 image = pygame.transform.flip(image, True, False) | |
365 self._image_dict[key].append(image) | |
366 self._image_dict[standing_key] = [self._image_dict[key][0]] | |
296
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
367 |
359
eacb3e1bc1d1
Only drop items while standing on ground.
Jeremy Thurgood <firxen@gmail.com>
parents:
357
diff
changeset
|
368 |
296
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
369 def discard_item(self): |
335 | 370 self._me.item = None |
296
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
371 |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
372 |
342 | 373 def get_tile_pos(self): |
374 return [a/b for a, b in zip(self.rect.center, TILE_SIZE)] | |
375 | |
355
5bdb4677510a
Have fireballs appear in the right place.
Simon Cross <hodgestar@gmail.com>
parents:
353
diff
changeset
|
376 def get_tile_for_pos(self, pos): |
5bdb4677510a
Have fireballs appear in the right place.
Simon Cross <hodgestar@gmail.com>
parents:
353
diff
changeset
|
377 return [a/b for a, b in zip(pos, TILE_SIZE)] |
5bdb4677510a
Have fireballs appear in the right place.
Simon Cross <hodgestar@gmail.com>
parents:
353
diff
changeset
|
378 |
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
|
379 def get_sprite(self, set_level): |
335 | 380 my_item = self._me.item |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
381 if my_item is None: |
315
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
382 return None |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
383 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
|
384 if set_level: |
335 | 385 world_item.level = self._me.level |
342 | 386 world_item.pos = self.get_tile_pos() |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
387 sprite_dict = world_item.copy() |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
388 sprite_dict.pop('level') |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
389 sprite_dict['name'] = my_item |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
390 sprite_dict['world'] = self.the_world |
315
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
391 return find_sprite(sprite_dict, 'items') |
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
392 |
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
393 |
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
394 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
|
395 sprite = self.get_sprite(True) |
315
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
396 if sprite is None: |
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
397 return |
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
398 self.discard_item() |
339
a9d760134706
Rename ItemRepopulationEvent to AddSprite before repurposing it for fireballs.
Simon Cross <hodgestar@gmail.com>
parents:
336
diff
changeset
|
399 AddSpriteEvent.post(sprite) |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
400 |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
401 |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
402 def take_item(self, item): |
296
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
403 self.take_item_by_name(item.name) |
314 | 404 # We create a scaled version of the image for the inventory display |
335 | 405 item.remove() |
315
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
406 print "took", item |
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
407 |
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
408 |
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
409 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
|
410 sprite = self.get_sprite(False) |
315
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
411 if sprite is None: |
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
412 self.inventory_image = None |
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
413 image = sprite.image |
314 | 414 if image.get_width() > image.get_height(): |
357
742731e6e6fd
Centre small inventory items rather than resizing
Stefano Rivera <stefano@rivera.za.net>
parents:
355
diff
changeset
|
415 new_width = FoxHud.INVENTORY_SIZE |
742731e6e6fd
Centre small inventory items rather than resizing
Stefano Rivera <stefano@rivera.za.net>
parents:
355
diff
changeset
|
416 new_height = int(image.get_height() * (float(FoxHud.INVENTORY_SIZE) / image.get_width())) |
314 | 417 else: |
357
742731e6e6fd
Centre small inventory items rather than resizing
Stefano Rivera <stefano@rivera.za.net>
parents:
355
diff
changeset
|
418 new_height = FoxHud.INVENTORY_SIZE |
742731e6e6fd
Centre small inventory items rather than resizing
Stefano Rivera <stefano@rivera.za.net>
parents:
355
diff
changeset
|
419 new_width = int(image.get_width() * (float(FoxHud.INVENTORY_SIZE) / image.get_height())) |
742731e6e6fd
Centre small inventory items rather than resizing
Stefano Rivera <stefano@rivera.za.net>
parents:
355
diff
changeset
|
420 if image.get_width() <= FoxHud.INVENTORY_SIZE and image.get_height() <= FoxHud.INVENTORY_SIZE: |
742731e6e6fd
Centre small inventory items rather than resizing
Stefano Rivera <stefano@rivera.za.net>
parents:
355
diff
changeset
|
421 self.inventory_image = image |
742731e6e6fd
Centre small inventory items rather than resizing
Stefano Rivera <stefano@rivera.za.net>
parents:
355
diff
changeset
|
422 else: |
742731e6e6fd
Centre small inventory items rather than resizing
Stefano Rivera <stefano@rivera.za.net>
parents:
355
diff
changeset
|
423 self.inventory_image = pygame.transform.smoothscale(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
|
424 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
|
425 |
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
272
diff
changeset
|
426 |
296
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
427 def take_item_by_name(self, item_name): |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
428 self.drop_item() |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
429 getattr(self.the_world.items, item_name).level = "_limbo" |
335 | 430 self._me.item = item_name |
320
0dc80b153580
Refresh inventory image on all inventory changes
Neil Muller <drnlmuller@gmail.com>
parents:
315
diff
changeset
|
431 self.make_inventory_image() |
296
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
432 |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
433 |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
434 def has_item(self, item): |
335 | 435 return self._me.item == item |
296
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
436 |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
437 |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
272
diff
changeset
|
438 def add_actionable(self, actionable): |
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
272
diff
changeset
|
439 self._touching_actionables.append(actionable) |
335 | 440 |
340 | 441 |
335 | 442 def eat_aburage(self): |
443 self._me.tofu += 1 | |
340 | 444 |
445 | |
446 def collect_scroll(self, scroll): | |
447 self._me.scrolls.append(scroll.text) | |
448 |