source: nagslang/screens/area.py@ 319:01e98732de46

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

Open bulkheads are no longer opaque to furniture

File size: 8.7 KB
RevLine 
[18]1"""Display a game area."""
2
[20]3import pygame
[21]4import pymunk
5import pymunk.pygame_util
[20]6
[301]7from nagslang.constants import (
[319]8 COLLISION_TYPE_WALL, COLLISION_TYPE_PLAYER, CALLBACK_COLLIDERS,
9 COLLISION_TYPE_FURNITURE)
[308]10from nagslang.enemies import DeadEnemy
[313]11from nagslang.events import ScreenChange, DoorEvent, FireEvent, \
12 EnemyDeathEvent, ClawEvent
[50]13from nagslang.level import Level
[65]14from nagslang.screens.base import Screen
[312]15from nagslang.game_object import Bullet, ClawAttack
[269]16from nagslang.sound import sound
[18]17
[83]18
[34]19class ControlKeys(object):
[100]20 direction_keys = {
21 (0, 1): set([pygame.locals.K_UP, pygame.locals.K_w]),
22 (0, -1): set([pygame.locals.K_DOWN, pygame.locals.K_s]),
23 (-1, 0): set([pygame.locals.K_LEFT, pygame.locals.K_a]),
24 (1, 0): set([pygame.locals.K_RIGHT, pygame.locals.K_d]),
25 }
26
[34]27 def __init__(self):
28 self.keys_down = set()
29
30 def key_down(self, key):
31 self.keys_down.add(key)
32
33 def key_up(self, key):
34 self.keys_down.discard(key)
35
36 def handle_event(self, ev):
37 if ev.type == pygame.locals.KEYDOWN:
38 self.key_down(ev.key)
39 elif ev.type == pygame.locals.KEYUP:
40 self.key_up(ev.key)
41
[100]42 def get_direction(self):
43 dx, dy = 0, 0
44 for (tx, ty), keys in self.direction_keys.iteritems():
45 if self.keys_down & keys:
46 dx += tx
47 dy += ty
48 return (dx, dy)
49
[34]50
[107]51class Drawables(object):
52 def __init__(self):
53 self._drawables = {}
54
55 def add(self, drawable):
56 self._drawables.setdefault(drawable.zorder, []).append(drawable)
57
58 def remove(self, drawable):
59 self._drawables[drawable.zorder].remove(drawable)
60
61 def get_drawables(self):
62 for zorder in sorted(self._drawables):
63 for drawable in self._drawables[zorder]:
64 yield drawable
65
66 __iter__ = get_drawables
67
68
[18]69class AreaScreen(Screen):
[21]70
[37]71 def setup(self):
[190]72 self._disable_render = False # Avoid redrawing on scene changes
[34]73 self.keys = ControlKeys()
[277]74 self._level = Level(self.name, self.world)
[145]75 self._level.load(self.space)
[107]76 self._drawables = Drawables()
[37]77 self.add_walls()
[188]78 self._add_collision_handlers()
[276]79 self.add_protagonist()
[145]80 self.add_game_objects()
[269]81 sound.play_music("POL-cyber-factory-short.ogg")
[21]82
[272]83 def teardown(self):
84 sound.stop()
85
[319]86 def _player_collision_pre_solve_handler(self, space, arbiter):
[188]87 gobj = arbiter.shapes[1].physicser.game_object
[256]88 result = gobj.collide_with_protagonist(self.protagonist)
[192]89 # The collision handler must return `True` or `False`. We don't want to
90 # accidentally reject collisions from handlers that return `None`, so
91 # we explicitly check for `False` and treate everything else as `True`.
[188]92 if result is False:
93 return False
94 return True
95
[319]96 def _furniture_collision_pre_solve_handler(self, space, arbiter):
97 furniture = arbiter.shapes[0].physicser.game_object
98 gobj = arbiter.shapes[1].physicser.game_object
99 result = gobj.collide_with_furniture(furniture)
100 return result is not False
101
[188]102 def _add_collision_handlers(self):
103 for collision_type in CALLBACK_COLLIDERS:
104 self.space.add_collision_handler(
105 COLLISION_TYPE_PLAYER, collision_type,
[319]106 pre_solve=self._player_collision_pre_solve_handler)
107 self.space.add_collision_handler(
108 COLLISION_TYPE_FURNITURE, collision_type,
109 pre_solve=self._furniture_collision_pre_solve_handler)
[188]110
[37]111 def add_walls(self):
[21]112 self.walls = []
113 body = pymunk.Body()
[84]114 body.position = (0, 0)
[53]115 walls = self._level.get_walls()
116 for wall in walls:
[279]117 if len(wall) < 2:
118 # Don't try to add a useless wall
119 continue
[53]120 corners = wall
121 corner = corners[-1]
122 for next_corner in corners:
123 wall = pymunk.Segment(body, corner, next_corner, 5)
[301]124 wall.collision_type = COLLISION_TYPE_WALL
[53]125 wall.elasticity = 1.0
126 self.walls.append(wall)
127 corner = next_corner
[21]128 self.space.add(*self.walls)
129
[145]130 def add_game_objects(self):
[191]131 for drawable in self._level.drawables:
[145]132 self._drawables.add(drawable)
133
[37]134 def add_protagonist(self):
[276]135 self.protagonist = self.world.protagonist
136 self.protagonist.change_space(self.space)
[107]137 self._drawables.add(self.protagonist)
[21]138
[18]139 def handle_event(self, ev):
[20]140 if ev.type == pygame.locals.KEYDOWN:
141 if ev.key == pygame.locals.K_ESCAPE:
142 ScreenChange.post('menu')
[97]143 if ev.key == pygame.locals.K_c:
[47]144 self.protagonist.toggle_form()
[238]145 self.world.transformations += 1
[283]146 if ev.key in (pygame.locals.K_z, pygame.locals.K_LCTRL):
[261]147 self.world.attacks += 1
148 self.protagonist.attack()
[281]149 if ev.key == pygame.locals.K_SPACE:
150 self.protagonist.perform_action()
[180]151 elif DoorEvent.matches(ev):
152 self.protagonist.set_position(ev.dest_pos)
153 if ev.destination != self.name:
154 # Go to anther screen
[190]155 self._disable_render = True
[238]156 self.world.rooms += 1
[276]157 ScreenChange.post(ev.destination)
[190]158 return
[180]159 # else we're teleporting within the screen, and just the
160 # position change is enough
[261]161 elif FireEvent.matches(ev):
[305]162 bullet = Bullet(self.space, ev.source, ev.impulse, ev.damage,
[293]163 ev.source_collision_type)
[261]164 self._drawables.add(bullet)
[308]165 elif EnemyDeathEvent.matches(ev):
166 self.world.kills += 1
167 dead_enemy = DeadEnemy(self.space, self.world, ev.position,
168 ev.enemy_type)
169 self._drawables.add(dead_enemy)
[312]170 elif ClawEvent.matches(ev):
171 claw_attack = ClawAttack(self.space, ev.source, ev.vector,
172 ev.source_collision_type)
173 self._drawables.add(claw_attack)
[34]174 self.keys.handle_event(ev)
[18]175
[87]176 def _calc_viewport(self, level_surface, display_surface):
177 level_size = level_surface.get_size()
178 display_size = display_surface.get_size()
[93]179 protagnist_pos = self.protagonist.physicser.get_render_position(
[127]180 level_surface)
[87]181 x_wide = display_size[0] // 2
182 y_wide = display_size[1] // 2
[127]183 if display_size[0] > level_size[0]:
184 x = -(display_size[0] - level_size[0]) // 2
185 elif protagnist_pos[0] < x_wide:
[87]186 x = 0
187 elif protagnist_pos[0] > level_size[0] - x_wide:
188 x = level_size[0] - display_size[0]
189 else:
190 x = protagnist_pos[0] - x_wide
[127]191 if display_size[1] > level_size[1]:
192 y = -(display_size[1] - level_size[1]) // 2
193 elif protagnist_pos[1] < y_wide:
[87]194 y = 0
195 elif protagnist_pos[1] > level_size[1] - y_wide:
196 y = level_size[1] - display_size[1]
197 else:
198 y = protagnist_pos[1] - y_wide
199 return pygame.rect.Rect(x, y, display_size[0], display_size[1])
200
[18]201 def render(self, surface):
[190]202 if self._disable_render:
203 return
[50]204 background = self._level.get_background()
[84]205 mysurface = background.copy()
[105]206 for drawable in self._drawables:
207 drawable.render(mysurface)
[87]208 render_rect = self._calc_viewport(mysurface, surface)
209 surface.blit(mysurface, (0, 0), render_rect)
[191]210 for overlay in self._level.overlay_drawables:
211 if overlay.is_visible():
[211]212 overlay.render(surface, render_rect.topleft)
[243]213 self.render_health_bar(surface)
[34]214
215 def tick_protagonist(self):
[100]216 dx, dy = self.keys.get_direction()
[47]217 self.protagonist.set_direction(dx, dy)
[34]218
219 def tick(self, seconds):
[303]220 super(AreaScreen, self).tick(seconds)
[34]221 self.tick_protagonist()
[143]222 for drawable in self._drawables:
[307]223 drawable.update(seconds)
[293]224 if drawable.remove:
225 self._drawables.remove(drawable)
[243]226
[258]227 def render_health_bar(self, surface, damage_experienced=None):
[243]228 rect = pygame.Rect(50, 500, 110, 50)
[258]229 if damage_experienced:
230 health_box_colour = pygame.color.THECOLORS['red']
231 else:
232 health_box_colour = pygame.color.THECOLORS['white']
[257]233 pygame.draw.rect(surface, health_box_colour, rect, 0)
[246]234 if self.protagonist.in_human_form():
[243]235 health_colour = pygame.color.THECOLORS['red']
236 else:
237 health_colour = pygame.color.THECOLORS['purple']
[246]238 rect = pygame.Rect(55, 505, self.protagonist.get_health_level(), 40)
[243]239 pygame.draw.rect(surface, health_colour,
[246]240 rect, 0)
Note: See TracBrowser for help on using the repository browser.