First Commit

This commit is contained in:
Syntriax 2021-12-23 12:41:01 +03:00
parent dbe683c982
commit 6d6687d225
33 changed files with 454 additions and 0 deletions

8
2D.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 247a7892cc48fa54c971733d8e0213db
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,54 @@
using Syntriax.Modules.Movement.ColliderCheck;
using Syntriax.Modules.Movement.ColliderCheck.Ground;
using UnityEngine;
namespace Syntriax.Modules.Movement
{
[RequireComponent(typeof(Rigidbody2D))]
public class OneDimensional2DAirMovement : MonoBehaviour, IMovement, IState
{
private IGroundCheck groundCheck = null;
private Rigidbody2D rigid = null;
private float moveValue = 0f;
public bool IsActive => StateEnabled && !groundCheck.IsCollided();
public bool StateEnabled { get; set; } = true;
public float SpeedMultiplier { get; set; } = 1f;
private void Start()
{
rigid = GetComponent<Rigidbody2D>();
groundCheck = GetComponentInChildren<IGroundCheck>();
}
private void FixedUpdate()
{
if (!IsActive)
return;
ApplyMovement();
}
private void ApplyMovement()
{
Vector2 velocity = rigid.velocity;
velocity.x += moveValue * Time.fixedDeltaTime;
if (moveValue != 0f)
{
if (Mathf.Abs(velocity.x) > Mathf.Abs(moveValue))
velocity.x = moveValue;
else if (Mathf.Abs(velocity.x - moveValue) > Mathf.Abs(moveValue))
velocity.x += moveValue * Time.fixedDeltaTime;
}
rigid.velocity = velocity;
}
public void Move(float x = 0, float y = 0, float z = 0)
{
moveValue = x * SpeedMultiplier;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 12a58daabbf3fa8418a164390a593753
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,44 @@
using Syntriax.Modules.Movement.ColliderCheck;
using Syntriax.Modules.Movement.ColliderCheck.Ground;
using UnityEngine;
namespace Syntriax.Modules.Movement
{
[RequireComponent(typeof(Rigidbody2D))]
public class OneDimensional2DGroundMovement : MonoBehaviour, IMovement, IState
{
private IGroundCheck groundCheck = null;
private Rigidbody2D rigid = null;
private float moveValue = 0f;
public bool IsActive => StateEnabled && groundCheck.IsCollided();
public bool StateEnabled { get; set; } = true;
public float SpeedMultiplier { get; set; } = 1f;
private void Start()
{
rigid = GetComponent<Rigidbody2D>();
groundCheck = GetComponentInChildren<IGroundCheck>();
}
private void FixedUpdate()
{
if (!IsActive)
return;
ApplyMovement();
}
private void ApplyMovement()
{
Vector2 velocity = rigid.velocity;
velocity.x = moveValue;
rigid.velocity = velocity;
}
public void Move(float x = 0, float y = 0, float z = 0)
{
moveValue = x * SpeedMultiplier;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 849a6316df6662a48ae6cfcd4c2fbc1e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

8
ColliderCheck.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 115027fb4693d6247bd07cab06d81091
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 2c348589c341f3d4f8db2e34198ec32e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,4 @@
namespace Syntriax.Modules.Movement.ColliderCheck.Ground
{
public interface IGroundCheck : IColliderCheck { }
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2cbb917bc29d63241aa50c7fe8dc141d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,4 @@
namespace Syntriax.Modules.Movement.ColliderCheck.Ground
{
public class TwoDimensionalBoxChildGroundCheck : TwoDimensionalBoxChildColliderCheck, IGroundCheck { }
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a878fb141e25e954c9b73eff8657605a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,10 @@
using UnityEngine;
namespace Syntriax.Modules.Movement.ColliderCheck
{
public interface IColliderCheck
{
LayerMask ColliderMask { get; set; }
bool IsColliderDetected { get; }
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 164c4b9411986c14e8012b2e7fdd2a6a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
namespace Syntriax.Modules.Movement.ColliderCheck
{
public static class IColliderCheckExtensions
{
public static bool IsCollided(this IColliderCheck colliderCheck)
=> colliderCheck == null ? true : colliderCheck.IsColliderDetected;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8a31fdb05e206b44cbe615353e924344
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,24 @@
using UnityEngine;
namespace Syntriax.Modules.Movement.ColliderCheck
{
public class TwoDimensionalBoxChildColliderCheck : MonoBehaviour, IColliderCheck, IState
{
[SerializeField] private LayerMask colliderMask = 0;
public LayerMask ColliderMask { get => colliderMask; set => colliderMask = value; }
public bool IsColliderDetected
=> StateEnabled ? Physics2D.OverlapBox(transform.position, transform.localScale, 0, ColliderMask) != null : true;
public bool StateEnabled { get; set; } = true;
#if UNITY_EDITOR
private void OnDrawGizmosSelected()
{
Gizmos.color = IsColliderDetected ? Color.green : Color.red;
Gizmos.DrawWireCube(transform.position, transform.lossyScale);
}
#endif
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 090650329160e7b42ba89ccfad00375e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

9
IMovement.cs Normal file
View File

@ -0,0 +1,9 @@
namespace Syntriax.Modules.Movement
{
public interface IMovement
{
float SpeedMultiplier { get; set; }
bool IsActive { get; }
void Move(float x = 0f, float y = 0f, float z = 0f);
}
}

11
IMovement.cs.meta Normal file
View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0c332dd4b3d45ea469fcfb42aa50e3db
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

13
IMovementExtensions.cs Normal file
View File

@ -0,0 +1,13 @@
using UnityEngine;
namespace Syntriax.Modules.Movement
{
public static class IMovementExtensions
{
public static void Move(this IMovement movement, Vector2 moveVector)
=> movement.Move(moveVector.x, moveVector.y);
public static void Move(this IMovement movement, Vector3 moveVector)
=> movement.Move(moveVector.x, moveVector.y, moveVector.z);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a1d3a4ba263ed1648aa205a8a37a574b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

7
IStateSwitch.cs Normal file
View File

@ -0,0 +1,7 @@
namespace Syntriax.Modules.Movement
{
public interface IState
{
bool StateEnabled { get; set; }
}
}

11
IStateSwitch.cs.meta Normal file
View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bc4a8610b5876e14ab81beef027850b2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

3
Movement.asmdef Normal file
View File

@ -0,0 +1,3 @@
{
"name": "Movement"
}

7
Movement.asmdef.meta Normal file
View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: c967acc4be781ca44b42a1887eb1ac7a
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

8
SpecialAction.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 6f411b98930309f4a9251106523e357a
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: a67ce871b9457064096bcbc81804138c
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,70 @@
using Syntriax.Modules.Movement.ColliderCheck;
using Syntriax.Modules.Movement.ColliderCheck.Ground;
using UnityEngine;
namespace Syntriax.Modules.Movement.SpecialAction
{
[RequireComponent(typeof(Rigidbody2D))]
public class PlatformerJump : MonoBehaviour, ISpecialActionActivate, ISpecialActionDeactivate, IState
{
public float JumpSpeed { get; set; } = 0f;
public bool StateEnabled { get; set; } = true;
private bool airSuspension = false;
private float fallMultiplier = 0f;
private float suspensionMultiplier = 0f;
private IGroundCheck groundCheck = null;
private Rigidbody2D rigid = null;
private void Awake()
{
JumpSpeed = 10f;
fallMultiplier = 1.5f * Time.fixedDeltaTime;
suspensionMultiplier = 1f * Time.fixedDeltaTime;
}
private void Start()
{
rigid = GetComponent<Rigidbody2D>();
groundCheck = GetComponentInChildren<IGroundCheck>();
}
private void FixedUpdate()
{
if (!StateEnabled)
return;
if (rigid.velocity.y < 0f)
ApplySuspension(suspensionMultiplier);
else if (!airSuspension)
ApplySuspension(fallMultiplier);
}
private void ApplySuspension(float multipier)
{
rigid.velocity += Physics2D.gravity * multipier;
}
private void Jump()
{
Vector2 velocity = rigid.velocity;
velocity.y = JumpSpeed;
rigid.velocity = velocity;
}
public void OnActivation()
{
if (groundCheck.IsCollided())
Jump();
airSuspension = true;
}
public void OnDeactivation()
{
airSuspension = false;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e45c80df261a5714e9a22fd73a2cd39a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,7 @@
namespace Syntriax.Modules.Movement.SpecialAction
{
public interface ISpecialActionActivate
{
void OnActivation();
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a73af08e7cc67da42bdbfebb05a87d20
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,7 @@
namespace Syntriax.Modules.Movement.SpecialAction
{
public interface ISpecialActionDeactivate
{
void OnDeactivation();
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 81fa878c771128a4d8232969f0c2e460
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: