annotate nagslang/game_object.py @ 159:f80323140317

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