Skip to content

Commit

Permalink
feat: Added EntityPhysicsManagerBase.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: PhysicsManager now allows for your own implementation, with two given implementations for 3D and 2D.
  • Loading branch information
christides11 committed Sep 30, 2020
1 parent b28032b commit 1f6d586
Show file tree
Hide file tree
Showing 26 changed files with 254 additions and 115 deletions.
6 changes: 3 additions & 3 deletions Assets/CAF/Entities/EntityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class EntityManager : SimObject, ITargetable, IStatusEffectable
public EntityInputManager InputManager { get { return entityInput; } }
public EntityStateManager StateManager { get { return entityStateManager; } }
public EntityCombatManager CombatManager { get { return entityCombatManager; } }
public EntityPhysicsManager PhysicsManager { get { return entityPhysicsManager; } }
public EntityPhysicsManagerBase PhysicsManager { get { return entityPhysicsManager; } }

public virtual bool Targetable { get { return targetable; } }
public bool IsGrounded { get; set; } = false;
Expand All @@ -23,7 +23,7 @@ public class EntityManager : SimObject, ITargetable, IStatusEffectable
[SerializeField] protected EntityInputManager entityInput;
[SerializeField] protected EntityStateManager entityStateManager;
[SerializeField] protected EntityCombatManager entityCombatManager;
[SerializeField] protected EntityPhysicsManager entityPhysicsManager;
[SerializeField] protected EntityPhysicsManagerBase entityPhysicsManager;
public GameObject visual;
public LookHandler lookHandler;

Expand All @@ -40,7 +40,7 @@ public override void SimUpdate()
}
else
{
PhysicsManager.SetForceDirect(Vector3.zero, Vector3.zero);
PhysicsManager.ResetForces();
}
}

Expand Down
97 changes: 97 additions & 0 deletions Assets/CAF/Entities/Managers/EntityPhysicsManager2D.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace CAF.Entities
{
public class EntityPhysicsManager2D : EntityPhysicsManagerBase
{
public float GravityScale { get; set; } = 1.0f;

protected float decelerationFactor = 0.97f;

[Header("Forces")]
public Vector2 forceMovement;
public Vector2 forceGravity;
public Vector2 forceDamage;
public Vector2 forcePushbox;

public override void ResetForces()
{
forceMovement = Vector2.zero;
forceGravity = Vector2.zero;
forceDamage = Vector2.zero;
forcePushbox = Vector2.zero;
}

public virtual Vector2 GetOverallForce()
{
return forceMovement + forceGravity + forceDamage;
}

public virtual void HandleGravity(float maxFallSpeed, float gravity, float gravityScale)
{
if (forceGravity.y > -(maxFallSpeed))
{
forceGravity.y -= gravity * gravityScale;
if (forceGravity.y < -(maxFallSpeed))
{
forceGravity.y = -maxFallSpeed;
}
}
else if (forceGravity.y < -(maxFallSpeed))
{
forceGravity.y *= decelerationFactor;
}
}

public virtual void ApplyMovementFriction(float friction = -1)
{
if (friction == -1)
{
friction = 1;
}
Vector3 realFriction = forceMovement.normalized * friction;
forceMovement.x = ApplyFriction(forceMovement.x, Mathf.Abs(realFriction.x));
}

public virtual void ApplyGravityFriction(float friction)
{
forceGravity.y = ApplyFriction(forceGravity.y, friction);
}

/// <summary>
/// Applies friction on the given value based on the traction given.
/// </summary>
/// <param name="value">The value to apply traction to.</param>
/// <param name="traction">The traction to apply.</param>
/// <returns>The new value with the traction applied.</returns>
protected virtual float ApplyFriction(float value, float traction)
{
if (value > 0)
{
value -= traction;
if (value < 0)
{
value = 0;
}
}
else if (value < 0)
{
value += traction;
if (value > 0)
{
value = 0;
}
}
return value;
}

public virtual void RedirectInertia(float forceMovementX, Vector2 movementInput)
{
float redirectedMovement = Mathf.Sign(movementInput.x)
* forceMovementX;
this.forceMovement.x = redirectedMovement;
}
}
}
11 changes: 11 additions & 0 deletions Assets/CAF/Entities/Managers/EntityPhysicsManager2D.cs.meta

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

Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,24 @@

