Skip to content

Commit 27e2c2d

Browse files
author
Pyrograf
committed
Variable light range.
1 parent 24d2424 commit 27e2c2d

5 files changed

Lines changed: 67 additions & 34 deletions

File tree

lochpython/core/renderer.py

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from functools import reduce
44

55
import pygame.color
6+
from pygame.rect import Rect
67

78
from lochpython.core.debug import Debugger
89
from lochpython.core.settings import *
@@ -89,7 +90,6 @@ def remove_visible_object(cls, sprite_prop):
8990
if sprite_prop in cls.stack[dst_layer]:
9091
cls.stack[dst_layer].remove(sprite_prop)
9192

92-
9393
@classmethod
9494
def sort_order(cls):
9595
Debugger.print('Renderer sorting')
@@ -100,52 +100,68 @@ def render(cls):
100100
for layer in cls.stack:
101101
layer.attach_camera(cls.world.camera)
102102
layer.draw(MainRenderer.world_surface)
103+
103104
@classmethod
104105
@property
105106
def visible_objects_count(cls):
106107
return reduce(lambda a, b: a + len(b), cls.stack, 0)
107108

109+
108110
class SkyRenderer:
111+
POINT_LIGHT_PATH = 'data/lighting/point_light.png'
109112
camera = None
110113
sky_surface = None
111-
point_light_image = None
114+
point_light_images = []
112115
light_sources = []
113116
darknes = 0
117+
114118
@classmethod
115119
def attach_camera(cls, camera):
116120
cls.camera = camera
117121

122+
@classmethod
123+
def _load_point_light_graphics(cls, levels_count=8):
124+
src_image = pygame.image.load(cls.POINT_LIGHT_PATH)
125+
src_size, src_depth = (src_image.get_width(), src_image.get_height()), src_image.get_bitsize()
126+
inv_surface = pygame.Surface(src_size, depth=src_depth)
127+
inv_surface.fill(pygame.color.Color('White'))
128+
inv_surface.blit(src_image, (0, 0), special_flags=pygame.BLEND_RGB_SUB)
129+
130+
result = []
131+
min_size = POINT_LIGHT_MIN_IMAGE_SIZE
132+
levels_count = POINT_LIGHT_STRENGTH_LEVELS
133+
for level in range(levels_count - 1):
134+
scale_factor = level / levels_count
135+
img_width = int(min_size[0] + scale_factor * (src_size[0] - min_size[0]))
136+
img_height = int(min_size[1] + scale_factor * (src_size[1] - min_size[1]))
137+
result.append(pygame.transform.scale(inv_surface, (img_width, img_height)))
138+
result.append(inv_surface)
139+
return result
140+
118141
@classmethod
119142
def init(cls):
120143
width, height = MainRenderer.rendering_width, MainRenderer.rendering_height
121144
cls.sky_surface = pygame.Surface((width, height), depth=MainRenderer.world_surface.get_bitsize())
122-
#aww
123-
cls.point_light_image = pygame.image.load('data/lighting/point_light.png')
145+
cls.point_light_images = cls._load_point_light_graphics()
146+
# aww
124147
if not cls.light_sources:
125148
cls.light_sources = []
126149
# cls.light_sources.append()
127150
# print(cls.sky_surface)
128151

