Files
tbd-station-14/Content.Shared/GameObjects/Components/Movement/SharedSlipperyComponent.cs
metalgearsloth 3e64fd56a1 Physics (#3452)
* Content side new physics structure

* BroadPhase outline done

* But we need to fix WorldAABB

* Fix static pvs AABB

* Fix import

* Rando fixes

* B is for balloon

* Change human mob hitbox to circle

* Decent movement

* Start adding friction to player controller

I think it's the best way to go about it to keep other objects somewhat consistent for physics.

* This baby can fit so many physics bugs in it.

* Slight mob mover optimisations.

* Player mover kinda works okay.

* Beginnings of testbed

* More testbed

* Circlestack bed

* Namespaces

* BB fixes

* Pull WorldAABB

* Joint pulling

* Semi-decent movement I guess.

* Pulling better

* Bullet controller + old movement

* im too dumb for this shit

* Use kinematic mob controller again

It's probably for the best TBH

* Stashed shitcode

* Remove SlipController

* In which movement code is entirely refactored

* Singularity fix

* Fix ApplyLinearImpulse

* MoveRelay fix

* Fix door collisions

* Disable subfloor collisions

Saves on broadphase a fair bit

* Re-implement ClimbController

* Zumzum's pressure

* Laggy item throwing

* Minor atmos change

* Some caching

* Optimise controllers

* Optimise CollideWith to hell and back

* Re-do throwing and tile friction

* Landing too

* Optimise controllers

* Move CCVars and other stuff swept is beautiful

* Cleanup a bunch of controllers

* Fix shooting and high pressure movement controller

* Flashing improvements

* Stuff and things

* Combat collisions

* Combat mode collisions

* Pulling distance joint again

* Cleanup physics interfaces

* More like scuffedularity

* Shit's fucked

* Haha tests go green

* Bigmoneycrab

Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
2021-03-01 03:11:29 +11:00

171 lines
5.7 KiB
C#

#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using Content.Shared.GameObjects.Components.Mobs;
using Content.Shared.GameObjects.EntitySystems.ActionBlocker;
using Content.Shared.GameObjects.EntitySystems.EffectBlocker;
using Content.Shared.Physics;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.Physics;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Shared.GameObjects.Components.Movement
{
public abstract class SharedSlipperyComponent : Component, ICollideBehavior
{
public sealed override string Name => "Slippery";
/// <summary>
/// The list of entities that have been slipped by this component,
/// and which have not stopped colliding with its owner yet.
/// </summary>
protected readonly List<EntityUid> _slipped = new();
/// <summary>
/// How many seconds the mob will be paralyzed for.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public virtual float ParalyzeTime { get; set; } = 2f;
/// <summary>
/// Percentage of shape intersection for a slip to occur.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public virtual float IntersectPercentage { get; set; } = 0.3f;
/// <summary>
/// Entities will only be slipped if their speed exceeds this limit.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public virtual float RequiredSlipSpeed { get; set; } = 0f;
/// <summary>
/// The entity's speed will be multiplied by this to slip it forwards.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public virtual float LaunchForwardsMultiplier { get; set; } = 1f;
/// <summary>
/// Whether or not this component will try to slip entities.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public virtual bool Slippery { get; set; }
private bool TrySlip(IPhysBody ourBody, IPhysBody otherBody)
{
if (!Slippery
|| Owner.IsInContainer()
|| _slipped.Contains(otherBody.Entity.Uid)
|| !otherBody.Entity.TryGetComponent(out SharedStunnableComponent? stun))
{
return false;
}
if (otherBody.LinearVelocity.Length < RequiredSlipSpeed || stun.KnockedDown)
{
return false;
}
var percentage = otherBody.GetWorldAABB().IntersectPercentage(ourBody.GetWorldAABB());
if (percentage < IntersectPercentage)
{
return false;
}
if (!EffectBlockerSystem.CanSlip(otherBody.Entity))
{
return false;
}
otherBody.LinearVelocity *= LaunchForwardsMultiplier;
stun.Paralyze(5);
_slipped.Add(otherBody.Entity.Uid);
OnSlip();
return true;
}
protected virtual void OnSlip() { }
public void CollideWith(IPhysBody ourBody, IPhysBody otherBody)
{
TrySlip(ourBody, otherBody);
}
public void Update()
{
foreach (var uid in _slipped.ToArray())
{
if (!uid.IsValid() || !Owner.EntityManager.EntityExists(uid))
{
_slipped.Remove(uid);
continue;
}
var entity = Owner.EntityManager.GetEntity(uid);
var physics = Owner.GetComponent<IPhysBody>();
var otherPhysics = entity.GetComponent<IPhysBody>();
if (!physics.GetWorldAABB().Intersects(otherPhysics.GetWorldAABB()))
{
_slipped.Remove(uid);
}
}
}
public override void Initialize()
{
base.Initialize();
var physics = Owner.EnsureComponent<PhysicsComponent>();
physics.Hard = false;
var fixtures = physics.Fixtures.FirstOrDefault();
if (fixtures != null)
{
fixtures.CollisionLayer |= (int) CollisionGroup.SmallImpassable;
fixtures.CollisionMask = (int) CollisionGroup.None;
}
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(this, x => x.ParalyzeTime, "paralyzeTime", 3f);
serializer.DataField(this, x => x.IntersectPercentage, "intersectPercentage", 0.3f);
serializer.DataField(this, x => x.RequiredSlipSpeed, "requiredSlipSpeed", 0.1f);
serializer.DataField(this, x => x.LaunchForwardsMultiplier, "launchForwardsMultiplier", 1f);
serializer.DataField(this, x => x.Slippery, "slippery", true);
}
}
[Serializable, NetSerializable]
public class SlipperyComponentState : ComponentState
{
public float ParalyzeTime { get; }
public float IntersectPercentage { get; }
public float RequiredSlipSpeed { get; }
public float LaunchForwardsMultiplier { get; }
public bool Slippery { get; }
public SlipperyComponentState(float paralyzeTime, float intersectPercentage, float requiredSlipSpeed, float launchForwardsMultiplier, bool slippery) : base(ContentNetIDs.SLIP)
{
ParalyzeTime = paralyzeTime;
IntersectPercentage = intersectPercentage;
RequiredSlipSpeed = requiredSlipSpeed;
LaunchForwardsMultiplier = launchForwardsMultiplier;
Slippery = slippery;
}
}
}