Merge physics rewrite

This commit is contained in:
Pieter-Jan Briers
2020-05-23 01:23:36 +02:00
parent b6b4482ca0
commit 18ce80a43c
20 changed files with 224 additions and 104 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Content.Server.GameObjects.Components.Access;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.GameObjects.Components.Doors;
@@ -17,7 +18,7 @@ namespace Content.Server.GameObjects
{
[RegisterComponent]
[ComponentReference(typeof(IActivate))]
public class ServerDoorComponent : Component, IActivate
public class ServerDoorComponent : Component, IActivate, ICollideBehavior
{
public override string Name => "Door";
@@ -85,26 +86,16 @@ namespace Content.Server.GameObjects
ActivateImpl(eventArgs);
}
public override void HandleMessage(ComponentMessage message, IComponent component)
void ICollideBehavior.CollideWith(IEntity entity)
{
base.HandleMessage(message, component);
switch (message)
if (State != DoorState.Closed)
{
case BumpedEntMsg msg:
if (State != DoorState.Closed)
{
return;
}
// Only open when bumped by mobs.
if (!msg.Entity.HasComponent(typeof(SpeciesComponent)))
{
return;
}
TryOpen(msg.Entity);
break;
return;
}
if (entity.HasComponent(typeof(SpeciesComponent)))
{
TryOpen(entity);
}
}
@@ -155,7 +146,7 @@ namespace Content.Server.GameObjects
Timer.Spawn(OpenTimeOne, async () =>
{
collidableComponent.IsHardCollidable = false;
collidableComponent.CanCollide = false;
await Timer.Delay(OpenTimeTwo, _cancellationTokenSource.Token);
@@ -191,14 +182,14 @@ namespace Content.Server.GameObjects
public bool Close()
{
if (collidableComponent.TryCollision(Vector2.Zero))
if (collidableComponent.IsColliding(Vector2.Zero))
{
// Do nothing, somebody's in the door.
return false;
}
State = DoorState.Closing;
collidableComponent.IsHardCollidable = true;
collidableComponent.CanCollide = true;
OpenTimeCounter = 0;
SetAppearance(DoorVisualState.Closing);

View File

@@ -34,7 +34,7 @@ namespace Content.Server.GameObjects.Components.Fluids
// Small puddles will evaporate after a set delay
// TODO: 'leaves fluidtracks', probably in a separate component for stuff like gibb chunks?;
// TODO: Add stuff like slipping -> probably in a separate component (for stuff like bananas) and using BumpEntMsg
// TODO: Add stuff like slipping -> probably in a separate component (for stuff like bananas)
// based on behaviour (e.g. someone being punched vs slashed with a sword would have different blood sprite)
// to check for low volumes for evaporation or whatever

View File

@@ -190,7 +190,7 @@ namespace Content.Server.GameObjects.Components
{
if (Owner.TryGetComponent<ICollidableComponent>(out var collidableComponent))
{
collidableComponent.CollisionEnabled = IsCollidableWhenOpen || !Open;
collidableComponent.CanCollide = IsCollidableWhenOpen || !Open;
}
if (Owner.TryGetComponent<PlaceableSurfaceComponent>(out var placeableSurfaceComponent))
@@ -250,7 +250,7 @@ namespace Content.Server.GameObjects.Components
entity.Transform.WorldPosition = worldPos;
if (entityCollidableComponent != null)
{
entityCollidableComponent.CollisionEnabled = false;
entityCollidableComponent.CanCollide = false;
}
return true;
}
@@ -265,7 +265,7 @@ namespace Content.Server.GameObjects.Components
{
if (contained.TryGetComponent<ICollidableComponent>(out var entityCollidableComponent))
{
entityCollidableComponent.CollisionEnabled = true;
entityCollidableComponent.CanCollide = true;
}
}
}

View File

@@ -195,7 +195,7 @@ namespace Content.Server.GameObjects
if (entity.TryGetComponent(out CollidableComponent collidable))
{
collidable.CollisionEnabled = false;
collidable.CanCollide = false;
}
}
@@ -205,7 +205,7 @@ namespace Content.Server.GameObjects
if (entity.TryGetComponent(out CollidableComponent collidable))
{
collidable.CollisionEnabled = true;
collidable.CanCollide = true;
}
}

View File

@@ -63,7 +63,7 @@ namespace Content.Server.GameObjects.Components.Movement
base.OnAdd();
if (Owner.TryGetComponent<CollidableComponent>(out var collide))
{
collide.IsHardCollidable = false;
//collide.IsHardCollidable = false;
}
_state = PortalState.Pending;

View File

@@ -65,8 +65,8 @@ namespace Content.Server.GameObjects.Components.Movement
if (!gridEntity.HasComponent<ICollidableComponent>())
{
var collideComp = gridEntity.AddComponent<CollidableComponent>();
collideComp.CollisionEnabled = true;
collideComp.IsHardCollidable = true;
collideComp.CanCollide = true;
//collideComp.IsHardCollidable = true;
collideComp.PhysicsShapes.Add(new PhysShapeGrid(grid));
}

View File

@@ -62,36 +62,32 @@ namespace Content.Server.GameObjects.Components.Projectiles
}
/// <summary>
/// Applys the damage when our projectile collides with its victim
/// Applies the damage when our projectile collides with its victim
/// </summary>
/// <param name="collidedwith"></param>
void ICollideBehavior.CollideWith(List<IEntity> collidedwith)
/// <param name="entity"></param>
void ICollideBehavior.CollideWith(IEntity entity)
{
foreach (var entity in collidedwith)
if (entity.TryGetComponent(out DamageableComponent damage))
{
if (entity.TryGetComponent(out DamageableComponent damage))
Owner.EntityManager.TryGetEntity(Shooter, out var shooter);
foreach (var (damageType, amount) in _damages)
{
Owner.EntityManager.TryGetEntity(Shooter, out var shooter);
foreach (var (damageType, amount) in _damages)
{
damage.TakeDamage(damageType, amount, Owner, shooter);
}
}
if (!entity.Deleted && entity.TryGetComponent(out CameraRecoilComponent recoilComponent)
&& Owner.TryGetComponent(out PhysicsComponent physicsComponent))
{
var direction = physicsComponent.LinearVelocity.Normalized;
recoilComponent.Kick(direction);
damage.TakeDamage(damageType, amount, Owner, shooter);
}
}
if (collidedwith.Count > 0)
if (!entity.Deleted && entity.TryGetComponent(out CameraRecoilComponent recoilComponent)
&& Owner.TryGetComponent(out PhysicsComponent physicsComponent))
{
Owner.Delete();
var direction = physicsComponent.LinearVelocity.Normalized;
recoilComponent.Kick(direction);
}
}
void ICollideBehavior.PostCollide(int collideCount)
{
if (collideCount > 0) Owner.Delete();
}
}
}