129-
130152
@classmethod
131153
def render(cls):
132-
cls.darknes = (math.sin(time.time())+1)/2
133-
# print(cls.darknes)
134-
channel_value = int(255 * cls.darknes)
135-
channel_value = min(215,max(30,channel_value))
136-
cls.sky_surface.fill((channel_value,channel_value,channel_value))
137-
138-
translation = sub_tuples_2D(cls.camera, (HALF_RENDERING_WIDTH, HALF_RENDERING_HEIGHT))
139-
translation = add_tuples_2D(translation, (cls.point_light_image.get_width()//2, cls.point_light_image.get_height()//2))
154+
channel_value = 180
155+
cls.sky_surface.fill((channel_value, channel_value, channel_value))
140156
for ls_prop in cls.light_sources:
141-
light_pos = sub_tuples_2D(ls_prop.position, translation)
142-
143-
# image_data = sprite_prop.image_data
144-
# sprite_clip_rect = sprite_prop.clip_rect
145-
# scaled_image = pygame.transform.scale(sprite_image, (w,h))
146-
147-
cls.sky_surface.blit(cls.point_light_image, light_pos)
148-
MainRenderer.world_surface.blit(cls.sky_surface, (0,0), special_flags=pygame.BLEND_RGBA_SUB)
157+
light_strength = ls_prop.strength
158+
point_light_image = cls.point_light_images[light_strength]
159+
point_light_img_size = (point_light_image.get_width() // 2, point_light_image.get_height() // 2)
160+
light_pos = sub_tuples_2D(ls_prop.position, point_light_img_size)
161+
light_pos = sub_tuples_2D(light_pos, cls.camera)
162+
light_pos = add_tuples_2D(light_pos, (HALF_RENDERING_WIDTH, HALF_RENDERING_HEIGHT))
163+
cls.sky_surface.blit(point_light_image, light_pos, special_flags=pygame.BLEND_RGB_SUB)
164+
MainRenderer.world_surface.blit(cls.sky_surface, (0, 0), special_flags=pygame.BLEND_RGB_SUB)
149165

150166
@classmethod
151167
def add_point_light(cls, light):

lochpython/core/settings.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
FPS = 30
1313
TILESIZE = 20
14-
DEBUG = False
14+
DEBUG = True
1515
DEBUG_VISIBLE_OBJECTS = False
16-
DEBUG_COLLISION_BLOCKS = True
16+
DEBUG_COLLISION_BLOCKS = False
1717
COLLISION_RANGE = TILESIZE
1818

1919
GLOBAL_COOLDOWN = 500
@@ -28,6 +28,13 @@
2828

2929
"""Initial"""
3030

31+
"""Ligting"""
32+
POINT_LIGHT_MIN_STRENGTH = 0
33+
POINT_LIGHT_MAX_STRENGTH = 7
34+
POINT_LIGHT_STRENGTH_LEVELS = POINT_LIGHT_MAX_STRENGTH - POINT_LIGHT_MIN_STRENGTH + 1
35+
POINT_LIGHT_MIN_IMAGE_SIZE = (16, 16)
36+
37+
3138

3239
class Init:
3340
screen = None

lochpython/files/content.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ def from_tsx_file(cls, abs_tsx_filepath):
7676

7777
class TileProperties:
7878
class LightEffect:
79-
def __init__(self, strength=0, relative_position = (0,0)):
79+
def __init__(self, strength=0, relative_position = (0,0), active=False):
8080
self.strength = strength
8181
self.relative_position = relative_position
82+
self.active = active
8283
class KeyFrame:
8384
def __init__(self, tile_id, interval):
8485
self.tile_id = tile_id
@@ -147,7 +148,8 @@ def get_property_by_attr_name(properties, attr_name, default=None):
147148
light_strength = int(light_strength)
148149
source_x = int(get_property_by_attr_name(other_properties, 'source_x', 0))
149150
source_y = int(get_property_by_attr_name(other_properties, 'source_y', 0))
150-
tile_data.light_effect = cls.LightEffect(light_strength, (source_x, source_y))
151+
active = int(get_property_by_attr_name(other_properties, 'active', False))
152+
tile_data.light_effect = cls.LightEffect(light_strength, (source_x, source_y), active)
151153

152154

153155
tiles_data[tile_id] = tile_data

lochpython/objects/property.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from abc import ABC, abstractmethod
99

1010
import core.timers
11-
from core.settings import TILESIZE, DEBUG_VISIBLE_OBJECTS, DEBUG_COLLISION_BLOCKS
11+
from core.settings import TILESIZE, DEBUG_VISIBLE_OBJECTS, DEBUG_COLLISION_BLOCKS, POINT_LIGHT_MIN_STRENGTH, POINT_LIGHT_MAX_STRENGTH
1212

1313
from core.debug import Debugger
1414
from core.renderer import WorldRenderer, SkyRenderer
@@ -38,6 +38,7 @@ def render(self, *args, **kwargs):
3838

3939

4040
class LightSourceProperty(UpdateProperty):
41+
4142
def __init__(self, parent_rect, relative_position, strength, active=True):
4243
self.parent_rect = parent_rect
4344
self.relative_position = relative_position
@@ -48,6 +49,14 @@ def __init__(self, parent_rect, relative_position, strength, active=True):
4849
self.deviation_timer.attach(self.rand_deviation)
4950
self.deviation = (0, 0)
5051

52+
@property
53+
def strength(self):
54+
return self._strength
55+
56+
@strength.setter
57+
def strength(self, strngth):
58+
self._strength = min(max(POINT_LIGHT_MIN_STRENGTH, strngth), POINT_LIGHT_MAX_STRENGTH)
59+
5160
@property
5261
def active(self):
5362
return self._active
@@ -61,7 +70,7 @@ def active(self, ac):
6170
SkyRenderer.remove_point_light(self)
6271

6372
def rand_deviation(self):
64-
self.deviation = randint(-1, 1), randint(-1,1)
73+
self.deviation = randint(-1, 1), randint(-1, 1)
6574

6675
def update(self, *args, **kwargs):
6776
if self._active:

lochpython/world/worldloader.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ def __init__(self, world):
1313

1414
def load(self, position):
1515
mock_tileset_data = loader.mock.tileset_data()
16-
player_sprite = p.SpriteProperty(mock_tileset_data, self.world, position, visible=False, dst_layer=1) #todo nie dziala visible
16+
player_sprite = p.SpriteProperty(mock_tileset_data, self.world, position, visible=False,
17+
dst_layer=1) # todo nie dziala visible
1718

1819
player_coll = p.CollisionProperty(player_sprite.rect, self.world)
1920
player_moving = p.MovingProperty(player_coll, self.world)
@@ -41,7 +42,7 @@ def new_tile(self, global_index, position):
4142
gameobject = GameObject()
4243

4344
# shilft large objects
44-
pos_x = position[0]# - (tileset_data.tile_size[0] - TILESIZE)
45+
pos_x = position[0] # - (tileset_data.tile_size[0] - TILESIZE)
4546
pos_y = position[1] - (tileset_data.tile_size[1] - TILESIZE)
4647
position = (pos_x, pos_y)
4748

@@ -74,14 +75,12 @@ def new_tile(self, global_index, position):
7475
anim_prop.keyframes.append(keyframe)
7576
anim_prop.active = True
7677
anim_prop.attach_to_controller(global_controllers.get_controller(frames_count, frames_interval))
77-
gameobject.with_animation(anim_prop) #todo
78+
gameobject.with_animation(anim_prop) # todo
7879

80+
# light
7981
if tile_properties.has_light_effect():
8082
le = tile_properties.light_effect
81-
# def __init__(self, parent_rect, relative_position, strength, active=True):
82-
# ls = LightSource(le.position, le.strength)
83-
light_prop = p.LightSourceProperty(sprite_property.rect, le.relative_position, le.strength)
84-
light_prop.active = True
83+
light_prop = p.LightSourceProperty(sprite_property.rect, le.relative_position, le.strength, le.active)
8584
gameobject.with_light_source(light_prop)
8685

8786
self.world.add_game_object(gameobject, dst)

0 commit comments

Comments
 (0)