Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion game_engine/core/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ class Light:
`render`
"""

def __init__(self, size) -> None:
def __init__(self, size, light_color=(255, 255, 255)) -> None:
self.width, self.height = size
self.light_color = light_color
self.darkness = pygame.Surface(size, pygame.SRCALPHA)
try:
image = pygame.image.load("../game_engine/ui/images/light.png").convert_alpha()
Expand Down Expand Up @@ -232,6 +233,7 @@ def render(self, light_pos: tuple, items: list) -> pygame.Surface:
mask_surf = pygame.transform.smoothscale(small, (self.width, self.height))

light_cut = self.light_image.copy()
light_cut.fill(self.light_color, special_flags=pygame.BLEND_RGBA_MULT)
light_cut.blit(mask_surf, (0, 0), special_flags=pygame.BLEND_RGBA_MULT)

self.darkness.fill((0, 0, 0, 0))
Expand Down
5 changes: 4 additions & 1 deletion game_engine/items/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,11 @@ def wrapper(tiles):
pass

if self.lights:
light_color = self.lights.get("RGB", (255, 255, 255))
if self.light_system is None:
self.light_system = Light((self.lights["size"], self.lights["size"]))
self.light_system = Light((self.lights["size"], self.lights["size"]), light_color)
else:
self.light_system.light_color = light_color

l_c_x, l_c_y = self.lights["coords"]
size_mid = self.lights["size"] // 2
Expand Down
73 changes: 73 additions & 0 deletions tests/test_light.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# ruff: noqa: E402,I001
import os


os.environ.setdefault("SDL_VIDEODRIVER", "dummy")

import pygame

from game_engine.core.light import Light


def make_surface(size=(8, 8), color=(255, 255, 255, 255)):
surface = pygame.Surface(size, pygame.SRCALPHA)
surface.fill(color)
return surface


def test_render_tints_light_with_light_color(monkeypatch):

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good for testing case, I appreciate it...

pygame.display.init()
pygame.display.set_mode((1, 1))
monkeypatch.setattr(pygame.image, "load", lambda path: make_surface())

light = Light((8, 8), light_color=(255, 0, 0))
light.visibility_polygon = lambda light_pos, segments: [(0, 0), (7, 0), (7, 7), (0, 7)]

result = light.render((0, 0), [((0, 0), make_surface((1, 1)))])

assert result.get_at((4, 4)) == (255, 0, 0, 255)


def test_template_uses_configured_light_rgb(monkeypatch, tmp_path):
pygame.display.init()
pygame.display.set_mode((1, 1))
from game_engine.items import template

items_dir = tmp_path / "items"
items_dir.mkdir()
(items_dir / "info.json").write_text(
"""
{
"streetLight": {
"sizes": [8, 8],
"coords": [10, 20],
"health": 100,
"scale": 1,
"animations": [],
"image": "street-light.png",
"lights": {"coords": [4, 4], "size": 16, "RGB": [150, 30, 10]}
}
}
""",
encoding="utf-8",
)

created_lights = []

class FakeLight:
def __init__(self, size, light_color=(255, 255, 255)):
self.light_color = tuple(light_color)
created_lights.append((size, self.light_color))

def render(self, light_pos, items):
return make_surface((16, 16), (*self.light_color, 255))

monkeypatch.chdir(tmp_path)
monkeypatch.setattr(template.pygame.image, "load", lambda path: make_surface())
monkeypatch.setattr(template, "Light", FakeLight)
monkeypatch.setattr(template, "ITEMS", {})

item = template.Temp("streetLight")
item.decorate(lambda tiles: None)({})

assert created_lights == [((16, 16), (150, 30, 10))]
Loading