Mercurial > skaapsteker
annotate skaapsteker/sprites/player.py @ 628:1fdfc7f03d98
Stop flying when you land.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Sat, 07 May 2011 20:30:25 +0200 |
parents | 35919d12b792 |
children | 0675f390653c |
rev | line source |
---|---|
90 | 1 """Class for dealing with the player""" |
2 | |
167
f7fb2ee24c70
Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
160
diff
changeset
|
3 import time |
90 | 4 |
620 | 5 import pygame.transform |
6 from pygame.constants import BLEND_RGBA_MULT | |
7 | |
433
fe99a4e04b2b
Make recharge time depend on the number of tails.
Simon Cross <hodgestar@gmail.com>
parents:
431
diff
changeset
|
8 from ..constants import Layers, FoxHud, DOUBLE_TAP_TIME |
248 | 9 from ..data import get_files, load_image |
395 | 10 from ..engine import PlayerDied, AddSpriteEvent, OpenNotification |
621
851c8726696c
More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
11 from ..utils import cadd |
90 | 12 |
622
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
13 from .base import (find_sprite, AnimatedGameSprite, Monster, NPC, Item, Doorway, |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
14 TILE_SIZE, PC_LAYER, MONSTER_LAYER, PROJECTILE_LAYER, |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
15 tile_midbottom) |
620 | 16 from .projectiles import Fireball, Lightning |
17 from .items import BreakableItem | |
18 | |
19 | |
408 | 20 |
622
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
21 class Player(AnimatedGameSprite): |
90 | 22 |
622
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
23 sprite_layer = Layers.PLAYER |
97
a1d95c6152a0
Shiny new collision detection. Read code for usage information.
Simon Cross <hodgestar@gmail.com>
parents:
90
diff
changeset
|
24 collision_layer = PC_LAYER |
362 | 25 collides_with = set([MONSTER_LAYER, PROJECTILE_LAYER]) |
116
69a97094417a
Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents:
115
diff
changeset
|
26 wants_updates = True |
271
56a529a69e97
Only backout / move-off "solid" collisions
Neil Muller <drnlmuller@gmail.com>
parents:
269
diff
changeset
|
27 block = True |
56a529a69e97
Only backout / move-off "solid" collisions
Neil Muller <drnlmuller@gmail.com>
parents:
269
diff
changeset
|
28 |
263 | 29 def __init__(self, the_world, soundsystem): |
622
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
30 super(Player, self).__init__((0, 0)) |
90 | 31 self.image = None |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
32 self.rect = None |
263 | 33 self._soundsystem = soundsystem |
34 self._soundsystem.load_sound('yelp', 'sounds/yelp.ogg') | |
120 | 35 self._animation_frame = 0.0 |
167
f7fb2ee24c70
Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
160
diff
changeset
|
36 self._last_time = time.time() |
433
fe99a4e04b2b
Make recharge time depend on the number of tails.
Simon Cross <hodgestar@gmail.com>
parents:
431
diff
changeset
|
37 self._recharge_timers = { |
435
b6fdaf8e453c
Make shield last for one second. Separate out recharge times to allow the shield to take longer to recharge.
Simon Cross <hodgestar@gmail.com>
parents:
433
diff
changeset
|
38 'fireball': [ time.time(), lambda : 2.0 / len(self._me.tails) ], |
b6fdaf8e453c
Make shield last for one second. Separate out recharge times to allow the shield to take longer to recharge.
Simon Cross <hodgestar@gmail.com>
parents:
433
diff
changeset
|
39 'lightning': [ time.time(), lambda : 2.0 / len(self._me.tails) ], |
b6fdaf8e453c
Make shield last for one second. Separate out recharge times to allow the shield to take longer to recharge.
Simon Cross <hodgestar@gmail.com>
parents:
433
diff
changeset
|
40 'shield': [ time.time(), lambda : 16.0 / len(self._me.tails) ], |
411 | 41 } |
426
29e376484f31
Start of shield for player.
Simon Cross <hodgestar@gmail.com>
parents:
411
diff
changeset
|
42 self._inv_cache = {} # invisible fox image cache |
29e376484f31
Start of shield for player.
Simon Cross <hodgestar@gmail.com>
parents:
411
diff
changeset
|
43 self._shield_cache = {} # shielded fox image cache |
90 | 44 # State flags and such |
204
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
45 self.attacking = 0 |
90 | 46 self.running = False |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
47 self.sprinting = 0 |
90 | 48 self.jumping = False |
408 | 49 self.flying = 0 |
396
dc534c2c475c
Fix up various power activation checks.
Simon Cross <hodgestar@gmail.com>
parents:
395
diff
changeset
|
50 self.prep_flight = 0.0 |
408 | 51 self.invisible = 0 |
426
29e376484f31
Start of shield for player.
Simon Cross <hodgestar@gmail.com>
parents:
411
diff
changeset
|
52 self.using_shield = 0 |
315
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
53 self.inventory_image = None |
90 | 54 # 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
|
55 self.the_world = the_world |
348 | 56 self.shape = the_world.fox.shape |
335 | 57 self._me = the_world.fox |
620 | 58 self.facing = 'left' |
90 | 59 self.set_image() |
60 self.set_pos((0, 0)) | |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
61 self._collisions_seen = 0 |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
62 self._last_collide = [] |
622
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
63 |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
64 |
623
65881746dc20
More Sprite hierarchy work.
Jeremy Thurgood <firxen@gmail.com>
parents:
622
diff
changeset
|
65 def setup_image_data(self, pos, **opts): |
622
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
66 self.shape = 'fox' # Needed so load image does the right thing |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
67 self._image_dict = {} |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
68 self._shield_image = load_image('sprites/kitsune_shield.png') |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
69 for action in ['standing', 'running', 'jumping', 'attacking']: |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
70 for tails in [0, 1, 2, 4]: |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
71 directory = 'sprites/kitsune_%s/kitsune_%s_%dtail' % (action, action, tails) |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
72 for facing in ['left', 'right']: |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
73 self.facing = facing |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
74 key = self._make_key(tails, action) |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
75 self._image_dict[key] = [] |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
76 for image_file in get_files(directory): |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
77 if image_file.startswith('.'): |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
78 # Skip extra junk for now |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
79 continue |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
80 image = load_image('%s/%s' % (directory, image_file)) |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
81 if action == 'running': |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
82 sprint_key = self._make_key(tails, 'sprinting') |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
83 if sprint_key not in self._image_dict: |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
84 self._image_dict[sprint_key] = [] |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
85 shockwave = load_image('sprites/kitsune_shockwave.png') |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
86 if facing == 'right': |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
87 shockwave = pygame.transform.flip(shockwave, True, False) |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
88 if facing == 'right': |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
89 image = pygame.transform.flip(image, True, False) |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
90 self._image_dict[key].append(image) |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
91 if action == 'running': |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
92 sprint_image = image.copy() |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
93 sprint_image.blit(shockwave, (0, 0)) |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
94 self._image_dict[sprint_key].append(sprint_image) |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
95 for shape, name in [('human', 'disguise_'), ('human_with_fan', 'disguise-fan_')]: |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
96 directory = 'sprites/kitsune_disguise' |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
97 for facing in ['left', 'right']: |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
98 key = '%s_running_%s' % (shape, facing) |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
99 standing_key = '%s_standing_%s' % (shape, facing) |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
100 self._image_dict[key] = [] |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
101 for image_file in get_files(directory): |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
102 if name not in image_file: |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
103 continue |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
104 image = load_image('%s/%s' % (directory, image_file)) |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
105 if facing == 'right': |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
106 image = pygame.transform.flip(image, True, False) |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
107 self._image_dict[key].append(image) |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
108 self._image_dict[standing_key] = [self._image_dict[key][0]] |
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
109 |
90 | 110 |
111 def set_image(self): | |
335 | 112 key = self._make_key(len(self._me.tails)) |
120 | 113 images = self._image_dict[key] |
114 if self._animation_frame >= len(images): | |
115 self._animation_frame = 0.0 | |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
116 if self.rect: |
151
06be025c821c
Use midbottom, rather than bottomleft, when changing fox frames
Neil Muller <drnlmuller@gmail.com>
parents:
149
diff
changeset
|
117 cur_pos = self.collide_rect.midbottom |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
118 else: |
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
119 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
|
120 # 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
|
121 cand_image = images[int(self._animation_frame)] |
627
35919d12b792
Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents:
623
diff
changeset
|
122 # cand_collide_rect = cand_image.get_bounding_rect(1).inflate(-2,-2) |
35919d12b792
Path-based collision minimisation and axis-projection backout.
Jeremy Thurgood <firxen@gmail.com>
parents:
623
diff
changeset
|
123 cand_collide_rect = cand_image.get_bounding_rect(1) |
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
|
124 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
|
125 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
|
126 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
|
127 cand_collide_rect.midbottom = cur_pos |
204
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
128 # We always allow the attacking animation frames |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
129 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
|
130 return False |
408 | 131 if self.invisible > 0: |
132 id_cand_image = id(cand_image) | |
133 if id_cand_image in self._inv_cache: | |
134 cand_image = self._inv_cache[id_cand_image] | |
135 else: | |
136 cand_image = cand_image.copy() | |
137 cand_image.fill((0, 0, 0, 140), None, BLEND_RGBA_MULT) | |
138 self._inv_cache[id_cand_image] = cand_image | |
426
29e376484f31
Start of shield for player.
Simon Cross <hodgestar@gmail.com>
parents:
411
diff
changeset
|
139 if self.using_shield > 0: |
29e376484f31
Start of shield for player.
Simon Cross <hodgestar@gmail.com>
parents:
411
diff
changeset
|
140 id_cand_image = id(cand_image) |
29e376484f31
Start of shield for player.
Simon Cross <hodgestar@gmail.com>
parents:
411
diff
changeset
|
141 if id_cand_image in self._shield_cache: |
29e376484f31
Start of shield for player.
Simon Cross <hodgestar@gmail.com>
parents:
411
diff
changeset
|
142 cand_image = self._shield_cache[id_cand_image] |
29e376484f31
Start of shield for player.
Simon Cross <hodgestar@gmail.com>
parents:
411
diff
changeset
|
143 else: |
29e376484f31
Start of shield for player.
Simon Cross <hodgestar@gmail.com>
parents:
411
diff
changeset
|
144 cand_image = cand_image.copy() |
427 | 145 cand_image.blit(self._shield_image, (0, 0), None) |
426
29e376484f31
Start of shield for player.
Simon Cross <hodgestar@gmail.com>
parents:
411
diff
changeset
|
146 self._shield_cache[id_cand_image] = cand_image |
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
|
147 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
|
148 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
|
149 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
|
150 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
|
151 self.init_pos() |
353
3ac03073218f
Don't allow tranformations if they'll get us stuck
Neil Muller <drnlmuller@gmail.com>
parents:
348
diff
changeset
|
152 return True |
90 | 153 |
429
8b047e74dfd1
Bump the player back into the world.
Neil Muller <drnlmuller@gmail.com>
parents:
427
diff
changeset
|
154 def fix_bounds(self, bounds): |
8b047e74dfd1
Bump the player back into the world.
Neil Muller <drnlmuller@gmail.com>
parents:
427
diff
changeset
|
155 """Force the player back into the world.""" |
8b047e74dfd1
Bump the player back into the world.
Neil Muller <drnlmuller@gmail.com>
parents:
427
diff
changeset
|
156 if self.rect.bottom > bounds.bottom: |
8b047e74dfd1
Bump the player back into the world.
Neil Muller <drnlmuller@gmail.com>
parents:
427
diff
changeset
|
157 self.rect.bottom = bounds.bottom - TILE_SIZE[1] - 5 |
8b047e74dfd1
Bump the player back into the world.
Neil Muller <drnlmuller@gmail.com>
parents:
427
diff
changeset
|
158 elif self.rect.top < bounds.top: |
8b047e74dfd1
Bump the player back into the world.
Neil Muller <drnlmuller@gmail.com>
parents:
427
diff
changeset
|
159 self.rect.top = bounds.top + TILE_SIZE[1] + 10 |
8b047e74dfd1
Bump the player back into the world.
Neil Muller <drnlmuller@gmail.com>
parents:
427
diff
changeset
|
160 if self.rect.left < bounds.left: |
8b047e74dfd1
Bump the player back into the world.
Neil Muller <drnlmuller@gmail.com>
parents:
427
diff
changeset
|
161 self.rect.left = bounds.left + TILE_SIZE[0] |
8b047e74dfd1
Bump the player back into the world.
Neil Muller <drnlmuller@gmail.com>
parents:
427
diff
changeset
|
162 elif self.rect.right > bounds.right: |
8b047e74dfd1
Bump the player back into the world.
Neil Muller <drnlmuller@gmail.com>
parents:
427
diff
changeset
|
163 self.rect.right = bounds.left - TILE_SIZE[0] |
8b047e74dfd1
Bump the player back into the world.
Neil Muller <drnlmuller@gmail.com>
parents:
427
diff
changeset
|
164 # FIXME: Find clear tile |
8b047e74dfd1
Bump the player back into the world.
Neil Muller <drnlmuller@gmail.com>
parents:
427
diff
changeset
|
165 # Hack - bump vertical position up by 5 so we're not colliding |
8b047e74dfd1
Bump the player back into the world.
Neil Muller <drnlmuller@gmail.com>
parents:
427
diff
changeset
|
166 # with the floor when we come back |
8b047e74dfd1
Bump the player back into the world.
Neil Muller <drnlmuller@gmail.com>
parents:
427
diff
changeset
|
167 self.rect.bottom = self.rect.bottom - 5 |
8b047e74dfd1
Bump the player back into the world.
Neil Muller <drnlmuller@gmail.com>
parents:
427
diff
changeset
|
168 self.collide_rect.midbottom = self.rect.midbottom |
8b047e74dfd1
Bump the player back into the world.
Neil Muller <drnlmuller@gmail.com>
parents:
427
diff
changeset
|
169 self.init_pos() |
8b047e74dfd1
Bump the player back into the world.
Neil Muller <drnlmuller@gmail.com>
parents:
427
diff
changeset
|
170 self.set_image() |
8b047e74dfd1
Bump the player back into the world.
Neil Muller <drnlmuller@gmail.com>
parents:
427
diff
changeset
|
171 |
116
69a97094417a
Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents:
115
diff
changeset
|
172 def update(self): |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
272
diff
changeset
|
173 self._touching_actionables = [] |
120 | 174 v_x, v_y = self.velocity |
167
f7fb2ee24c70
Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
160
diff
changeset
|
175 # 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
|
176 if self.attacking > 0: |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
177 if self._last_time: |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
178 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
|
179 self._animation_frame += 1 |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
180 self.attacking -= 1 |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
181 self._last_time = time.time() |
167
f7fb2ee24c70
Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
160
diff
changeset
|
182 else: |
f7fb2ee24c70
Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
160
diff
changeset
|
183 self._last_time = time.time() |
204
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
184 else: |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
185 old_frame = self._animation_frame |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
186 self._animation_frame += abs(v_x) / 300 |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
187 time_diff = time.time() - self._last_time |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
188 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
|
189 # Check time diff |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
190 if time_diff < 0.10: |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
191 # Delay animation frame jump |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
192 self._animation_frame -= abs(v_x) / 300 |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
193 else: |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
194 self._last_time = time.time() |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
195 elif time_diff > 0.20: |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
196 # Force animation frame jump |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
197 self._animation_frame = old_frame + 1 |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
198 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
|
199 now = time.time() |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
200 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
|
201 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
|
202 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
|
203 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
|
204 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
|
205 self.flying = 0 |
628
1fdfc7f03d98
Stop flying when you land.
Jeremy Thurgood <firxen@gmail.com>
parents:
627
diff
changeset
|
206 if self.on_solid: |
1fdfc7f03d98
Stop flying when you land.
Jeremy Thurgood <firxen@gmail.com>
parents:
627
diff
changeset
|
207 self.flying = 0 |
408 | 208 if self.invisible > 0: |
209 if (now - self._invisibility_start_time) > self._max_invisibility_time: | |
210 self.invisible = 0 | |
426
29e376484f31
Start of shield for player.
Simon Cross <hodgestar@gmail.com>
parents:
411
diff
changeset
|
211 if self.using_shield > 0: |
435
b6fdaf8e453c
Make shield last for one second. Separate out recharge times to allow the shield to take longer to recharge.
Simon Cross <hodgestar@gmail.com>
parents:
433
diff
changeset
|
212 if (now - self._shield_start_time) > 1.0: |
b6fdaf8e453c
Make shield last for one second. Separate out recharge times to allow the shield to take longer to recharge.
Simon Cross <hodgestar@gmail.com>
parents:
433
diff
changeset
|
213 self.using_shield = 0 |
167
f7fb2ee24c70
Twiddle player animation behaviour
Neil Muller <drnlmuller@gmail.com>
parents:
160
diff
changeset
|
214 if abs(v_x) < 80: |
148
0474df61d5b1
Clamp very slow horizontal motion
Neil Muller <drnlmuller@gmail.com>
parents:
147
diff
changeset
|
215 # 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
|
216 self.velocity = (0, v_y) |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
217 if self.sprinting == 1: |
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
218 self.sprinting = 0 |
160 | 219 self.running = not self.on_solid # if you're not on something you can't stand |
120 | 220 else: |
176
57a78f19c673
Hack player.py to have platformer physics
Neil Muller <drnlmuller@gmail.com>
parents:
172
diff
changeset
|
221 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
|
222 if self.sprinting > 0: |
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
223 self.sprinting = 1 |
120 | 224 self.running = True |
225 self.set_image() | |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
226 if self._collisions_seen > 2 * len(self._last_collide): |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
227 # 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
|
228 # surface |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
229 best_move = (0, 0) |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
230 clip_area = 0 |
200
4e3f9cb49489
Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents:
192
diff
changeset
|
231 for obj in self._last_collide[:]: |
4e3f9cb49489
Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents:
192
diff
changeset
|
232 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
|
233 # Prune stale objects from the list |
4e3f9cb49489
Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents:
192
diff
changeset
|
234 self._last_collide.remove(obj) |
4e3f9cb49489
Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents:
192
diff
changeset
|
235 continue |
188
30a5f7cf670a
Hack'ish logic to move out of colliding with floors better
Neil Muller <drnlmuller@gmail.com>
parents:
187
diff
changeset
|
236 clip = obj.collide_rect.clip(self.collide_rect) |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
237 clip_area += clip.width * clip.height |
398 | 238 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
|
239 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
|
240 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
|
241 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
|
242 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
|
243 self.init_pos() |
4e3f9cb49489
Fix bug in jump to tile top logic
Neil Muller <drnlmuller@gmail.com>
parents:
192
diff
changeset
|
244 return # Jump out of this case |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
245 min_area = clip_area |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
246 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
|
247 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
|
248 for obj in self._last_collide: |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
249 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
|
250 clip = obj.collide_rect.clip(cand_rect) |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
251 clip_area += clip.width * clip.height |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
252 if clip_area < min_area: |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
253 min_area = clip_area |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
254 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
|
255 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
|
256 # 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
|
257 best_move = attempt |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
258 self.collide_rect.move_ip(best_move) |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
259 self.rect.move_ip(best_move) |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
260 self.init_pos() |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
261 self._last_collide = [] |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
262 self._collisions_seen = 0 |
116
69a97094417a
Hook up per-tick sprite animations.
Simon Cross <hodgestar@gmail.com>
parents:
115
diff
changeset
|
263 |
90 | 264 def set_facing(self, new_facing): |
620 | 265 if self.facing != new_facing: |
266 self.facing = new_facing | |
267 self.set_image() | |
90 | 268 |
149
5b4d73d301a1
Don't change image if it will introduce new collisions
Neil Muller <drnlmuller@gmail.com>
parents:
148
diff
changeset
|
269 def collided(self, other): |
210 | 270 if self.attacking and hasattr(other, 'damage'): |
271 # FIXME: Check if we're facing the right way | |
272 other.damage(5) | |
271
56a529a69e97
Only backout / move-off "solid" collisions
Neil Muller <drnlmuller@gmail.com>
parents:
269
diff
changeset
|
273 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
|
274 self._last_collide.append(other) |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
275 self._collide_pos = self.collide_rect.midbottom |
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
276 self._collisions_seen = 0 |
271
56a529a69e97
Only backout / move-off "solid" collisions
Neil Muller <drnlmuller@gmail.com>
parents:
269
diff
changeset
|
277 elif other in self._last_collide: |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
278 self._collisions_seen += 1 |
202
a11325bc5ff0
Sprite refactoring stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
200
diff
changeset
|
279 if hasattr(other, 'collided_player'): |
208
c72d9bf911fb
Add start of player damage
Neil Muller <drnlmuller@gmail.com>
parents:
206
diff
changeset
|
280 other.collided_player(self) |
209 | 281 |
208
c72d9bf911fb
Add start of player damage
Neil Muller <drnlmuller@gmail.com>
parents:
206
diff
changeset
|
282 def damage(self, damage): |
435
b6fdaf8e453c
Make shield last for one second. Separate out recharge times to allow the shield to take longer to recharge.
Simon Cross <hodgestar@gmail.com>
parents:
433
diff
changeset
|
283 if self.using_shield > 0: |
b6fdaf8e453c
Make shield last for one second. Separate out recharge times to allow the shield to take longer to recharge.
Simon Cross <hodgestar@gmail.com>
parents:
433
diff
changeset
|
284 "Shield on." |
b6fdaf8e453c
Make shield last for one second. Separate out recharge times to allow the shield to take longer to recharge.
Simon Cross <hodgestar@gmail.com>
parents:
433
diff
changeset
|
285 return |
b6fdaf8e453c
Make shield last for one second. Separate out recharge times to allow the shield to take longer to recharge.
Simon Cross <hodgestar@gmail.com>
parents:
433
diff
changeset
|
286 elif 'shield' in self._me.tails and self.check_fire_rate('shield'): |
b6fdaf8e453c
Make shield last for one second. Separate out recharge times to allow the shield to take longer to recharge.
Simon Cross <hodgestar@gmail.com>
parents:
433
diff
changeset
|
287 self._shield_start_time = time.time() |
b6fdaf8e453c
Make shield last for one second. Separate out recharge times to allow the shield to take longer to recharge.
Simon Cross <hodgestar@gmail.com>
parents:
433
diff
changeset
|
288 self.using_shield = 1 |
426
29e376484f31
Start of shield for player.
Simon Cross <hodgestar@gmail.com>
parents:
411
diff
changeset
|
289 return |
335 | 290 self._me.cur_health -= damage |
263 | 291 self._soundsystem.play_sound('yelp') |
335 | 292 if self._me.cur_health <= 0: |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
212
diff
changeset
|
293 PlayerDied.post() |
187
92ab784ecf5a
Repel fx from longstanding collisions
Neil Muller <drnlmuller@gmail.com>
parents:
177
diff
changeset
|
294 |
441 | 295 def steal_life(self, damage_done): |
296 if 'steal' in self._me.tails: | |
297 self._me.cur_health += damage_done * len(self._me.tails) / 32 | |
298 self._me.cur_health = min(self._me.cur_health, self._me.max_health) | |
299 | |
244
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
212
diff
changeset
|
300 def restore(self): |
5bbf90e6a94b
Add minimal support for player death
Neil Muller <drnlmuller@gmail.com>
parents:
212
diff
changeset
|
301 """Restore player to max health (for restarting levels, etc.)""" |
335 | 302 self._me.cur_health = self._me.max_health |
122
51bcc909873d
Saner, buggier collision rectangles
Neil Muller <drnlmuller@gmail.com>
parents:
120
diff
changeset
|
303 |
90 | 304 def set_pos(self, pos): |
621
851c8726696c
More cleanup. Mostly using utils.c* instead of doing things the long way.
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
305 self.rect.midbottom = cadd(tile_midbottom(pos), self.rect_offset) |
622
da331c80ec08
Clean up sprite inheritance hierarchy a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
621
diff
changeset
|
306 self.collide_rect.midbottom = tile_midbottom(pos) |
90 | 307 |
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
|
308 def action_left(self): |
620 | 309 self.set_facing('left') |
348 | 310 if self.shape != 'fox': |
311 self.deltav((-300.0, 0.0)) | |
312 elif self.sprinting > 0: | |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
313 self.sprinting = 1 |
266 | 314 self.deltav((-900.0, 0.0)) |
315 else: | |
316 self.deltav((-450.0, 0.0)) | |
317 | |
318 def action_double_left(self): | |
396
dc534c2c475c
Fix up various power activation checks.
Simon Cross <hodgestar@gmail.com>
parents:
395
diff
changeset
|
319 if self.sprinting > 0 or self.flying > 0 or 'sprint' not in self._me.tails or self._me.shape != 'fox': |
266 | 320 return |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
321 self.sprinting = 2 |
391
d5381be5079f
Also set _max_sprint_time when sprinting left.
Simon Cross <hodgestar@gmail.com>
parents:
390
diff
changeset
|
322 self._max_sprint_time = float(len(self._me.tails)) / 4.0 |
266 | 323 self._sprint_start_time = time.time() |
324 | |
325 def action_double_right(self): | |
396
dc534c2c475c
Fix up various power activation checks.
Simon Cross <hodgestar@gmail.com>
parents:
395
diff
changeset
|
326 if self.sprinting > 0 or self.flying > 0 or 'sprint' not in self._me.tails or self._me.shape != 'fox': |
266 | 327 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
|
328 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
|
329 self._max_sprint_time = float(len(self._me.tails)) / 4.0 |
266 | 330 self._sprint_start_time = time.time() |
90 | 331 |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
332 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
|
333 if self.flying > 0 or 'flight' not in self._me.tails or \ |
450
e2b43fe37ac4
Sort actionables with items first, then npcs then doorways. Fix flight prep_time bug.
Simon Cross <hodgestar@gmail.com>
parents:
442
diff
changeset
|
334 time.time() - self.prep_flight > 2.5 * DOUBLE_TAP_TIME \ |
396
dc534c2c475c
Fix up various power activation checks.
Simon Cross <hodgestar@gmail.com>
parents:
395
diff
changeset
|
335 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
|
336 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
|
337 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
|
338 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
|
339 self._flight_start_time = time.time() |
426
29e376484f31
Start of shield for player.
Simon Cross <hodgestar@gmail.com>
parents:
411
diff
changeset
|
340 |
408 | 341 def action_invisible(self): |
342 if self.invisible > 0 or 'invisibility' not in self._me.tails: | |
343 return | |
344 self.invisible = 1 | |
345 self._max_invisibility_time = float(len(self._me.tails)) | |
346 self._invisibility_start_time = time.time() | |
347 | |
348 | 348 def action_transform(self): |
349 """Transform the fox""" | |
350 if not self.on_solid: | |
351 return | |
431 | 352 if 'shapeshift' not in self._me.tails: |
348 | 353 return |
354 if self.shape == 'fox': | |
355 # Become human | |
431 | 356 if self._me.has_fan: |
353
3ac03073218f
Don't allow tranformations if they'll get us stuck
Neil Muller <drnlmuller@gmail.com>
parents:
348
diff
changeset
|
357 self.shape = 'human_with_fan' |
348 | 358 else: |
353
3ac03073218f
Don't allow tranformations if they'll get us stuck
Neil Muller <drnlmuller@gmail.com>
parents:
348
diff
changeset
|
359 self.shape = 'human' |
348 | 360 else: |
353
3ac03073218f
Don't allow tranformations if they'll get us stuck
Neil Muller <drnlmuller@gmail.com>
parents:
348
diff
changeset
|
361 self.shape = 'fox' |
3ac03073218f
Don't allow tranformations if they'll get us stuck
Neil Muller <drnlmuller@gmail.com>
parents:
348
diff
changeset
|
362 # 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
|
363 if self.set_image(): |
3ac03073218f
Don't allow tranformations if they'll get us stuck
Neil Muller <drnlmuller@gmail.com>
parents:
348
diff
changeset
|
364 # Transformation succeeded |
431 | 365 self._me.shape = self.shape |
353
3ac03073218f
Don't allow tranformations if they'll get us stuck
Neil Muller <drnlmuller@gmail.com>
parents:
348
diff
changeset
|
366 else: |
3ac03073218f
Don't allow tranformations if they'll get us stuck
Neil Muller <drnlmuller@gmail.com>
parents:
348
diff
changeset
|
367 # Back out of transformation |
431 | 368 self.shape = self._me.shape |
348 | 369 |
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
|
370 def action_right(self): |
620 | 371 self.set_facing('right') |
348 | 372 if self.shape != 'fox': |
373 self.deltav((300.0, 0.0)) | |
374 elif self.sprinting > 0: | |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
375 self.sprinting = 1 # Flag so stopping works |
266 | 376 self.deltav((900.0, 0.0)) |
377 else: | |
378 self.deltav((450.0, 0.0)) | |
379 | |
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
|
380 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
|
381 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
|
382 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
|
383 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
|
384 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
|
385 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
|
386 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
|
387 |
450
e2b43fe37ac4
Sort actionables with items first, then npcs then doorways. Fix flight prep_time bug.
Simon Cross <hodgestar@gmail.com>
parents:
442
diff
changeset
|
388 def _action_key(self, sprite): |
e2b43fe37ac4
Sort actionables with items first, then npcs then doorways. Fix flight prep_time bug.
Simon Cross <hodgestar@gmail.com>
parents:
442
diff
changeset
|
389 # sort action items so Items are first, then NPCs, then others, then Doorways |
e2b43fe37ac4
Sort actionables with items first, then npcs then doorways. Fix flight prep_time bug.
Simon Cross <hodgestar@gmail.com>
parents:
442
diff
changeset
|
390 # This prevents problems if players drop items on NPCs or Doorways |
e2b43fe37ac4
Sort actionables with items first, then npcs then doorways. Fix flight prep_time bug.
Simon Cross <hodgestar@gmail.com>
parents:
442
diff
changeset
|
391 return ( |
e2b43fe37ac4
Sort actionables with items first, then npcs then doorways. Fix flight prep_time bug.
Simon Cross <hodgestar@gmail.com>
parents:
442
diff
changeset
|
392 not isinstance(sprite, Item), |
e2b43fe37ac4
Sort actionables with items first, then npcs then doorways. Fix flight prep_time bug.
Simon Cross <hodgestar@gmail.com>
parents:
442
diff
changeset
|
393 not isinstance(sprite, NPC), |
e2b43fe37ac4
Sort actionables with items first, then npcs then doorways. Fix flight prep_time bug.
Simon Cross <hodgestar@gmail.com>
parents:
442
diff
changeset
|
394 not isinstance(sprite, Doorway)) |
e2b43fe37ac4
Sort actionables with items first, then npcs then doorways. Fix flight prep_time bug.
Simon Cross <hodgestar@gmail.com>
parents:
442
diff
changeset
|
395 |
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
|
396 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
|
397 if self.flying: |
440
ae0c67788f96
Stop flying by hitting down when on the ground.
Simon Cross <hodgestar@gmail.com>
parents:
439
diff
changeset
|
398 if self.on_solid: |
ae0c67788f96
Stop flying by hitting down when on the ground.
Simon Cross <hodgestar@gmail.com>
parents:
439
diff
changeset
|
399 self.flying = 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
|
400 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
|
401 elif self._touching_actionables: |
439
400d68993977
Become uninvisible when interacting when actionables.
Simon Cross <hodgestar@gmail.com>
parents:
438
diff
changeset
|
402 self.invisible = 0 |
450
e2b43fe37ac4
Sort actionables with items first, then npcs then doorways. Fix flight prep_time bug.
Simon Cross <hodgestar@gmail.com>
parents:
442
diff
changeset
|
403 self._touching_actionables.sort(key=self._action_key) |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
404 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
|
405 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
|
406 self.drop_item() |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
272
diff
changeset
|
407 |
473 | 408 def add_tail(self, tail_type): |
409 if tail_type not in self._me.tails: | |
410 self._me.tails.append(tail_type) | |
411 | |
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
|
412 def _bite_attack(self): |
410
115e738e209c
Become visible when attacking. Enemies shouldn't attack invisible foxes.
Simon Cross <hodgestar@gmail.com>
parents:
408
diff
changeset
|
413 self.invisible = 0 |
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
|
414 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
|
415 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
|
416 |
366 | 417 def _launch_projectile(self, cls): |
418 if self.facing == 'left': | |
419 pos = pygame.Rect(self.rect.midleft, (0, 0)) | |
420 else: | |
421 pos = pygame.Rect(self.rect.midright, (0, 0)) | |
442
a084af91d5dc
Make breakable items break
Neil Muller <drnlmuller@gmail.com>
parents:
441
diff
changeset
|
422 projectile = cls(pos, direction=self.facing, hits=(Monster, BreakableItem), source=self) |
538
c1b0ad1c0932
Hook up projectile sounds.
Simon Cross <hodgestar@gmail.com>
parents:
496
diff
changeset
|
423 projectile.launch() |
366 | 424 AddSpriteEvent.post(projectile) |
425 | |
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
|
426 def _fireball_attack(self): |
411 | 427 if not self.check_fire_rate('fireball'): |
428 return | |
410
115e738e209c
Become visible when attacking. Enemies shouldn't attack invisible foxes.
Simon Cross <hodgestar@gmail.com>
parents:
408
diff
changeset
|
429 self.invisible = 0 |
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
|
430 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
|
431 self._last_time = time.time() # Reset the animation clock |
366 | 432 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
|
433 |
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
|
434 def _lightning_attack(self): |
411 | 435 if not self.check_fire_rate('lightning'): |
436 return | |
410
115e738e209c
Become visible when attacking. Enemies shouldn't attack invisible foxes.
Simon Cross <hodgestar@gmail.com>
parents:
408
diff
changeset
|
437 self.invisible = 0 |
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
|
438 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
|
439 self._last_time = time.time() # Reset the animation clock |
366 | 440 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
|
441 |
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
|
442 def action_fire1(self): |
411 | 443 if self._me.shape != 'fox': |
390
93f13f7d97f2
Initial fire rate-limiting
Stefano Rivera <stefano@rivera.za.net>
parents:
381
diff
changeset
|
444 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
|
445 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
|
446 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
|
447 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
|
448 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
|
449 |
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
|
450 def action_fire2(self): |
411 | 451 if self._me.shape != 'fox': |
390
93f13f7d97f2
Initial fire rate-limiting
Stefano Rivera <stefano@rivera.za.net>
parents:
381
diff
changeset
|
452 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
|
453 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
|
454 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
|
455 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
|
456 self._lightning_attack() |
90 | 457 |
411 | 458 def check_fire_rate(self, attack): |
459 if self.recharge_level(attack) < 1: | |
390
93f13f7d97f2
Initial fire rate-limiting
Stefano Rivera <stefano@rivera.za.net>
parents:
381
diff
changeset
|
460 return False |
435
b6fdaf8e453c
Make shield last for one second. Separate out recharge times to allow the shield to take longer to recharge.
Simon Cross <hodgestar@gmail.com>
parents:
433
diff
changeset
|
461 self._recharge_timers[attack][0] = time.time() |
390
93f13f7d97f2
Initial fire rate-limiting
Stefano Rivera <stefano@rivera.za.net>
parents:
381
diff
changeset
|
462 return True |
93f13f7d97f2
Initial fire rate-limiting
Stefano Rivera <stefano@rivera.za.net>
parents:
381
diff
changeset
|
463 |
411 | 464 def recharge_level(self, attack): |
435
b6fdaf8e453c
Make shield last for one second. Separate out recharge times to allow the shield to take longer to recharge.
Simon Cross <hodgestar@gmail.com>
parents:
433
diff
changeset
|
465 recharge_time = self._recharge_timers[attack][1]() |
b6fdaf8e453c
Make shield last for one second. Separate out recharge times to allow the shield to take longer to recharge.
Simon Cross <hodgestar@gmail.com>
parents:
433
diff
changeset
|
466 return min((time.time() - self._recharge_timers[attack][0]) / recharge_time, 1) |
390
93f13f7d97f2
Initial fire rate-limiting
Stefano Rivera <stefano@rivera.za.net>
parents:
381
diff
changeset
|
467 |
430
25efc74ac19c
Hook up flight and invisibility discharge
Stefano Rivera <stefano@rivera.za.net>
parents:
429
diff
changeset
|
468 def discharge_level(self, tail): |
438
8ee16cec0f9f
Add check for whether we're still flying or invisible to recharge meters.
Simon Cross <hodgestar@gmail.com>
parents:
435
diff
changeset
|
469 if tail == 'invisibility' and hasattr(self, '_invisibility_start_time') and self.invisible: |
430
25efc74ac19c
Hook up flight and invisibility discharge
Stefano Rivera <stefano@rivera.za.net>
parents:
429
diff
changeset
|
470 start_time = self._invisibility_start_time |
25efc74ac19c
Hook up flight and invisibility discharge
Stefano Rivera <stefano@rivera.za.net>
parents:
429
diff
changeset
|
471 max_time = self._max_invisibility_time |
438
8ee16cec0f9f
Add check for whether we're still flying or invisible to recharge meters.
Simon Cross <hodgestar@gmail.com>
parents:
435
diff
changeset
|
472 elif tail == 'flight' and hasattr(self, '_flight_start_time') and self.flying: |
430
25efc74ac19c
Hook up flight and invisibility discharge
Stefano Rivera <stefano@rivera.za.net>
parents:
429
diff
changeset
|
473 start_time = self._flight_start_time |
25efc74ac19c
Hook up flight and invisibility discharge
Stefano Rivera <stefano@rivera.za.net>
parents:
429
diff
changeset
|
474 max_time = self._max_flight_time |
25efc74ac19c
Hook up flight and invisibility discharge
Stefano Rivera <stefano@rivera.za.net>
parents:
429
diff
changeset
|
475 else: |
25efc74ac19c
Hook up flight and invisibility discharge
Stefano Rivera <stefano@rivera.za.net>
parents:
429
diff
changeset
|
476 return 0 |
25efc74ac19c
Hook up flight and invisibility discharge
Stefano Rivera <stefano@rivera.za.net>
parents:
429
diff
changeset
|
477 discharge = (time.time() - start_time) / max_time |
25efc74ac19c
Hook up flight and invisibility discharge
Stefano Rivera <stefano@rivera.za.net>
parents:
429
diff
changeset
|
478 if discharge > 1: |
25efc74ac19c
Hook up flight and invisibility discharge
Stefano Rivera <stefano@rivera.za.net>
parents:
429
diff
changeset
|
479 return 0 |
25efc74ac19c
Hook up flight and invisibility discharge
Stefano Rivera <stefano@rivera.za.net>
parents:
429
diff
changeset
|
480 return discharge |
25efc74ac19c
Hook up flight and invisibility discharge
Stefano Rivera <stefano@rivera.za.net>
parents:
429
diff
changeset
|
481 |
90 | 482 def _get_action(self): |
204
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
483 if self.attacking: |
0f9715a2f07b
Make fire 1 do the attacking animation
Neil Muller <drnlmuller@gmail.com>
parents:
203
diff
changeset
|
484 return 'attacking' |
269
c24588c7ba09
Make sprinting single use after double tap
Neil Muller <drnlmuller@gmail.com>
parents:
266
diff
changeset
|
485 if (self.sprinting > 0) and self.running: |
266 | 486 return 'sprinting' |
90 | 487 if self.running: |
488 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
|
489 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
|
490 return 'running' |
90 | 491 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
|
492 return 'jumping' |
90 | 493 return 'standing' |
494 | |
206
e2acf4663065
Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
495 def _make_key(self, tails, action=None): |
348 | 496 if self.shape != 'fox': |
497 # special logic for human shapes | |
498 if self.running: | |
499 return '%s_running_%s' % (self.shape, self.facing) | |
500 else: | |
501 return '%s_standing_%s' % (self.shape, self.facing) | |
90 | 502 if action is None: |
503 action = self._get_action() | |
206
e2acf4663065
Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
504 if tails >= 4: |
90 | 505 tails = 4 |
206
e2acf4663065
Move fox properties to the world
Neil Muller <drnlmuller@gmail.com>
parents:
204
diff
changeset
|
506 elif tails >= 2: |
90 | 507 tails = 2 |
508 return '%s %s %d' % (action, self.facing, tails) | |
509 | |
296
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
510 def discard_item(self): |
335 | 511 self._me.item = None |
296
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
512 |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
513 |
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
|
514 def get_sprite(self, set_level): |
335 | 515 my_item = self._me.item |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
516 if my_item is None: |
315
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
517 return None |
490
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
473
diff
changeset
|
518 |
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
|
519 if set_level: |
490
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
473
diff
changeset
|
520 to_level = self._me.level |
496 | 521 to_pos = self.get_tile_pos() |
490
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
473
diff
changeset
|
522 else: |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
473
diff
changeset
|
523 to_level, to_pos = None, None |
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
473
diff
changeset
|
524 |
491
6f110e347f30
Fix inventory item fetching.
Simon Cross <hodgestar@gmail.com>
parents:
490
diff
changeset
|
525 return self.the_world.gamestate().create_item_sprite( |
490
0eade58a71b9
Support item dropping (specifically tails). Have monk drop tail.
Simon Cross <hodgestar@gmail.com>
parents:
473
diff
changeset
|
526 my_item, to_level=to_level, to_pos=to_pos) |
315
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
527 |
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
528 |
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
529 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
|
530 sprite = self.get_sprite(True) |
315
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
531 if sprite is None: |
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
532 return |
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
533 self.discard_item() |
339
a9d760134706
Rename ItemRepopulationEvent to AddSprite before repurposing it for fireballs.
Simon Cross <hodgestar@gmail.com>
parents:
336
diff
changeset
|
534 AddSpriteEvent.post(sprite) |
290
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
535 |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
536 |
c68f2f3efc7f
Item dropping and swapping.
Jeremy Thurgood <firxen@gmail.com>
parents:
284
diff
changeset
|
537 def take_item(self, item): |
296
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
538 self.take_item_by_name(item.name) |
314 | 539 # We create a scaled version of the image for the inventory display |
335 | 540 item.remove() |
315
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
541 |
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
542 |
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
543 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
|
544 sprite = self.get_sprite(False) |
315
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
545 if sprite is None: |
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
546 self.inventory_image = None |
0fc2b9d1a9cb
Recreate inventory image on scene changes
Neil Muller <drnlmuller@gmail.com>
parents:
314
diff
changeset
|
547 image = sprite.image |
314 | 548 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
|
549 new_width = FoxHud.INVENTORY_SIZE |
742731e6e6fd
Centre small inventory items rather than resizing
Stefano Rivera <stefano@rivera.za.net>
parents:
355
diff
changeset
|
550 new_height = int(image.get_height() * (float(FoxHud.INVENTORY_SIZE) / image.get_width())) |
314 | 551 else: |
357
742731e6e6fd
Centre small inventory items rather than resizing
Stefano Rivera <stefano@rivera.za.net>
parents:
355
diff
changeset
|
552 new_height = FoxHud.INVENTORY_SIZE |
742731e6e6fd
Centre small inventory items rather than resizing
Stefano Rivera <stefano@rivera.za.net>
parents:
355
diff
changeset
|
553 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
|
554 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
|
555 self.inventory_image = image |
742731e6e6fd
Centre small inventory items rather than resizing
Stefano Rivera <stefano@rivera.za.net>
parents:
355
diff
changeset
|
556 else: |
742731e6e6fd
Centre small inventory items rather than resizing
Stefano Rivera <stefano@rivera.za.net>
parents:
355
diff
changeset
|
557 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
|
558 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
|
559 |
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
272
diff
changeset
|
560 |
296
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
561 def take_item_by_name(self, item_name): |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
562 self.drop_item() |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
563 getattr(self.the_world.items, item_name).level = "_limbo" |
335 | 564 self._me.item = item_name |
320
0dc80b153580
Refresh inventory image on all inventory changes
Neil Muller <drnlmuller@gmail.com>
parents:
315
diff
changeset
|
565 self.make_inventory_image() |
296
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
566 |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
567 |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
568 def has_item(self, item): |
335 | 569 return self._me.item == item |
296
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
570 |
15b2be883a40
Ancient and honorable tea ceremony.
Jeremy Thurgood <firxen@gmail.com>
parents:
290
diff
changeset
|
571 |
273
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
272
diff
changeset
|
572 def add_actionable(self, actionable): |
95e2ef31e714
Hit "down" to interact with things.
Jeremy Thurgood <firxen@gmail.com>
parents:
272
diff
changeset
|
573 self._touching_actionables.append(actionable) |
335 | 574 |
340 | 575 |
335 | 576 def eat_aburage(self): |
577 self._me.tofu += 1 | |
340 | 578 |
579 | |
580 def collect_scroll(self, scroll): | |
581 self._me.scrolls.append(scroll.text) | |
582 | |
395 | 583 |
584 def get_fan(self, fan): | |
585 if self.shape == 'fox': | |
586 OpenNotification.post("A fox cannot use a fan.") | |
587 return | |
588 fan.remove() | |
589 self._me.has_fan = True | |
590 self.shape = 'human_with_fan' | |
431 | 591 self._me.shape = self.shape |
395 | 592 self.set_image() |
593 |