annotate nagslang/protagonist.py @ 156:94a2456696af

front and back views
author Neil Muller <drnlmuller@gmail.com>
date Mon, 02 Sep 2013 23:52:24 +0200
parents deac6a4008e7
children 1ee8756888e4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
1 import pymunk
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
2 import pymunk.pygame_util
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
3
156
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
4 import math
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
5
107
b90d01e4d9d4 Layered drawing.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
6 from nagslang.constants import COLLISION_TYPE_PLAYER, ZORDER_MID
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
7 from nagslang.game_object import (
143
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
8 GameObject, SingleShapePhysicser, AnimatedFacingImageRenderer, make_body)
76
Stefano Rivera <stefano@rivera.za.net>
parents: 73
diff changeset
9 from nagslang.mutators import FLIP_H
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
10 from nagslang.resources import resources
28
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
11
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
12
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
13 class Protagonist(GameObject):
28
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
14 """Representation of our fearless protagonist.
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
15
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
16 TODO: Factor out a bunch of this stuff when we need it for other objects.
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
17 """
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
18
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
19 HUMAN_FORM = 'human'
156
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
20 HUMAN_FORM_BACK = 'human_back'
28
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
21 WOLF_FORM = 'wolf'
156
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
22 WOLF_FORM_BACK = 'wolf_back'
28
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
23
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
24 def __init__(self, space, position):
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
25 self._setup_physics(space, position)
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
26 self._setup_renderers()
28
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
27 self.inventory = {}
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
28 self.form = self.HUMAN_FORM
156
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
29 self.render_form = self.HUMAN_FORM
28
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
30
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
31 super(Protagonist, self).__init__(
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
32 self._physicsers[self.form], self._renderers[self.form])
107
b90d01e4d9d4 Layered drawing.
Jeremy Thurgood <firxen@gmail.com>
parents: 104
diff changeset
33 self.zorder = ZORDER_MID
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
34
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
35 self.go_human()
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
36
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
37 def _setup_physics(self, space, position):
133
Jeremy Thurgood <firxen@gmail.com>
parents: 123
diff changeset
38 self._body = make_body(10, pymunk.inf, position, 0.8)
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
39
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
40 self._shapes = {
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
41 self.HUMAN_FORM: pymunk.Poly(
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
42 self._body, [(-15, -30), (15, -30), (15, 30), (-15, 30)]),
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
43 self.WOLF_FORM: pymunk.Circle(self._body, 30),
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
44 }
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
45 self._physicsers = {}
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
46 for form, shape in self._shapes.iteritems():
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
47 shape.elasticity = 1.0
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
48 shape.friction = 10.0
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
49 shape.collision_type = COLLISION_TYPE_PLAYER
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
50 self._physicsers[form] = SingleShapePhysicser(space, shape)
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
51 self.angle = 0
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
52
66
8bf0459ebc56 Clean up a few things.
Jeremy Thurgood <firxen@gmail.com>
parents: 65
diff changeset
53 def _get_image(self, name, *transforms):
8bf0459ebc56 Clean up a few things.
Jeremy Thurgood <firxen@gmail.com>
parents: 65
diff changeset
54 return resources.get_image('creatures', name, transforms=transforms)
8bf0459ebc56 Clean up a few things.
Jeremy Thurgood <firxen@gmail.com>
parents: 65
diff changeset
55
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
56 def _setup_renderers(self):
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
57 self._renderers = {
143
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
58 self.HUMAN_FORM: AnimatedFacingImageRenderer(
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
59 (self._get_image('human_1.png'),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
60 self._get_image('human_1.png'),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
61 self._get_image('human_1.png'),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
62 self._get_image('human_2.png'),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
63 self._get_image('human_2.png'),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
64 self._get_image('human_2.png')),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
65 (self._get_image('human_1.png', FLIP_H),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
66 self._get_image('human_1.png', FLIP_H),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
67 self._get_image('human_1.png', FLIP_H),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
68 self._get_image('human_2.png', FLIP_H),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
69 self._get_image('human_2.png', FLIP_H),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
70 self._get_image('human_2.png', FLIP_H))),
156
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
71 self.HUMAN_FORM_BACK: AnimatedFacingImageRenderer(
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
72 (self._get_image('human_back_1.png'),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
73 self._get_image('human_back_1.png'),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
74 self._get_image('human_back_1.png'),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
75 self._get_image('human_back_2.png'),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
76 self._get_image('human_back_2.png'),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
77 self._get_image('human_back_2.png')),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
78 (self._get_image('human_back_1.png', FLIP_H),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
79 self._get_image('human_back_1.png', FLIP_H),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
80 self._get_image('human_back_1.png', FLIP_H),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
81 self._get_image('human_back_2.png', FLIP_H),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
82 self._get_image('human_back_2.png', FLIP_H),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
83 self._get_image('human_back_2.png', FLIP_H))),
143
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
84 self.WOLF_FORM: AnimatedFacingImageRenderer(
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
85 (self._get_image('werewolf_1.png'),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
86 self._get_image('werewolf_1.png'),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
87 self._get_image('werewolf_1.png'),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
88 self._get_image('werewolf_2.png'),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
89 self._get_image('werewolf_2.png'),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
90 self._get_image('werewolf_2.png')),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
91 (self._get_image('werewolf_1.png', FLIP_H),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
92 self._get_image('werewolf_1.png', FLIP_H),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
93 self._get_image('werewolf_1.png', FLIP_H),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
94 self._get_image('werewolf_2.png', FLIP_H),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
95 self._get_image('werewolf_2.png', FLIP_H),
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
96 self._get_image('werewolf_2.png', FLIP_H))),
156
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
97 self.WOLF_FORM_BACK: AnimatedFacingImageRenderer(
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
98 (self._get_image('werewolf_back_1.png'),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
99 self._get_image('werewolf_back_1.png'),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
100 self._get_image('werewolf_back_1.png'),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
101 self._get_image('werewolf_back_2.png'),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
102 self._get_image('werewolf_back_2.png'),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
103 self._get_image('werewolf_back_2.png')),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
104 (self._get_image('werewolf_back_1.png', FLIP_H),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
105 self._get_image('werewolf_back_1.png', FLIP_H),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
106 self._get_image('werewolf_back_1.png', FLIP_H),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
107 self._get_image('werewolf_back_2.png', FLIP_H),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
108 self._get_image('werewolf_back_2.png', FLIP_H),
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
109 self._get_image('werewolf_back_2.png', FLIP_H))),
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
110 }
123
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
111 for renderer in self._renderers.values():
23b533d6f27e Rearrange game objects a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 107
diff changeset
112 renderer.set_game_object(self)
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
113
28
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
114 @classmethod
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
115 def from_saved_state(cls, saved_state):
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
116 """Create an instance from the provided serialised state.
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
117 """
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
118 obj = cls()
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
119 # TODO: Update from saved state.
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
120 return obj
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
121
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
122 def get_render_angle(self):
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
123 return self.angle
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
124
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
125 def go_werewolf(self):
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
126 self._physicsers[self.form].remove_from_space()
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
127 self.form = self.WOLF_FORM
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
128 self._physicsers[self.form].add_to_space()
66
8bf0459ebc56 Clean up a few things.
Jeremy Thurgood <firxen@gmail.com>
parents: 65
diff changeset
129 self._body.mass = 100
8bf0459ebc56 Clean up a few things.
Jeremy Thurgood <firxen@gmail.com>
parents: 65
diff changeset
130 self._body.velocity_limit = 1000
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
131 self.impulse_factor = 4000
133
Jeremy Thurgood <firxen@gmail.com>
parents: 123
diff changeset
132 self._body.damping = 0.9
156
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
133 if self.render_form == self.HUMAN_FORM:
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
134 self.render_form = self.WOLF_FORM
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
135 elif self.render_form == self.HUMAN_FORM_BACK:
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
136 self.render_form = self.WOLF_FORM_BACK
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
137 else:
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
138 self.render_form = self.WOLF_FORM
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
139 self.renderer = self._renderers[self.render_form]
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
140
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
141 def go_human(self):
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
142 self._physicsers[self.form].remove_from_space()
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
143 self.form = self.HUMAN_FORM
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
144 self._physicsers[self.form].add_to_space()
66
8bf0459ebc56 Clean up a few things.
Jeremy Thurgood <firxen@gmail.com>
parents: 65
diff changeset
145 self._body.mass = 10
8bf0459ebc56 Clean up a few things.
Jeremy Thurgood <firxen@gmail.com>
parents: 65
diff changeset
146 self._body.velocity_limit = 1000
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
147 self.impulse_factor = 500
133
Jeremy Thurgood <firxen@gmail.com>
parents: 123
diff changeset
148 self._body.damping = 0.8
156
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
149 if self.render_form == self.WOLF_FORM:
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
150 self.render_form = self.HUMAN_FORM
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
151 elif self.render_form == self.WOLF_FORM_BACK:
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
152 self.render_form = self.HUMAN_FORM_BACK
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
153 else:
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
154 self.render_form = self.HUMAN_FORM
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
155 self.renderer = self._renderers[self.render_form]
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
156
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
157 def _switch_to_back(self):
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
158 if self.render_form == self.HUMAN_FORM:
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
159 self.render_form = self.HUMAN_FORM_BACK
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
160 elif self.render_form == self.WOLF_FORM:
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
161 self.render_form = self.WOLF_FORM_BACK
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
162 self.renderer = self._renderers[self.render_form]
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
163
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
164 def _switch_to_front(self):
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
165 if self.render_form == self.HUMAN_FORM_BACK:
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
166 self.render_form = self.HUMAN_FORM
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
167 elif self.render_form == self.WOLF_FORM_BACK:
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
168 self.render_form = self.WOLF_FORM
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
169 self.renderer = self._renderers[self.render_form]
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
170
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
171 def set_direction(self, dx, dy):
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
172 if (dx, dy) == (0, 0):
143
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
173 self.renderer.stop()
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
174 return
156
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
175 old_angle = self.angle
93
d6a49f0c1e6e Rectangular human protagonist shape, refactored physicsers.
Jeremy Thurgood <firxen@gmail.com>
parents: 81
diff changeset
176 self.angle = pymunk.Vec2d((dx, dy)).angle
156
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
177 # If we've gone from quadrants 2 & 3 to 1 & 4 (or vice versa)
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
178 # switch between front & back views
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
179 if self.angle != math.pi:
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
180 # == math.pi is going straight left, which can't
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
181 # trigger a front/back swap and simplifies these checks
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
182 if self.angle > 0 and old_angle != self.angle:
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
183 self._switch_to_back()
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
184 elif self.angle < 0 and old_angle != self.angle:
94a2456696af front and back views
Neil Muller <drnlmuller@gmail.com>
parents: 143
diff changeset
185 self._switch_to_front()
66
8bf0459ebc56 Clean up a few things.
Jeremy Thurgood <firxen@gmail.com>
parents: 65
diff changeset
186 self._body.apply_impulse(
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
187 (dx * self.impulse_factor, dy * self.impulse_factor))
143
deac6a4008e7 Hook up protagnist animations
Neil Muller <drnlmuller@gmail.com>
parents: 133
diff changeset
188 self.renderer.start()
65
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
189
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
190 def toggle_form(self):
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
191 if self.form == self.WOLF_FORM:
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
192 self.go_human()
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
193 else:
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
194 self.go_werewolf()
a99ac95a2940 Move protagonist object to the right place.
Jeremy Thurgood <firxen@gmail.com>
parents: 28
diff changeset
195
28
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
196 def act_on(self, target):
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
197 """Perform an action on the target.
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
198 """
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
199 # TODO: Decide how best to do this.
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
200 pass
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
201
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
202 def attack(self):
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
203 """Attempt to hurt something.
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
204 """
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
205 pass
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
206
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
207 def in_wolf_form(self):
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
208 return self.form == self.WOLF_FORM
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
209
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
210 def in_human_form(self):
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
211 return self.form == self.HUMAN_FORM
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
212
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
213 def has_item(self, item):
c03982fe3c70 Protagonist and environment.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
214 return item in self.inventory
73
e118458a4e9c Added environmental motion effects to protagonist.
davidsharpe@185.4.16.172.in-addr.arpa
parents: 66
diff changeset
215
e118458a4e9c Added environmental motion effects to protagonist.
davidsharpe@185.4.16.172.in-addr.arpa
parents: 66
diff changeset
216 def environmental_movement(self, dx, dy):
e118458a4e9c Added environmental motion effects to protagonist.
davidsharpe@185.4.16.172.in-addr.arpa
parents: 66
diff changeset
217 if (dx, dy) == (0, 0):
e118458a4e9c Added environmental motion effects to protagonist.
davidsharpe@185.4.16.172.in-addr.arpa
parents: 66
diff changeset
218 return
e118458a4e9c Added environmental motion effects to protagonist.
davidsharpe@185.4.16.172.in-addr.arpa
parents: 66
diff changeset
219 self._body.apply_impulse((dx, dy))