source: nagslang/protagonist.py@ 133:79e1888573d3

Last change on this file since 133:79e1888573d3 was 133:79e1888573d3, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

A box.

File size: 4.0 KB
Line 
1import pymunk
2import pymunk.pygame_util
3
4from nagslang.constants import COLLISION_TYPE_PLAYER, ZORDER_MID
5from nagslang.game_object import (
6 GameObject, SingleShapePhysicser, FacingImageRenderer, make_body)
7from nagslang.mutators import FLIP_H
8from nagslang.resources import resources
9
10
11class Protagonist(GameObject):
12 """Representation of our fearless protagonist.
13
14 TODO: Factor out a bunch of this stuff when we need it for other objects.
15 """
16
17 HUMAN_FORM = 'human'
18 WOLF_FORM = 'wolf'
19
20 def __init__(self, space, position):
21 self._setup_physics(space, position)
22 self._setup_renderers()
23 self.inventory = {}
24 self.form = self.HUMAN_FORM
25
26 super(Protagonist, self).__init__(
27 self._physicsers[self.form], self._renderers[self.form])
28 self.zorder = ZORDER_MID
29
30 self.go_human()
31
32 def _setup_physics(self, space, position):
33 self._body = make_body(10, pymunk.inf, position, 0.8)
34
35 self._shapes = {
36 self.HUMAN_FORM: pymunk.Poly(
37 self._body, [(-15, -30), (15, -30), (15, 30), (-15, 30)]),
38 self.WOLF_FORM: pymunk.Circle(self._body, 30),
39 }
40 self._physicsers = {}
41 for form, shape in self._shapes.iteritems():
42 shape.elasticity = 1.0
43 shape.friction = 10.0
44 shape.collision_type = COLLISION_TYPE_PLAYER
45 self._physicsers[form] = SingleShapePhysicser(space, shape)
46 self.angle = 0
47
48 def _get_image(self, name, *transforms):
49 return resources.get_image('creatures', name, transforms=transforms)
50
51 def _setup_renderers(self):
52 self._renderers = {
53 self.HUMAN_FORM: FacingImageRenderer(
54 self._get_image('human_1.png'),
55 self._get_image('human_1.png', FLIP_H)),
56 self.WOLF_FORM: FacingImageRenderer(
57 self._get_image('werewolf_1.png'),
58 self._get_image('werewolf_1.png', FLIP_H)),
59 }
60 for renderer in self._renderers.values():
61 renderer.set_game_object(self)
62
63 @classmethod
64 def from_saved_state(cls, saved_state):
65 """Create an instance from the provided serialised state.
66 """
67 obj = cls()
68 # TODO: Update from saved state.
69 return obj
70
71 def get_render_angle(self):
72 return self.angle
73
74 def go_werewolf(self):
75 self._physicsers[self.form].remove_from_space()
76 self.form = self.WOLF_FORM
77 self._physicsers[self.form].add_to_space()
78 self._body.mass = 100
79 self._body.velocity_limit = 1000
80 self.impulse_factor = 4000
81 self._body.damping = 0.9
82 self.renderer = self._renderers[self.form]
83
84 def go_human(self):
85 self._physicsers[self.form].remove_from_space()
86 self.form = self.HUMAN_FORM
87 self._physicsers[self.form].add_to_space()
88 self._body.mass = 10
89 self._body.velocity_limit = 1000
90 self.impulse_factor = 500
91 self._body.damping = 0.8
92 self.renderer = self._renderers[self.form]
93
94 def set_direction(self, dx, dy):
95 if (dx, dy) == (0, 0):
96 return
97 self.angle = pymunk.Vec2d((dx, dy)).angle
98 self._body.apply_impulse(
99 (dx * self.impulse_factor, dy * self.impulse_factor))
100
101 def toggle_form(self):
102 if self.form == self.WOLF_FORM:
103 self.go_human()
104 else:
105 self.go_werewolf()
106
107 def act_on(self, target):
108 """Perform an action on the target.
109 """
110 # TODO: Decide how best to do this.
111 pass
112
113 def attack(self):
114 """Attempt to hurt something.
115 """
116 pass
117
118 def in_wolf_form(self):
119 return self.form == self.WOLF_FORM
120
121 def in_human_form(self):
122 return self.form == self.HUMAN_FORM
123
124 def has_item(self, item):
125 return item in self.inventory
126
127 def environmental_movement(self, dx, dy):
128 if (dx, dy) == (0, 0):
129 return
130 self._body.apply_impulse((dx, dy))
Note: See TracBrowser for help on using the repository browser.