Files
RobustToolbox/Robust.Client/GameObjects/Components/Collidable/CollidableComponent.cs
T
SilverandGitHub 25926a17b7 Renames SS14.* to Robust.* (#793)
RobustToolbox projects should be named Robust.* 

This PR changes the RobustToolbox projects from SS14.* to Robust.*

Updates SS14.* prefixes/namespaces to Robust.*
Updates SpaceStation14.sln to RobustToolbox.sln
Updates MSBUILD/SS14.* to MSBUILD/Robust.*
Updates CSProject and MSBuild references for the above
Updates git_helper.py
Removes Runserver and Runclient as they are unusable
2019-04-15 20:24:59 -06:00

161 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.Interfaces.Physics;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Physics;
namespace Robust.Client.GameObjects
{
public class CollidableComponent : Component, ICollidableComponent
{
[Dependency] private readonly IPhysicsManager _physicsManager;
private bool _collisionIsActuallyEnabled;
private bool _collisionEnabled;
/// <inheritdoc />
public override string Name => "Collidable";
/// <inheritdoc />
public override uint? NetID => NetIDs.COLLIDABLE;
/// <inheritdoc />
public override Type StateType => typeof(CollidableComponentState);
/// <inheritdoc />
Box2 ICollidable.WorldAABB => Owner.GetComponent<BoundingBoxComponent>().WorldAABB;
/// <inheritdoc />
Box2 ICollidable.AABB => Owner.GetComponent<BoundingBoxComponent>().AABB;
/// <inheritdoc />
public MapId MapID => Owner.Transform.MapID;
/// <inheritdoc />
void ICollidable.Bumped(IEntity bumpedby)
{
SendMessage(new BumpedEntMsg(bumpedby));
}
/// <inheritdoc />
void ICollidable.Bump(List<IEntity> bumpedinto)
{
var collidecomponents = Owner.GetAllComponents<ICollideBehavior>().ToList();
for (var i = 0; i < collidecomponents.Count; i++)
{
collidecomponents[i].CollideWith(bumpedinto);
}
}
/// <inheritdoc />
public bool CollisionEnabled
{
get => _collisionEnabled;
set
{
if (value == _collisionEnabled)
{
return;
}
_collisionEnabled = value;
if (value)
{
EnableCollision();
}
else
{
DisableCollision();
}
}
}
/// <inheritdoc />
public bool IsHardCollidable { get; set; }
/// <inheritdoc />
public int CollisionLayer { get; set; }
/// <inheritdoc />
public int CollisionMask { get; set; }
/// <summary>
/// gets the AABB from the sprite component and sends it to the CollisionManager.
/// </summary>
public override void Initialize()
{
base.Initialize();
if (_collisionEnabled && !_collisionIsActuallyEnabled)
{
_physicsManager.AddCollidable(this);
_collisionIsActuallyEnabled = true;
}
}
/// <summary>
/// removes the AABB from the CollisionManager.
/// </summary>
public override void Shutdown()
{
if (_collisionEnabled)
{
_physicsManager.RemoveCollidable(this);
}
base.Shutdown();
}
/// <inheritdoc />
public override void HandleComponentState(ComponentState curState, ComponentState nextState)
{
if (curState == null)
return;
var newState = (CollidableComponentState) curState;
// edge triggered
if (newState.CollisionEnabled == _collisionEnabled)
return;
if (newState.CollisionEnabled)
EnableCollision();
else
DisableCollision();
}
/// <inheritdoc />
public bool TryCollision(Vector2 offset, bool bump = false)
{
return _physicsManager.TryCollide(Owner, offset, bump);
}
/// <summary>
/// Enables collidable
/// </summary>
private void EnableCollision()
{
_collisionEnabled = true;
_collisionIsActuallyEnabled = true;
_physicsManager.AddCollidable(this);
}
/// <summary>
/// Disables Collidable
/// </summary>
private void DisableCollision()
{
_collisionEnabled = false;
_collisionIsActuallyEnabled = false;
_physicsManager.RemoveCollidable(this);
}
}
}