Actual lockers (#195)

Adds storing entities into lockers the way we all know and love.
Relies on an implementation of ITileDefinition in https://github.com/space-wizards/space-station-14/pull/193 (just like origin/master)
#191
This commit is contained in:
PrPleGoo
2019-04-17 23:26:00 +02:00
committed by Pieter-Jan Briers
parent 1fe24eeb12
commit 903961771b
19 changed files with 272 additions and 41 deletions

View File

@@ -0,0 +1,160 @@
using Content.Server.GameObjects.EntitySystems;
using Robust.Server.GameObjects.Components.Container;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.Interfaces.Network;
using Robust.Shared.Maths;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
using System.Linq;
namespace Content.Server.GameObjects.Components
{
public class EntityStorageComponent : Component, IAttackHand
{
public override string Name => "EntityStorage";
private ServerStorageComponent StorageComponent;
private int StorageCapacityMax;
private bool IsCollidableWhenOpen;
private Container Contents;
private IEntityQuery entityQuery;
public override void Initialize()
{
base.Initialize();
Contents = ContainerManagerComponent.Ensure<Container>($"{typeof(EntityStorageComponent).FullName}{Owner.Uid.ToString()}", Owner);
StorageComponent = Owner.AddComponent<ServerStorageComponent>();
StorageComponent.Initialize();
entityQuery = new IntersectingEntityQuery(Owner);
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref StorageCapacityMax, "Capacity", 30);
serializer.DataField(ref IsCollidableWhenOpen, "IsCollidableWhenOpen", false);
}
[ViewVariables(VVAccess.ReadWrite)]
public bool Open
{
get => StorageComponent.Open;
set => StorageComponent.Open = value;
}
public bool AttackHand(AttackHandEventArgs eventArgs)
{
if (Open)
{
CloseStorage();
}
else
{
OpenStorage();
}
return true;
}
private void CloseStorage()
{
Open = false;
var entities = Owner.EntityManager.GetEntities(entityQuery);
int count = 0;
foreach (var entity in entities)
{
if (!AddToContents(entity))
{
continue;
}
count++;
if (count >= StorageCapacityMax)
{
break;
}
}
ModifyComponents();
}
private void OpenStorage()
{
Open = true;
EmptyContents();
ModifyComponents();
}
private void ModifyComponents()
{
if (Owner.TryGetComponent<ICollidableComponent>(out var collidableComponent))
{
collidableComponent.CollisionEnabled = IsCollidableWhenOpen || !Open;
}
if (Owner.TryGetComponent<PlaceableSurfaceComponent>(out var placeableSurfaceComponent))
{
placeableSurfaceComponent.IsPlaceable = Open;
}
}
private bool AddToContents(IEntity entity)
{
var collidableComponent = Owner.GetComponent<ICollidableComponent>();
if(entity.TryGetComponent<ICollidableComponent>(out var entityCollidableComponent))
{
if(collidableComponent.WorldAABB.Size.X < entityCollidableComponent.WorldAABB.Size.X
|| collidableComponent.WorldAABB.Size.Y < entityCollidableComponent.WorldAABB.Size.Y)
{
return false;
}
if (collidableComponent.WorldAABB.Left > entityCollidableComponent.WorldAABB.Left)
{
entity.Transform.WorldPosition += new Vector2(collidableComponent.WorldAABB.Left - entityCollidableComponent.WorldAABB.Left, 0);
}
else if (collidableComponent.WorldAABB.Right < entityCollidableComponent.WorldAABB.Right)
{
entity.Transform.WorldPosition += new Vector2(collidableComponent.WorldAABB.Right - entityCollidableComponent.WorldAABB.Right, 0);
}
if (collidableComponent.WorldAABB.Bottom > entityCollidableComponent.WorldAABB.Bottom)
{
entity.Transform.WorldPosition += new Vector2(0, collidableComponent.WorldAABB.Bottom - entityCollidableComponent.WorldAABB.Bottom);
}
else if (collidableComponent.WorldAABB.Top < entityCollidableComponent.WorldAABB.Top)
{
entity.Transform.WorldPosition += new Vector2(0, collidableComponent.WorldAABB.Top - entityCollidableComponent.WorldAABB.Top);
}
}
if (Contents.CanInsert(entity))
{
Contents.Insert(entity);
return true;
}
return false;
}
private void EmptyContents()
{
while (Contents.ContainedEntities.Count > 0 )
{
var containedEntity = Contents.ContainedEntities.First();
Contents.Remove(containedEntity);
}
}
public override void HandleMessage(ComponentMessage message, INetChannel netChannel = null, IComponent component = null)
{
base.HandleMessage(message, netChannel, component);
switch (message)
{
case RelayMovementEntityMessage msg:
if(msg.Entity.TryGetComponent<HandsComponent>(out var handsComponent))
{
OpenStorage();
}
break;
}
}
}
}

