annotate nagslang/game_object.py @ 145:0c49627920eb

Load game objects from level.
author Jeremy Thurgood <firxen@gmail.com>
date Mon, 02 Sep 2013 23:05:25 +0200
parents deac6a4008e7
children b455873020be
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
63
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
1 import math
62
1d67a8c9861d WTF whitespace?!?!?!?!
Jeremy Thurgood <firxen@gmail.com>
parents: 60
diff changeset
2
81
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
3 import pygame
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
4 import pymunk
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
5 import pymunk.pygame_util
81
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
6
107
b90d01e4d9d4 Layered drawing.
Jeremy Thurgood <firxen@gmail.com>
parents: 106
diff changeset
7 from nagslang.constants import (
133
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
8 SWITCH_PUSHERS, COLLISION_TYPE_SWITCH, COLLISION_TYPE_BOX, ZORDER_LOW)
104
1be3eebb87c4 More consistent debug rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 93
diff changeset
9 from nagslang.options import options
81
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
10
82
11b0017b5e4b Fix whitespace.
davidsharpe@185.4.16.172.in-addr.arpa
parents: 81
diff changeset
11
106
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
12 class PuzzleGlue(object):
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
13 """Glue that holds bits of a puzzle together.
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
14 """
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
15 def __init__(self):
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
16 self._components = {}
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
17
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
18 def add_component(self, name, puzzler):
145
0c49627920eb Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents: 143
diff changeset
19 if not isinstance(puzzler, Puzzler):
0c49627920eb Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents: 143
diff changeset
20 puzzler = puzzler.puzzler
106
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
21 self._components[name] = puzzler
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
22 puzzler.set_glue(self)
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
23
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
24 def get_state_of(self, name):
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
25 return self._components[name].get_state()
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
26
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
27
81
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
28 class Puzzler(object):
106
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
29 """Behaviour specific to a puzzle component.
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
30 """
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
31 def set_glue(self, glue):
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
32 self.glue = glue
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
33
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
34 def set_game_object(self, game_object):
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
35 self.game_object = game_object
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
36
106
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
37 def get_state(self):
81
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
38 raise NotImplementedError()
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
39
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
40
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
41 class FloorSwitchPuzzler(Puzzler):
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
42 def get_state(self):
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
43 space = self.game_object.get_space()
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
44 for shape in space.shape_query(self.game_object.get_shape()):
81
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
45 if shape.collision_type in SWITCH_PUSHERS:
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
46 return True
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
47 return False
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
48
59
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
49
106
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
50 class StateProxyPuzzler(Puzzler):
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
51 def __init__(self, state_source):
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
52 self._state_source = state_source
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
53
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
54 def get_state(self):
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
55 return self.glue.get_state_of(self._state_source)
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
56
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
57
140
f36a7075d9a0 Two switch puzzle!
Jeremy Thurgood <firxen@gmail.com>
parents: 133
diff changeset
58 class StateLogicalAndPuzzler(Puzzler):
f36a7075d9a0 Two switch puzzle!
Jeremy Thurgood <firxen@gmail.com>
parents: 133
diff changeset
59 def __init__(self, *state_sources):
f36a7075d9a0 Two switch puzzle!
Jeremy Thurgood <firxen@gmail.com>
parents: 133
diff changeset
60 self._state_sources = state_sources
f36a7075d9a0 Two switch puzzle!
Jeremy Thurgood <firxen@gmail.com>
parents: 133
diff changeset
61
f36a7075d9a0 Two switch puzzle!
Jeremy Thurgood <firxen@gmail.com>
parents: 133
diff changeset
62 def get_state(self):
f36a7075d9a0 Two switch puzzle!
Jeremy Thurgood <firxen@gmail.com>
parents: 133
diff changeset
63 for state_source in self._state_sources:
f36a7075d9a0 Two switch puzzle!
Jeremy Thurgood <firxen@gmail.com>
parents: 133
diff changeset
64 if not self.glue.get_state_of(state_source):
f36a7075d9a0 Two switch puzzle!
Jeremy Thurgood <firxen@gmail.com>
parents: 133
diff changeset
65 return False
f36a7075d9a0 Two switch puzzle!
Jeremy Thurgood <firxen@gmail.com>
parents: 133
diff changeset
66 return True
f36a7075d9a0 Two switch puzzle!
Jeremy Thurgood <firxen@gmail.com>
parents: 133
diff changeset
67
f36a7075d9a0 Two switch puzzle!
Jeremy Thurgood <firxen@gmail.com>
parents: 133
diff changeset
68
59
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
69 class Physicser(object):
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
70 def __init__(self, space):
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
71 self._space = space
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
72
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
73 def get_space(self):
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
74 return self._space
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
75
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
76 def set_game_object(self, game_object):
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
77 self.game_object = game_object
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
78
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
79 def get_shape(self):
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
80 raise NotImplementedError()
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
81
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
82 def add_to_space(self):
59
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
83 raise NotImplementedError()
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
84
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
85 def remove_from_space(self):
59
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
86 raise NotImplementedError()
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
87
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
88 def get_render_position(self, surface):
63
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
89 raise NotImplementedError()
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
90
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
91 def get_angle(self):
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
92 raise NotImplementedError()
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
94 def apply_impulse(self, j, r=(0, 0)):
59
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
95 raise NotImplementedError()
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
96
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
97
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
98 class SingleShapePhysicser(Physicser):
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
99 def __init__(self, space, shape):
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
100 super(SingleShapePhysicser, self).__init__(space)
59
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
101 self._shape = shape
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
102
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
103 def get_shape(self):
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
104 return self._shape
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
105
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
106 def add_to_space(self):
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
107 self.get_space().add(self._shape)
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
108 if not self._shape.body.is_static:
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
109 self.get_space().add(self._shape.body)
59
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
110
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
111 def remove_from_space(self):
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
112 self.get_space().remove(self._shape)
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
113 if not self._shape.body.is_static:
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
114 self.get_space().remove(self._shape.body)
59
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
115
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
116 def get_render_position(self, surface):
59
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
117 pos = self._shape.body.position
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
118 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
119
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
120 def get_angle(self):
63
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
121 return self._shape.body.angle
59
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
122
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
123 def apply_impulse(self, j, r=(0, 0)):
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
124 return self._shape.body.apply_impulse(j, r)
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
125
59
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
126
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
127 class Renderer(object):
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
128 def set_game_object(self, game_object):
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
129 self.game_object = game_object
104
1be3eebb87c4 More consistent debug rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 93
diff changeset
130
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
131 def _render_shape(self, surface):
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
132 shape = self.game_object.get_shape()
104
1be3eebb87c4 More consistent debug rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 93
diff changeset
133 # Less general that pymunk.pygame_util.draw, but also a lot less noisy.
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
134 color = getattr(shape, 'color', pygame.color.THECOLORS['lightblue'])
104
1be3eebb87c4 More consistent debug rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 93
diff changeset
135 # We only explicitly draw Circle and Poly shapes. Everything else we
1be3eebb87c4 More consistent debug rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 93
diff changeset
136 # forward to pymunk.
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
137 if isinstance(shape, pymunk.Circle):
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
138 centre = pymunk.pygame_util.to_pygame(shape.body.position, surface)
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
139 radius = int(shape.radius)
104
1be3eebb87c4 More consistent debug rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 93
diff changeset
140 pygame.draw.circle(surface, color, centre, radius, 2)
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
141 elif isinstance(shape, pymunk.Poly):
104
1be3eebb87c4 More consistent debug rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 93
diff changeset
142 # polygon bounding box
1be3eebb87c4 More consistent debug rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 93
diff changeset
143 points = [pymunk.pygame_util.to_pygame(p, surface)
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
144 for p in shape.get_vertices()]
104
1be3eebb87c4 More consistent debug rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 93
diff changeset
145 pygame.draw.lines(surface, color, True, points, 2)
1be3eebb87c4 More consistent debug rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 93
diff changeset
146 else:
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
147 pymunk.pygame_util.draw(surface, shape)
104
1be3eebb87c4 More consistent debug rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 93
diff changeset
148
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
149 def render(self, surface):
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
150 pos = self.game_object.get_render_position(surface)
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
151 angle = self.game_object.get_render_angle()
104
1be3eebb87c4 More consistent debug rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 93
diff changeset
152 if options.debug:
1be3eebb87c4 More consistent debug rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 93
diff changeset
153 self._render_shape(surface, pos, angle)
59
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
154
143
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
155 def animate(self):
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
156 # Used by time animatations to advance the clock
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
157 pass
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
158
59
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
159
63
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
160 def image_pos(image, pos):
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
161 return (pos[0] - image.get_width() / 2,
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
162 pos[1] - image.get_height() / 2)
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
163
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
164
59
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
165 class ImageRenderer(Renderer):
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
166 def __init__(self, image):
59
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
167 self._image = image
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
168
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
169 def render(self, surface):
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
170 pos = self.game_object.get_render_position(surface)
63
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
171 surface.blit(self._image, image_pos(self._image, pos))
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
172 super(ImageRenderer, self).render(surface)
63
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
173
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
174
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
175 class FacingImageRenderer(Renderer):
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
176 def __init__(self, left_image, right_image):
63
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
177 self._images = {
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
178 'left': left_image,
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
179 'right': right_image,
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
180 }
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
181
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
182 def get_image(self, angle):
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
183 if abs(angle) < math.pi / 2:
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
184 return self._images['right']
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
185 return self._images['left']
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
186
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
187 def render(self, surface):
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
188 pos = self.game_object.get_render_position(surface)
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
189 image = self.get_image(self.game_object.get_render_angle())
63
7f038ee778ad Put werewolf facing direction magic back.
Jeremy Thurgood <firxen@gmail.com>
parents: 62
diff changeset
190 surface.blit(image, image_pos(image, pos))
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
191 super(FacingImageRenderer, self).render(surface)
59
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
192
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
193
143
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
194 class AnimatedFacingImageRenderer(FacingImageRenderer):
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
195 def __init__(self, left_images, right_images):
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
196 self._images = {
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
197 'left': left_images,
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
198 'right': right_images,
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
199 }
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
200 self._frame = 0
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
201 self._moving = False
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
202
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
203 def get_image(self, angle):
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
204 if abs(angle) < math.pi / 2:
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
205 face = 'right'
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
206 else:
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
207 face = 'left'
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
208 if self._frame >= len(self._images[face]):
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
209 self._frame = 0
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
210 return self._images[face][self._frame]
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
211
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
212 def render(self, surface):
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
213 pos = self.game_object.get_render_position(surface)
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
214 image = self.get_image(self.game_object.get_render_angle())
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
215 surface.blit(image, image_pos(image, pos))
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
216 super(FacingImageRenderer, self).render(surface)
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
217
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
218 def animate(self):
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
219 if self._moving:
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
220 self._frame += 1
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
221 else:
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
222 self._frame = 0
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
223
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
224 def start(self):
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
225 self._moving = True
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
226
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
227 def stop(self):
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
228 self._moving = False
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
229
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
230
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
231 class TimedAnimatedRenderer(ImageRenderer):
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
232
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
233 def __init__(self, images):
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
234 self._images = images
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
235 self._frame = 0
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
236 self._image = None
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
237
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
238 def _get_image(self):
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
239 if self._frame > len(self._imaages):
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
240 self._frame = 0
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
241 return self._images[self._frame]
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
242
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
243 def render(self, surface):
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
244 self._image = self._get_image()
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
245 super(TimedAnimatedRenderer, self).render(surface)
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
246
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
247 def animate(self):
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
248 self._frame += 1
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
249
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
250
133
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
251 class ShapeRenderer(Renderer):
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
252 def render(self, surface):
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
253 self._render_shape(surface)
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
254 super(ShapeRenderer, self).render(surface)
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
255
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
256
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
257 class ShapeStateRenderer(ShapeRenderer):
126
c3af35561494 Cleaner switch/light rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 123
diff changeset
258 """Renders the shape in a different colour depending on the state.
c3af35561494 Cleaner switch/light rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 123
diff changeset
259
c3af35561494 Cleaner switch/light rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 123
diff changeset
260 Requires the game object it's attached to to have a puzzler.
c3af35561494 Cleaner switch/light rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 123
diff changeset
261 """
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
262 def render(self, surface):
126
c3af35561494 Cleaner switch/light rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 123
diff changeset
263 if self.game_object.puzzler.get_state():
c3af35561494 Cleaner switch/light rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 123
diff changeset
264 color = pygame.color.THECOLORS['green']
c3af35561494 Cleaner switch/light rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 123
diff changeset
265 else:
c3af35561494 Cleaner switch/light rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 123
diff changeset
266 color = pygame.color.THECOLORS['red']
c3af35561494 Cleaner switch/light rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 123
diff changeset
267
c3af35561494 Cleaner switch/light rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 123
diff changeset
268 self.game_object.get_shape().color = color
c3af35561494 Cleaner switch/light rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 123
diff changeset
269 super(ShapeStateRenderer, self).render(surface)
59
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
270
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
271
133
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
272 def damping_velocity_func(body, gravity, damping, dt):
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
273 """Apply custom damping to this body's velocity.
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
274 """
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
275 damping = getattr(body, 'damping', damping)
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
276 return pymunk.Body.update_velocity(body, gravity, damping, dt)
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
277
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
278
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
279 def make_body(mass, moment, position, damping=None):
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
280 body = pymunk.Body(mass, moment)
145
0c49627920eb Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents: 143
diff changeset
281 body.position = tuple(position)
133
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
282 if damping is not None:
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
283 body.damping = damping
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
284 body.velocity_func = damping_velocity_func
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
285 return body
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
286
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
287
59
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
288 class GameObject(object):
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
289 """A representation of a thing in the game world.
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
290
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
291 This has a rendery thing, physicsy things and maybe some other things.
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
292 """
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
293
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
294 def __init__(self, physicser, renderer, puzzler=None):
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
295 self.physicser = physicser
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
296 physicser.set_game_object(self)
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
297 self.physicser.add_to_space()
59
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
298 self.renderer = renderer
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
299 renderer.set_game_object(self)
81
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
300 self.puzzler = puzzler
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
301 if puzzler is not None:
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
302 puzzler.set_game_object(self)
107
b90d01e4d9d4 Layered drawing.
Jeremy Thurgood <firxen@gmail.com>
parents: 106
diff changeset
303 self.zorder = ZORDER_LOW
59
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
304
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
305 def get_space(self):
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
306 return self.physicser.get_space()
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
307
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
308 def get_shape(self):
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
309 return self.physicser.get_shape()
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
310
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
311 def get_render_position(self, surface):
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
312 return self.physicser.get_render_position(surface)
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
313
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
314 def get_render_angle(self):
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
315 return self.physicser.get_angle()
59
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
316
b412704a6737 Start of game object stuff.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
317 def render(self, surface):
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
318 return self.renderer.render(surface)
81
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
319
143
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
320 def animate(self):
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
321 self.renderer.animate()
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 140
diff changeset
322
81
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
323
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
324 class FloorSwitch(GameObject):
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
325 def __init__(self, space, position):
145
0c49627920eb Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents: 143
diff changeset
326 body = make_body(None, None, position)
81
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
327 self.shape = pymunk.Circle(body, 30)
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
328 self.shape.collision_type = COLLISION_TYPE_SWITCH
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
329 self.shape.sensor = True
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
330 super(FloorSwitch, self).__init__(
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 91
diff changeset
331 SingleShapePhysicser(space, self.shape),
126
c3af35561494 Cleaner switch/light rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 123
diff changeset
332 ShapeStateRenderer(),
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
333 FloorSwitchPuzzler(),
81
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
334 )
a1b4d09e6f23 Floor switch with horrible hackery.
davidsharpe@lantea.local
parents: 63
diff changeset
335
106
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
336
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
337 class FloorLight(GameObject):
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
338 def __init__(self, space, position, state_source):
145
0c49627920eb Load game objects from level.
Jeremy Thurgood <firxen@gmail.com>
parents: 143
diff changeset
339 body = make_body(None, None, position)
106
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
340 self.shape = pymunk.Circle(body, 10)
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
341 self.shape.collision_type = COLLISION_TYPE_SWITCH
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
342 self.shape.sensor = True
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
343 super(FloorLight, self).__init__(
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
344 SingleShapePhysicser(space, self.shape),
126
c3af35561494 Cleaner switch/light rendering.
Jeremy Thurgood <firxen@gmail.com>
parents: 123
diff changeset
345 ShapeStateRenderer(),
106
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
346 StateProxyPuzzler(state_source),
bce9cd8a4a8c FloorLight, linked to a FloorSwitch.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
347 )
133
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
348
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
349
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
350 class Box(GameObject):
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
351 def __init__(self, space, position):
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
352 body = make_body(10, 10000, position, damping=0.5)
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
353 self.shape = pymunk.Poly(
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
354 body, [(-20, -20), (20, -20), (20, 20), (-20, 20)])
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
355 self.shape.collision_type = COLLISION_TYPE_BOX
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
356 super(Box, self).__init__(
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
357 SingleShapePhysicser(space, self.shape),
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
358 ShapeRenderer(),
Jeremy Thurgood <firxen@gmail.com>
parents: 126
diff changeset
359 )