source: nagslang/screens/area.py@ 333:3dd32686dbc3

Last change on this file since 333:3dd32686dbc3 was 333:3dd32686dbc3, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

Better wolf claw attack.

File size: 9.2 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,
[333]9 COLLISION_TYPE_FURNITURE, COLLISION_TYPE_WEREWOLF_ATTACK)
[308]10from nagslang.enemies import DeadEnemy
[333]11from nagslang.events import (
12 ScreenChange, DoorEvent, FireEvent, 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
[321]86 @classmethod
87 def list_areas(self):
88 return Level.list_levels()
89
[319]90 def _player_collision_pre_solve_handler(self, space, arbiter):
[188]91 gobj = arbiter.shapes[1].physicser.game_object
[256]92 result = gobj.collide_with_protagonist(self.protagonist)
[192]93 # The collision handler must return `True` or `False`. We don't want to
94 # accidentally reject collisions from handlers that return `None`, so
95 # we explicitly check for `False` and treate everything else as `True`.
[333]96 return result is not False
97
98 def _claw_attack_collision_pre_solve_handler(self, space, arbiter):
99 claw = arbiter.shapes[0].physicser.game_object
100 gobj = arbiter.shapes[1].physicser.game_object
101 result = gobj.collide_with_claw_attack(claw)
102 return result is not False
[188]103
[319]104 def _furniture_collision_pre_solve_handler(self, space, arbiter):
105 furniture = arbiter.shapes[0].physicser.game_object
106 gobj = arbiter.shapes[1].physicser.game_object
107 result = gobj.collide_with_furniture(furniture)
108 return result is not False
109
[188]110 def _add_collision_handlers(self):
111 for collision_type in CALLBACK_COLLIDERS:
112 self.space.add_collision_handler(
113 COLLISION_TYPE_PLAYER, collision_type,
[319]114 pre_solve=self._player_collision_pre_solve_handler)
115 self.space.add_collision_handler(
116 COLLISION_TYPE_FURNITURE, collision_type,
117 pre_solve=self._furniture_collision_pre_solve_handler)
[333]118 self.space.add_collision_handler(
119 COLLISION_TYPE_WEREWOLF_ATTACK, collision_type,
120 pre_solve=self._claw_attack_collision_pre_solve_handler)
[188]121
[37]122 def add_walls(self):
[21]123 self.walls = []
124 body = pymunk.Body()
[84]125 body.position = (0, 0)
[53]126 walls = self._level.get_walls()
127 for wall in walls:
[279]128 if len(wall) < 2:
129 # Don't try to add a useless wall
130 continue
[53]131 corners = wall
132 corner = corners[-1]
133 for next_corner in corners:
134 wall = pymunk.Segment(body, corner, next_corner, 5)
[301]135 wall.collision_type = COLLISION_TYPE_WALL
[53]136 wall.elasticity = 1.0
137 self.walls.append(wall)
138 corner = next_corner
[21]139 self.space.add(*self.walls)
140
[145]141 def add_game_objects(self):
[191]142 for drawable in self._level.drawables:
[145]143 self._drawables.add(drawable)
144
[37]145 def add_protagonist(self):
[276]146 self.protagonist = self.world.protagonist
147 self.protagonist.change_space(self.space)
[107]148 self._drawables.add(self.protagonist)
[21]149
[18]150 def handle_event(self, ev):
[20]151 if ev.type == pygame.locals.KEYDOWN:
152 if ev.key == pygame.locals.K_ESCAPE:
153 ScreenChange.post('menu')
[97]154 if ev.key == pygame.locals.K_c:
[47]155 self.protagonist.toggle_form()
[238]156 self.world.transformations += 1
[283]157 if ev.key in (pygame.locals.K_z, pygame.locals.K_LCTRL):
[261]158 self.world.attacks += 1
159 self.protagonist.attack()
[281]160 if ev.key == pygame.locals.K_SPACE:
161 self.protagonist.perform_action()
[180]162 elif DoorEvent.matches(ev):
163 self.protagonist.set_position(ev.dest_pos)
164 if ev.destination != self.name:
165 # Go to anther screen
[190]166 self._disable_render = True
[238]167 self.world.rooms += 1
[276]168 ScreenChange.post(ev.destination)
[190]169 return
[180]170 # else we're teleporting within the screen, and just the
171 # position change is enough
[261]172 elif FireEvent.matches(ev):
[305]173 bullet = Bullet(self.space, ev.source, ev.impulse, ev.damage,
[293]174 ev.source_collision_type)
[261]175 self._drawables.add(bullet)
[308]176 elif EnemyDeathEvent.matches(ev):
177 self.world.kills += 1
178 dead_enemy = DeadEnemy(self.space, self.world, ev.position,
179 ev.enemy_type)
180 self._drawables.add(dead_enemy)
[312]181 elif ClawEvent.matches(ev):
182 claw_attack = ClawAttack(self.space, ev.source, ev.vector,
[333]183 ev.damage)
[312]184 self._drawables.add(claw_attack)
[34]185 self.keys.handle_event(ev)
[18]186
[87]187 def _calc_viewport(self, level_surface, display_surface):
188 level_size = level_surface.get_size()
189 display_size = display_surface.get_size()
[93]190 protagnist_pos = self.protagonist.physicser.get_render_position(
[127]191 level_surface)
[87]192 x_wide = display_size[0] // 2
193 y_wide = display_size[1] // 2
[127]194 if display_size[0] > level_size[0]:
195 x = -(display_size[0] - level_size[0]) // 2
196 elif protagnist_pos[0] < x_wide:
[87]197 x = 0
198 elif protagnist_pos[0] > level_size[0] - x_wide:
199 x = level_size[0] - display_size[0]
200 else:
201 x = protagnist_pos[0] - x_wide
[127]202 if display_size[1] > level_size[1]:
203 y = -(display_size[1] - level_size[1]) // 2
204 elif protagnist_pos[1] < y_wide:
[87]205 y = 0
206 elif protagnist_pos[1] > level_size[1] - y_wide:
207 y = level_size[1] - display_size[1]
208 else:
209 y = protagnist_pos[1] - y_wide
210 return pygame.rect.Rect(x, y, display_size[0], display_size[1])
211
[18]212 def render(self, surface):
[190]213 if self._disable_render:
214 return
[50]215 background = self._level.get_background()
[84]216 mysurface = background.copy()
[105]217 for drawable in self._drawables:
218 drawable.render(mysurface)
[87]219 render_rect = self._calc_viewport(mysurface, surface)
220 surface.blit(mysurface, (0, 0), render_rect)
[191]221 for overlay in self._level.overlay_drawables:
222 if overlay.is_visible():
[211]223 overlay.render(surface, render_rect.topleft)
[243]224 self.render_health_bar(surface)
[34]225
226 def tick_protagonist(self):
[100]227 dx, dy = self.keys.get_direction()
[47]228 self.protagonist.set_direction(dx, dy)
[34]229
230 def tick(self, seconds):
[303]231 super(AreaScreen, self).tick(seconds)
[34]232 self.tick_protagonist()
[143]233 for drawable in self._drawables:
[307]234 drawable.update(seconds)
[293]235 if drawable.remove:
236 self._drawables.remove(drawable)
[243]237
[258]238 def render_health_bar(self, surface, damage_experienced=None):
[243]239 rect = pygame.Rect(50, 500, 110, 50)
[258]240 if damage_experienced:
241 health_box_colour = pygame.color.THECOLORS['red']
242 else:
243 health_box_colour = pygame.color.THECOLORS['white']
[333]244 pygame.draw.rect(surface, health_box_colour, rect, 0)
[246]245 if self.protagonist.in_human_form():
[243]246 health_colour = pygame.color.THECOLORS['red']
247 else:
248 health_colour = pygame.color.THECOLORS['purple']
[246]249 rect = pygame.Rect(55, 505, self.protagonist.get_health_level(), 40)
[333]250 pygame.draw.rect(surface, health_colour, rect, 0)
Note: See TracBrowser for help on using the repository browser.