View File

@@ -18,6 +18,8 @@ namespace Content.Server.GameObjects.Components
[Dependency] private readonly IEntitySystemManager _entitySystemManager;
#pragma warning restore 649
private bool _shouldCollide = true;
public override string Name => "ThrownItem";
/// <summary>
@@ -25,32 +27,37 @@ namespace Content.Server.GameObjects.Components
/// </summary>
public IEntity User;
void ICollideBehavior.CollideWith(List<IEntity> collidedwith)
void ICollideBehavior.CollideWith(IEntity entity)
{
foreach (var entity in collidedwith)
if (!_shouldCollide) return;
if (entity.TryGetComponent(out DamageableComponent damage))
{
if (entity.TryGetComponent(out DamageableComponent damage))
{
damage.TakeDamage(DamageType.Brute, 10, Owner, User);
}
damage.TakeDamage(DamageType.Brute, 10, Owner, User);
}
// Stop colliding with mobs, this mimics not having enough velocity to do damage
// 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 CollidableComponent body) && body.PhysicsShapes.Count >= 1)
if (Owner.TryGetComponent(out CollidableComponent body) && body.PhysicsShapes.Count >= 1)
{
_shouldCollide = false;
}
}
public void PostCollide(int collideCount)
{
if (collideCount > 0 && Owner.TryGetComponent(out CollidableComponent body) && body.PhysicsShapes.Count >= 1)
{
body.PhysicsShapes[0].CollisionMask &= (int)~CollisionGroup.MobImpassable;
body.IsScrapingFloor = true;
// KYS, your job is finished. Trigger ILand as well.
var physics = Owner.GetComponent<PhysicsComponent>();
(physics.Controller as ThrowController).StopThrow();
physics.RemoveController();
Owner.RemoveComponent<ThrownItemComponent>();
_entitySystemManager.GetEntitySystem<InteractionSystem>().LandInteraction(User, Owner, Owner.Transform.GridPosition);
}
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.GameObjects;
using Content.Shared.GameObjects.Components.Items;
@@ -140,7 +141,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Melee
for (var i = 0; i < increments; i++)
{
var castAngle = new Angle(baseAngle + increment * i);
var res = _physicsManager.IntersectRay(mapId, new CollisionRay(position, castAngle.ToVec(), 23), _range, ignore, ignoreNonHardCollidables: true);
var res = _physicsManager.IntersectRay(mapId, new CollisionRay(position, castAngle.ToVec(), 23), _range, ignore).First();
if (res.HitEntity != null)
{
resSet.Add(res.HitEntity);

View File

@@ -1,4 +1,5 @@
using System;
using System.Linq;
using Content.Server.GameObjects.Components.Power;
using Content.Server.GameObjects.Components.Sound;
using Content.Server.GameObjects.EntitySystems;
@@ -90,10 +91,13 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Hitscan
var angle = new Angle(clickLocation.Position - userPosition);
var ray = new CollisionRay(userPosition, angle.ToVec(), (int)(CollisionGroup.Impassable | CollisionGroup.MobImpassable));
var rayCastResults = IoCManager.Resolve<IPhysicsManager>().IntersectRay(user.Transform.MapID, ray, MaxLength, user, ignoreNonHardCollidables: true);
var rayCastResults = IoCManager.Resolve<IPhysicsManager>().IntersectRay(user.Transform.MapID, ray, MaxLength, user).ToList();
Hit(rayCastResults, energyModifier, user);
AfterEffects(user, rayCastResults, angle, energyModifier);
if (rayCastResults.Count == 1)
{
Hit(rayCastResults[0], energyModifier, user);
AfterEffects(user, rayCastResults[0], angle, energyModifier);
}
}
protected virtual void Hit(RayCastResults ray, float damageModifier, IEntity user = null)
@@ -109,7 +113,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Hitscan
protected virtual void AfterEffects(IEntity user, RayCastResults ray, Angle angle, float energyModifier)
{
var time = IoCManager.Resolve<IGameTiming>().CurTime;
var dist = ray.DidHitObject ? ray.Distance : MaxLength;
var dist = ray.Distance;
var offset = angle.ToVec() * dist / 2;
var message = new EffectSystemMessage
{

View File

@@ -42,7 +42,7 @@ namespace Content.Server.GameObjects.Components.Weapon.Ranged.Projectile
{
var angle = GetAngleFromClickLocation(source, coord);
FireAtAngle(source, angle, projectileType, spreadStdDev, projectilesFired, evenSpreadAngle, velocity);
}
}
/// <summary>
/// Fires projectile in the direction of an angle.