2022-04-11 17:51:46 +02:00
|
|
|
import pygame
|
2022-04-24 22:06:20 +02:00
|
|
|
from common.constants import BAR_ANIMATION_SPEED, GRID_CELL_SIZE
|
|
|
|
from common.colors import FONT_DARK, ORANGE, WHITE, RED, GREEN, BLACK
|
2022-04-11 17:51:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
class HealthBar:
|
2022-04-24 22:06:20 +02:00
|
|
|
def __init__(self, screen, rect, current_hp, max_hp, x=None, y=None, width=None, height=None):
|
2022-04-11 17:51:46 +02:00
|
|
|
self.rect = rect
|
2022-04-24 22:06:20 +02:00
|
|
|
self.screen = screen
|
2022-04-11 17:51:46 +02:00
|
|
|
self.current_hp = current_hp
|
|
|
|
self.target_hp = current_hp
|
|
|
|
self.max_hp = max_hp
|
2022-04-24 22:06:20 +02:00
|
|
|
self.width = width
|
|
|
|
self.height = height
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
|
|
|
|
if self.width is None:
|
|
|
|
self.width = int(GRID_CELL_SIZE * 0.9)
|
|
|
|
if self.height is None:
|
|
|
|
self.height = int(GRID_CELL_SIZE * 0.05)
|
|
|
|
|
2022-04-11 17:51:46 +02:00
|
|
|
self.health_ratio = self.max_hp/self.width
|
|
|
|
|
2022-04-24 22:06:20 +02:00
|
|
|
if self.x is None:
|
|
|
|
self.x = int(GRID_CELL_SIZE * 0.1) + self.rect.x
|
|
|
|
if self.y is None:
|
|
|
|
self.y = int(GRID_CELL_SIZE/2) + self.rect.y
|
|
|
|
|
2022-04-11 17:51:46 +02:00
|
|
|
def update(self):
|
2022-04-24 22:06:20 +02:00
|
|
|
self.show()
|
2022-04-11 17:51:46 +02:00
|
|
|
|
|
|
|
def take_dmg(self, dmg_taken):
|
|
|
|
if self.target_hp > 0:
|
|
|
|
self.target_hp -= dmg_taken
|
|
|
|
elif self.target_hp < 0:
|
|
|
|
self.target_hp = 0
|
|
|
|
|
|
|
|
def heal(self, amount):
|
|
|
|
if self.target_hp < self.max_hp:
|
|
|
|
self.target_hp += amount
|
|
|
|
elif self.target_hp > self.max_hp:
|
|
|
|
self.target_hp = self.max_hp
|
|
|
|
|
2022-04-24 22:06:20 +02:00
|
|
|
def show(self):
|
|
|
|
pygame.Surface.fill(self.screen, BLACK, (self.x-1, self.y-1, self.width+2, self.height+2))
|
|
|
|
pygame.Surface.fill(self.screen, RED, (self.x, self.y, self.width, self.height))
|
|
|
|
pygame.Surface.fill(self.screen, GREEN, (self.x, self.y, int(self.target_hp * self.health_ratio)-1, self.height))
|
2022-04-11 17:51:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|