brutalmaze/brutalmaze/characters.py

327 lines
12 KiB
Python
Raw Normal View History

2017-10-12 15:29:55 +02:00
# -*- coding: utf-8 -*-
# characters.py - module for hero and enemy classes
2017-10-12 15:29:55 +02:00
# This file is part of brutalmaze
#
# brutalmaze is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# brutalmaze 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with brutalmaze. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2017 Nguyễn Gia Phong
2017-11-02 15:39:06 +01:00
__doc__ = 'brutalmaze module for hero and enemy classes'
2017-10-12 15:29:55 +02:00
from collections import deque
2017-11-04 15:43:05 +01:00
from math import atan, atan2, sin, pi
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
import pygame
from .constants import *
2017-11-12 15:08:14 +01:00
from .utils import sign, cosin, randsign, regpoly, fill_aapolygon, choices
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 (int): the tick that the hero gains back healing ability
2017-11-04 15:43:05 +01:00
next_strike (int): the tick that the hero can do the next attack
slashing (bool): flag indicates if the hero is doing close-range attack
firing (bool): flag indicates if the hero is doing long-range attack
dead (bool): flag indicates 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
"""
2017-10-19 10:24:56 +02:00
def __init__(self, surface, fps):
2017-10-12 15:29:55 +02:00
self.surface = surface
w, h = self.surface.get_width(), self.surface.get_height()
self.x, self.y = w >> 1, h >> 1
self.angle, self.color = pi / 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 = self.next_strike = 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
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-10-21 16:22:06 +02:00
old_speed, time = self.spin_speed, pygame.time.get_ticks()
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 not self.dead and time > self.next_heal:
self.wound -= HEAL_SPEED / self.spin_speed / HERO_HP
if self.wound < 0: self.wound = 0.0
2017-10-15 11:20:14 +02:00
2017-10-21 16:22:06 +02:00
if self.slashing and time >= self.next_strike:
self.next_strike = time + ATTACK_SPEED
self.spin_queue = randsign() * self.spin_speed
if abs(self.spin_queue) > 0.5:
self.angle += sign(self.spin_queue) * pi / 2 / self.spin_speed
self.spin_queue -= sign(self.spin_queue)
2017-10-12 15:29:55 +02:00
else:
# Follow the mouse cursor
x, y = pygame.mouse.get_pos()
self.angle = atan2(y - self.y, x - self.x)
2017-10-21 16:22:06 +02:00
self.spin_queue = 0.0
trigon = regpoly(3, self.R, self.angle, self.x, self.y)
try:
fill_aapolygon(self.surface, trigon, self.color[int(self.wound)])
except IndexError: # When the hero is wounded over his HP
self.wound = HERO_HP
def die(self):
"""Handle the hero's death."""
self.dead = True
self.slashing = self.firing = False
2017-10-12 15:29:55 +02:00
2017-10-13 10:10:16 +02:00
def resize(self):
"""Resize the hero."""
w, h = self.surface.get_width(), self.surface.get_height()
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
2017-11-04 15:43:05 +01:00
awake (bool): flag indicates if the enemy is active
next_strike (int): the tick that the enemy can do the next attack
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
self.maze.map[x][y] = ENEMY
2017-11-12 15:08:14 +01:00
self.angle, self.color = pi / 4, color
2017-10-12 15:29:55 +02:00
2017-10-13 16:56:03 +02:00
self.awake = False
2017-11-04 15:43:05 +01:00
self.next_strike = 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
def pos(self):
2017-10-15 11:20:14 +02:00
"""Return coordinate of the center of the enemy."""
2017-11-02 15:39:06 +01:00
x, y = self.maze.pos(self.x, self.y)
step = self.maze.distance * HERO_SPEED / self.maze.fps
return x + self.offsetx*step, y + self.offsety*step
2017-10-15 11:20:14 +02: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
self.maze.map[self.x][self.y] = ENEMY
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
2017-11-04 15:43:05 +01:00
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 length(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: continue
x, y = self.maze.pos(i, j)
2017-11-12 15:08:14 +01:00
if length(x - self.maze.x, y - self.maze.y) <= mind:
return False
2017-11-04 15:43:05 +01:00
self.awake = True
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."""
x, y = self.pos()
2017-11-02 15:39:06 +01:00
if (self.maze.length(x, y) > FIRANGE*self.maze.distance
2017-11-04 15:43:05 +01:00
or self.next_strike > pygame.time.get_ticks()
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
2017-11-04 15:43:05 +01:00
self.next_strike = pygame.time.get_ticks() + ATTACK_SPEED
self.maze.bullets.append(Bullet(
self.maze.surface, x, y,
2017-11-12 15:08:14 +01:00
atan2(self.maze.y - y, self.maze.x - x), 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
2017-11-09 09:22:39 +01:00
if self.next_strike > pygame.time.get_ticks(): 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)
directions.append(choice(CROSS))
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
2017-11-14 02:51:18 +01:00
def slash(self):
"""Return the enemy's close-range damage."""
if self.spin_queue:
d = self.maze.slashd - self.maze.length(*self.pos())
wound = d / self.maze.hero.R / self.spin_speed
if wound >= 0:
self.maze.hit(wound, self.color)
return wound
return 0.0
2017-11-14 02:51:18 +01:00
2017-11-12 15:08:14 +01:00
def draw(self):
"""Draw the enemy."""
radious = self.maze.distance/SQRT2 - self.awake*2
square = regpoly(4, radious, self.angle, *self.pos())
color = TANGO[self.color][int(self.wound)] if self.awake else FG_COLOR
fill_aapolygon(self.maze.surface, square, color)
def update(self):
2017-10-13 16:56:03 +02:00
"""Update the enemy."""
if self.awake:
self.spin_speed, old_speed = self.maze.fps / ENEMY_HP, self.spin_speed
2017-10-19 15:28:56 +02:00
self.spin_queue *= self.spin_speed / old_speed
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 abs(self.spin_queue) > 0.5:
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
2017-11-12 15:08:14 +01:00
self.draw()
2017-11-02 15:39:06 +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
2017-10-15 11:20:14 +02:00
def die(self):
"""Handle the enemy's death."""
2017-11-12 15:08:14 +01:00
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
2017-11-12 15:08:14 +01:00
else:
self.maze.map[self.x][self.y] = WALL
class Butter(Enemy):
"""Object representing an enemy of Butter type."""
def __init__(self, maze, x, y):
Enemy.__init__(self, maze, x, y, 'Butter')
class Orange(Enemy):
"""Object representing an enemy of Orange type."""
def __init__(self, maze, x, y):
Enemy.__init__(self, maze, x, y, 'Orange')
class Chocolate(Enemy):
"""Object representing an enemy of Chocolate type."""
def __init__(self, maze, x, y):
Enemy.__init__(self, maze, x, y, 'Chocolate')
class Chameleon(Enemy):
"""Object representing an enemy of Chameleon type.
Additional attributes:
visible (int): the tick until which the Chameleon is visible
"""
def __init__(self, maze, x, y):
Enemy.__init__(self, maze, x, y, 'Chameleon')
self.visible = 0
def wake(self):
"""Wake the Chameleon up if it can see the hero."""
if Enemy.wake(self) is True:
self.visible = pygame.time.get_ticks() + 1000//ENEMY_SPEED
def draw(self):
"""Draw the Chameleon."""
if not self.awake or pygame.time.get_ticks() <= self.visible:
Enemy.draw(self)
def hit(self, wound):
"""Handle the Chameleon when it's hit by a bullet."""
self.visible = pygame.time.get_ticks() + 1000//ENEMY_SPEED
self.wound += wound
class SkyBlue(Enemy):
"""Object representing an enemy of Sky Blue type."""
def __init__(self, maze, x, y):
Enemy.__init__(self, maze, x, y, 'SkyBlue')
class Plum(Enemy):
"""Object representing an enemy of Plum type."""
def __init__(self, maze, x, y):
Enemy.__init__(self, maze, x, y, 'Plum')
class ScarletRed(Enemy):
"""Object representing an enemy of Scarlet Red type."""
def __init__(self, maze, x, y):
Enemy.__init__(self, maze, x, y, 'ScarletRed')
2017-11-14 02:51:18 +01:00
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
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 = choices(maze.enemy_weights)
return getattr(modules[__name__], color)(maze, x, y)