using UnityEngine;
using System.Collections;
namespace MoreMountains.Tools
{
///
/// This class acts as an interface to allow the demo levels to work whether the environment (colliders, rigidbodies) are set as 2D or 3D.
/// If you already know for sure that you're going for a 2D or 3D game, I suggest you replace the use of this class with the appropriate classes.
///
[AddComponentMenu("More Mountains/Tools/Rigidbody Interface/MMRigidbodyInterface")]
public class MMRigidbodyInterface : MonoBehaviour
{
///
/// Returns the rigidbody's position
///
/// The position.
public Vector3 position
{
get
{
if (_rigidbody2D != null)
{
return _rigidbody2D.position;
}
if (_rigidbody != null)
{
return _rigidbody.position;
}
return Vector3.zero;
}
set { }
}
///
/// Only use if you absolutely need to target the rigidbody2D specifically
///
/// The internal rigid body2 d.
public Rigidbody2D InternalRigidBody2D
{
get {
return _rigidbody2D;
}
}
///
/// Only use if you absolutely need to target the rigidbody2D specifically
///
/// The internal rigid body.
public Rigidbody InternalRigidBody
{
get {
return _rigidbody;
}
}
///
/// Gets or sets the velocity of the rigidbody associated to the interface.
///
/// The velocity.
public Vector3 Velocity
{
get
{
if (_mode == "2D")
{
return(_rigidbody2D.velocity);
}
else
{
if (_mode == "3D")
{
return(_rigidbody.velocity);
}
else
{
return new Vector3(0,0,0);
}
}
}
set
{
if (_mode == "2D") {
_rigidbody2D.velocity = value;
}
if (_mode == "3D") {
_rigidbody.velocity = value;
}
}
}
///
/// Gets the collider bounds.
///
/// The collider bounds.
public Bounds ColliderBounds
{
get
{
if (_rigidbody2D != null)
{
return _collider2D.bounds;
}
if (_rigidbody != null)
{
return _collider.bounds;
}
return new Bounds();
}
}
///
/// Gets a value indicating whether this is kinematic.
///
/// true if is kinematic; otherwise, false.
public bool isKinematic
{
get
{
if (_mode == "2D")
{
return(_rigidbody2D.isKinematic);
}
if (_mode == "3D")
{
return(_rigidbody.isKinematic);
}
return false;
}
}
protected string _mode;
protected Rigidbody2D _rigidbody2D;
protected Rigidbody _rigidbody;
protected Collider2D _collider2D;
protected Collider _collider;
protected Bounds _colliderBounds;
///
/// Initialization
///
protected virtual void Awake ()
{
// we check for rigidbodies, and depending on their presence determine if the interface will work with 2D or 3D rigidbodies and colliders.
_rigidbody2D=GetComponent();
_rigidbody=GetComponent();
if (_rigidbody2D != null)
{
_mode="2D";
_collider2D = GetComponent ();
}
if (_rigidbody != null)
{
_mode="3D";
_collider = GetComponent ();
}
if (_rigidbody==null && _rigidbody2D==null)
{
Debug.LogWarning("A RigidBodyInterface has been added to "+gameObject+" but there's no Rigidbody or Rigidbody2D on it.", gameObject);
}
}
///
/// Adds the specified force to the rigidbody associated to the interface..
///
/// Force.
public virtual void AddForce(Vector3 force)
{
if (_mode == "2D")
{
_rigidbody2D.AddForce(force,ForceMode2D.Impulse);
}
if (_mode == "3D")
{
_rigidbody.AddForce(force);
}
}
///
/// Adds the specified relative force to the rigidbody associated to the interface..
///
/// Force.
public virtual void AddRelativeForce(Vector3 force)
{
if (_mode == "2D")
{
_rigidbody2D.AddRelativeForce(force,ForceMode2D.Impulse);
}
if (_mode == "3D")
{
_rigidbody.AddRelativeForce(force);
}
}
///
/// Move the rigidbody to the position vector specified
///
///
public virtual void MovePosition(Vector3 newPosition)
{
if (_mode == "2D")
{
_rigidbody2D.MovePosition(newPosition);
}
if (_mode == "3D")
{
_rigidbody.MovePosition(newPosition);
}
}
///
/// Resets the angular velocity.
///
public virtual void ResetAngularVelocity()
{
if (_mode == "2D")
{
_rigidbody2D.angularVelocity = 0;
}
if (_mode == "3D")
{
_rigidbody.angularVelocity = Vector3.zero;
}
}
///
/// Resets the rotation.
///
public virtual void ResetRotation()
{
if (_mode == "2D")
{
_rigidbody2D.rotation = 0;
}
if (_mode == "3D")
{
_rigidbody.rotation = Quaternion.identity;
}
}
///
/// Determines whether the rigidbody associated to the interface is kinematic
///
/// true if this instance is kinematic the specified status; otherwise, false.
/// If set to true status.
public virtual void IsKinematic(bool status)
{
if (_mode == "2D")
{
_rigidbody2D.isKinematic=status;
}
if (_mode == "3D")
{
_rigidbody.isKinematic=status;
}
}
///
/// Enables the box collider associated to the interface.
///
/// If set to true status.
public virtual void EnableBoxCollider(bool status)
{
if (_mode == "2D")
{
GetComponent().enabled=status;
}
if (_mode == "3D")
{
GetComponent().enabled=status;
}
}
///
/// Use this to check if you're dealing with a 3D object
///
/// true if this instance is3 d; otherwise, false.
public bool Is3D
{
get
{
if (_mode=="3D")
{
return true;
}
else
{
return false;
}
}
}
///
/// Use this to check if you're dealing with a 2D object
///
/// The position.
public bool Is2D
{
get
{
if (_mode=="2D")
{
return true;
}
else
{
return false;
}
}
}
}
}