source: nagslang/protagonist.py@ 76:29ace74cc9de

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

Human

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