2023-07-11 00:25:44 +00:00
|
|
|
import pygame
|
2023-09-27 18:03:37 +00:00
|
|
|
|
2023-11-29 10:53:30 +00:00
|
|
|
from configs.game.weapon_config import weapon_data
|
|
|
|
from configs.game.spell_config import magic_data
|
|
|
|
|
|
|
|
from .ui_settings import UI_FONT,\
|
|
|
|
UI_FONT_SIZE,\
|
|
|
|
HEALTH_BAR_WIDTH,\
|
|
|
|
HEALTH_COLOR,\
|
|
|
|
ENERGY_BAR_WIDTH,\
|
|
|
|
ENERGY_COLOR,\
|
|
|
|
BAR_HEIGHT,\
|
|
|
|
UI_BG_COLOR,\
|
|
|
|
UI_BORDER_COLOR_ACTIVE,\
|
|
|
|
UI_BORDER_COLOR,\
|
|
|
|
TEXT_COLOR,\
|
|
|
|
ITEM_BOX_SIZE
|
2023-09-27 18:03:37 +00:00
|
|
|
|
2023-07-11 00:25:44 +00:00
|
|
|
|
|
|
|
class UI:
|
|
|
|
def __init__(self):
|
2023-09-27 18:03:37 +00:00
|
|
|
|
2023-07-11 00:25:44 +00:00
|
|
|
# General info
|
|
|
|
self.display_surface = pygame.display.get_surface()
|
|
|
|
self.font = pygame.font.Font(UI_FONT, UI_FONT_SIZE)
|
2023-09-27 18:03:37 +00:00
|
|
|
|
2023-07-11 00:25:44 +00:00
|
|
|
# Bar setup
|
2023-09-27 18:03:37 +00:00
|
|
|
self.health_bar_rect = pygame.Rect(
|
|
|
|
10, 10, HEALTH_BAR_WIDTH, BAR_HEIGHT)
|
|
|
|
self.energy_bar_rect = pygame.Rect(
|
|
|
|
10, 34, ENERGY_BAR_WIDTH, BAR_HEIGHT)
|
|
|
|
|
2023-07-11 00:25:44 +00:00
|
|
|
# Convert weapon dictionary
|
|
|
|
self.weapon_graphics = []
|
|
|
|
for weapon in weapon_data.values():
|
|
|
|
path = weapon['graphic']
|
|
|
|
weapon = pygame.image.load(path).convert_alpha()
|
|
|
|
self.weapon_graphics.append(weapon)
|
2023-09-27 18:03:37 +00:00
|
|
|
|
2023-07-11 00:25:44 +00:00
|
|
|
# Convert weapon dictionary
|
|
|
|
self.magic_graphics = []
|
|
|
|
for spell in magic_data.values():
|
|
|
|
path = spell['graphic']
|
|
|
|
spell = pygame.image.load(path).convert_alpha()
|
|
|
|
self.magic_graphics.append(spell)
|
2023-09-27 18:03:37 +00:00
|
|
|
|
2023-07-11 00:25:44 +00:00
|
|
|
def show_bar(self, current_amount, max_amount, bg_rect, color):
|
2023-09-27 18:03:37 +00:00
|
|
|
|
2023-07-11 00:25:44 +00:00
|
|
|
# Draw background
|
|
|
|
pygame.draw.rect(self.display_surface, UI_BG_COLOR, bg_rect)
|
2023-09-27 18:03:37 +00:00
|
|
|
|
2023-07-11 00:25:44 +00:00
|
|
|
# Convert stat amount to pixels
|
|
|
|
ratio = current_amount / max_amount
|
|
|
|
current_width = bg_rect.width * ratio
|
|
|
|
current_rect = bg_rect.copy()
|
|
|
|
current_rect.width = current_width
|
2023-09-27 18:03:37 +00:00
|
|
|
|
2023-07-11 00:25:44 +00:00
|
|
|
# Draw stat bar
|
|
|
|
pygame.draw.rect(self.display_surface, color, current_rect)
|
|
|
|
pygame.draw.rect(self.display_surface, UI_BORDER_COLOR, bg_rect, 4)
|
2023-09-27 18:03:37 +00:00
|
|
|
|
2023-07-11 00:25:44 +00:00
|
|
|
def show_exp(self, exp):
|
|
|
|
if exp >= 0:
|
2023-09-27 18:03:37 +00:00
|
|
|
text_surf = self.font.render(
|
|
|
|
f"EXP: {str(int(exp))}", False, TEXT_COLOR)
|
2023-07-11 00:25:44 +00:00
|
|
|
x = self.display_surface.get_size()[0] - 20
|
|
|
|
y = self.display_surface.get_size()[1] - 20
|
2023-09-27 18:03:37 +00:00
|
|
|
text_rect = text_surf.get_rect(bottomright=(x, y))
|
|
|
|
|
|
|
|
pygame.draw.rect(self.display_surface, UI_BG_COLOR,
|
|
|
|
text_rect.inflate(10, 10))
|
2023-07-11 00:25:44 +00:00
|
|
|
self.display_surface.blit(text_surf, text_rect)
|
2023-09-27 18:03:37 +00:00
|
|
|
pygame.draw.rect(self.display_surface,
|
|
|
|
UI_BORDER_COLOR, text_rect.inflate(10, 10), 4)
|
2023-07-11 00:25:44 +00:00
|
|
|
else:
|
2023-11-29 10:53:30 +00:00
|
|
|
text_surf = self.font.render("OBSERVER", False, TEXT_COLOR)
|
2023-07-11 00:25:44 +00:00
|
|
|
x = self.display_surface.get_size()[0] - 20
|
|
|
|
y = self.display_surface.get_size()[1] - 20
|
2023-09-27 18:03:37 +00:00
|
|
|
text_rect = text_surf.get_rect(bottomright=(x, y))
|
|
|
|
|
|
|
|
pygame.draw.rect(self.display_surface, UI_BG_COLOR,
|
|
|
|
text_rect.inflate(10, 10))
|
2023-07-11 00:25:44 +00:00
|
|
|
self.display_surface.blit(text_surf, text_rect)
|
2023-09-27 18:03:37 +00:00
|
|
|
pygame.draw.rect(self.display_surface,
|
|
|
|
UI_BORDER_COLOR, text_rect.inflate(10, 10), 4)
|
|
|
|
|
2023-07-11 00:25:44 +00:00
|
|
|
def selection_box(self, left, top, has_rotated):
|
|
|
|
bg_rect = pygame.Rect(left, top, ITEM_BOX_SIZE, ITEM_BOX_SIZE)
|
|
|
|
pygame.draw.rect(self.display_surface, UI_BG_COLOR, bg_rect)
|
|
|
|
if not has_rotated:
|
2023-09-27 18:03:37 +00:00
|
|
|
pygame.draw.rect(self.display_surface,
|
|
|
|
UI_BORDER_COLOR_ACTIVE, bg_rect, 4)
|
2023-07-11 00:25:44 +00:00
|
|
|
else:
|
|
|
|
pygame.draw.rect(self.display_surface, UI_BORDER_COLOR, bg_rect, 4)
|
|
|
|
return bg_rect
|
2023-09-27 18:03:37 +00:00
|
|
|
|
2023-07-11 00:25:44 +00:00
|
|
|
def weapon_overlay(self, weapon_index, has_rotated):
|
|
|
|
bg_rect = self.selection_box(10, 630, has_rotated)
|
|
|
|
weapon_surf = self.weapon_graphics[weapon_index]
|
2023-09-27 18:03:37 +00:00
|
|
|
weapon_rect = weapon_surf.get_rect(center=bg_rect.center)
|
|
|
|
|
2023-07-11 00:25:44 +00:00
|
|
|
self.display_surface.blit(weapon_surf, weapon_rect)
|
2023-09-27 18:03:37 +00:00
|
|
|
|
2023-07-11 00:25:44 +00:00
|
|
|
def magic_overlay(self, magic_index, has_swaped):
|
2023-09-27 18:03:37 +00:00
|
|
|
bg_rect = self.selection_box(100, 630, has_swaped)
|
2023-07-11 00:25:44 +00:00
|
|
|
magic_surf = self.magic_graphics[magic_index]
|
2023-09-27 18:03:37 +00:00
|
|
|
magic_rect = magic_surf.get_rect(center=bg_rect.center)
|
|
|
|
|
2023-07-11 00:25:44 +00:00
|
|
|
self.display_surface.blit(magic_surf, magic_rect)
|
2023-09-27 18:03:37 +00:00
|
|
|
|
2023-12-14 17:28:45 +00:00
|
|
|
def display(self):
|
|
|
|
self.show_exp(-1)
|