source: nagslang/game_object.py@ 91:5c31b4d1851c

Last change on this file since 91:5c31b4d1851c was 91:5c31b4d1851c, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

Remove NullPhysicser, since it looks like everything needs PHYSICS!!!

File size: 3.8 KB
Line 
1import math
2
3import pygame
4import pymunk
5
6from nagslang.constants import SWITCH_PUSHERS, COLLISION_TYPE_SWITCH
7
8
9class Puzzler(object):
10 def get_state(self, space):
11 raise NotImplementedError()
12
13 def notify(self):
14 pass
15
16
17class FloorSwitchPuzzler(Puzzler):
18 def __init__(self, shape):
19 self.shape = shape
20
21 def get_state(self, space):
22 for shape in space.shape_query(self.shape):
23 if shape.collision_type in SWITCH_PUSHERS:
24 return True
25 return False
26
27
28class Physicser(object):
29 def add_to_space(self, space):
30 raise NotImplementedError()
31
32 def remove_from_space(self, space):
33 raise NotImplementedError()
34
35 def render_position(self, surface):
36 raise NotImplementedError()
37
38 def render_angle(self):
39 raise NotImplementedError()
40
41
42class SingleShapePhysicser(Physicser):
43 def __init__(self, shape):
44 self._shape = shape
45
46 def add_to_space(self, space):
47 space.add(self._shape, self._shape.body)
48
49 def remove_from_space(self, space):
50 space.remove(self._shape, self._shape.body)
51
52 def render_position(self, surface):
53 pos = self._shape.body.position
54 import pymunk.pygame_util
55 return pymunk.pygame_util.to_pygame(pos, surface)
56
57 def render_angle(self):
58 return self._shape.body.angle
59
60
61class Renderer(object):
62 def render(self, surface, pos, angle):
63 raise NotImplementedError()
64
65
66def image_pos(image, pos):
67 return (pos[0] - image.get_width() / 2,
68 pos[1] - image.get_height() / 2)
69
70
71class ImageRenderer(Renderer):
72 def __init__(self, image):
73 self._image = image
74
75 def render(self, surface, pos, angle):
76 surface.blit(self._image, image_pos(self._image, pos))
77
78
79class FacingImageRenderer(Renderer):
80 def __init__(self, left_image, right_image):
81 self._images = {
82 'left': left_image,
83 'right': right_image,
84 }
85
86 def get_image(self, angle):
87 if abs(angle) < math.pi / 2:
88 return self._images['right']
89 return self._images['left']
90
91 def render(self, surface, pos, angle):
92 image = self.get_image(angle)
93 surface.blit(image, image_pos(image, pos))
94
95
96class ShapeRenderer(Renderer):
97 def __init__(self, shape):
98 self._shape = shape
99
100 def render(self, surface, pos, angle):
101 import pymunk.pygame_util
102 pymunk.pygame_util.draw(surface, self._shape)
103
104
105class GameObject(object):
106 """A representation of a thing in the game world.
107
108 This has a rendery thing, physicsy things and maybe some other things.
109 """
110
111 def __init__(self, renderer, physicser, puzzler=None):
112 self.renderer = renderer
113 self.physicser = physicser
114 self.puzzler = puzzler
115
116 def add_to_space(self, space):
117 self.physicser.add_to_space(space)
118
119 def render(self, surface):
120 return self.renderer.render(
121 surface, self.physicser.render_position(surface),
122 self.physicser.render_angle())
123
124
125class FloorSwitch(GameObject):
126 def __init__(self, position):
127 body = pymunk.Body()
128 body.position = position
129 self.shape = pymunk.Circle(body, 30)
130 self.shape.collision_type = COLLISION_TYPE_SWITCH
131 self.shape.sensor = True
132 super(FloorSwitch, self).__init__(
133 ShapeRenderer(self.shape),
134 SingleShapePhysicser(self.shape),
135 FloorSwitchPuzzler(self.shape),
136 )
137
138 def add_to_space(self, space):
139 # XXX: Hacky hack.
140 self._space = space
141
142 def render(self, surface):
143 if self.puzzler.get_state(self._space):
144 self.shape.color = pygame.color.THECOLORS['green']
145 else:
146 self.shape.color = pygame.color.THECOLORS['red']
147 super(FloorSwitch, self).render(surface)
Note: See TracBrowser for help on using the repository browser.