annotate skaapsteker/sprites/player.py @ 398:6850a3ab3aac

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