namespace CAF.Entities
{
public class EntityPhysicsManager : MonoBehaviour
public class EntityPhysicsManager3D : EntityPhysicsManagerBase
{
public float GravityScale { get; set; } = 1.0f;

[SerializeField] protected EntityManager manager;
protected float decelerationFactor = 0.97f;

[Header("Forces")]
public Vector3 forceMovement;
public Vector3 forceGravity;
public Vector3 forceDamage;
public Vector3 forcePushbox;

protected float decelerationFactor = 0.97f;

public virtual void Tick()
public override void ResetForces()
{

}

public virtual void SetForceDirect(Vector3 movement, Vector3 gravity)
{

forceMovement = Vector3.zero;
forceGravity = Vector3.zero;
forceDamage = Vector3.zero;
forcePushbox = Vector3.zero;
}

public virtual Vector3 GetOverallForce()
Expand Down Expand Up @@ -92,54 +88,12 @@ protected virtual float ApplyFriction(float value, float traction)
return value;
}

/// <summary>
/// Create a force based on the parameters given and
/// adds it to our movement force.
/// </summary>
/// <param name="accel">How fast the entity accelerates in the movement direction.</param>
/// <param name="max">The max magnitude of our movement force.</param>
/// <param name="decel">How much the entity decelerates when moving faster than the max magnitude.
/// 1.0 = doesn't decelerate, 0.0 = force set to 0.</param>
public virtual void ApplyMovement(float accel, float max, float decel)
{/*
Vector2 movement = controller.InputManager.GetMovement(0);
if (movement.magnitude >= InputConstants.movementMagnitude)
{
//Translate movment based on "camera."
Vector3 translatedMovement = controller.lookTransform.TransformDirection(new Vector3(movement.x, 0, movement.y));
translatedMovement.y = 0;
translatedMovement *= accel;
forceMovement += translatedMovement;
//Limit movement velocity.
if (forceMovement.magnitude > max * movement.magnitude)
{
forceMovement *= decel;
}
}*/
}

/// <summary>
/// Check if we are on the ground.
/// </summary>
public virtual void CheckIfGrounded()
{

}

public virtual void RedirectInertia3D(Vector3 forceMovement, Vector2 movementInput)
public virtual void RedirectInertia(Vector3 forceMovement, Vector2 movementInput)
{
movementInput.Normalize();
Vector3 redirectedMovement = forceMovement.magnitude
* manager.GetMovementVector(movementInput.x, movementInput.y);
this.forceMovement = redirectedMovement;
}

public virtual void RedirectInertia2D(float forceMovementX, Vector2 movementInput)
{
float redirectedMovement = Mathf.Sign(movementInput.x)
* forceMovementX;
this.forceMovement.x = redirectedMovement;
}
}
}
11 changes: 11 additions & 0 deletions Assets/CAF/Entities/Managers/EntityPhysicsManager3D.cs.meta

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

