source: nagslang/screens/area.py@ 363:3dd08e18580f

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

Acid attacks shoot things that look like acid

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