source: nagslang/screens/area.py@ 188:3894cfe15823

Last change on this file since 188:3894cfe15823 was 188:3894cfe15823, checked in by Jeremy Thurgood <firxen@…>, 7 years ago

Better collision handling, potentially locked doors.

File size: 5.8 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.keys = ControlKeys()
68 self._level = Level(self.name)
69 self._level.load(self.space)
70 self._drawables = Drawables()
71 self.add_walls()
72 self._add_collision_handlers()
73 if self.protagonist is not None:
74 # We do things this way to avoid extra pymunk
75 # juggling to move objects between spaces
76 old_protagonist = self.protagonist
77 self.add_protagonist()
78 self.protagonist.copy_state(old_protagonist)
79 else:
80 self.add_protagonist()
81 self.add_game_objects()
82
83 def _collision_pre_solve_handler(self, space, arbiter):
84 gobj = arbiter.shapes[1].physicser.game_object
85 result = gobj.collide_with_protagonist()
86 if result is False:
87 return False
88 return True
89
90 def _add_collision_handlers(self):
91 for collision_type in CALLBACK_COLLIDERS:
92 self.space.add_collision_handler(
93 COLLISION_TYPE_PLAYER, collision_type,
94 pre_solve=self._collision_pre_solve_handler)
95
96 def add_walls(self):
97 self.walls = []
98 body = pymunk.Body()
99 body.position = (0, 0)
100 walls = self._level.get_walls()
101 for wall in walls:
102 corners = wall
103 corner = corners[-1]
104 for next_corner in corners:
105 wall = pymunk.Segment(body, corner, next_corner, 5)
106 wall.elasticity = 1.0
107 self.walls.append(wall)
108 corner = next_corner
109 self.space.add(*self.walls)
110
111 def add_game_objects(self):
112 for drawable in self._level.get_drawables():
113 self._drawables.add(drawable)
114
115 def add_protagonist(self):
116 self.protagonist = Protagonist(self.space, (350, 300))
117 self._drawables.add(self.protagonist)
118
119 def handle_event(self, ev):
120 if ev.type == pygame.locals.KEYDOWN:
121 if ev.key == pygame.locals.K_ESCAPE:
122 ScreenChange.post('menu')
123 if ev.key == pygame.locals.K_c:
124 self.protagonist.toggle_form()
125 elif DoorEvent.matches(ev):
126 self.protagonist.set_position(ev.dest_pos)
127 if ev.destination != self.name:
128 # Go to anther screen
129 ScreenChange.post(ev.destination, self.protagonist)
130 # else we're teleporting within the screen, and just the
131 # position change is enough
132 self.keys.handle_event(ev)
133
134 def _calc_viewport(self, level_surface, display_surface):
135 level_size = level_surface.get_size()
136 display_size = display_surface.get_size()
137 protagnist_pos = self.protagonist.physicser.get_render_position(
138 level_surface)
139 x_wide = display_size[0] // 2
140 y_wide = display_size[1] // 2
141 if display_size[0] > level_size[0]:
142 x = -(display_size[0] - level_size[0]) // 2
143 elif protagnist_pos[0] < x_wide:
144 x = 0
145 elif protagnist_pos[0] > level_size[0] - x_wide:
146 x = level_size[0] - display_size[0]
147 else:
148 x = protagnist_pos[0] - x_wide
149 if display_size[1] > level_size[1]:
150 y = -(display_size[1] - level_size[1]) // 2
151 elif protagnist_pos[1] < y_wide:
152 y = 0
153 elif protagnist_pos[1] > level_size[1] - y_wide:
154 y = level_size[1] - display_size[1]
155 else:
156 y = protagnist_pos[1] - y_wide
157 return pygame.rect.Rect(x, y, display_size[0], display_size[1])
158
159 def render(self, surface):
160 #surface.fill(pygame.color.Color(0, 0, 0))
161 background = self._level.get_background()
162 mysurface = background.copy()
163 for drawable in self._drawables:
164 drawable.render(mysurface)
165 render_rect = self._calc_viewport(mysurface, surface)
166 surface.blit(mysurface, (0, 0), render_rect)
167
168 def tick_protagonist(self):
169 dx, dy = self.keys.get_direction()
170 self.protagonist.set_direction(dx, dy)
171
172 def tick(self, seconds):
173 self.tick_protagonist()
174 for drawable in self._drawables:
175 drawable.animate()
176 super(AreaScreen, self).tick(seconds)
Note: See TracBrowser for help on using the repository browser.