30 changes: 30 additions & 0 deletions Assets/CAF/Entities/Managers/EntityPhysicsManagerBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace CAF.Entities
{
public class EntityPhysicsManagerBase : MonoBehaviour
{

[SerializeField] protected EntityManager manager;

public virtual void Tick()
{

}

public virtual void ResetForces()
{

}

/// <summary>
/// Check if we are on the ground.
/// </summary>
public virtual void CheckIfGrounded()
{

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ MonoBehaviour:
damageOnHit: 0
damageOnBlock: 0
causesTumble: 0
opponentForceMagnitude: 1
opponentForceMagnitude: 0.1
opponentForceDir: {x: 1, y: 0, z: 0}
forceIncludeYForce: 0
opponentMaxMagnitude: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,49 +28,49 @@ public void AirDrift()
tempMax = stats.airMaxSpeed * movement.x;
}

if ((tempMax < 0 && manager.PhysicsManager.forceMovement.x < tempMax)
|| (tempMax > 0 && manager.PhysicsManager.forceMovement.x > tempMax))
if ((tempMax < 0 && forceMovement.x < tempMax)
|| (tempMax > 0 && forceMovement.x > tempMax))
{
if (manager.PhysicsManager.forceMovement.x > 0)
if (forceMovement.x > 0)
{
manager.PhysicsManager.forceMovement.x -= stats.aerialFriction;
if (manager.PhysicsManager.forceMovement.x < 0)
forceMovement.x -= stats.aerialFriction;
if (forceMovement.x < 0)
{
manager.PhysicsManager.forceMovement.x = 0;
forceMovement.x = 0;
}
}
else
{
manager.PhysicsManager.forceMovement.x += stats.aerialFriction;
if (manager.PhysicsManager.forceMovement.x > 0)
forceMovement.x += stats.aerialFriction;
if (forceMovement.x > 0)
{
manager.PhysicsManager.forceMovement.x = 0;
forceMovement.x = 0;
}
}
}
else if (Mathf.Abs(movement.x) > 0.3f &&
((tempMax < 0 && manager.PhysicsManager.forceMovement.x > tempMax) || (tempMax > 0 && manager.PhysicsManager.forceMovement.x < tempMax)))
((tempMax < 0 && forceMovement.x > tempMax) || (tempMax > 0 && forceMovement.x < tempMax)))
{
manager.PhysicsManager.forceMovement.x += (stats.airAcceleration * movement.x)
forceMovement.x += (stats.airAcceleration * movement.x)
+ (Mathf.Sign(movement.x) * stats.airBaseAcceleration);
}

if (Mathf.Abs(movement.x) < InputConstants.moveDeadzone)
{
if (manager.PhysicsManager.forceMovement.x > 0)
if (forceMovement.x > 0)
{
manager.PhysicsManager.forceMovement.x -= stats.aerialFriction;
if (manager.PhysicsManager.forceMovement.x < 0)
forceMovement.x -= stats.aerialFriction;
if (forceMovement.x < 0)
{
manager.PhysicsManager.forceMovement.x = 0;
forceMovement.x = 0;
}
}
else
{
manager.PhysicsManager.forceMovement.x += stats.aerialFriction;
if (manager.PhysicsManager.forceMovement.x > 0)
forceMovement.x += stats.aerialFriction;
if (forceMovement.x > 0)
{
manager.PhysicsManager.forceMovement.x = 0;
forceMovement.x = 0;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ public override void Initialize()
{
base.Initialize();
CharacterManager c = GetCharacterController();
storedMovement = c.PhysicsManager.forceMovement;
c.PhysicsManager.forceMovement = Vector3.zero;
c.PhysicsManager.forceGravity = Vector3.zero;
EntityPhysicsManager physicsManager = (EntityPhysicsManager)GetPhysicsManager();
storedMovement = physicsManager.forceMovement;
physicsManager.forceMovement = Vector3.zero;
physicsManager.forceGravity = Vector3.zero;
}

public override void OnUpdate()
Expand All @@ -35,6 +36,7 @@ public override void OnUpdate()
public override bool CheckInterrupt()
{
CharacterManager c = GetCharacterController();
EntityPhysicsManager physicsManager = (EntityPhysicsManager)GetPhysicsManager();
CharacterStats stats = (CharacterStats)c.entityDefinition.GetEntityStats();
if (c.StateManager.CurrentStateFrame == stats.enemyStepLength)
{
Expand All @@ -46,11 +48,11 @@ public override bool CheckInterrupt()
Vector2 movementInput = c.InputManager.GetAxis2D((int)EntityInputs.MOVEMENT);
if (movementInput.magnitude > InputConstants.moveDeadzone)
{
c.PhysicsManager.RedirectInertia2D(storedMovement.x, movementInput);
physicsManager.RedirectInertia(storedMovement.x, movementInput);
}
else
{
c.PhysicsManager.forceMovement = storedMovement;
physicsManager.forceMovement = storedMovement;
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public override void OnUpdate()
CharacterManager c = GetCharacterController();
CharacterStats stats = (CharacterStats)c.entityDefinition.GetEntityStats();

c.PhysicsManager.ApplyMovementFriction(stats.groundFriction);
GetPhysicsManager().ApplyMovementFriction(stats.groundFriction);

CheckInterrupt();
}
Expand Down
Loading

0 comments on commit 1f6d586

Please sign in to comment.