Mercurial > nagslang
annotate nagslang/game_object.py @ 332:ffefb93127c5
laser gun
author | Adrianna Pińska <adrianna.pinska@gmail.com> |
---|---|
date | Fri, 06 Sep 2013 03:10:04 +0200 |
parents | 5030ef2718ae |
children | 3dd32686dbc3 |
rev | line source |
---|---|
81
a1b4d09e6f23
Floor switch with horrible hackery.
davidsharpe@lantea.local
parents:
63
diff
changeset
|
1 import pymunk |
93
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
91
diff
changeset
|
2 import pymunk.pygame_util |
81
a1b4d09e6f23
Floor switch with horrible hackery.
davidsharpe@lantea.local
parents:
63
diff
changeset
|
3 |
263 | 4 import math |
312
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
5 import time |
263 | 6 |
281
9b56e954c674
Protagonist actions, now required for operating doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
276
diff
changeset
|
7 from nagslang import environment |
201
3495a2025bc6
Break puzzlers out of game_object.py
Stefano Rivera <stefano@rivera.za.net>
parents:
196
diff
changeset
|
8 from nagslang import puzzle |
207 | 9 from nagslang import render |
296
eb08426a58fe
Levers look like levers.
Jeremy Thurgood <firxen@gmail.com>
parents:
295
diff
changeset
|
10 from nagslang.mutators import FLIP_H |
107 | 11 from nagslang.constants import ( |
318
26d1978fa1da
BOX is no longer accurate
Stefano Rivera <stefano@rivera.za.net>
parents:
313
diff
changeset
|
12 COLLISION_TYPE_DOOR, COLLISION_TYPE_FURNITURE, COLLISION_TYPE_PROJECTILE, |
26d1978fa1da
BOX is no longer accurate
Stefano Rivera <stefano@rivera.za.net>
parents:
313
diff
changeset
|
13 COLLISION_TYPE_SWITCH, COLLISION_TYPE_WEREWOLF_ATTACK, |
26d1978fa1da
BOX is no longer accurate
Stefano Rivera <stefano@rivera.za.net>
parents:
313
diff
changeset
|
14 SWITCH_PUSHERS, ZORDER_FLOOR, ZORDER_LOW) |
155
b455873020be
Crates look like crates.
Jeremy Thurgood <firxen@gmail.com>
parents:
145
diff
changeset
|
15 from nagslang.resources import resources |
180
026297a03963
Add DoorEvent and tweak ScreenChange to keep more state when the player goes through a door
Neil Muller <drnlmuller@gmail.com>
parents:
176
diff
changeset
|
16 from nagslang.events import DoorEvent |
81
a1b4d09e6f23
Floor switch with horrible hackery.
davidsharpe@lantea.local
parents:
63
diff
changeset
|
17 |
82 | 18 |
235
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
19 def get_editable_game_objects(): |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
20 classes = [] |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
21 for cls_name, cls in globals().iteritems(): |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
22 if isinstance(cls, type) and hasattr(cls, 'requires'): |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
23 classes.append((cls_name, cls)) |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
24 return classes |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
25 |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
26 |
59 | 27 class Physicser(object): |
93
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
91
diff
changeset
|
28 def __init__(self, space): |
123
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
29 self._space = space |
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
30 |
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
31 def get_space(self): |
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
32 return self._space |
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
33 |
276
3153196517fc
Move protagonist to the world
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
34 def set_space(self, new_space): |
3153196517fc
Move protagonist to the world
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
35 self._space = new_space |
3153196517fc
Move protagonist to the world
Neil Muller <drnlmuller@gmail.com>
parents:
264
diff
changeset
|
36 |
123
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
37 def set_game_object(self, game_object): |
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
38 self.game_object = game_object |
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
39 |
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
40 def get_shape(self): |
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
41 raise NotImplementedError() |
93
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
91
diff
changeset
|
42 |
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
91
diff
changeset
|
43 def add_to_space(self): |
215
325c317cbfa1
Better protagonist physicser.
Jeremy Thurgood <firxen@gmail.com>
parents:
211
diff
changeset
|
44 shape = self.get_shape() |
325c317cbfa1
Better protagonist physicser.
Jeremy Thurgood <firxen@gmail.com>
parents:
211
diff
changeset
|
45 self.get_space().add(shape) |
325c317cbfa1
Better protagonist physicser.
Jeremy Thurgood <firxen@gmail.com>
parents:
211
diff
changeset
|
46 if not shape.body.is_static: |
325c317cbfa1
Better protagonist physicser.
Jeremy Thurgood <firxen@gmail.com>
parents:
211
diff
changeset
|
47 self.get_space().add(shape.body) |
59 | 48 |
93
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
91
diff
changeset
|
49 def remove_from_space(self): |
215
325c317cbfa1
Better protagonist physicser.
Jeremy Thurgood <firxen@gmail.com>
parents:
211
diff
changeset
|
50 shape = self.get_shape() |
325c317cbfa1
Better protagonist physicser.
Jeremy Thurgood <firxen@gmail.com>
parents:
211
diff
changeset
|
51 self.get_space().remove(shape) |
325c317cbfa1
Better protagonist physicser.
Jeremy Thurgood <firxen@gmail.com>
parents:
211
diff
changeset
|
52 if not shape.body.is_static: |
325c317cbfa1
Better protagonist physicser.
Jeremy Thurgood <firxen@gmail.com>
parents:
211
diff
changeset
|
53 self.get_space().remove(shape.body) |
59 | 54 |
93
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
91
diff
changeset
|
55 def get_render_position(self, surface): |
215
325c317cbfa1
Better protagonist physicser.
Jeremy Thurgood <firxen@gmail.com>
parents:
211
diff
changeset
|
56 pos = self.get_shape().body.position |
325c317cbfa1
Better protagonist physicser.
Jeremy Thurgood <firxen@gmail.com>
parents:
211
diff
changeset
|
57 return pymunk.pygame_util.to_pygame(pos, surface) |
63
7f038ee778ad
Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents:
62
diff
changeset
|
58 |
93
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
91
diff
changeset
|
59 def get_angle(self): |
215
325c317cbfa1
Better protagonist physicser.
Jeremy Thurgood <firxen@gmail.com>
parents:
211
diff
changeset
|
60 return self.get_shape().body.angle |
325c317cbfa1
Better protagonist physicser.
Jeremy Thurgood <firxen@gmail.com>
parents:
211
diff
changeset
|
61 |
217
d98daba73055
Composition-based renderers.
Jeremy Thurgood <firxen@gmail.com>
parents:
216
diff
changeset
|
62 def get_velocity(self): |
d98daba73055
Composition-based renderers.
Jeremy Thurgood <firxen@gmail.com>
parents:
216
diff
changeset
|
63 return self.get_shape().body.velocity |
d98daba73055
Composition-based renderers.
Jeremy Thurgood <firxen@gmail.com>
parents:
216
diff
changeset
|
64 |
216
f23ab2dd6ce8
Clunkier properties to make pyflakes happy.
Jeremy Thurgood <firxen@gmail.com>
parents:
215
diff
changeset
|
65 def _get_position(self): |
215
325c317cbfa1
Better protagonist physicser.
Jeremy Thurgood <firxen@gmail.com>
parents:
211
diff
changeset
|
66 return self.get_shape().body.position |
325c317cbfa1
Better protagonist physicser.
Jeremy Thurgood <firxen@gmail.com>
parents:
211
diff
changeset
|
67 |
216
f23ab2dd6ce8
Clunkier properties to make pyflakes happy.
Jeremy Thurgood <firxen@gmail.com>
parents:
215
diff
changeset
|
68 def _set_position(self, position): |
215
325c317cbfa1
Better protagonist physicser.
Jeremy Thurgood <firxen@gmail.com>
parents:
211
diff
changeset
|
69 self.get_shape().body.position = position |
93
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
91
diff
changeset
|
70 |
216
f23ab2dd6ce8
Clunkier properties to make pyflakes happy.
Jeremy Thurgood <firxen@gmail.com>
parents:
215
diff
changeset
|
71 position = property(_get_position, _set_position) |
f23ab2dd6ce8
Clunkier properties to make pyflakes happy.
Jeremy Thurgood <firxen@gmail.com>
parents:
215
diff
changeset
|
72 |
93
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
91
diff
changeset
|
73 def apply_impulse(self, j, r=(0, 0)): |
215
325c317cbfa1
Better protagonist physicser.
Jeremy Thurgood <firxen@gmail.com>
parents:
211
diff
changeset
|
74 return self.get_shape().body.apply_impulse(j, r) |
59 | 75 |
76 | |
77 class SingleShapePhysicser(Physicser): | |
93
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
91
diff
changeset
|
78 def __init__(self, space, shape): |
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
91
diff
changeset
|
79 super(SingleShapePhysicser, self).__init__(space) |
59 | 80 self._shape = shape |
186
d63c19003aec
Some refactoring and fixing, start of better collision handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
185
diff
changeset
|
81 shape.physicser = self |
59 | 82 |
123
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
83 def get_shape(self): |
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
84 return self._shape |
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
85 |
59 | 86 |
133 | 87 def damping_velocity_func(body, gravity, damping, dt): |
88 """Apply custom damping to this body's velocity. | |
89 """ | |
90 damping = getattr(body, 'damping', damping) | |
91 return pymunk.Body.update_velocity(body, gravity, damping, dt) | |
92 | |
93 | |
94 def make_body(mass, moment, position, damping=None): | |
95 body = pymunk.Body(mass, moment) | |
145
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
143
diff
changeset
|
96 body.position = tuple(position) |
133 | 97 if damping is not None: |
98 body.damping = damping | |
99 body.velocity_func = damping_velocity_func | |
100 return body | |
101 | |
102 | |
59 | 103 class GameObject(object): |
104 """A representation of a thing in the game world. | |
105 | |
106 This has a rendery thing, physicsy things and maybe some other things. | |
107 """ | |
108 | |
162
507df17cfbaf
Pictures for lights and switches.
Jeremy Thurgood <firxen@gmail.com>
parents:
160
diff
changeset
|
109 zorder = ZORDER_LOW |
218
9e2ef2f15035
Better rendering and movement detection.
Jeremy Thurgood <firxen@gmail.com>
parents:
217
diff
changeset
|
110 is_moving = False # `True` if a movement animation should play. |
162
507df17cfbaf
Pictures for lights and switches.
Jeremy Thurgood <firxen@gmail.com>
parents:
160
diff
changeset
|
111 |
281
9b56e954c674
Protagonist actions, now required for operating doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
276
diff
changeset
|
112 def __init__(self, physicser, renderer, puzzler=None, overlay=None, |
9b56e954c674
Protagonist actions, now required for operating doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
276
diff
changeset
|
113 interactible=None): |
93
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
91
diff
changeset
|
114 self.physicser = physicser |
123
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
115 physicser.set_game_object(self) |
93
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
91
diff
changeset
|
116 self.physicser.add_to_space() |
59 | 117 self.renderer = renderer |
123
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
118 renderer.set_game_object(self) |
81
a1b4d09e6f23
Floor switch with horrible hackery.
davidsharpe@lantea.local
parents:
63
diff
changeset
|
119 self.puzzler = puzzler |
123
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
120 if puzzler is not None: |
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
121 puzzler.set_game_object(self) |
191 | 122 self.overlay = overlay |
123 if overlay is not None: | |
124 self.overlay.set_game_object(self) | |
281
9b56e954c674
Protagonist actions, now required for operating doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
276
diff
changeset
|
125 self.interactible = interactible |
9b56e954c674
Protagonist actions, now required for operating doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
276
diff
changeset
|
126 if interactible is not None: |
9b56e954c674
Protagonist actions, now required for operating doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
276
diff
changeset
|
127 self.interactible.set_game_object(self) |
293
47226c661ae2
Bullets that mostly die when they hit things
Stefano Rivera <stefano@rivera.za.net>
parents:
286
diff
changeset
|
128 self.remove = False # If true, will be removed from drawables |
59 | 129 |
123
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
130 def get_space(self): |
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
131 return self.physicser.get_space() |
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
132 |
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
133 def get_shape(self): |
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
134 return self.physicser.get_shape() |
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
135 |
93
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
91
diff
changeset
|
136 def get_render_position(self, surface): |
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
91
diff
changeset
|
137 return self.physicser.get_render_position(surface) |
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
91
diff
changeset
|
138 |
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
91
diff
changeset
|
139 def get_render_angle(self): |
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
91
diff
changeset
|
140 return self.physicser.get_angle() |
59 | 141 |
229
329b3044ddef
Much better facing renderers.
Jeremy Thurgood <firxen@gmail.com>
parents:
224
diff
changeset
|
142 def get_facing_direction(self): |
329b3044ddef
Much better facing renderers.
Jeremy Thurgood <firxen@gmail.com>
parents:
224
diff
changeset
|
143 """Used by rendererd that care what direction an object is facing. |
329b3044ddef
Much better facing renderers.
Jeremy Thurgood <firxen@gmail.com>
parents:
224
diff
changeset
|
144 """ |
329b3044ddef
Much better facing renderers.
Jeremy Thurgood <firxen@gmail.com>
parents:
224
diff
changeset
|
145 return None |
329b3044ddef
Much better facing renderers.
Jeremy Thurgood <firxen@gmail.com>
parents:
224
diff
changeset
|
146 |
59 | 147 def render(self, surface): |
123
23b533d6f27e
Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents:
107
diff
changeset
|
148 return self.renderer.render(surface) |
81
a1b4d09e6f23
Floor switch with horrible hackery.
davidsharpe@lantea.local
parents:
63
diff
changeset
|
149 |
307
c2bbb1e70d6f
Rename animate to update and pass seconds, for future fun
Neil Muller <drnlmuller@gmail.com>
parents:
305
diff
changeset
|
150 def update(self, seconds): |
c2bbb1e70d6f
Rename animate to update and pass seconds, for future fun
Neil Muller <drnlmuller@gmail.com>
parents:
305
diff
changeset
|
151 self.renderer.update(seconds) |
143
deac6a4008e7
Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents:
140
diff
changeset
|
152 |
302
a0a471ad2ee8
Pass bullet hits through to the target
Stefano Rivera <stefano@rivera.za.net>
parents:
297
diff
changeset
|
153 def hit(self, weapon): |
a0a471ad2ee8
Pass bullet hits through to the target
Stefano Rivera <stefano@rivera.za.net>
parents:
297
diff
changeset
|
154 '''Was hit with a weapon (such as a bullet)''' |
a0a471ad2ee8
Pass bullet hits through to the target
Stefano Rivera <stefano@rivera.za.net>
parents:
297
diff
changeset
|
155 pass |
a0a471ad2ee8
Pass bullet hits through to the target
Stefano Rivera <stefano@rivera.za.net>
parents:
297
diff
changeset
|
156 |
256
2a0bad886956
Collision handlers get the protagonist.
Jeremy Thurgood <firxen@gmail.com>
parents:
235
diff
changeset
|
157 def collide_with_protagonist(self, protagonist): |
186
d63c19003aec
Some refactoring and fixing, start of better collision handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
185
diff
changeset
|
158 """Called as a `pre_solve` collision callback with the protagonist. |
d63c19003aec
Some refactoring and fixing, start of better collision handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
185
diff
changeset
|
159 |
d63c19003aec
Some refactoring and fixing, start of better collision handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
185
diff
changeset
|
160 You can return `False` to ignore the collision, anything else |
d63c19003aec
Some refactoring and fixing, start of better collision handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
185
diff
changeset
|
161 (including `None`) to process the collision as normal. |
d63c19003aec
Some refactoring and fixing, start of better collision handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
185
diff
changeset
|
162 """ |
192
3dc2b6290e66
Document collision handler a little better.
Jeremy Thurgood <firxen@gmail.com>
parents:
191
diff
changeset
|
163 return True |
186
d63c19003aec
Some refactoring and fixing, start of better collision handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
185
diff
changeset
|
164 |
319
01e98732de46
Open bulkheads are no longer opaque to furniture
Stefano Rivera <stefano@rivera.za.net>
parents:
318
diff
changeset
|
165 def collide_with_furniture(self, furniture): |
01e98732de46
Open bulkheads are no longer opaque to furniture
Stefano Rivera <stefano@rivera.za.net>
parents:
318
diff
changeset
|
166 return True |
01e98732de46
Open bulkheads are no longer opaque to furniture
Stefano Rivera <stefano@rivera.za.net>
parents:
318
diff
changeset
|
167 |
235
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
168 @classmethod |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
169 def requires(cls): |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
170 """Hints for the level editor""" |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
171 return [("name", "string")] |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
172 |
81
a1b4d09e6f23
Floor switch with horrible hackery.
davidsharpe@lantea.local
parents:
63
diff
changeset
|
173 |
a1b4d09e6f23
Floor switch with horrible hackery.
davidsharpe@lantea.local
parents:
63
diff
changeset
|
174 class FloorSwitch(GameObject): |
162
507df17cfbaf
Pictures for lights and switches.
Jeremy Thurgood <firxen@gmail.com>
parents:
160
diff
changeset
|
175 zorder = ZORDER_FLOOR |
507df17cfbaf
Pictures for lights and switches.
Jeremy Thurgood <firxen@gmail.com>
parents:
160
diff
changeset
|
176 |
93
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
91
diff
changeset
|
177 def __init__(self, space, position): |
145
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
143
diff
changeset
|
178 body = make_body(None, None, position) |
81
a1b4d09e6f23
Floor switch with horrible hackery.
davidsharpe@lantea.local
parents:
63
diff
changeset
|
179 self.shape = pymunk.Circle(body, 30) |
a1b4d09e6f23
Floor switch with horrible hackery.
davidsharpe@lantea.local
parents:
63
diff
changeset
|
180 self.shape.collision_type = COLLISION_TYPE_SWITCH |
a1b4d09e6f23
Floor switch with horrible hackery.
davidsharpe@lantea.local
parents:
63
diff
changeset
|
181 self.shape.sensor = True |
a1b4d09e6f23
Floor switch with horrible hackery.
davidsharpe@lantea.local
parents:
63
diff
changeset
|
182 super(FloorSwitch, self).__init__( |
93
d6a49f0c1e6e
Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents:
91
diff
changeset
|
183 SingleShapePhysicser(space, self.shape), |
207 | 184 render.ImageStateRenderer({ |
162
507df17cfbaf
Pictures for lights and switches.
Jeremy Thurgood <firxen@gmail.com>
parents:
160
diff
changeset
|
185 True: resources.get_image('objects', 'sensor_on.png'), |
507df17cfbaf
Pictures for lights and switches.
Jeremy Thurgood <firxen@gmail.com>
parents:
160
diff
changeset
|
186 False: resources.get_image('objects', 'sensor_off.png'), |
507df17cfbaf
Pictures for lights and switches.
Jeremy Thurgood <firxen@gmail.com>
parents:
160
diff
changeset
|
187 }), |
201
3495a2025bc6
Break puzzlers out of game_object.py
Stefano Rivera <stefano@rivera.za.net>
parents:
196
diff
changeset
|
188 puzzle.CollidePuzzler(*SWITCH_PUSHERS), |
81
a1b4d09e6f23
Floor switch with horrible hackery.
davidsharpe@lantea.local
parents:
63
diff
changeset
|
189 ) |
a1b4d09e6f23
Floor switch with horrible hackery.
davidsharpe@lantea.local
parents:
63
diff
changeset
|
190 |
235
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
191 @classmethod |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
192 def requires(cls): |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
193 return [("name", "string"), ("position", "coordinates")] |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
194 |
106
bce9cd8a4a8c
FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents:
104
diff
changeset
|
195 |
191 | 196 class Note(GameObject): |
197 zorder = ZORDER_FLOOR | |
198 | |
199 def __init__(self, space, position, message): | |
200 body = make_body(None, None, position) | |
201 self.shape = pymunk.Circle(body, 30) | |
202 self.shape.sensor = True | |
203 super(Note, self).__init__( | |
204 SingleShapePhysicser(space, self.shape), | |
207 | 205 render.ImageRenderer(resources.get_image('objects', 'note.png')), |
201
3495a2025bc6
Break puzzlers out of game_object.py
Stefano Rivera <stefano@rivera.za.net>
parents:
196
diff
changeset
|
206 puzzle.CollidePuzzler(), |
222
cc5f2a5ac501
Overlays belong in render
Stefano Rivera <stefano@rivera.za.net>
parents:
218
diff
changeset
|
207 render.TextOverlay(message), |
191 | 208 ) |
209 | |
235
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
210 @classmethod |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
211 def requires(cls): |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
212 return [("name", "string"), ("position", "coordinates"), |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
213 ("message", "text")] |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
214 |
191 | 215 |
106
bce9cd8a4a8c
FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents:
104
diff
changeset
|
216 class FloorLight(GameObject): |
162
507df17cfbaf
Pictures for lights and switches.
Jeremy Thurgood <firxen@gmail.com>
parents:
160
diff
changeset
|
217 zorder = ZORDER_FLOOR |
507df17cfbaf
Pictures for lights and switches.
Jeremy Thurgood <firxen@gmail.com>
parents:
160
diff
changeset
|
218 |
106
bce9cd8a4a8c
FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents:
104
diff
changeset
|
219 def __init__(self, space, position, state_source): |
145
0c49627920eb
Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents:
143
diff
changeset
|
220 body = make_body(None, None, position) |
106
bce9cd8a4a8c
FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents:
104
diff
changeset
|
221 self.shape = pymunk.Circle(body, 10) |
bce9cd8a4a8c
FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents:
104
diff
changeset
|
222 self.shape.collision_type = COLLISION_TYPE_SWITCH |
bce9cd8a4a8c
FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents:
104
diff
changeset
|
223 self.shape.sensor = True |
bce9cd8a4a8c
FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents:
104
diff
changeset
|
224 super(FloorLight, self).__init__( |
bce9cd8a4a8c
FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents:
104
diff
changeset
|
225 SingleShapePhysicser(space, self.shape), |
207 | 226 render.ImageStateRenderer({ |
162
507df17cfbaf
Pictures for lights and switches.
Jeremy Thurgood <firxen@gmail.com>
parents:
160
diff
changeset
|
227 True: resources.get_image('objects', 'light_on.png'), |
507df17cfbaf
Pictures for lights and switches.
Jeremy Thurgood <firxen@gmail.com>
parents:
160
diff
changeset
|
228 False: resources.get_image('objects', 'light_off.png'), |
507df17cfbaf
Pictures for lights and switches.
Jeremy Thurgood <firxen@gmail.com>
parents:
160
diff
changeset
|
229 }), |
201
3495a2025bc6
Break puzzlers out of game_object.py
Stefano Rivera <stefano@rivera.za.net>
parents:
196
diff
changeset
|
230 puzzle.StateProxyPuzzler(state_source), |
106
bce9cd8a4a8c
FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents:
104
diff
changeset
|
231 ) |
133 | 232 |
235
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
233 @classmethod |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
234 def requires(cls): |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
235 return [("name", "string"), ("position", "coordinates"), |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
236 ("state_source", "puzzler")] |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
237 |
133 | 238 |
239 class Box(GameObject): | |
240 def __init__(self, space, position): | |
241 body = make_body(10, 10000, position, damping=0.5) | |
242 self.shape = pymunk.Poly( | |
243 body, [(-20, -20), (20, -20), (20, 20), (-20, 20)]) | |
208
3d54fe7a2998
Fun with mass and friction.
Jeremy Thurgood <firxen@gmail.com>
parents:
207
diff
changeset
|
244 self.shape.friction = 0.5 |
318
26d1978fa1da
BOX is no longer accurate
Stefano Rivera <stefano@rivera.za.net>
parents:
313
diff
changeset
|
245 self.shape.collision_type = COLLISION_TYPE_FURNITURE |
133 | 246 super(Box, self).__init__( |
247 SingleShapePhysicser(space, self.shape), | |
207 | 248 render.ImageRenderer(resources.get_image('objects', 'crate.png')), |
133 | 249 ) |
176 | 250 |
235
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
251 @classmethod |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
252 def requires(cls): |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
253 return [("name", "string"), ("position", "coordinates"), |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
254 ("state_source", "puzzler")] |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
255 |
176 | 256 |
257 class Door(GameObject): | |
258 zorder = ZORDER_FLOOR | |
259 | |
263 | 260 def __init__(self, space, position, destination, dest_pos, angle, |
261 key_state=None): | |
176 | 262 body = make_body(pymunk.inf, pymunk.inf, position, damping=0.5) |
281
9b56e954c674
Protagonist actions, now required for operating doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
276
diff
changeset
|
263 self.shape = pymunk.Circle(body, 30) |
176 | 264 self.shape.collision_type = COLLISION_TYPE_DOOR |
264 | 265 self.shape.body.angle = float(angle) / 180 * math.pi |
176 | 266 self.shape.sensor = True |
267 self.destination = destination | |
268 self.dest_pos = tuple(dest_pos) | |
281
9b56e954c674
Protagonist actions, now required for operating doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
276
diff
changeset
|
269 puzzler = None |
9b56e954c674
Protagonist actions, now required for operating doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
276
diff
changeset
|
270 action = environment.Action(self._post_door_event) |
9b56e954c674
Protagonist actions, now required for operating doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
276
diff
changeset
|
271 if key_state is not None: |
201
3495a2025bc6
Break puzzlers out of game_object.py
Stefano Rivera <stefano@rivera.za.net>
parents:
196
diff
changeset
|
272 puzzler = puzzle.StateProxyPuzzler(key_state) |
281
9b56e954c674
Protagonist actions, now required for operating doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
276
diff
changeset
|
273 action.condition = environment.PuzzleStateCondition(puzzler) |
176 | 274 super(Door, self).__init__( |
275 SingleShapePhysicser(space, self.shape), | |
207 | 276 render.ImageRenderer(resources.get_image('objects', 'door.png')), |
186
d63c19003aec
Some refactoring and fixing, start of better collision handling.
Jeremy Thurgood <firxen@gmail.com>
parents:
185
diff
changeset
|
277 puzzler, |
281
9b56e954c674
Protagonist actions, now required for operating doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
276
diff
changeset
|
278 interactible=environment.Interactible(action), |
176 | 279 ) |
280 | |
281
9b56e954c674
Protagonist actions, now required for operating doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
276
diff
changeset
|
281 def _post_door_event(self, protagonist): |
9b56e954c674
Protagonist actions, now required for operating doors.
Jeremy Thurgood <firxen@gmail.com>
parents:
276
diff
changeset
|
282 DoorEvent.post(self.destination, self.dest_pos) |
224
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
283 |
235
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
284 @classmethod |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
285 def requires(cls): |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
286 return [("name", "string"), ("position", "coordinates"), |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
287 ("destination", "level name"), ("dest_pos", "coordinate"), |
263 | 288 ("angle", "degrees"), |
289 ("key_state", "puzzler (optional)")] | |
235
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
290 |
224
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
291 |
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
292 class Bulkhead(GameObject): |
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
293 zorder = ZORDER_FLOOR |
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
294 |
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
295 def __init__(self, space, end1, end2, key_state=None): |
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
296 body = make_body(None, None, (0, 0)) |
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
297 self.shape = pymunk.Segment(body, tuple(end1), tuple(end2), 3) |
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
298 self.shape.collision_type = COLLISION_TYPE_DOOR |
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
299 if key_state is None: |
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
300 puzzler = puzzle.YesPuzzler() |
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
301 else: |
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
302 puzzler = puzzle.StateProxyPuzzler(key_state) |
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
303 super(Bulkhead, self).__init__( |
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
304 SingleShapePhysicser(space, self.shape), |
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
305 render.ShapeStateRenderer(), |
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
306 puzzler, |
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
307 ) |
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
308 |
256
2a0bad886956
Collision handlers get the protagonist.
Jeremy Thurgood <firxen@gmail.com>
parents:
235
diff
changeset
|
309 def collide_with_protagonist(self, protagonist): |
224
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
310 if self.puzzler.get_state(): |
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
311 # Reject the collision, we can walk through. |
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
312 return False |
b6db213e53a2
Bulkheads are bits of wall you can walk through.
Jeremy Thurgood <firxen@gmail.com>
parents:
222
diff
changeset
|
313 return True |
235
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
314 |
319
01e98732de46
Open bulkheads are no longer opaque to furniture
Stefano Rivera <stefano@rivera.za.net>
parents:
318
diff
changeset
|
315 collide_with_furniture = collide_with_protagonist |
01e98732de46
Open bulkheads are no longer opaque to furniture
Stefano Rivera <stefano@rivera.za.net>
parents:
318
diff
changeset
|
316 |
235
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
317 @classmethod |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
318 def requires(cls): |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
319 return [("name", "string"), ("end1", "coordinates"), |
831e4f6b3d18
Add hints for the level editor
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
320 ("end2", "coordinates"), ("key_state", "puzzler")] |
261
db7c8e74efb4
(really rubbish) bullets
Stefano Rivera <stefano@rivera.za.net>
parents:
256
diff
changeset
|
321 |
db7c8e74efb4
(really rubbish) bullets
Stefano Rivera <stefano@rivera.za.net>
parents:
256
diff
changeset
|
322 |
282
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
323 class ToggleSwitch(GameObject): |
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
324 zorder = ZORDER_LOW |
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
325 |
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
326 def __init__(self, space, position): |
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
327 body = make_body(None, None, position) |
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
328 self.shape = pymunk.Circle(body, 20) |
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
329 self.shape.sensor = True |
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
330 self.toggle_on = False |
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
331 super(ToggleSwitch, self).__init__( |
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
332 SingleShapePhysicser(space, self.shape), |
296
eb08426a58fe
Levers look like levers.
Jeremy Thurgood <firxen@gmail.com>
parents:
295
diff
changeset
|
333 render.ImageStateRenderer({ |
eb08426a58fe
Levers look like levers.
Jeremy Thurgood <firxen@gmail.com>
parents:
295
diff
changeset
|
334 True: resources.get_image('objects', 'lever.png'), |
eb08426a58fe
Levers look like levers.
Jeremy Thurgood <firxen@gmail.com>
parents:
295
diff
changeset
|
335 False: resources.get_image( |
eb08426a58fe
Levers look like levers.
Jeremy Thurgood <firxen@gmail.com>
parents:
295
diff
changeset
|
336 'objects', 'lever.png', transforms=(FLIP_H,)), |
eb08426a58fe
Levers look like levers.
Jeremy Thurgood <firxen@gmail.com>
parents:
295
diff
changeset
|
337 }), |
282
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
338 puzzle.ParentAttrPuzzler('toggle_on'), |
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
339 interactible=environment.Interactible( |
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
340 environment.Action(self._toggle)), |
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
341 ) |
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
342 |
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
343 def _toggle(self, protagonist): |
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
344 self.toggle_on = not self.toggle_on |
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
345 |
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
346 @classmethod |
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
347 def requires(cls): |
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
348 return [("name", "string"), ("position", "coordinates")] |
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
349 |
9d186b897d82
Toggle switch, sans art.
Jeremy Thurgood <firxen@gmail.com>
parents:
281
diff
changeset
|
350 |
261
db7c8e74efb4
(really rubbish) bullets
Stefano Rivera <stefano@rivera.za.net>
parents:
256
diff
changeset
|
351 class Bullet(GameObject): |
305 | 352 def __init__(self, space, position, impulse, damage, |
353 source_collision_type): | |
261
db7c8e74efb4
(really rubbish) bullets
Stefano Rivera <stefano@rivera.za.net>
parents:
256
diff
changeset
|
354 body = make_body(1, pymunk.inf, position) |
293
47226c661ae2
Bullets that mostly die when they hit things
Stefano Rivera <stefano@rivera.za.net>
parents:
286
diff
changeset
|
355 self.last_position = position |
286
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
356 self.shape = pymunk.Circle(body, 2) |
293
47226c661ae2
Bullets that mostly die when they hit things
Stefano Rivera <stefano@rivera.za.net>
parents:
286
diff
changeset
|
357 self.shape.sensor = True |
261
db7c8e74efb4
(really rubbish) bullets
Stefano Rivera <stefano@rivera.za.net>
parents:
256
diff
changeset
|
358 self.shape.collision_type = COLLISION_TYPE_PROJECTILE |
305 | 359 self.damage = damage |
293
47226c661ae2
Bullets that mostly die when they hit things
Stefano Rivera <stefano@rivera.za.net>
parents:
286
diff
changeset
|
360 self.source_collision_type = source_collision_type |
261
db7c8e74efb4
(really rubbish) bullets
Stefano Rivera <stefano@rivera.za.net>
parents:
256
diff
changeset
|
361 super(Bullet, self).__init__( |
db7c8e74efb4
(really rubbish) bullets
Stefano Rivera <stefano@rivera.za.net>
parents:
256
diff
changeset
|
362 SingleShapePhysicser(space, self.shape), |
db7c8e74efb4
(really rubbish) bullets
Stefano Rivera <stefano@rivera.za.net>
parents:
256
diff
changeset
|
363 render.ImageRenderer(resources.get_image('objects', 'bullet.png')), |
db7c8e74efb4
(really rubbish) bullets
Stefano Rivera <stefano@rivera.za.net>
parents:
256
diff
changeset
|
364 ) |
db7c8e74efb4
(really rubbish) bullets
Stefano Rivera <stefano@rivera.za.net>
parents:
256
diff
changeset
|
365 self.physicser.apply_impulse(impulse) |
286
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
366 |
307
c2bbb1e70d6f
Rename animate to update and pass seconds, for future fun
Neil Muller <drnlmuller@gmail.com>
parents:
305
diff
changeset
|
367 def update(self, seconds): |
c2bbb1e70d6f
Rename animate to update and pass seconds, for future fun
Neil Muller <drnlmuller@gmail.com>
parents:
305
diff
changeset
|
368 super(Bullet, self).update(seconds) |
297
b00ed05f7364
Create a tuple of bullet position, so we don't just hold a reference to a mutable object
Stefano Rivera <stefano@rivera.za.net>
parents:
296
diff
changeset
|
369 position = (self.physicser.position.x, self.physicser.position.y) |
293
47226c661ae2
Bullets that mostly die when they hit things
Stefano Rivera <stefano@rivera.za.net>
parents:
286
diff
changeset
|
370 r = self.get_space().segment_query(self.last_position, position) |
47226c661ae2
Bullets that mostly die when they hit things
Stefano Rivera <stefano@rivera.za.net>
parents:
286
diff
changeset
|
371 self.last_position = position |
47226c661ae2
Bullets that mostly die when they hit things
Stefano Rivera <stefano@rivera.za.net>
parents:
286
diff
changeset
|
372 for collision in r: |
302
a0a471ad2ee8
Pass bullet hits through to the target
Stefano Rivera <stefano@rivera.za.net>
parents:
297
diff
changeset
|
373 shape = collision.shape |
a0a471ad2ee8
Pass bullet hits through to the target
Stefano Rivera <stefano@rivera.za.net>
parents:
297
diff
changeset
|
374 if (shape.collision_type == self.source_collision_type |
a0a471ad2ee8
Pass bullet hits through to the target
Stefano Rivera <stefano@rivera.za.net>
parents:
297
diff
changeset
|
375 or shape == self.physicser.get_shape() |
a0a471ad2ee8
Pass bullet hits through to the target
Stefano Rivera <stefano@rivera.za.net>
parents:
297
diff
changeset
|
376 or shape.sensor): |
293
47226c661ae2
Bullets that mostly die when they hit things
Stefano Rivera <stefano@rivera.za.net>
parents:
286
diff
changeset
|
377 continue |
302
a0a471ad2ee8
Pass bullet hits through to the target
Stefano Rivera <stefano@rivera.za.net>
parents:
297
diff
changeset
|
378 if hasattr(shape, 'physicser'): |
a0a471ad2ee8
Pass bullet hits through to the target
Stefano Rivera <stefano@rivera.za.net>
parents:
297
diff
changeset
|
379 shape.physicser.game_object.hit(self) |
293
47226c661ae2
Bullets that mostly die when they hit things
Stefano Rivera <stefano@rivera.za.net>
parents:
286
diff
changeset
|
380 self.physicser.remove_from_space() |
47226c661ae2
Bullets that mostly die when they hit things
Stefano Rivera <stefano@rivera.za.net>
parents:
286
diff
changeset
|
381 self.remove = True |
47226c661ae2
Bullets that mostly die when they hit things
Stefano Rivera <stefano@rivera.za.net>
parents:
286
diff
changeset
|
382 break |
47226c661ae2
Bullets that mostly die when they hit things
Stefano Rivera <stefano@rivera.za.net>
parents:
286
diff
changeset
|
383 |
286
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
384 |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
385 class CollectibleGameObject(GameObject): |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
386 zorder = ZORDER_LOW |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
387 |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
388 def __init__(self, space, name, shape, renderer): |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
389 self._name = name |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
390 shape.sensor = True |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
391 super(CollectibleGameObject, self).__init__( |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
392 SingleShapePhysicser(space, shape), |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
393 renderer, |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
394 interactible=environment.Interactible( |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
395 environment.Action( |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
396 self._collect, environment.HumanFormCondition())), |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
397 ) |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
398 |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
399 def _collect(self, protagonist): |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
400 protagonist.inventory[self._name] = self |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
401 # TODO: Make this less hacky. |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
402 self.physicser.remove_from_space() |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
403 self.renderer = render.NullRenderer() |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
404 |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
405 |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
406 class Gun(CollectibleGameObject): |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
407 def __init__(self, space, position): |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
408 body = make_body(None, None, position) |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
409 self.shape = pymunk.Circle(body, 20) |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
410 super(Gun, self).__init__( |
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
411 space, 'gun', self.shape, |
332 | 412 render.ImageRenderer(resources.get_image('objects', 'gun.png')), |
286
248b8fdb160c
Collect gun (which currently looks like a bullet) to shoot.
Jeremy Thurgood <firxen@gmail.com>
parents:
282
diff
changeset
|
413 ) |
312
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
414 |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
415 |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
416 class ClawAttack(GameObject): |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
417 def __init__(self, space, position, vector, source_collision_type): |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
418 body = make_body(1, pymunk.inf, position) |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
419 self.last_position = position |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
420 self.shape = pymunk.Circle(body, 30) |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
421 self.shape.sensor = True |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
422 self.shape.collision_type = COLLISION_TYPE_WEREWOLF_ATTACK |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
423 self.source_collision_type = source_collision_type |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
424 super(ClawAttack, self).__init__( |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
425 SingleShapePhysicser(space, self.shape), |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
426 render.ImageRenderer(resources.get_image |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
427 ('objects', 'werewolf_SW_claw_attack.png')), |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
428 ) |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
429 self.time_created = time.time() |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
430 |
331
5030ef2718ae
Fix claw attack rendering a bit.
Simon Cross <hodgestar@gmail.com>
parents:
319
diff
changeset
|
431 def update(self, seconds): |
5030ef2718ae
Fix claw attack rendering a bit.
Simon Cross <hodgestar@gmail.com>
parents:
319
diff
changeset
|
432 super(ClawAttack, self).update(seconds) |
312
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
433 position = (self.physicser.position.x, self.physicser.position.y) |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
434 r = self.get_space().segment_query(self.last_position, position) |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
435 self.last_position = position |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
436 for collision in r: |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
437 shape = collision.shape |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
438 if (shape.collision_type == self.source_collision_type |
331
5030ef2718ae
Fix claw attack rendering a bit.
Simon Cross <hodgestar@gmail.com>
parents:
319
diff
changeset
|
439 or shape == self.physicser.get_shape() |
5030ef2718ae
Fix claw attack rendering a bit.
Simon Cross <hodgestar@gmail.com>
parents:
319
diff
changeset
|
440 or shape.sensor): |
312
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
441 continue |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
442 if hasattr(shape, 'physicser'): |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
443 shape.physicser.game_object.hit(self) |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
444 if time.time() - self.time_created > 0.2: |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
445 self.physicser.remove_from_space() |
72aca01c87ed
Basic claw attack, stealing liberally from other people's code! ;)
David Sharpe
parents:
307
diff
changeset
|
446 self.remove = True |