brutalmaze/brutalmaze/characters.py

421 lines
16 KiB
Python
Raw Normal View History

# characters.py - module for hero and enemy classes
2020-01-21 09:18:19 +01:00
# Copyright (C) 2017-2020 Nguyễn Gia Phong
2017-10-12 15:29:55 +02:00
#
# This file is part of Brutal Maze.
2017-10-12 15:29:55 +02:00
#
# 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,
2017-10-12 15:29:55 +02:00
# 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.
2017-10-12 15:29:55 +02:00
#
# 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/>.
2017-10-12 15:29:55 +02:00
__doc__ = 'Brutal Maze module for hero and enemy classes'
2017-11-02 15:39:06 +01:00
from collections import deque
2020-09-19 06:12:54 +02:00
from math import atan2, gcd, pi, sin
2017-11-09 09:22:39 +01:00
from random import choice, randrange, shuffle
2017-11-12 15:08:14 +01:00
from sys import modules
2017-10-12 15:29:55 +02:00
2020-09-19 06:12:54 +02:00
from .constants import (ADJACENTS, AROUND_HERO, ATTACK_SPEED, EMPTY,
ENEMIES, ENEMY, ENEMY_HP, ENEMY_SPEED, FIRANGE,
HEAL_SPEED, HERO_HP, MIDDLE, MIN_BEAT, SFX_HEART,
SFX_SLASH_HERO, SFX_SPAWN, SQRT2, TANGO, WALL)
from .misc import fill_aapolygon, play, randsign, regpoly, sign
from .weapons import Bullet
2017-10-13 16:56:03 +02:00
2017-10-12 15:29:55 +02:00
class Hero:
2017-11-04 15:43:05 +01:00
"""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
2018-03-06 15:01:27 +01:00
next_beat (float): time until next heart beat (in ms)
next_strike (float): time until the hero can do the next attack (in ms)
2018-07-01 16:53:14 +02:00
highness (float): likelihood that the hero shoots toward other angles
2020-01-21 09:18:19 +01:00
slashing (bool): flag indicating if the hero's doing close-range attack
2018-10-07 16:59:39 +02:00
firing (bool): flag indicating if the hero is doing long-range attack
dead (bool): flag indicating if the hero is dead
2017-11-04 15:43:05 +01:00
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)
2017-11-04 15:43:05 +01:00
"""
def __init__(self, surface, fps, maze_size):
2017-10-12 15:29:55 +02:00
self.surface = surface
w, h = maze_size
2017-10-12 15:29:55 +02:00
self.x, self.y = w >> 1, h >> 1
2018-03-01 10:07:29 +01:00
self.angle, self.color = -pi * 3 / 4, TANGO['Aluminium']
2017-11-12 15:08:14 +01:00
self.R = (w * h / sin(pi*2/3) / 624) ** 0.5
2017-10-12 15:29:55 +02:00
self.next_heal = -1.0
self.next_beat = self.next_strike = 0.0
2018-07-01 16:53:14 +02:00
self.highness = 0.0
self.slashing = self.firing = self.dead = False
self.spin_speed = fps / HERO_HP
2017-10-21 16:22:06 +02:00
self.spin_queue = self.wound = 0.0
self.wounds = deque([0.0])
2017-10-12 15:29:55 +02:00
2017-10-19 10:24:56 +02:00
def update(self, fps):
2017-10-12 15:29:55 +02:00
"""Update the hero."""
2017-12-17 11:05:17 +01:00
if self.dead:
2018-01-21 14:38:51 +01:00
self.spin_queue = 0.0
2017-12-17 11:05:17 +01:00
return
2018-03-06 15:01:27 +01:00
old_speed = self.spin_speed
self.spin_speed = fps / (HERO_HP-self.wound**0.5)
2017-10-21 16:22:06 +02:00
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)
2018-03-06 15:01:27 +01:00
if self.next_beat <= 0:
play(SFX_HEART)
2018-03-06 15:01:27 +01:00
self.next_beat = MIN_BEAT*(2 - self.wound/HERO_HP)
else:
2020-01-21 09:18:19 +01:00
self.next_beat -= 1000 / fps
self.next_strike -= 1000 / fps
2017-10-15 11:20:14 +02:00
2018-10-09 16:20:38 +02:00
full_spin = pi * 2 / self.sides
2018-03-06 15:01:27 +01:00
if self.slashing and self.next_strike <= 0:
self.next_strike = ATTACK_SPEED
2017-10-21 16:22:06 +02:00
self.spin_queue = randsign() * self.spin_speed
2018-03-01 10:07:29 +01:00
self.angle -= sign(self.spin_queue) * full_spin
if round(self.spin_queue) != 0:
2018-03-01 10:07:29 +01:00
self.angle += sign(self.spin_queue) * full_spin / self.spin_speed
2017-10-21 16:22:06 +02:00
self.spin_queue -= sign(self.spin_queue)
2018-03-01 10:07:29 +01:00
else:
self.spin_queue = 0.0
2018-10-09 16:20:38 +02:00
@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.
2018-03-01 10:07:29 +01:00
"""
return 3 if self.next_heal < 0 else 4
2018-02-26 15:02:11 +01:00
def update_angle(self, angle):
"""Turn to the given angle if the hero is not busy slashing."""
if round(self.spin_queue) != 0: return
2018-03-01 10:07:29 +01:00
delta = (angle - self.angle + pi) % (pi * 2) - pi
2018-10-09 16:20:38 +02:00
unit = pi * 2 / self.sides / self.spin_speed
2018-03-01 10:07:29 +01:00
if abs(delta) < unit:
self.angle, self.spin_queue = angle, 0.0
else:
2018-03-01 10:07:29 +01:00
self.spin_queue = delta / unit
2018-10-09 16:20:38 +02:00
@property
def shots(self):
"""List of Bullet the hero has just shot."""
2018-07-01 16:53:14 +02:00
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
2018-10-09 16:20:38 +02:00
n = self.sides
2018-07-01 16:53:14 +02:00
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."""
2018-10-09 16:20:38 +02:00
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):
2017-10-13 10:10:16 +02:00
"""Resize the hero."""
w, h = maze_size
2017-10-13 10:10:16 +02:00
self.x, self.y = w >> 1, h >> 1
self.R = (w * h / sin(pi*2/3) / 624) ** 0.5
2017-10-13 10:10:16 +02:00
2017-10-12 15:29:55 +02:00
class Enemy:
2017-11-04 15:43:05 +01:00
"""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)
2017-11-12 15:08:14 +01:00
color (str): enemy's color name
2019-07-24 07:45:25 +02:00
alive (bool): flag indicating if the enemy is alive
2018-10-07 16:59:39 +02:00
awake (bool): flag indicating if the enemy is active
2018-03-06 15:01:27 +01:00
next_strike (float): time until the enemy's next action (in ms)
2017-11-04 15:43:05 +01:00
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
"""
2017-11-12 15:08:14 +01:00
def __init__(self, maze, x, y, color):
self.maze = maze
2017-10-13 16:56:03 +02:00
self.x, self.y = x, y
2017-11-12 15:08:14 +01:00
self.angle, self.color = pi / 4, color
2017-10-12 15:29:55 +02:00
2019-07-24 07:45:25 +02:00
self.alive, self.awake = True, False
2018-03-06 15:01:27 +01:00
self.next_strike = 0.0
self.move_speed = self.maze.fps / ENEMY_SPEED
2017-10-13 16:56:03 +02:00
self.offsetx = self.offsety = 0
self.spin_speed = self.maze.fps / ENEMY_HP
2017-10-19 15:28:56 +02:00
self.spin_queue = self.wound = 0.0
2017-10-12 15:29:55 +02:00
2018-10-09 16:20:38 +02:00
@property
def pos(self):
"""Coordinates (in pixels) of the center of the enemy."""
2017-11-20 16:29:56 +01:00
x, y = self.maze.get_pos(self.x, self.y)
2018-02-12 06:17:39 +01:00
step = self.maze.distance * ENEMY_SPEED / self.maze.fps
2017-11-02 15:39:06 +01:00
return x + self.offsetx*step, y + self.offsety*step
2017-10-15 11:20:14 +02:00
2018-10-09 16:20:38 +02:00
@property
def distance(self):
"""Distance from the center of the enemy
to the center of the maze.
2018-01-26 16:50:52 +01:00
"""
2018-10-09 16:20:38 +02:00
return self.maze.get_distance(*self.pos)
2018-01-26 16:50:52 +01:00
2017-10-13 16:56:03 +02:00
def place(self, x=0, y=0):
"""Move the enemy by (x, y) (in grids)."""
2017-10-12 15:29:55 +02:00
self.x += x
self.y += y
if self.awake: self.maze.map[self.x][self.y] = ENEMY
2018-10-09 16:20:38 +02:00
@property
2019-10-09 06:15:55 +02:00
def spawn_volume(self):
2018-10-09 16:20:38 +02:00
"""Volumn of spawning sound effect."""
return 1 - self.distance / self.maze.get_distance(0, 0) / 2
2017-11-04 15:43:05 +01:00
def wake(self):
2017-11-12 15:08:14 +01:00
"""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
2019-10-09 06:15:55 +02:00
srcx, destx = self.x, MIDDLE
if abs(destx - srcx) != 1: srcx += sign(destx - srcx) or 1
srcy, desty = self.y, MIDDLE
if abs(desty - srcy) != 1: srcy += sign(desty - srcy) or 1
m, n = destx - srcx, desty - srcy
lcm = abs(m * n // gcd(m, n))
w, u = lcm // m, lcm // n
for i in range(lcm):
if self.maze.map[srcx+i//w][srcy+i//u] == WALL: return False
2017-11-04 15:43:05 +01:00
self.awake = True
self.maze.map[self.x][self.y] = ENEMY
play(SFX_SPAWN, self.x, self.y)
2017-11-12 15:08:14 +01:00
return True
2017-11-04 15:43:05 +01:00
def fire(self):
2017-11-12 15:08:14 +01:00
"""Return True if the enemy has just fired, False otherwise."""
if self.maze.hero.dead: return False
2018-10-09 16:20:38 +02:00
x, y = self.pos
2017-11-20 16:29:56 +01:00
if (self.maze.get_distance(x, y) > FIRANGE*self.maze.distance
2018-03-06 15:01:27 +01:00
or self.next_strike > 0
or (self.x, self.y) in AROUND_HERO or self.offsetx or self.offsety
2017-11-09 09:22:39 +01:00
or randrange((self.maze.hero.slashing+self.maze.isfast()+1) * 3)):
return False
2018-03-06 15:01:27 +01:00
self.next_strike = ATTACK_SPEED
2018-01-24 17:46:44 +01:00
self.maze.bullets.append(
Bullet(self.maze.surface, x, y, self.get_angle() + pi, self.color))
return True
2017-10-12 15:29:55 +02:00
2017-11-14 02:51:18 +01:00
def move(self, speed=ENEMY_SPEED):
2017-11-12 15:08:14 +01:00
"""Return True if it has just moved, False otherwise."""
2017-10-13 16:56:03 +02:00
if self.offsetx:
self.offsetx -= sign(self.offsetx)
return True
if self.offsety:
self.offsety -= sign(self.offsety)
return True
2018-03-06 15:01:27 +01:00
if self.next_strike > 0: return False
2017-10-13 16:56:03 +02:00
2017-11-14 02:51:18 +01:00
self.move_speed = self.maze.fps / speed
2017-10-13 16:56:03 +02:00
directions = [(sign(MIDDLE - self.x), 0), (0, sign(MIDDLE - self.y))]
shuffle(directions)
2018-05-20 15:33:50 +02:00
directions.append(choice(ADJACENTS))
if self.maze.hero.dead: directions = choice(ADJACENTS),
2017-10-13 16:56:03 +02:00
for x, y in directions:
if (x or y) and self.maze.map[self.x + x][self.y + y] == EMPTY:
2017-10-19 10:24:56 +02:00
self.offsetx = round(x * (1 - self.move_speed))
self.offsety = round(y * (1 - self.move_speed))
self.maze.map[self.x][self.y] = EMPTY
2017-10-13 16:56:03 +02:00
self.place(x, y)
return True
return False
2018-01-21 14:38:51 +01:00
def get_slash(self):
"""Return the enemy's close-range damage."""
2018-10-09 16:20:38 +02:00
wound = (self.maze.slashd - self.distance) / self.maze.hero.R
2018-01-21 14:38:51 +01:00
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)
2018-01-21 14:38:51 +01:00
return wound
2017-11-14 02:51:18 +01:00
2020-09-19 06:12:54 +02:00
def get_angle(self):
2018-01-24 17:46:44 +01:00
"""Return the angle of the vector whose initial point is
the center of the screen and terminal point is the center of
the enemy.
"""
2018-10-09 16:20:38 +02:00
x, y = self.pos
2018-01-24 17:46:44 +01:00
return atan2(y - self.maze.y, x - self.maze.x)
2018-02-26 15:02:11 +01:00
def get_color(self):
"""Return current color of the enemy."""
return TANGO[self.color][int(self.wound)]
2018-02-26 15:02:11 +01:00
2018-10-07 16:59:39 +02:00
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
2018-02-26 15:02:11 +01:00
2017-11-12 15:08:14 +01:00
def draw(self):
"""Draw the enemy."""
if self.isunnoticeable(): return
radius = self.maze.distance / SQRT2
2018-10-09 16:20:38 +02:00
square = regpoly(4, radius, self.angle, *self.pos)
2018-02-26 15:02:11 +01:00
fill_aapolygon(self.maze.surface, square, self.get_color())
2017-11-12 15:08:14 +01:00
def update(self):
2017-10-13 16:56:03 +02:00
"""Update the enemy."""
if self.awake:
2017-11-20 16:29:56 +01:00
self.spin_speed, tmp = self.maze.fps / ENEMY_HP, self.spin_speed
self.spin_queue *= self.spin_speed / tmp
2020-01-21 09:18:19 +01:00
self.next_strike -= 1000 / self.maze.fps
if not self.spin_queue and not self.fire() and not self.move():
2017-10-19 15:28:56 +02:00
self.spin_queue = randsign() * self.spin_speed
if not self.maze.hero.dead:
play(SFX_SLASH_HERO, self.x, self.y, self.get_slash())
if round(self.spin_queue) != 0:
2017-10-19 15:28:56 +02:00
self.angle += sign(self.spin_queue) * pi / 2 / self.spin_speed
self.spin_queue -= sign(self.spin_queue)
else:
2017-10-20 15:51:39 +02:00
self.angle, self.spin_queue = pi / 4, 0.0
2018-01-21 14:38:51 +01:00
def hit(self, wound):
2017-11-12 15:08:14 +01:00
"""Handle the enemy when it's attacked."""
2017-11-02 15:39:06 +01:00
self.wound += wound
2018-10-07 16:59:39 +02:00
@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
2017-10-15 11:20:14 +02:00
def die(self):
"""Handle the enemy's death."""
self.maze.map[self.x][self.y] = EMPTY if self.wake else WALL
2019-07-24 07:45:25 +02:00
self.alive = False
2017-11-12 15:08:14 +01:00
class Chameleon(Enemy):
2017-11-21 12:01:32 +01:00
"""Object representing an enemy of Chameleon.
2017-11-12 15:08:14 +01:00
Additional attributes:
2018-03-06 15:01:27 +01:00
visible (float): time until the Chameleon is visible (in ms)
2017-11-12 15:08:14 +01:00
"""
def __init__(self, maze, x, y):
2020-09-19 06:12:54 +02:00
super().__init__(maze, x, y, 'Chameleon')
2018-03-06 15:01:27 +01:00
self.visible = 0.0
2017-11-12 15:08:14 +01:00
def wake(self):
"""Wake the Chameleon up if it can see the hero."""
2020-09-19 06:12:54 +02:00
if super().wake() is True:
2020-01-21 09:18:19 +01:00
self.visible = 1000 / ENEMY_SPEED
2017-11-12 15:08:14 +01:00
2018-10-07 16:59:39 +02:00
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.
"""
2020-09-19 06:12:54 +02:00
return (super().isunnoticeable(x, y)
or self.visible <= 0 and not self.spin_queue
and self.maze.next_move <= 0)
2017-11-12 15:08:14 +01:00
2018-03-06 15:01:27 +01:00
def update(self):
"""Update the Chameleon."""
2020-09-19 06:12:54 +02:00
super().update()
2020-01-21 09:18:19 +01:00
if self.awake: self.visible -= 1000 / self.maze.fps
2018-03-06 15:01:27 +01:00
2018-01-21 14:38:51 +01:00
def hit(self, wound):
2017-12-17 11:05:17 +01:00
"""Handle the Chameleon when it's attacked."""
2018-03-06 15:01:27 +01:00
self.visible = 1000.0 / ENEMY_SPEED
2020-09-19 06:12:54 +02:00
super().hit(wound)
2017-11-12 15:08:14 +01:00
2017-11-21 12:01:32 +01:00
class Plum(Enemy):
"""Object representing an enemy of Plum."""
def __init__(self, maze, x, y):
2020-09-19 06:12:54 +02:00
super().__init__(maze, x, y, 'Plum')
2017-11-21 12:01:32 +01:00
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
2017-11-12 15:08:14 +01:00
class ScarletRed(Enemy):
2017-11-21 12:01:32 +01:00
"""Object representing an enemy of Scarlet Red."""
2017-11-12 15:08:14 +01:00
def __init__(self, maze, x, y):
2020-09-19 06:12:54 +02:00
super().__init__(maze, x, y, 'ScarletRed')
2017-11-12 15:08:14 +01:00
2017-11-14 02:51:18 +01:00
def fire(self):
"""Scarlet Red doesn't shoot."""
return False
def move(self):
2020-09-19 06:12:54 +02:00
return super().move(self, ENEMY_SPEED * SQRT2)
2017-11-14 02:51:18 +01:00
def slash(self):
"""Handle the Scarlet Red's close-range attack."""
2020-09-19 06:12:54 +02:00
self.wound -= super().slash()
if self.wound < 0: self.wound = 0.0
2017-11-14 02:51:18 +01:00
2017-11-12 15:08:14 +01:00
def new_enemy(maze, x, y):
"""Return an enemy of a random type in the grid (x, y)."""
color = choice(ENEMIES)
2017-11-19 09:00:24 +01:00
try:
return getattr(modules[__name__], color)(maze, x, y)
except AttributeError:
return Enemy(maze, x, y, color)