-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added EntityPhysicsManagerBase.
BREAKING CHANGE: PhysicsManager now allows for your own implementation, with two given implementations for 3D and 2D.
- Loading branch information
1 parent
b28032b
commit 1f6d586
Showing
26 changed files
with
254 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
Assets/CAF/Entities/Managers/EntityPhysicsManager2D.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
Assets/CAF/Entities/Managers/EntityPhysicsManager3D.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
|
||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.