View File

@@ -248,6 +248,10 @@ namespace Content.Server.GameObjects
// TODO: The item should be dropped to the container our owner is in, if any.
item.Owner.Transform.GridPosition = Owner.Transform.GridPosition;
if (item.Owner.TryGetComponent<SpriteComponent>(out var spriteComponent))
{
spriteComponent.RenderOrder = item.Owner.EntityManager.CurrentTick.Value;
}
Dirty();
return true;

View File

@@ -6,10 +6,13 @@ using Content.Server.GameObjects.EntitySystems;
using Robust.Shared.GameObjects;
using System;
using Content.Shared.GameObjects.Components.Items;
using Content.Server.GameObjects.Components;
using Robust.Server.GameObjects;
using Robust.Shared.Maths;
namespace Content.Server.GameObjects
{
public class ItemComponent : StoreableComponent, IAttackHand
public class ItemComponent : StoreableComponent, IAttackHand, IAfterAttack
{
public override string Name => "Item";
public override uint? NetID => ContentNetIDs.ITEM;
@@ -87,5 +90,38 @@ namespace Content.Server.GameObjects
{
return new ItemComponentState(EquippedPrefix);
}
public void AfterAttack(AfterAttackEventArgs eventArgs)
{
if (!eventArgs.User.TryGetComponent<HandsComponent>(out var handComponent))
{
return;
}
if (!eventArgs.Attacked.TryGetComponent<PlaceableSurfaceComponent>(out var placeableSurfaceComponent))
{
return;
}
handComponent.Drop(handComponent.ActiveIndex);
Owner.Transform.WorldPosition = eventArgs.ClickLocation.Position;
return;
}
public void Fumble()
{
if (Owner.TryGetComponent<PhysicsComponent>(out var physicsComponent))
{
physicsComponent.LinearVelocity += RandomOffset();
}
}
private Vector2 RandomOffset()
{
return new Vector2(RandomOffset(), RandomOffset());
float RandomOffset()
{
var size = 15.0F;
return (new Random().NextFloat() * size) - size / 2;
}
}
}
}

View File

@@ -17,6 +17,7 @@ using System.Collections.Generic;
using Content.Shared.Interfaces;
using Robust.Shared.GameObjects.EntitySystemMessages;
using Robust.Shared.ViewVariables;
using Content.Server.GameObjects.Components;
namespace Content.Server.GameObjects
{
@@ -131,13 +132,20 @@ namespace Content.Server.GameObjects
/// <param name="user"></param>
/// <param name="attackwith"></param>
/// <returns></returns>
bool IAttackBy.AttackBy(AttackByEventArgs eventArgs)
public bool AttackBy(AttackByEventArgs eventArgs)
{
_ensureInitialCalculated();
Logger.DebugS("Storage", "Storage (UID {0}) attacked by user (UID {1}) with entity (UID {2}).", Owner.Uid, eventArgs.User.Uid, eventArgs.AttackWith.Uid);
if (!eventArgs.User.TryGetComponent(out HandsComponent hands))
if(Owner.TryGetComponent<PlaceableSurfaceComponent>(out var placeableSurfaceComponent))
{
return false;
}
if (!eventArgs.User.TryGetComponent(out HandsComponent hands))
{
return false;
}
//Check that we can drop the item from our hands first otherwise we obviously cant put it inside
if (CanInsert(hands.GetActiveHand.Owner) && hands.Drop(hands.ActiveIndex))
@@ -324,10 +332,5 @@ namespace Content.Server.GameObjects
_storageInitialCalculated = true;
}
public bool Attackby(AttackByEventArgs eventArgs)
{
throw new System.NotImplementedException();
}
}
}

View File

@@ -0,0 +1,20 @@
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
namespace Content.Server.GameObjects.Components
{
public class PlaceableSurfaceComponent : Component
{
public override string Name => "PlaceableSurface";
private bool _isPlaceable;
public bool IsPlaceable { get => _isPlaceable; set => _isPlaceable = value; }
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _isPlaceable, "IsPlaceable", true);
}
}
}

View File

@@ -2,6 +2,7 @@
using Content.Server.GameObjects.Components.Projectiles;
using Content.Shared.GameObjects;
using Content.Shared.Physics;
using Robust.Server.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.GameObjects.Components;
@@ -25,9 +26,10 @@ namespace Content.Server.GameObjects.Components
// after impacting the first object.
// For realism this should actually be changed when the velocity of the object is less than a threshold.
// This would allow ricochets off walls, and weird gravity effects from slowing the object.
if (collidedwith.Count > 0 && Owner.TryGetComponent(out ICollidableComponent body))
if (collidedwith.Count > 0 && Owner.TryGetComponent(out CollidableComponent body))
{
body.CollisionMask &= (int) ~CollisionGroup.Mob;
body.CollisionMask &= (int)~CollisionGroup.Mob;
body.IsScrapingFloor = true;
// KYS, your job is finished.
Owner.RemoveComponent<ThrownItemComponent>();

View File

@@ -16,6 +16,7 @@ using Robust.Shared.GameObjects;
using Content.Server.GameObjects.EntitySystems;
using Content.Server.GameObjects.Components.Power;
using Content.Shared.Interfaces;
using Content.Shared.Physics;
namespace Content.Server.GameObjects.Components.Weapon.Ranged.Hitscan
{
@@ -84,7 +85,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Hitscan
var userPosition = user.Transform.WorldPosition; //Remember world positions are ephemeral and can only be used instantaneously
var angle = new Angle(clickLocation.Position - userPosition);
var ray = new Ray(userPosition, angle.ToVec(), 0); //TODO set the CollsionMask for this ray.
var ray = new Ray(userPosition, angle.ToVec(), (int)(CollisionGroup.Grid | CollisionGroup.Mob));
var rayCastResults = IoCManager.Resolve<IPhysicsManager>().IntersectRay(ray, MaxLength,
Owner.Transform.GetMapTransform().Owner);

View File

@@ -109,22 +109,16 @@ namespace Content.Server.GameObjects.EntitySystems
{
var ent = ((IPlayerSession) session).AttachedEntity;
if(ent == null || !ent.IsValid())
if (ent == null || !ent.IsValid())
{
return;
}
if (!ent.TryGetComponent(out HandsComponent handsComp))
{
return;
var transform = ent.Transform;
if (transform.GridPosition.InRange(coords, InteractionSystem.INTERACTION_RANGE))
{
handsComp.Drop(handsComp.ActiveIndex, coords);
}
else
{
handsComp.Drop(handsComp.ActiveIndex);
}
handsComp.Drop(handsComp.ActiveIndex);
}
private static void HandleActivateItem(ICommonSession session)
@@ -163,22 +157,17 @@ namespace Content.Server.GameObjects.EntitySystems
if (!throwEnt.TryGetComponent(out CollidableComponent colComp))
{
colComp = throwEnt.AddComponent<CollidableComponent>();
if(!colComp.Running)
colComp.Startup();
return;
}
colComp.CollisionEnabled = true;
colComp.CollisionLayer |= (int)CollisionGroup.Items;
colComp.CollisionMask |= (int)CollisionGroup.Grid;
// I can now collide with player, so that i can do damage.
colComp.CollisionMask |= (int) CollisionGroup.Mob;
if (!throwEnt.TryGetComponent(out ThrownItemComponent projComp))
{
projComp = throwEnt.AddComponent<ThrownItemComponent>();
colComp.CollisionMask |= (int)CollisionGroup.Mob;
colComp.IsScrapingFloor = false;
}
projComp.IgnoreEntity(plyEnt);