2021-03-05 01:08:38 +01:00
|
|
|
using System.Linq;
|
2023-07-08 14:08:32 +10:00
|
|
|
using System.Numerics;
|
2021-02-07 02:05:53 +03:00
|
|
|
using Content.Shared.Physics;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Tag;
|
2021-02-07 02:05:53 +03:00
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
using Robust.Shared.Map;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.Physics;
|
2022-09-14 17:26:26 +10:00
|
|
|
using Robust.Shared.Physics.Systems;
|
2022-02-08 14:08:11 +11:00
|
|
|
using Robust.Shared.Utility;
|
2021-02-07 02:05:53 +03:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Shared.Construction.Conditions
|
2021-02-08 22:46:28 +01:00
|
|
|
{
|
2021-02-07 02:05:53 +03:00
|
|
|
[UsedImplicitly]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataDefinition]
|
2023-08-22 18:14:33 -07:00
|
|
|
public sealed partial class WallmountCondition : IConstructionCondition
|
2021-02-07 02:05:53 +03:00
|
|
|
{
|
2021-12-04 12:47:09 +01:00
|
|
|
public bool Condition(EntityUid user, EntityCoordinates location, Direction direction)
|
2021-02-07 02:05:53 +03:00
|
|
|
{
|
|
|
|
|
var entManager = IoCManager.Resolve<IEntityManager>();
|
|
|
|
|
|
|
|
|
|
// get blueprint and user position
|
2024-03-20 21:59:56 -04:00
|
|
|
var transformSystem = entManager.System<SharedTransformSystem>();
|
2024-08-06 04:02:01 -07:00
|
|
|
var userWorldPosition = transformSystem.GetWorldPosition(user);
|
2024-03-20 21:59:56 -04:00
|
|
|
var objWorldPosition = location.ToMap(entManager, transformSystem).Position;
|
2021-02-07 02:05:53 +03:00
|
|
|
|
|
|
|
|
// find direction from user to blueprint
|
|
|
|
|
var userToObject = (objWorldPosition - userWorldPosition);
|
2022-01-25 02:29:11 -07:00
|
|
|
// get direction of the grid being placed on as an offset.
|
2024-08-06 04:02:01 -07:00
|
|
|
var gridRotation = transformSystem.GetWorldRotation(location.EntityId);
|
2022-01-25 02:29:11 -07:00
|
|
|
var directionWithOffset = gridRotation.RotateVec(direction.ToVec());
|
2021-02-07 02:05:53 +03:00
|
|
|
|
|
|
|
|
// dot product will be positive if user direction and blueprint are co-directed
|
2023-07-08 14:08:32 +10:00
|
|
|
var dotProd = Vector2.Dot(directionWithOffset.Normalized(), userToObject.Normalized());
|
2021-02-07 02:05:53 +03:00
|
|
|
if (dotProd > 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
2021-02-08 22:46:28 +01:00
|
|
|
// now we need to check that user actually tries to build wallmount on a wall
|
2022-10-04 14:24:19 +11:00
|
|
|
var physics = entManager.System<SharedPhysicsSystem>();
|
2023-07-08 14:08:32 +10:00
|
|
|
var rUserToObj = new CollisionRay(userWorldPosition, userToObject.Normalized(), (int) CollisionGroup.Impassable);
|
|
|
|
|
var length = userToObject.Length();
|
2022-02-08 14:08:11 +11:00
|
|
|
|
2022-10-04 14:24:19 +11:00
|
|
|
var tagSystem = entManager.System<TagSystem>();
|
2022-02-08 14:08:11 +11:00
|
|
|
|
2022-01-25 02:29:11 -07:00
|
|
|
var userToObjRaycastResults = physics.IntersectRayWithPredicate(entManager.GetComponent<TransformComponent>(user).MapID, rUserToObj, maxLength: length,
|
2022-02-08 14:08:11 +11:00
|
|
|
predicate: (e) => !tagSystem.HasTag(e, "Wall"));
|
|
|
|
|
|
|
|
|
|
var targetWall = userToObjRaycastResults.FirstOrNull();
|
|
|
|
|
|
|
|
|
|
if (targetWall == null)
|
2021-02-07 02:05:53 +03:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// get this wall entity
|
|
|
|
|
// check that we didn't try to build wallmount that facing another adjacent wall
|
2023-07-08 14:08:32 +10:00
|
|
|
var rAdjWall = new CollisionRay(objWorldPosition, directionWithOffset.Normalized(), (int) CollisionGroup.Impassable);
|
2022-01-25 02:29:11 -07:00
|
|
|
var adjWallRaycastResults = physics.IntersectRayWithPredicate(entManager.GetComponent<TransformComponent>(user).MapID, rAdjWall, maxLength: 0.5f,
|
2022-10-30 02:48:53 -04:00
|
|
|
predicate: e => e == targetWall.Value.HitEntity || !tagSystem.HasTag(e, "Wall"));
|
2022-02-08 14:08:11 +11:00
|
|
|
|
2021-02-07 02:05:53 +03:00
|
|
|
return !adjWallRaycastResults.Any();
|
|
|
|
|
}
|
2021-11-02 11:24:32 +01:00
|
|
|
|
2023-01-19 03:56:45 +01:00
|
|
|
public ConstructionGuideEntry GenerateGuideEntry()
|
2021-11-02 11:24:32 +01:00
|
|
|
{
|
|
|
|
|
return new ConstructionGuideEntry()
|
|
|
|
|
{
|
|
|
|
|
Localization = "construction-step-condition-wallmount",
|
|
|
|
|
};
|
|
|
|
|
}
|
2021-02-07 02:05:53 +03:00
|
|
|
}
|
|
|
|
|
}
|