Container light occlusion (#967)

Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
This commit is contained in:
L.E.D
2020-07-26 07:28:25 -04:00
committed by GitHub
parent d5e422f34e
commit 16d50f3ecf
10 changed files with 70 additions and 21 deletions

View File

@@ -27,7 +27,10 @@ namespace Robust.Client.GameObjects.Components.Containers
[ViewVariables] public IEntity Owner => Manager.Owner;
[ViewVariables] public bool Deleted { get; private set; }
[ViewVariables] public IReadOnlyList<IEntity> ContainedEntities => Entities;
[ViewVariables]
public bool ShowContents { get; set; }
[ViewVariables]
public bool OccludesLight { get; set; }
public bool CanInsert(IEntity toinsert)
{

View File

@@ -108,9 +108,8 @@ namespace Robust.Client.GameObjects.Components.Containers
}
// Add new containers and update existing contents.
foreach (var (id, contEnts) in cast.Containers)
foreach (var (id, data) in cast.Containers)
{
var (show, entities) = contEnts;
if (!_containers.TryGetValue(id, out var container))
{
@@ -119,13 +118,14 @@ namespace Robust.Client.GameObjects.Components.Containers
}
// sync show flag
container.ShowContents = show;
container.ShowContents = data.ShowContents;
container.OccludesLight = data.OccludesLight;
// Remove gone entities.
List<IEntity>? toRemove = null;
foreach (var entity in container.Entities)
{
if (!entities.Contains(entity.Uid))
if (!data.ContainedEntities.Contains(entity.Uid))
{
toRemove ??= new List<IEntity>();
toRemove.Add(entity);
@@ -141,7 +141,7 @@ namespace Robust.Client.GameObjects.Components.Containers
}
// Add new entities.
foreach (var uid in entities)
foreach (var uid in data.ContainedEntities)
{
var entity = Owner.EntityManager.GetEntity(uid);

View File

@@ -41,6 +41,9 @@ namespace Robust.Client.GameObjects
set => _enabled = value;
}
[ViewVariables(VVAccess.ReadWrite)]
public bool ContainerOccluded { get; set; }
/// <summary>
/// Determines if the light mask should automatically rotate with the entity. (like a flashlight)
/// </summary>

View File

@@ -55,21 +55,39 @@ namespace Robust.Client.GameObjects.EntitySystems
private static void UpdateEntity(IEntity entity)
{
if (!entity.TryGetComponent(out SpriteComponent sprite))
if (entity.TryGetComponent(out SpriteComponent sprite))
{
return;
sprite.ContainerOccluded = false;
// We have to recursively scan for containers upwards in case of nested containers.
var tempParent = entity;
while (ContainerHelpers.TryGetContainer(tempParent, out var container))
{
if (!container.ShowContents)
{
sprite.ContainerOccluded = true;
break;
}
tempParent = container.Owner;
}
}
sprite.ContainerOccluded = false;
// We have to recursively scan for containers upwards in case of nested containers.
var tempParent = entity;
while (ContainerHelpers.TryGetContainer(tempParent, out var container))
if (entity.TryGetComponent(out PointLightComponent light))
{
if (!container.ShowContents)
light.ContainerOccluded = false;
// We have to recursively scan for containers upwards in case of nested containers.
var tempParent = entity;
while (ContainerHelpers.TryGetContainer(tempParent, out var container))
{
sprite.ContainerOccluded = true;
return;
if (container.OccludesLight)
{
light.ContainerOccluded = true;
break;
}
tempParent = container.Owner;
}
}
}

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Buffers;
using OpenToolkit.Graphics.OpenGL4;
using Robust.Client.GameObjects;
@@ -356,6 +356,7 @@ namespace Robust.Client.Graphics.Clyde
for (var i = 0; i < count; i++)
{
var (component, lightPos) = lights[i];
var transform = component.Owner.Transform;
var circle = new Circle(lightPos, component.Radius);
@@ -461,7 +462,7 @@ namespace Robust.Client.Graphics.Clyde
{
var transform = component.Owner.Transform;
if (!component.Enabled)
if (!component.Enabled || component.ContainerOccluded)
{
continue;
}

View File

@@ -6,6 +6,7 @@ using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.Maths;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
@@ -91,6 +92,9 @@ namespace Robust.Server.GameObjects.Components.Container
[ViewVariables(VVAccess.ReadWrite)]
public bool ShowContents { get; set; }
[ViewVariables(VVAccess.ReadWrite)]
public bool OccludesLight { get; set; }
/// <summary>
/// DO NOT CALL THIS METHOD DIRECTLY!
/// You want <see cref="IContainerManager.MakeContainer{T}(string)" /> instead.

View File

@@ -268,7 +268,17 @@ namespace Robust.Server.GameObjects.Components.Container
return new ContainerManagerComponentState(
_allContainers.ToDictionary(
c => c.ID,
c => (c.ShowContents, c.ContainedEntities.Select(e => e.Uid).ToList())));
DataFor));
}
private static ContainerManagerComponentState.ContainerData DataFor(IContainer container)
{
return new ContainerManagerComponentState.ContainerData
{
ContainedEntities = container.ContainedEntities.Select(e => e.Uid).ToArray(),
ShowContents = container.ShowContents,
OccludesLight = container.OccludesLight
};
}
private struct ContainerPrototypeData : IExposeData

View File

@@ -1,4 +1,4 @@
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Maths;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
@@ -9,6 +9,7 @@ namespace Robust.Server.GameObjects
{
private Color _color;
private bool _enabled;
private bool _occluded;
private float _radius;
private Vector2 _offset;

View File

@@ -27,12 +27,20 @@ namespace Robust.Shared.GameObjects.Components.Containers
[Serializable, NetSerializable]
protected class ContainerManagerComponentState : ComponentState
{
public Dictionary<string,(bool, List<EntityUid>)> Containers { get; }
public Dictionary<string, ContainerData> Containers { get; }
public ContainerManagerComponentState(Dictionary<string, (bool, List<EntityUid>)> containers) : base(NetIDs.CONTAINER_MANAGER)
public ContainerManagerComponentState(Dictionary<string, ContainerData> containers) : base(NetIDs.CONTAINER_MANAGER)
{
Containers = containers;
}
[Serializable, NetSerializable]
public struct ContainerData
{
public bool ShowContents;
public bool OccludesLight;
public EntityUid[] ContainedEntities;
}
}
public IEnumerable<IContainer> GetAllContainers() => GetAllContainersImpl();

View File

@@ -53,6 +53,7 @@ namespace Robust.Shared.Interfaces.GameObjects.Components
/// things like glass display cases.
/// </summary>
bool ShowContents { get; set; }
bool OccludesLight { get; set; }
/// <summary>
/// Checks if the entity can be inserted into this container.