Skip to content
Snippets Groups Projects
Commit 3e34e7ad authored by Fabian Heinrich's avatar Fabian Heinrich
Browse files

View restriction in own method

parent 7995e9b9
No related branches found
No related tags found
1 merge request!62Resolve "Game Flow"
Pipeline #47167 passed
......@@ -149,92 +149,92 @@ class Visualizer:
grid_size,
)
if "view_restrictions" in state and state["view_restrictions"]:
self.draw_lightcones(screen, grid_size, width, height, state)
def draw_lightcones(self, screen, grid_size, width, height, state):
view_restrictions = state["view_restrictions"]
mask = pygame.Surface(screen.get_size(), pygame.SRCALPHA)
mask.fill((0, 0, 0, 0))
mask_color = (0, 0, 0, 0)
if "view_restrictions" in state and state["view_restrictions"]:
for idx, restriction in enumerate(state["view_restrictions"]):
# if idx != 0:
# break
# rotate direction vector in both direction with the angel
# draw 2 large rect which are rotated so that one edge is the viewing border
direction = pygame.math.Vector2(restriction["direction"])
pos = pygame.math.Vector2(restriction["position"])
angle = restriction["angle"] / 2
range = restriction["range"]
angle = min(angle, 180)
pos = pos * grid_size + pygame.math.Vector2(
[grid_size / 2, grid_size / 2]
for idx, restriction in enumerate(view_restrictions):
direction = pygame.math.Vector2(restriction["direction"])
pos = pygame.math.Vector2(restriction["position"])
angle = restriction["angle"] / 2
view_range = restriction["range"]
angle = min(angle, 180)
pos = pos * grid_size + pygame.math.Vector2([grid_size / 2, grid_size / 2])
rect_scale = max(width, height) * 2
# rect_scale = 2 * grid_size
left_beam = pos + (direction.rotate(angle) * rect_scale * 2)
right_beam = pos + (direction.rotate(-angle) * rect_scale * 2)
cone_mask = pygame.surface.Surface(screen.get_size(), pygame.SRCALPHA)
cone_mask.fill((255, 255, 255, 255))
offset_front = direction * grid_size * 0.7
if angle != 180:
shadow_cone_points = [
pos - offset_front,
left_beam - offset_front,
left_beam + (direction.rotate(90) * rect_scale),
pos
- (direction * rect_scale * 2)
+ (direction.rotate(90) * rect_scale),
pos
- (direction * rect_scale * 2)
+ (direction.rotate(-90) * rect_scale),
right_beam + (direction.rotate(-90) * rect_scale),
right_beam - offset_front,
]
light_cone_points = [pos - offset_front, left_beam, right_beam]
pygame.draw.polygon(
cone_mask,
mask_color,
shadow_cone_points,
)
rect_scale = max(width, height) * 2
# rect_scale = 2 * grid_size
left_beam = pos + (direction.rotate(angle) * rect_scale * 2)
right_beam = pos + (direction.rotate(-angle) * rect_scale * 2)
cone_mask = pygame.surface.Surface(screen.get_size(), pygame.SRCALPHA)
cone_mask.fill((255, 255, 255, 255))
offset_front = direction * grid_size * 0.7
if angle != 180:
pygame.draw.polygon(
cone_mask,
mask_color,
(
pos - offset_front,
left_beam - offset_front,
left_beam + (direction.rotate(90) * rect_scale),
pos
- (direction * rect_scale * 2)
+ (direction.rotate(90) * rect_scale),
pos
- (direction * rect_scale * 2)
+ (direction.rotate(-90) * rect_scale),
right_beam + (direction.rotate(-90) * rect_scale),
right_beam - offset_front,
),
)
if range:
n_circle_points = 40
start_vec = np.array(-direction * range)
points = (
np.array(create_polygon(n_circle_points, start_vec)) * grid_size
) + pos
circle_closed = np.concatenate([points, points[0:1]], axis=0)
corners = [
pos - (direction * rect_scale),
*circle_closed,
pos - (direction * rect_scale),
pos
- (direction * rect_scale)
+ (direction.rotate(90) * rect_scale),
pos
+ (direction * rect_scale)
+ (direction.rotate(90) * rect_scale),
pos
+ (direction * rect_scale)
+ (direction.rotate(-90) * rect_scale),
pos
- (direction * rect_scale)
+ (direction.rotate(-90) * rect_scale),
]
pygame.draw.polygon(cone_mask, mask_color, [*corners])
mask.blit(cone_mask, (0, 0), special_flags=pygame.BLEND_MAX)
screen.blit(
mask,
mask.get_rect(),
special_flags=pygame.BLEND_RGBA_MULT,
)
if view_range:
n_circle_points = 40
start_vec = np.array(-direction * view_range)
points = (
np.array(create_polygon(n_circle_points, start_vec)) * grid_size
) + pos
circle_closed = np.concatenate([points, points[0:1]], axis=0)
corners = [
pos - (direction * rect_scale),
*circle_closed,
pos - (direction * rect_scale),
pos
- (direction * rect_scale)
+ (direction.rotate(90) * rect_scale),
pos
+ (direction * rect_scale)
+ (direction.rotate(90) * rect_scale),
pos
+ (direction * rect_scale)
+ (direction.rotate(-90) * rect_scale),
pos
- (direction * rect_scale)
+ (direction.rotate(-90) * rect_scale),
]
pygame.draw.polygon(cone_mask, mask_color, corners)
mask.blit(cone_mask, (0, 0), special_flags=pygame.BLEND_MAX)
screen.blit(
mask,
mask.get_rect(),
special_flags=pygame.BLEND_RGBA_MULT,
)
def draw_background(
self, surface: pygame.Surface, width: int, height: int, grid_size: int
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment