Skip to content

Commit

Permalink
Moved 2D behaviours and tools to a separate folder and namespace. 2D …
Browse files Browse the repository at this point in the history
…behaviours and tools have separate component menus.
  • Loading branch information
Daniel Nunes committed Nov 3, 2015
1 parent 4cdf98e commit 5cfe880
Show file tree
Hide file tree
Showing 169 changed files with 5,421 additions and 1,361 deletions.
9 changes: 9 additions & 0 deletions 2D.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions 2D/Behaviors.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 106 additions & 0 deletions 2D/Behaviors/AutonomousVehicle.cs
@@ -0,0 +1,106 @@
using System;
using UnityEngine;

namespace UnitySteer2D.Behaviors
{
/// <summary>
/// Vehicle subclass which automatically applies the steering forces from
/// the components attached to the object. AutonomousVehicle is characterized
/// for the vehicle always moving in the same direction as its forward vector,
/// unlike Bipeds which are able to side-step.
/// </summary>
[AddComponentMenu("UnitySteer2D/Vehicle/Autonomous")]
public class AutonomousVehicle : TickedVehicle
{
#region Internal state values

private float _speed;

#endregion

/// <summary>
/// Acceleration rate - it'll be used as a multiplier for the speed
/// at which the velocity is interpolated when accelerating. A rate
/// of 1 means that we interpolate across 1 second; a rate of 5 means
/// we do it five times as fast.
/// </summary>
[SerializeField] private float _accelerationRate = 5;

/// <summary>
/// Deceleration rate - it'll be used as a multiplier for the speed
/// at which the velocity is interpolated when decelerating. A rate
/// of 1 means that we interpolate across 1 second; a rate of 5 means
/// we do it five times as fast.
/// </summary>
[SerializeField] private float _decelerationRate = 8;


/// <summary>
/// Current vehicle speed
/// </summary>
public override float Speed
{
get { return _speed; }
}

/// <summary>
/// Current vehicle velocity
/// </summary>
public override Vector2 Velocity
{
get { return Forward * Speed; }
protected set { throw new NotSupportedException("Cannot set the velocity directly on AutonomousVehicle"); }
}

#region Speed-related methods

/// <summary>
/// Uses a desired velocity vector to adjust the vehicle's target speed and
/// orientation velocity.
/// </summary>
/// <param name="velocity">Newly calculated velocity</param>
protected override void SetCalculatedVelocity(Vector2 velocity)
{
TargetSpeed = velocity.magnitude;
//More casts to make sure the if statement goes nicely
OrientationVelocity = Mathf.Approximately(_speed, 0) ? Forward : velocity / TargetSpeed;
}

/// <summary>
/// Calculates how much the agent's position should change in a manner that
/// is specific to the vehicle's implementation.
/// </summary>
/// <param name="deltaTime">Time delta to use in position calculations</param>
protected override Vector2 CalculatePositionDelta(float deltaTime)
{
/*
* Notice that we clamp the target speed and not the speed itself,
* because the vehicle's maximum speed might just have been lowered
* and we don't want its actual speed to suddenly drop.
*/
var targetSpeed = Mathf.Clamp(TargetSpeed, 0, MaxSpeed);
if (Mathf.Approximately(_speed, targetSpeed))
{
_speed = targetSpeed;
}
else
{
var rate = TargetSpeed > _speed ? _accelerationRate : _decelerationRate;
_speed = Mathf.Lerp(_speed, targetSpeed, deltaTime * rate);
}

return Velocity * deltaTime;
}

/// <summary>
/// Zeros this vehicle's target speed, which results on its desired velocity
/// being zero.
/// </summary>
protected override void ZeroVelocity()
{
TargetSpeed = 0;
}

#endregion
}
}
10 changes: 10 additions & 0 deletions 2D/Behaviors/AutonomousVehicle.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions 2D/Behaviors/Biped.cs
@@ -0,0 +1,92 @@
using UnityEngine;

namespace UnitySteer2D.Behaviors
{
/// <summary>
/// Vehicle subclass oriented towards autonomous bipeds, which have a movement
/// vector which can be separate from their forward vector (can side-step or
/// walk backwards).
/// </summary>
[AddComponentMenu("UnitySteer2D/Vehicle/Biped")]
public class Biped : TickedVehicle
{
#region Internal state values

/// <summary>
/// The magnitude of the last velocity vector assigned to the vehicle
/// </summary>
private float _speed;

/// <summary>
/// The biped's current velocity vector
/// </summary>
private Vector2 _velocity;

#endregion

/// <summary>
/// Current vehicle speed
/// </summary>
/// <remarks>
/// If the vehicle has a speedometer, then we return the actual measured
/// value instead of simply the length of the velocity vector.
/// </remarks>
public override float Speed
{
get { return Speedometer == null ? _speed : Speedometer.Speed; }
}

/// <summary>
/// Current vehicle velocity
/// </summary>
public override Vector2 Velocity
{
get { return _velocity; }
protected set
{
_velocity = Vector2.ClampMagnitude(value, MaxSpeed);
_speed = _velocity.magnitude;
TargetSpeed = _speed;
OrientationVelocity = !Mathf.Approximately(_speed, 0) ? _velocity / _speed : Vector2.zero;
}
}

#region Methods

protected override void OnEnable()
{
base.OnEnable();
Velocity = Vector2.zero;
}


/// <summary>
/// Assigns a new velocity vector to the biped.
/// </summary>
/// <param name="velocity">Newly calculated velocity</param>
protected override void SetCalculatedVelocity(Vector2 velocity)
{
Velocity = velocity;
}

/// <summary>
/// Calculates how much the agent's position should change in a manner that
/// is specific to the vehicle's implementation.
/// </summary>
/// <param name="deltaTime">Time delta to use in position calculations</param>
protected override Vector2 CalculatePositionDelta(float deltaTime)
{
return Velocity * deltaTime;
}

/// <summary>
/// Zeros this vehicle's velocity vector.
/// </summary>
protected override void ZeroVelocity()
{
Velocity = Vector2.zero;
}

#endregion
}
}
10 changes: 10 additions & 0 deletions 2D/Behaviors/Biped.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5cfe880

Please sign in to comment.