source: nagslang/screens/area.py@ 191:e080fcd07fa9

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

Overlay notes

File size: 6.1 KB
Line 
1"""Display a game area."""
2
3import pygame
4import pymunk
5import pymunk.pygame_util
6
7from nagslang.constants import COLLISION_TYPE_PLAYER, CALLBACK_COLLIDERS
8from nagslang.events import ScreenChange, DoorEvent
9from nagslang.level import Level
10from nagslang.protagonist import Protagonist
11from nagslang.screens.base import Screen
12
13
14class ControlKeys(object):
15 direction_keys = {
16 (0, 1): set([pygame.locals.K_UP, pygame.locals.K_w]),
17 (0, -1): set([pygame.locals.K_DOWN, pygame.locals.K_s]),
18 (-1, 0): set([pygame.locals.K_LEFT, pygame.locals.K_a]),
19 (1, 0): set([pygame.locals.K_RIGHT, pygame.locals.K_d]),
20 }
21
22 def __init__(self):
23 self.keys_down = set()
24
25 def key_down(self, key):
26 self.keys_down.add(key)
27
28 def key_up(self, key):
29 self.keys_down.discard(key)
30
31 def handle_event(self, ev):
32 if ev.type == pygame.locals.KEYDOWN:
33 self.key_down(ev.key)
34 elif ev.type == pygame.locals.KEYUP:
35 self.key_up(ev.key)
36
37 def get_direction(self):
38 dx, dy = 0, 0
39 for (tx, ty), keys in self.direction_keys.iteritems():
40 if self.keys_down & keys:
41 dx += tx
42 dy += ty
43 return (dx, dy)
44
45
46class Drawables(object):
47 def __init__(self):
48 self._drawables = {}
49
50 def add(self, drawable):
51 self._drawables.setdefault(drawable.zorder, []).append(drawable)
52
53 def remove(self, drawable):
54 self._drawables[drawable.zorder].remove(drawable)
55
56 def get_drawables(self):
57 for zorder in sorted(self._drawables):
58 for drawable in self._drawables[zorder]:
59 yield drawable
60
61 __iter__ = get_drawables
62
63
64class AreaScreen(Screen):
65
66 def setup(self):
67 self._disable_render = False # Avoid redrawing on scene changes
68 self.keys = ControlKeys()
69 self._level = Level(self.name)
70 self._level.load(self.space)
71 self._drawables = Drawables()
72 self.add_walls()
73 self._add_collision_handlers()
74 if self.protagonist is not None:
75 # We do things this way to avoid extra pymunk
76 # juggling to move objects between spaces
77 old_protagonist = self.protagonist
78 self.add_protagonist()
79 self.protagonist.copy_state(old_protagonist)
80 else:
81 self.add_protagonist()
82 self.add_game_objects()
83
84 def _collision_pre_solve_handler(self, space, arbiter):
85 gobj = arbiter.shapes[1].physicser.game_object
86 result = gobj.collide_with_protagonist()
87 if result is False:
88 return False
89 return True
90
91 def _add_collision_handlers(self):
92 for collision_type in CALLBACK_COLLIDERS:
93 self.space.add_collision_handler(
94 COLLISION_TYPE_PLAYER, collision_type,
95 pre_solve=self._collision_pre_solve_handler)
96
97 def add_walls(self):
98 self.walls = []
99 body = pymunk.Body()
100 body.position = (0, 0)
101 walls = self._level.get_walls()
102 for wall in walls:
103 corners = wall
104 corner = corners[-1]
105 for next_corner in corners:
106 wall = pymunk.Segment(body, corner, next_corner, 5)
107 wall.elasticity = 1.0
108 self.walls.append(wall)
109 corner = next_corner
110 self.space.add(*self.walls)
111
112 def add_game_objects(self):
113 for drawable in self._level.drawables:
114 self._drawables.add(drawable)
115
116 def add_protagonist(self):
117 self.protagonist = Protagonist(self.space, (350, 300))
118 self._drawables.add(self.protagonist)
119
120 def handle_event(self, ev):
121 if ev.type == pygame.locals.KEYDOWN:
122 if ev.key == pygame.locals.K_ESCAPE:
123 ScreenChange.post('menu')
124 if ev.key == pygame.locals.K_c:
125 self.protagonist.toggle_form()
126 elif DoorEvent.matches(ev):
127 self.protagonist.set_position(ev.dest_pos)
128 if ev.destination != self.name:
129 # Go to anther screen
130 self._disable_render = True
131 ScreenChange.post(ev.destination, self.protagonist)
132 return
133 # else we're teleporting within the screen, and just the
134 # position change is enough
135 self.keys.handle_event(ev)
136
137 def _calc_viewport(self, level_surface, display_surface):
138 level_size = level_surface.get_size()
139 display_size = display_surface.get_size()
140 protagnist_pos = self.protagonist.physicser.get_render_position(
141 level_surface)
142 x_wide = display_size[0] // 2
143 y_wide = display_size[1] // 2
144 if display_size[0] > level_size[0]:
145 x = -(display_size[0] - level_size[0]) // 2
146 elif protagnist_pos[0] < x_wide:
147 x = 0
148 elif protagnist_pos[0] > level_size[0] - x_wide:
149 x = level_size[0] - display_size[0]
150 else:
151 x = protagnist_pos[0] - x_wide
152 if display_size[1] > level_size[1]:
153 y = -(display_size[1] - level_size[1]) // 2
154 elif protagnist_pos[1] < y_wide:
155 y = 0
156 elif protagnist_pos[1] > level_size[1] - y_wide:
157 y = level_size[1] - display_size[1]
158 else:
159 y = protagnist_pos[1] - y_wide
160 return pygame.rect.Rect(x, y, display_size[0], display_size[1])
161
162 def render(self, surface):
163 if self._disable_render:
164 return
165 background = self._level.get_background()
166 mysurface = background.copy()
167 for drawable in self._drawables:
168 drawable.render(mysurface)
169 render_rect = self._calc_viewport(mysurface, surface)
170 surface.blit(mysurface, (0, 0), render_rect)
171 for overlay in self._level.overlay_drawables:
172 if overlay.is_visible():
173 overlay.render(surface)
174
175 def tick_protagonist(self):
176 dx, dy = self.keys.get_direction()
177 self.protagonist.set_direction(dx, dy)
178
179 def tick(self, seconds):
180 self.tick_protagonist()
181 for drawable in self._drawables:
182 drawable.animate()
183 super(AreaScreen, self).tick(seconds)
Note: See TracBrowser for help on using the repository browser.