brutalmaze/brutalmaze/characters.py

160 lines
5.8 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
__doc__ = 'brutalmaze module for hero and enemy classes'
2017-10-12 15:29:55 +02:00
from collections import deque
2017-10-19 15:28:56 +02:00
from math import atan2, sin, pi
2017-10-13 16:56:03 +02:00
from random import shuffle
2017-10-12 15:29:55 +02:00
import pygame
from .constants import *
from .utils import round2, randsign, regpoly, fill_aapolygon, pos, sign
2017-10-13 16:56:03 +02:00
2017-10-12 15:29:55 +02:00
class Hero:
"""Object representing the hero."""
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']
self.R = int((w * h / sin(pi*2/3) / 624) ** 0.5)
self.spin_speed = round2(fps / len(self.color))
2017-10-12 15:29:55 +02:00
self.spin_queue, self.slashing = deque(), False
2017-10-19 10:24:56 +02:00
self.wound = 0.0
2017-10-12 15:29:55 +02:00
2017-10-15 11:20:14 +02:00
def get_color(self):
"""Return the color of the hero based on the amount of wounds."""
return self.color[int(self.wound)]
2017-10-12 15:29:55 +02:00
def slash(self, hold=False):
"""Spin the hero. If the button is hold, delay before continue
each spin.
"""
if self.slashing and not self.spin_queue:
2017-10-19 10:24:56 +02:00
if hold: self.spin_queue.extend([0] * (self.spin_speed >> 1))
self.spin_queue.extend([randsign()] * self.spin_speed)
2017-10-12 15:29:55 +02:00
2017-10-19 15:28:56 +02:00
def draw(self):
2017-10-12 15:29:55 +02:00
"""Draw the hero."""
2017-10-13 16:56:03 +02:00
trigon = regpoly(3, self.R, self.angle, self.x, self.y)
2017-10-19 15:28:56 +02:00
fill_aapolygon(self.surface, trigon, self.get_color())
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."""
self.spin_speed = round2(fps / (len(self.color)-self.wound))
2017-10-19 15:28:56 +02:00
self.wound -= HEAL_SPEED / len(self.color) / self.spin_speed
2017-10-15 11:20:14 +02:00
if self.wound < 0: self.wound = 0.0
2017-10-12 15:29:55 +02:00
self.slash(hold=True)
direction = self.spin_queue.popleft() if self.spin_queue else 0
if direction:
2017-10-19 10:24:56 +02:00
self.angle += direction * pi * 2 / 3 / self.spin_speed
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)
self.draw()
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:
"""Object representing an enemy."""
2017-10-19 10:24:56 +02:00
def __init__(self, surface, fps, maze, kind, x, y):
2017-10-13 16:56:03 +02:00
self.surface, self.maze = surface, maze
self.angle, self.color = pi / 4, TANGO[kind]
2017-10-13 16:56:03 +02:00
self.x, self.y = x, y
self.maze[x][y] = ENEMY
2017-10-12 15:29:55 +02:00
2017-10-13 16:56:03 +02:00
self.awake = False
2017-10-19 10:24:56 +02:00
self.move_speed = fps / MOVE_SPEED
2017-10-13 16:56:03 +02:00
self.offsetx = self.offsety = 0
2017-10-19 15:28:56 +02:00
self.spin_speed = fps / len(self.color)
self.spin_queue = self.wound = 0.0
2017-10-12 15:29:55 +02:00
2017-10-15 11:20:14 +02:00
def pos(self, distance, middlex, middley):
"""Return coordinate of the center of the enemy."""
2017-10-13 10:10:16 +02:00
x, y = pos(self.x, self.y, distance, middlex, middley)
2017-10-19 10:24:56 +02:00
step = distance / self.move_speed
2017-10-15 11:20:14 +02:00
return x + self.offsetx*step, y + self.offsety*step
2017-10-19 15:28:56 +02:00
def draw(self, distance, middlex, middley):
2017-10-15 11:20:14 +02:00
"""Draw the enemy, given distance between grids and the middle grid."""
2017-10-19 10:24:56 +02:00
radious = distance/SQRT2 - self.awake*2
square = regpoly(4, radious, self.angle,
2017-10-15 11:20:14 +02:00
*self.pos(distance, middlex, middley))
2017-10-19 15:28:56 +02:00
color = self.color[int(self.wound)] if self.awake else FG_COLOR
2017-10-15 11:20:14 +02:00
fill_aapolygon(self.surface, square, color)
2017-10-12 15:29:55 +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[self.x][self.y] = ENEMY
2017-10-12 15:29:55 +02:00
2017-10-19 10:24:56 +02:00
def move(self, fps):
2017-10-13 16:56:03 +02:00
"""Handle the movement of the enemy.
Return True if it moved, False otherwise.
"""
if self.offsetx:
self.offsetx -= sign(self.offsetx)
return True
if self.offsety:
self.offsety -= sign(self.offsety)
return True
2017-10-19 10:24:56 +02:00
self.move_speed = fps / MOVE_SPEED
2017-10-13 16:56:03 +02:00
directions = [(sign(MIDDLE - self.x), 0), (0, sign(MIDDLE - self.y))]
shuffle(directions)
for x, y in directions:
if (x or y) and self.maze[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[self.x][self.y] = EMPTY
2017-10-13 16:56:03 +02:00
self.place(x, y)
return True
return False
2017-10-19 10:24:56 +02:00
def update(self, fps, distance, middlex, middley):
2017-10-13 16:56:03 +02:00
"""Update the enemy."""
if self.awake:
2017-10-19 15:28:56 +02:00
self.spin_speed, old_speed = fps / len(self.color), self.spin_speed
self.spin_queue *= self.spin_speed / old_speed
2017-10-19 10:24:56 +02:00
if not self.spin_queue and not self.move(fps):
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-10-19 15:28:56 +02:00
self.draw(distance, middlex, middley)
2017-10-15 11:20:14 +02:00
def die(self):
"""Kill the enemy."""
self.maze[self.x][self.y] = EMPTY if self.awake else WALL