1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
# -*- coding: utf-8 -*-
# characters.py - module for hero and enemy classes
# Copyright (C) 2017, 2018 Nguyễn Gia Phong
#
# This file is part of Brutal Maze.
#
# Brutal Maze is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Brutal Maze is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Brutal Maze. If not, see <https://www.gnu.org/licenses/>.
__doc__ = 'Brutal Maze module for hero and enemy classes'
from collections import deque
from math import atan, atan2, sin, pi
from random import choice, randrange, shuffle
from sys import modules
from .constants import (
TANGO, HERO_HP, SFX_HEART, HEAL_SPEED, MIN_BEAT, ATTACK_SPEED, ENEMY,
ENEMY_SPEED, ENEMY_HP, SFX_SLASH_HERO, MIDDLE, WALL, FIRANGE, AROUND_HERO,
ADJACENTS, EMPTY, SQRT2, MINW)
from .misc import sign, cosin, randsign, regpoly, fill_aapolygon, choices, play
from .weapons import Bullet
class Hero:
"""Object representing the hero.
Attributes:
surface (pygame.Surface): the display to draw on
x, y (int): coordinates of the center of the hero (in pixels)
angle (float): angle of the direction the hero pointing (in radians)
color (tuple of pygame.Color): colors of the hero on different HPs
R (int): circumradius of the regular triangle representing the hero
next_heal (float): minimum wound in ATTACK_SPEED allowing healing again
next_beat (float): time until next heart beat (in ms)
next_strike (float): time until the hero can do the next attack (in ms)
highness (float): likelihood that the hero shoots toward other angles
slashing (bool): flag indicating if the hero is doing close-range attack
firing (bool): flag indicating if the hero is doing long-range attack
dead (bool): flag indicating if the hero is dead
spin_speed (float): speed of spinning (in frames per slash)
spin_queue (float): frames left to finish spinning
wound (float): amount of wound
wounds (deque of float): wounds in time of an attack (ATTACK_SPEED)
sfx_heart (pygame.mixer.Sound): heart beat sound effect
"""
def __init__(self, surface, fps, maze_size):
self.surface = surface
w, h = maze_size
self.x, self.y = w >> 1, h >> 1
self.angle, self.color = -pi * 3 / 4, TANGO['Aluminium']
self.R = (w * h / sin(pi*2/3) / 624) ** 0.5
self.next_heal = -1.0
self.next_beat = self.next_strike = 0.0
self.highness = 0.0
self.slashing = self.firing = self.dead = False
self.spin_speed = fps / HERO_HP
self.spin_queue = self.wound = 0.0
self.wounds = deque([0.0])
self.sfx_heart = SFX_HEART
def update(self, fps):
"""Update the hero."""
if self.dead:
self.spin_queue = 0.0
return
old_speed = self.spin_speed
self.spin_speed = fps / (HERO_HP-self.wound**0.5)
self.spin_queue *= self.spin_speed / old_speed
if len(self.wounds) > fps * ATTACK_SPEED / 1000: self.wounds.popleft()
if sum(self.wounds) < self.next_heal: self.next_heal = -1.0
self.wound += self.wounds[-1]
if self.next_heal < 0:
self.wound -= HEAL_SPEED / self.spin_speed / HERO_HP
if self.wound < 0: self.wound = 0.0
self.wounds.append(0.0)
if self.next_beat <= 0:
play(self.sfx_heart)
self.next_beat = MIN_BEAT*(2 - self.wound/HERO_HP)
else:
self.next_beat -= 1000.0 / fps
self.next_strike -= 1000.0 / fps
full_spin = pi * 2 / self.sides
if self.slashing and self.next_strike <= 0:
self.next_strike = ATTACK_SPEED
self.spin_queue = randsign() * self.spin_speed
self.angle -= sign(self.spin_queue) * full_spin
if round(self.spin_queue) != 0:
self.angle += sign(self.spin_queue) * full_spin / self.spin_speed
self.spin_queue -= sign(self.spin_queue)
else:
self.spin_queue = 0.0
@property
def sides(self):
"""Number of sides the hero has. While the hero is generally
a trigon, Agent Orange may turn him into a square.
"""
return 3 if self.next_heal < 0 else 4
def update_angle(self, angle):
"""Turn to the given angle if the hero is not busy slashing."""
if round(self.spin_queue) != 0: return
delta = (angle - self.angle + pi) % (pi * 2) - pi
unit = pi * 2 / self.sides / self.spin_speed
if abs(delta) < unit:
self.angle, self.spin_queue = angle, 0.0
else:
self.spin_queue = delta / unit
@property
def shots(self):
"""List of Bullet the hero has just shot."""
if not self.firing or self.slashing or self.next_strike > 0: return []
self.next_strike = ATTACK_SPEED
if not randrange(int(self.highness + 1)):
return [Bullet(self.surface, self.x, self.y,
self.angle, 'Aluminium')]
self.highness -= 1.0
n = self.sides
corners = {randrange(n) for _ in range(n)}
angles = (self.angle + pi*2*corner/n for corner in corners)
return [Bullet(self.surface, self.x, self.y, angle, 'Aluminium')
for angle in angles]
def get_color(self):
"""Return current color of the hero."""
return self.color[int(self.wound)]
def draw(self):
"""Draw the hero."""
trigon = regpoly(self.sides, self.R, self.angle, self.x, self.y)
fill_aapolygon(self.surface, trigon, self.get_color())
def resize(self, maze_size):
"""Resize the hero."""
w, h = maze_size
self.x, self.y = w >> 1, h >> 1
self.R = (w * h / sin(pi*2/3) / 624) ** 0.5
class Enemy:
"""Object representing an enemy.
Attributes:
maze (Maze): the maze
x, y (int): coordinates of the center of the enemy (in grids)
angle (float): angle of the direction the enemy pointing (in radians)
color (str): enemy's color name
awake (bool): flag indicating if the enemy is active
next_strike (float): time until the enemy's next action (in ms)
move_speed (float): speed of movement (in frames per grid)
offsetx, offsety (integer): steps moved from the center of the grid
spin_speed (float): speed of spinning (in frames per slash)
spin_queue (float): frames left to finish spinning
wound (float): amount of wound
sfx_slash (pygame.mixer.Sound): sound effect of slashed hero
"""
def __init__(self, maze, x, y, color):
self.maze = maze
self.x, self.y = x, y
self.angle, self.color = pi / 4, color
self.awake = False
self.next_strike = 0.0
self.move_speed = self.maze.fps / ENEMY_SPEED
self.offsetx = self.offsety = 0
self.spin_speed = self.maze.fps / ENEMY_HP
self.spin_queue = self.wound = 0.0
self.sfx_slash = SFX_SLASH_HERO
@property
def pos(self):
"""Coordinates (in pixels) of the center of the enemy."""
x, y = self.maze.get_pos(self.x, self.y)
step = self.maze.distance * ENEMY_SPEED / self.maze.fps
return x + self.offsetx*step, y + self.offsety*step
@property
def distance(self):
"""Distance from the center of the enemy
to the center of the maze.
"""
return self.maze.get_distance(*self.pos)
def place(self, x=0, y=0):
"""Move the enemy by (x, y) (in grids)."""
self.x += x
self.y += y
if self.awake: self.maze.map[self.x][self.y] = ENEMY
@property
def spawn_volumn(self):
"""Volumn of spawning sound effect."""
return 1 - self.distance / self.maze.get_distance(0, 0) / 2
def wake(self):
"""Wake the enemy up if it can see the hero.
Return None if the enemy is already awake, True if the function
has just woken it, False otherwise.
"""
if self.awake: return None
startx = starty = MIDDLE
stopx, stopy, distance = self.x, self.y, self.maze.distance
if startx > stopx: startx, stopx = stopx, startx
if starty > stopy: starty, stopy = stopy, starty
dx = (self.x-MIDDLE)*distance + self.maze.centerx - self.maze.x
dy = (self.y-MIDDLE)*distance + self.maze.centery - self.maze.y
mind = cosin(abs(atan(dy / dx)) if dx else 0) * distance
def get_distance(x, y): return abs(dy*x - dx*y) / (dy**2 + dx**2)**0.5
for i in range(startx, stopx + 1):
for j in range(starty, stopy + 1):
if self.maze.map[i][j] != WALL or i == stopx and j == stopy:
continue
x, y = self.maze.get_pos(i, j)
if get_distance(x - self.maze.x, y - self.maze.y) <= mind:
return False
self.awake = True
self.maze.map[self.x][self.y] = ENEMY
play(self.maze.sfx_spawn, self.spawn_volumn, self.get_angle() + pi)
return True
def fire(self):
"""Return True if the enemy has just fired, False otherwise."""
if self.maze.hero.dead: return False
x, y = self.pos
if (self.maze.get_distance(x, y) > FIRANGE*self.maze.distance
or self.next_strike > 0
or (self.x, self.y) in AROUND_HERO or self.offsetx or self.offsety
or randrange((self.maze.hero.slashing+self.maze.isfast()+1) * 3)):
return False
self.next_strike = ATTACK_SPEED
self.maze.bullets.append(
Bullet(self.maze.surface, x, y, self.get_angle() + pi, self.color))
return True
def move(self, speed=ENEMY_SPEED):
"""Return True if it has just moved, False otherwise."""
if self.offsetx:
self.offsetx -= sign(self.offsetx)
return True
if self.offsety:
self.offsety -= sign(self.offsety)
return True
if self.next_strike > 0: return False
self.move_speed = self.maze.fps / speed
directions = [(sign(MIDDLE - self.x), 0), (0, sign(MIDDLE - self.y))]
shuffle(directions)
directions.append(choice(ADJACENTS))
if self.maze.hero.dead: directions = choice(ADJACENTS),
for x, y in directions:
if (x or y) and self.maze.map[self.x + x][self.y + y] == EMPTY:
self.offsetx = round(x * (1 - self.move_speed))
self.offsety = round(y * (1 - self.move_speed))
self.maze.map[self.x][self.y] = EMPTY
self.place(x, y)
return True
return False
def get_slash(self):
"""Return the enemy's close-range damage."""
wound = (self.maze.slashd - self.distance) / self.maze.hero.R
return wound if wound > 0 else 0.0
def slash(self):
"""Return the enemy's close-range damage per frame."""
wound = self.get_slash() / self.spin_speed
if self.spin_queue and wound: self.maze.hit_hero(wound, self.color)
return wound
def get_angle(self, reversed=False):
"""Return the angle of the vector whose initial point is
the center of the screen and terminal point is the center of
the enemy.
"""
x, y = self.pos
return atan2(y - self.maze.y, x - self.maze.x)
def get_color(self):
"""Return current color of the enemy."""
return TANGO[self.color][int(self.wound)]
def isunnoticeable(self, x=None, y=None):
"""Return whether the enemy can be noticed.
Only search within column x and row y if these coordinates
are provided.
"""
if x is not None and self.x != x: return True
if y is not None and self.y != y: return True
return not self.awake or self.wound >= ENEMY_HP
def draw(self):
"""Draw the enemy."""
if self.isunnoticeable(): return
radius = self.maze.distance / SQRT2
square = regpoly(4, radius, self.angle, *self.pos)
fill_aapolygon(self.maze.surface, square, self.get_color())
def update(self):
"""Update the enemy."""
if self.awake:
self.spin_speed, tmp = self.maze.fps / ENEMY_HP, self.spin_speed
self.spin_queue *= self.spin_speed / tmp
self.next_strike -= 1000.0 / self.maze.fps
if not self.spin_queue and not self.fire() and not self.move():
self.spin_queue = randsign() * self.spin_speed
if not self.maze.hero.dead:
play(self.sfx_slash, self.get_slash(), self.get_angle())
if round(self.spin_queue) != 0:
self.angle += sign(self.spin_queue) * pi / 2 / self.spin_speed
self.spin_queue -= sign(self.spin_queue)
else:
self.angle, self.spin_queue = pi / 4, 0.0
def hit(self, wound):
"""Handle the enemy when it's attacked."""
self.wound += wound
@property
def retired(self):
"""Provide compatibility with LockOn object."""
try:
return self._retired
except AttributeError:
return self.wound >= ENEMY_HP
@retired.setter
def retired(self, value):
self._retired = value
def die(self):
"""Handle the enemy's death."""
if self.awake:
self.maze.map[self.x][self.y] = EMPTY
if self.maze.enemy_weights[self.color] > MINW + 1.5:
self.maze.enemy_weights[self.color] -= 1.5
else:
self.maze.map[self.x][self.y] = WALL
class Chameleon(Enemy):
"""Object representing an enemy of Chameleon.
Additional attributes:
visible (float): time until the Chameleon is visible (in ms)
"""
def __init__(self, maze, x, y):
Enemy.__init__(self, maze, x, y, 'Chameleon')
self.visible = 0.0
def wake(self):
"""Wake the Chameleon up if it can see the hero."""
if Enemy.wake(self) is True:
self.visible = 1000.0 / ENEMY_SPEED
def isunnoticeable(self, x=None, y=None):
"""Return whether the enemy can be noticed.
Only search within column x and row y if these coordinates
are provided.
"""
return (Enemy.isunnoticeable(self, x, y)
or self.visible <= 0 and not self.spin_queue
and self.maze.next_move <= 0)
def update(self):
"""Update the Chameleon."""
Enemy.update(self)
if self.awake: self.visible -= 1000.0 / self.maze.fps
def hit(self, wound):
"""Handle the Chameleon when it's attacked."""
self.visible = 1000.0 / ENEMY_SPEED
Enemy.hit(self, wound)
class Plum(Enemy):
"""Object representing an enemy of Plum."""
def __init__(self, maze, x, y):
Enemy.__init__(self, maze, x, y, 'Plum')
def clone(self, other):
"""Turn the other enemy into a clone of this Plum and return
True if that enemy is also a Plum, otherwise return False.
"""
if other.color != 'Plum': return False
other.x, other.y, other.angle = self.x, self.y, self.angle
other.awake, other.next_strike = True, self.next_strike
other.offsetx, other.offsety = self.offsetx, self.offsety
other.spin_queue, other.wound = self.spin_queue, self.wound
return True
class ScarletRed(Enemy):
"""Object representing an enemy of Scarlet Red."""
def __init__(self, maze, x, y):
Enemy.__init__(self, maze, x, y, 'ScarletRed')
def fire(self):
"""Scarlet Red doesn't shoot."""
return False
def move(self):
return Enemy.move(self, ENEMY_SPEED * SQRT2)
def slash(self):
"""Handle the Scarlet Red's close-range attack."""
self.wound -= Enemy.slash(self)
if self.wound < 0: self.wound = 0.0
def new_enemy(maze, x, y):
"""Return an enemy of a random type in the grid (x, y)."""
color = choices(maze.enemy_weights)
try:
return getattr(modules[__name__], color)(maze, x, y)
except AttributeError:
return Enemy(maze, x, y, color)
|