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

Last change on this file since 76:29ace74cc9de was 63:7f038ee778ad, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

Put werewolf facing direction magic back.

File size: 2.7 KB
Line 
1import math
2
3
4class Physicser(object):
5 def add_to_space(self, space):
6 raise NotImplementedError()
7
8 def remove_from_space(self, space):
9 raise NotImplementedError()
10
11 def render_position(self, surface):
12 raise NotImplementedError()
13
14 def render_angle(self):
15 raise NotImplementedError()
16
17
18class NullPhysicser(Physicser):
19 def add_to_space(self, space):
20 pass
21
22 def remove_from_space(self, space):
23 pass
24
25 def render_position(self):
26 return (0, 0)
27
28 def render_angle(self):
29 return 0
30
31
32class SingleShapePhysicser(Physicser):
33 def __init__(self, shape):
34 self._shape = shape
35
36 def add_to_space(self, space):
37 space.add(self._shape, self._shape.body)
38
39 def remove_from_space(self, space):
40 space.remove(self._shape, self._shape.body)
41
42 def render_position(self, surface):
43 pos = self._shape.body.position
44 import pymunk.pygame_util
45 return pymunk.pygame_util.to_pygame(pos, surface)
46
47 def render_angle(self):
48 return self._shape.body.angle
49
50
51class Renderer(object):
52 def render(self, surface, pos, angle):
53 raise NotImplementedError()
54
55
56def image_pos(image, pos):
57 return (pos[0] - image.get_width() / 2,
58 pos[1] - image.get_height() / 2)
59
60
61class ImageRenderer(Renderer):
62 def __init__(self, image):
63 self._image = image
64
65 def render(self, surface, pos, angle):
66 surface.blit(self._image, image_pos(self._image, pos))
67
68
69class FacingImageRenderer(Renderer):
70 def __init__(self, left_image, right_image):
71 self._images = {
72 'left': left_image,
73 'right': right_image,
74 }
75
76 def get_image(self, angle):
77 if abs(angle) < math.pi / 2:
78 return self._images['right']
79 return self._images['left']
80
81 def render(self, surface, pos, angle):
82 image = self.get_image(angle)
83 surface.blit(image, image_pos(image, pos))
84
85
86class ShapeRenderer(Renderer):
87 def __init__(self, shape):
88 self._shape = shape
89
90 def render(self, surface, pos, angle):
91 import pymunk.pygame_util
92 pymunk.pygame_util.draw(surface, self._shape)
93
94
95class GameObject(object):
96 """A representation of a thing in the game world.
97
98 This has a rendery thing, physicsy things and maybe some other things.
99 """
100
101 def __init__(self, renderer, physicser=None):
102 self.renderer = renderer
103 if physicser is None:
104 physicser = NullPhysicser()
105 self.physicser = physicser
106
107 def add_to_space(self, space):
108 self.physicser.add_to_space(space)
109
110 def render(self, surface):
111 return self.renderer.render(
112 surface, self.physicser.render_position(surface),
113 self.physicser.render_angle())
Note: See TracBrowser for help on using the repository browser.