import pygame
from ..utils.constants import SCREEN_WIDTH
from ..utils.localization import localization_manager
[docs]
class HappinessGraph:
"""A UI component to display villager happiness over time."""
def __init__(self, width=200, height=100):
"""
Initialize the happiness graph.
Args:
width (int): The width of the graph surface.
height (int): The height of the graph surface.
"""
[docs]
self.x = SCREEN_WIDTH - self.width - 10
[docs]
self.y = 120 # Positioned below the stability graph
[docs]
self.bg_color = (20, 20, 20, 180) # Semi-transparent background
[docs]
self.font = pygame.font.Font(None, 18)
[docs]
self.surface = pygame.Surface((self.width, self.height), pygame.SRCALPHA)
[docs]
def render(self, screen, history):
"""
Render the happiness graph onto the screen.
Args:
screen: The main pygame screen surface.
history (list): A list of recent happiness values (0.0 to 1.0).
"""
self.surface.fill(self.bg_color)
pygame.draw.rect(self.surface, (100, 100, 100), self.surface.get_rect(), 1)
if history:
# Determine line color based on the most recent happiness value
current_happiness = history[-1]
if current_happiness < 0.4:
line_color = (255, 0, 0) # Red for unhappy
elif current_happiness < 0.6:
line_color = (255, 255, 0) # Yellow for neutral
else:
line_color = (0, 255, 0) # Green for happy
if len(history) > 1:
points = []
num_points = len(history)
if num_points > 1:
for i, happiness_value in enumerate(history):
px = int((i / (num_points - 1)) * self.width)
# Invert y-axis for drawing
py = int(self.height - (happiness_value * self.height))
points.append((px, py))
if len(points) > 1:
pygame.draw.lines(self.surface, line_color, False, points, 2)
# Draw title
title_text = self.font.render(localization_manager.get('happiness_graph_title', default="Villager Happiness"), True, (255, 255, 255))
self.surface.blit(title_text, (5, 5))
screen.blit(self.surface, (self.x, self.y))