source: nagslang/protagonist.py@ 261:db7c8e74efb4

Last change on this file since 261:db7c8e74efb4 was 261:db7c8e74efb4, checked in by Stefano Rivera <stefano@…>, 8 years ago

(really rubbish) bullets

File size: 9.2 KB
Line 
1import pymunk
2import pymunk.pygame_util
3from pymunk.vec2d import Vec2d
4
5from nagslang import render
6from nagslang.constants import COLLISION_TYPE_PLAYER, ZORDER_MID, \
7 WEREWOLF_SOAK_FACTOR, PROTAGONIST_HEALTH_MIN_LEVEL, \
8 PROTAGONIST_HEALTH_MAX_LEVEL
9from nagslang.events import FireEvent
10from nagslang.game_object import GameObject, Physicser, make_body
11from nagslang.mutators import FLIP_H
12from nagslang.resources import resources
13
14
15class ProtagonistPhysicser(Physicser):
16 def __init__(self, space, form_shapes):
17 self._space = space
18 self._form_shapes = form_shapes
19
20 def switch_form(self, old_form, new_form):
21 self._space.remove(self._form_shapes[old_form])
22 shape = self._form_shapes[new_form]
23 self._space.add(shape)
24 for attr, value in shape.protagonist_body_props.iteritems():
25 setattr(shape.body, attr, value)
26
27 def get_shape(self):
28 return self._form_shapes[self.game_object.form]
29
30
31class ProtagonistFormSelectionRenderer(render.RendererSelectionRenderer):
32 def select_renderer(self):
33 return self.game_object.form
34
35
36class Protagonist(GameObject):
37 """Representation of our fearless protagonist.
38
39 TODO: Factor out a bunch of this stuff when we need it for other objects.
40 """
41
42 HUMAN_FORM = 'human'
43 WOLF_FORM = 'wolf'
44
45 def __init__(self, space, position):
46 physicser = self._make_physics(space, position)
47 renderer = self._make_renderer()
48 self.inventory = {}
49 self.form = self.HUMAN_FORM
50 self.angle = 0
51 self.is_moving = False
52
53 super(Protagonist, self).__init__(physicser, renderer)
54 self.zorder = ZORDER_MID
55
56 self.health_level = 100
57
58 self.go_human()
59
60 def _make_physics(self, space, position):
61 body = make_body(10, pymunk.inf, position, 0.8)
62 body.velocity_limit = 1000
63
64 human = pymunk.Poly(body, [(-15, -30), (15, -30), (15, 30), (-15, 30)])
65 human.elasticity = 1.0
66 human.collision_type = COLLISION_TYPE_PLAYER
67 human.protagonist_body_props = {
68 'mass': 10,
69 'damping': 0.8,
70 }
71
72 wolf = pymunk.Circle(body, 30)
73 wolf.elasticity = 1.0
74 wolf.collision_type = COLLISION_TYPE_PLAYER
75 wolf.protagonist_body_props = {
76 'mass': 100,
77 'damping': 0.9,
78 }
79
80 return ProtagonistPhysicser(space, {
81 self.HUMAN_FORM: human,
82 self.WOLF_FORM: wolf,
83 })
84
85 def _get_image(self, name, *transforms):
86 return resources.get_image('creatures', name, transforms=transforms)
87
88 def _make_renderer(self):
89 return ProtagonistFormSelectionRenderer({
90 self.HUMAN_FORM: render.FacingSelectionRenderer(
91 {
92 'N': render.MovementAnimatedRenderer(
93 [self._get_image('human_N_1.png'),
94 self._get_image('human_N_2.png')], 3),
95 'S': render.MovementAnimatedRenderer(
96 [self._get_image('human_S_1.png'),
97 self._get_image('human_S_2.png')], 3),
98 'W': render.MovementAnimatedRenderer(
99 [self._get_image('human_W_1.png'),
100 self._get_image('human_W_2.png')], 3),
101 'E': render.MovementAnimatedRenderer(
102 [self._get_image('human_W_1.png', FLIP_H),
103 self._get_image('human_W_2.png', FLIP_H)], 3),
104 'NW': render.MovementAnimatedRenderer(
105 [self._get_image('human_NW_1.png'),
106 self._get_image('human_NW_2.png')], 3),
107 'NE': render.MovementAnimatedRenderer(
108 [self._get_image('human_NW_1.png', FLIP_H),
109 self._get_image('human_NW_2.png', FLIP_H)], 3),
110 'SW': render.MovementAnimatedRenderer(
111 [self._get_image('human_SW_1.png'),
112 self._get_image('human_SW_2.png')], 3),
113 'SE': render.MovementAnimatedRenderer(
114 [self._get_image('human_SW_1.png', FLIP_H),
115 self._get_image('human_SW_2.png', FLIP_H)], 3),
116 }),
117 self.WOLF_FORM: render.FacingSelectionRenderer(
118 {
119 'N': render.MovementAnimatedRenderer(
120 [self._get_image('werewolf_N_1.png'),
121 self._get_image('werewolf_N_2.png')], 3),
122 'S': render.MovementAnimatedRenderer(
123 [self._get_image('werewolf_S_1.png'),
124 self._get_image('werewolf_S_2.png')], 3),
125 'W': render.MovementAnimatedRenderer(
126 [self._get_image('werewolf_W_1.png'),
127 self._get_image('werewolf_W_2.png')], 3),
128 'E': render.MovementAnimatedRenderer(
129 [self._get_image('werewolf_W_1.png', FLIP_H),
130 self._get_image('werewolf_W_2.png', FLIP_H)], 3),
131 'NW': render.MovementAnimatedRenderer(
132 [self._get_image('werewolf_NW_1.png'),
133 self._get_image('werewolf_NW_2.png')], 3),
134 'NE': render.MovementAnimatedRenderer(
135 [self._get_image('werewolf_NW_1.png', FLIP_H),
136 self._get_image('werewolf_NW_2.png', FLIP_H)], 3),
137 'SW': render.MovementAnimatedRenderer(
138 [self._get_image('werewolf_SW_1.png'),
139 self._get_image('werewolf_SW_2.png')], 3),
140 'SE': render.MovementAnimatedRenderer(
141 [self._get_image('werewolf_SW_1.png', FLIP_H),
142 self._get_image('werewolf_SW_2.png', FLIP_H)], 3),
143 }),
144 })
145
146 @classmethod
147 def from_saved_state(cls, saved_state):
148 """Create an instance from the provided serialised state.
149 """
150 obj = cls()
151 # TODO: Update from saved state.
152 return obj
153
154 def get_render_angle(self):
155 # No image rotation when rendering, please.
156 return 0
157
158 def get_facing_direction(self):
159 # It's easier to work with a vector than an angle here.
160 vec = Vec2d.unit()
161 vec.angle = self.angle
162 # We probably don't have exactly -1, 0, or 1 here.
163 x = int(round(vec.x))
164 y = int(round(vec.y))
165
166 return {
167 (0, 1): 'N',
168 (0, -1): 'S',
169 (-1, 0): 'W',
170 (1, 0): 'E',
171 (1, 1): 'NE',
172 (1, -1): 'SE',
173 (-1, 1): 'NW',
174 (-1, -1): 'SW',
175 }[(x, y)]
176
177 def go_werewolf(self):
178 self.physicser.switch_form(self.form, self.WOLF_FORM)
179 self.form = self.WOLF_FORM
180 self.impulse_factor = 4000
181
182 def go_human(self):
183 self.physicser.switch_form(self.form, self.HUMAN_FORM)
184 self.form = self.HUMAN_FORM
185 self.impulse_factor = 500
186
187 def set_direction(self, dx, dy):
188 if (dx, dy) == (0, 0):
189 self.is_moving = False
190 return
191 self.is_moving = True
192 self.angle = pymunk.Vec2d((dx, dy)).angle
193 self.physicser.apply_impulse(
194 (dx * self.impulse_factor, dy * self.impulse_factor))
195
196 def set_position(self, position):
197 self.physicser.position = position
198
199 def copy_state(self, old_protagonist):
200 self.physicser.position = old_protagonist.physicser.position
201 self.physicser.switch_form(self.form, old_protagonist.form)
202 self.impulse_factor = old_protagonist.impulse_factor
203 self.form = old_protagonist.form
204 self.angle = old_protagonist.angle
205 self.inventory = old_protagonist.inventory
206
207 def toggle_form(self):
208 if self.form == self.WOLF_FORM:
209 self.go_human()
210 else:
211 self.go_werewolf()
212
213 def act_on(self, target):
214 """Perform an action on the target.
215 """
216 # TODO: Decide how best to do this.
217 pass
218
219 def attack(self):
220 """Attempt to hurt something.
221 """
222 vec = Vec2d.unit()
223 vec.angle = self.angle
224 vec.length = 100
225 FireEvent.post(self.physicser.position, vec)
226 print "Biff", self.physicser.position, vec
227
228 def in_wolf_form(self):
229 return self.form == self.WOLF_FORM
230
231 def in_human_form(self):
232 return self.form == self.HUMAN_FORM
233
234 def has_item(self, item):
235 return item in self.inventory
236
237 def environmental_movement(self, dx, dy):
238 if (dx, dy) == (0, 0):
239 return
240 self.physicser.apply_impulse((dx, dy))
241
242 def get_health_level(self):
243 """Return current health level
244 """
245 return self.health_level
246
247 def lose_health(self, amount):
248 if self.in_human_form():
249 self.health_level -= amount
250 else:
251 self.health_level -= amount / WEREWOLF_SOAK_FACTOR
252 if self.health_level < PROTAGONIST_HEALTH_MIN_LEVEL:
253 self.health_level = PROTAGONIST_HEALTH_MIN_LEVEL
254
255 def gain_health(self, amount):
256 self.health_level += amount
257 if self.health_level > PROTAGONIST_HEALTH_MAX_LEVEL:
258 self.health_level = PROTAGONIST_HEALTH_MAX_LEVEL
Note: See TracBrowser for help on using the repository browser.