- Movement & Controller
- Special Actions
- ToggleState
- ColliderChecker
- and 2D Implementations
This commit is contained in:
Syntriax 2022-03-08 10:13:27 +03:00
parent 91839d2323
commit f0ffc2a8c4
81 changed files with 419 additions and 751 deletions

62
2D/MovementController.cs Normal file
View File

@ -0,0 +1,62 @@
using System.Collections.Generic;
using Syntriax.Modules.Movement.State;
using UnityEngine;
using UnityEngine.Events;
namespace Syntriax.Modules.Movement
{
public class MovementController : MonoBehaviour, IMovementController
{
public IMovement ActiveMovement { get; protected set; } = null;
public UnityEvent<IMovement> OnMovementChanged { get; protected set; } = new UnityEvent<IMovement>();
protected List<IMovement> movements = new List<IMovement>(32);
protected IToggleState toggleState = null;
protected virtual void Start()
{
toggleState = GetComponent<IToggleState>();
UpdateMovementCache();
}
protected virtual void FixedUpdate()
{
if (!toggleState.Toggled)
return;
ActiveMovement.ApplyMovement();
}
protected virtual void UpdateMovementCache()
{
foreach (IMovement movement in movements)
movement.OnTakeOverStateChanged.RemoveListener(OnTakeOverListener);
movements.Clear();
GetComponents<IMovement>(movements);
UpdateActiveMovement();
foreach (IMovement movement in movements)
movement.OnTakeOverStateChanged.AddListener(OnTakeOverListener);
}
private void OnTakeOverListener(bool arg0) => UpdateActiveMovement();
protected virtual void UpdateActiveMovement()
{
foreach (IMovement movement in movements)
if (movement.CanTakeOver)
{
ActiveMovement = movement;
OnMovementChanged.Invoke(ActiveMovement);
return;
}
ActiveMovement = movements[movements.Count - 1];
OnMovementChanged.Invoke(ActiveMovement);
}
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: b1da221e5cee86a429230fca11bdf8dd guid: f9a57082945be29498d2785f8be6484d
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -1,54 +0,0 @@
using Syntriax.Modules.Movement.ColliderCheck;
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 BaseSpeed { get; set; } = 1f;
public float MovementMultiplier { 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 * BaseSpeed * MovementMultiplier;
}
}
}

View File

@ -1,44 +0,0 @@
using Syntriax.Modules.Movement.ColliderCheck;
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 BaseSpeed { get; set; } = 1f;
public float MovementMultiplier { 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 * BaseSpeed * MovementMultiplier;
}
}
}

37
2D/OneWay2DAirMovement.cs Normal file
View File

@ -0,0 +1,37 @@
using Syntriax.Modules.Movement.ColliderTrigger;
using UnityEngine;
namespace Syntriax.Modules.Movement
{
public class OneWay2DAirMovement : OneWay2DMovementBase
{
protected override float moveValue { get; set; } = 0f;
protected IGroundTrigger groundTrigger = null;
protected override void Start()
{
base.Start();
groundTrigger = GetComponentInChildren<IGroundTrigger>();
groundTrigger.OnTriggered.AddListener(OnGroundTrigger);
}
private void OnGroundTrigger(bool isGrounded)
=> CanTakeOver = !isGrounded;
public override 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;
}
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 12a58daabbf3fa8418a164390a593753 guid: 2f38efc38cadfd94ba8698189577f60f
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -0,0 +1,28 @@
using Syntriax.Modules.Movement.ColliderTrigger;
using UnityEngine;
namespace Syntriax.Modules.Movement
{
public class OneWay2DGroundMovement : OneWay2DMovementBase
{
protected override float moveValue { get; set; } = 0f;
protected IGroundTrigger groundTrigger = null;
protected override void Start()
{
base.Start();
groundTrigger = GetComponentInChildren<IGroundTrigger>();
groundTrigger.OnTriggered.AddListener(OnGroundTrigger);
}
private void OnGroundTrigger(bool isGrounded)
=> CanTakeOver = isGrounded;
public override void ApplyMovement()
{
Vector2 velocity = rigid.velocity;
velocity.x = moveValue;
rigid.velocity = velocity;
}
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 2cbb917bc29d63241aa50c7fe8dc141d guid: 9c73429b47407634daead7ee1b52629d
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -0,0 +1,10 @@
namespace Syntriax.Modules.Movement
{
public abstract class OneWay2DMovementBase : TwoDimensionalMovementBase
{
protected abstract float moveValue { get; set; }
public override void Move(float x = 0, float y = 0, float z = 0)
=> moveValue = x * BaseSpeed * MovementMultiplier;
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 849a6316df6662a48ae6cfcd4c2fbc1e guid: 51ee606e68723324b92b9d16835c1625
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -0,0 +1,48 @@
using Syntriax.Modules.Movement.State;
using UnityEngine;
using UnityEngine.Events;
namespace Syntriax.Modules.Movement
{
[RequireComponent(typeof(Rigidbody2D))]
public abstract class TwoDimensionalMovementBase : MonoBehaviour, IMovement
{
protected Rigidbody2D rigid = null;
protected IToggleState toggleState = null;
private bool _canTakeOver = false;
public float BaseSpeed { get; set; } = 1f;
public float MovementMultiplier { get; set; } = 1f;
public UnityEvent<bool> OnTakeOverStateChanged { get; protected set; } = null;
public bool CanTakeOver
{
get => _canTakeOver;
protected set
{
bool isNewValue = _canTakeOver != value;
_canTakeOver = value;
if (isNewValue)
OnTakeOverStateChanged.Invoke(value);
}
}
public IToggleState ToggleState { get; protected set; } = null;
protected virtual void Awake()
{
OnTakeOverStateChanged = new UnityEvent<bool>();
}
protected virtual void Start()
{
rigid = GetComponent<Rigidbody2D>();
toggleState = GetComponent<ToggleState>();
}
public abstract void Move(float x = 0, float y = 0, float z = 0);
public abstract void ApplyMovement();
}
}

View File

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

View File

@ -1,7 +0,0 @@
namespace Syntriax.Modules.Movement.ColliderCheck
{
public class ColliderCheckFactory : TypeFactoryBaseMonoBehaviour<ColliderCheckFactory, IColliderCheck>
{
protected override int InitialCapacity => 8;
}
}

View File

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

View File

@ -0,0 +1,4 @@
namespace Syntriax.Modules.Movement.ColliderTrigger
{
public interface IGroundTrigger : IColliderTrigger { }
}

View File

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

View File

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

View File

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

View File

@ -0,0 +1,4 @@
namespace Syntriax.Modules.Movement.ColliderTrigger
{
public class TwoDimensionalBoxChildGroundTrigger : TwoDimensionalBoxChildColliderTrigger, IGroundTrigger { }
}

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,12 @@
using UnityEngine;
using UnityEngine.Events;
namespace Syntriax.Modules.Movement.ColliderTrigger
{
public interface IColliderTrigger
{
LayerMask ColliderMask { get; set; }
bool IsTrigerred { get; }
UnityEvent<bool> OnTriggered { get; }
}
}

View File

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

View File

@ -1,22 +1,40 @@
using UnityEngine; using UnityEngine;
using UnityEngine.Events;
namespace Syntriax.Modules.Movement.ColliderCheck namespace Syntriax.Modules.Movement.ColliderTrigger
{ {
public class TwoDimensionalBoxChildColliderCheck : MonoBehaviour, IColliderCheck, IState public class TwoDimensionalBoxChildColliderTrigger : MonoBehaviour, IColliderTrigger
{ {
[SerializeField] private LayerMask colliderMask = 0; [SerializeField] private LayerMask colliderMask = 0;
private bool _isTrigerred = false;
public LayerMask ColliderMask { get => colliderMask; set => colliderMask = value; } public LayerMask ColliderMask { get => colliderMask; set => colliderMask = value; }
public bool IsColliderDetected public UnityEvent<bool> OnTriggered { get; protected set; } = null;
=> StateEnabled ? Physics2D.OverlapBox(transform.position, transform.localScale, 0, ColliderMask) != null : true; public bool IsTrigerred
{
get => _isTrigerred;
protected set
{
bool isNewValue = _isTrigerred != value;
public bool StateEnabled { get; set; } = true; _isTrigerred = value;
if (isNewValue)
OnTriggered.Invoke(value);
}
}
protected void Awake()
=> OnTriggered = new UnityEvent<bool>();
protected void FixedUpdate()
=> IsTrigerred = Physics2D.OverlapBox(transform.position, transform.localScale, 0, ColliderMask) != null;
#if UNITY_EDITOR #if UNITY_EDITOR
private void OnDrawGizmosSelected() private void OnDrawGizmosSelected()
{ {
Gizmos.color = IsColliderDetected ? Color.green : Color.red; Gizmos.color = IsTrigerred ? Color.green : Color.red;
Gizmos.DrawWireCube(transform.position, transform.lossyScale); Gizmos.DrawWireCube(transform.position, transform.lossyScale);
} }
#endif #endif

View File

@ -1,10 +1,17 @@
using UnityEngine.Events;
namespace Syntriax.Modules.Movement namespace Syntriax.Modules.Movement
{ {
public interface IMovement public interface IMovement
{ {
float BaseSpeed { get; set; } float BaseSpeed { get; set; }
float MovementMultiplier { get; set; } float MovementMultiplier { get; set; }
bool IsActive { get; }
bool CanTakeOver { get; }
UnityEvent<bool> OnTakeOverStateChanged { get; }
void Move(float x = 0f, float y = 0f, float z = 0f); void Move(float x = 0f, float y = 0f, float z = 0f);
void ApplyMovement();
} }
} }

View File

@ -1,4 +1,10 @@
using UnityEngine.Events;
namespace Syntriax.Modules.Movement namespace Syntriax.Modules.Movement
{ {
public interface IMovementController : IState { } public interface IMovementController
{
IMovement ActiveMovement { get; }
UnityEvent<IMovement> OnMovementChanged { get; }
}
} }

View File

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

View File

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

View File

@ -1,7 +0,0 @@
namespace Syntriax.Modules.Movement
{
public class MovementControllerFactory : TypeFactoryBaseMonoBehaviour<MovementControllerFactory, IMovementController>
{
protected override int InitialCapacity => 8;
}
}

View File

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

View File

@ -1,7 +0,0 @@
namespace Syntriax.Modules.Movement
{
public class MovementFactory : TypeFactoryBaseMonoBehaviour<MovementFactory, IMovement>
{
protected override int InitialCapacity => 8;
}
}

View File

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

View File

@ -1,20 +1,22 @@
using Syntriax.Modules.Movement.ColliderCheck; using Syntriax.Modules.Movement.ColliderTrigger;
using Syntriax.Modules.Movement.State;
using UnityEngine; using UnityEngine;
namespace Syntriax.Modules.Movement.SpecialAction namespace Syntriax.Modules.Movement.SpecialAction
{ {
[RequireComponent(typeof(Rigidbody2D))] [RequireComponent(typeof(Rigidbody2D))]
public class PlatformerJump : MonoBehaviour, ISpecialActionActivate, ISpecialActionDeactivate, IState public class PlatformerJump : MonoBehaviour, ISpecialActionActivate, ISpecialActionDeactivate
{ {
public float JumpSpeed { get; set; } = 0f; public float JumpSpeed { get; set; } = 0f;
public bool StateEnabled { get; set; } = true; public IToggleState EnabledToggleState { get; private set; } = null;
private bool airSuspension = false; private bool airSuspension = false;
private float fallMultiplier = 0f; private float fallMultiplier = 0f;
private float suspensionMultiplier = 0f; private float suspensionMultiplier = 0f;
private IGroundCheck groundCheck = null; private IGroundTrigger groundCheck = null;
private Rigidbody2D rigid = null; private Rigidbody2D rigid = null;
private IToggleState toggleState = null;
private void Awake() private void Awake()
@ -22,17 +24,19 @@ namespace Syntriax.Modules.Movement.SpecialAction
JumpSpeed = 10f; JumpSpeed = 10f;
fallMultiplier = 1.5f * Time.fixedDeltaTime; fallMultiplier = 1.5f * Time.fixedDeltaTime;
suspensionMultiplier = 1f * Time.fixedDeltaTime; suspensionMultiplier = 1f * Time.fixedDeltaTime;
EnabledToggleState = new MemberToggleState();
} }
private void Start() private void Start()
{ {
rigid = GetComponent<Rigidbody2D>(); rigid = GetComponent<Rigidbody2D>();
groundCheck = GetComponentInChildren<IGroundCheck>(); groundCheck = GetComponentInChildren<IGroundTrigger>();
toggleState = GetComponent<IToggleState>();
} }
private void FixedUpdate() private void FixedUpdate()
{ {
if (!StateEnabled) if (!EnabledToggleState.Toggled)
return; return;
if (rigid.velocity.y < 0f) if (rigid.velocity.y < 0f)
@ -53,16 +57,22 @@ namespace Syntriax.Modules.Movement.SpecialAction
rigid.velocity = velocity; rigid.velocity = velocity;
} }
public void OnActivation() public void Activate()
{ {
if (groundCheck.IsCollided()) if (!toggleState.Toggled)
return;
if (groundCheck.IsTrigerred)
Jump(); Jump();
airSuspension = true; airSuspension = true;
} }
public void OnDeactivation() public void Deactivate()
{ {
if (!toggleState.Toggled)
return;
airSuspension = false; airSuspension = false;
} }
} }

View File

@ -1,7 +1,11 @@
using Syntriax.Modules.Movement.State;
namespace Syntriax.Modules.Movement.SpecialAction namespace Syntriax.Modules.Movement.SpecialAction
{ {
public interface ISpecialActionActivate public interface ISpecialActionActivate
{ {
void OnActivation(); IToggleState EnabledToggleState { get; }
void Activate();
} }
} }

View File

@ -1,7 +1,11 @@
using Syntriax.Modules.Movement.State;
namespace Syntriax.Modules.Movement.SpecialAction namespace Syntriax.Modules.Movement.SpecialAction
{ {
public interface ISpecialActionDeactivate public interface ISpecialActionDeactivate
{ {
void OnDeactivation(); IToggleState EnabledToggleState { get; }
void Deactivate();
} }
} }

View File

@ -1,7 +0,0 @@
namespace Syntriax.Modules.Movement.SpecialAction
{
public class SpecialActionDeactivateFactory : TypeFactoryBaseMonoBehaviour<SpecialActionDeactivateFactory, ISpecialActionDeactivate>
{
protected override int InitialCapacity => 8;
}
}

View File

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

View File

@ -1,7 +0,0 @@
namespace Syntriax.Modules.Movement.SpecialAction
{
public class SpecialActionActivateFactory : TypeFactoryBaseMonoBehaviour<SpecialActionActivateFactory, ISpecialActionActivate>
{
protected override int InitialCapacity => 8;
}
}

View File

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

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: f563d60029ac74b4f960a1fb0f28ac5c guid: e5f744b49d70a8d418f1bc6fd0c128fa
folderAsset: yes folderAsset: yes
DefaultImporter: DefaultImporter:
externalObjects: {} externalObjects: {}

10
State/IToggleState.cs Normal file
View File

@ -0,0 +1,10 @@
using UnityEngine.Events;
namespace Syntriax.Modules.Movement.State
{
public interface IToggleState
{
bool Toggled { get; set; }
UnityEvent<bool> OnToggleStateChanged { get; }
}
}

View File

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

View File

@ -0,0 +1,25 @@
using UnityEngine.Events;
namespace Syntriax.Modules.Movement.State
{
public class MemberToggleState : IToggleState
{
private bool _enabled = true;
public bool Toggled
{
get => _enabled;
set
{
bool isNewValue = _enabled != value;
_enabled = value;
if (isNewValue)
OnToggleStateChanged.Invoke(value);
}
}
public UnityEvent<bool> OnToggleStateChanged { get; protected set; } = new UnityEvent<bool>();
}
}

View File

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

25
State/ToggleState.cs Normal file
View File

@ -0,0 +1,25 @@
using UnityEngine;
using UnityEngine.Events;
namespace Syntriax.Modules.Movement.State
{
public class ToggleState : MonoBehaviour, IToggleState
{
private bool _enabled = true;
public bool Toggled
{
get => _enabled;
set
{
bool isNewValue = _enabled != value;
_enabled = value;
if (isNewValue)
OnToggleStateChanged.Invoke(value);
}
}
public UnityEvent<bool> OnToggleStateChanged { get; protected set; } = new UnityEvent<bool>();
}
}

11
State/ToggleState.cs.meta Normal file
View File

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

View File

@ -1,98 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
namespace Syntriax.Modules.Movement
{
public abstract class TypeFactoryBaseMonoBehaviour<T, T2> : MonoBehaviour
where T : TypeFactoryBaseMonoBehaviour<T, T2>
where T2 : class
{
protected abstract int InitialCapacity { get; }
private Func<Type, bool> predicate => type => type.IsClass && !type.IsAbstract && type.IsSubclassOf(typeof(MonoBehaviour)) && type.GetInterfaces().Contains(typeof(T2));
private string factoryName => typeof(T).Name;
private static TypeFactoryBaseMonoBehaviour<T, T2> _instance = null;
public static TypeFactoryBaseMonoBehaviour<T, T2> Instance
{
get
{
if (_instance == null)
{
GameObject newGameObject = new GameObject();
TypeFactoryBaseMonoBehaviour<T, T2> typeFactoryBase = newGameObject.AddComponent<T>();
newGameObject.name = typeFactoryBase.factoryName;
_instance = typeFactoryBase;
}
return _instance;
}
}
private Dictionary<string, Type> _types = null;
public Dictionary<string, Type> Types
{
get
{
Initialize();
return _types;
}
}
public void Initialize()
{
if (_types != null)
return;
_types = new Dictionary<string, Type>(InitialCapacity);
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
foreach (Type type in assembly.GetTypes().Where(predicate))
_types.Add(GetTypeClassName(type), type);
}
public virtual void Start()
{
if (_instance != this)
Destroy(this);
}
public void AddTypes(List<(string ClassName, Type TypeReference)> types)
{
foreach (var pair in types)
AddType(pair.ClassName, pair.TypeReference);
}
public void AddType(string className, Type type)
{
if (Types.ContainsKey(className))
throw new ArgumentException($"Component with the key \"{ className }\" already been loaded!");
Types.Add(className, type);
}
public void LoadTypesFromAssembly(Assembly assembly)
{
foreach (Type type in assembly.GetTypes().Where(predicate))
AddType(GetTypeClassName(type), type);
}
public T2 AddComponentToGameObject(GameObject gameObject, string className)
{
if (!Types.ContainsKey(className))
throw new ArgumentException($"Component with the key \"{ className }\" does not exist!");
return gameObject.AddComponent(Types[className]) as T2;
}
public static string GetTypeClassName(Type type)
{
if (string.IsNullOrEmpty(type.Namespace))
return type.Name;
return $"{ type.Namespace }.{ type.Name }";
}
}
}

View File

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

View File

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

View File

@ -1,12 +0,0 @@
namespace Syntriax.Modules.Movement.VariableMovement
{
public class DefaulVariableMovementAsset : IVariableMovementAsset
{
public static DefaulVariableMovementAsset _instance = new DefaulVariableMovementAsset();
public static DefaulVariableMovementAsset Instance => _instance;
public string MovementName => "Default Movement";
public float MovementMultiplier { get; } = 1f;
}
}

View File

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

View File

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

View File

@ -1,7 +0,0 @@
namespace Syntriax.Modules.Movement.VariableMovement
{
public class VariableMovementAssetFactory : TypeFactoryBaseMonoBehaviour<VariableMovementAssetFactory, IVariableMovementAsset>
{
protected override int InitialCapacity => 8;
}
}

View File

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

View File

@ -1,7 +0,0 @@
namespace Syntriax.Modules.Movement.VariableMovement
{
public class VariableMovementCollectionFactory : TypeFactoryBaseMonoBehaviour<VariableMovementCollectionFactory, IVariableMovementCollection>
{
protected override int InitialCapacity => 8;
}
}

View File

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

View File

@ -1,7 +0,0 @@
namespace Syntriax.Modules.Movement.VariableMovement
{
public class VariableMovementControllerFactory : TypeFactoryBaseMonoBehaviour<VariableMovementControllerFactory, IVariableMovementController>
{
protected override int InitialCapacity => 8;
}
}

View File

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

View File

@ -1,7 +0,0 @@
namespace Syntriax.Modules.Movement.VariableMovement
{
public class VariableMovementFactory : TypeFactoryBaseMonoBehaviour<VariableMovementFactory, IVariableMovement>
{
protected override int InitialCapacity => 8;
}
}

View File

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

View File

@ -1,7 +0,0 @@
namespace Syntriax.Modules.Movement.VariableMovement
{
public interface IVariableMovement : IState
{
IVariableMovementAsset VariableMovementAsset { get; set; }
}
}

View File

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

View File

@ -1,8 +0,0 @@
namespace Syntriax.Modules.Movement.VariableMovement
{
public interface IVariableMovementAsset
{
string MovementName { get; }
float MovementMultiplier { get; }
}
}

View File

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

View File

@ -1,10 +0,0 @@
using System.Collections.Generic;
namespace Syntriax.Modules.Movement.VariableMovement
{
public interface IVariableMovementCollection
{
IVariableMovementAsset DefaultVariableMovementsAsset { get; }
List<IVariableMovementAsset> VariableMovementsAssets { get; }
}
}

View File

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

View File

@ -1,11 +0,0 @@
using System.Collections.Generic;
namespace Syntriax.Modules.Movement.VariableMovement
{
public interface IVariableMovementController : IState
{
List<IVariableMovement> VariableMovements { get; }
IVariableMovementCollection VariableMovementCollection { get; set; }
IVariableMovement CurrentVariableMovement { get; }
}
}

View File

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

View File

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

View File

@ -1,23 +0,0 @@
namespace Syntriax.Modules.Movement.VariableMovement.SimpleImplementation
{
public class SimpleVariableMovement : IVariableMovement
{
protected IVariableMovementAsset _variableMovementAsset = null;
public IVariableMovementAsset VariableMovementAsset
{
get => _variableMovementAsset;
set
{
_variableMovementAsset = value;
StateEnabled = false;
}
}
public bool StateEnabled { get; set; } = false;
public SimpleVariableMovement() { }
public SimpleVariableMovement(IVariableMovementAsset variableMovementAsset)
=> VariableMovementAsset = variableMovementAsset;
}
}

View File

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

View File

@ -1,13 +0,0 @@
using UnityEngine;
namespace Syntriax.Modules.Movement.VariableMovement.SimpleImplementation
{
[CreateAssetMenu(fileName = "Simple Variable Movement Asset", menuName = "Syntriax/Modules/Movement/VariableMovement/Simple Variable Movement Asset", order = 0)]
public class SimpleVariableMovementAsset : ScriptableObject, IVariableMovementAsset
{
[SerializeField] protected float movementMultiplier = 1f;
public string MovementName => this.name;
public float MovementMultiplier => movementMultiplier;
}
}

View File

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

View File

@ -1,15 +0,0 @@
using System.Collections.Generic;
using UnityEngine;
namespace Syntriax.Modules.Movement.VariableMovement.SimpleImplementation
{
[CreateAssetMenu(fileName = "Simple VM Asset Collection", menuName = "Syntriax/Modules/Movement/VariableMovement/Simple VM Asset Collection", order = 0)]
public class SimpleVariableMovementCollection : ScriptableObject, IVariableMovementCollection
{
[SerializeField] protected SimpleVariableMovementAsset defaultVariableMovementsAsset = null;
[SerializeField] protected List<SimpleVariableMovementAsset> variableMovementsAssets = new List<SimpleVariableMovementAsset>();
public IVariableMovementAsset DefaultVariableMovementsAsset => defaultVariableMovementsAsset;
public List<IVariableMovementAsset> VariableMovementsAssets => variableMovementsAssets.ConvertAll<IVariableMovementAsset>(simple => (IVariableMovementAsset)simple);
}
}

View File

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

View File

@ -1,50 +0,0 @@
using System.Collections.Generic;
using UnityEngine;
namespace Syntriax.Modules.Movement.VariableMovement.SimpleImplementation
{
public class SimpleVariableMovementController : MonoBehaviour, IVariableMovementController
{
protected IVariableMovement _defaultVariableMovement = new SimpleVariableMovement();
protected IVariableMovementCollection _variableMovementCollection = null;
public bool StateEnabled { get; set; } = false;
public List<IVariableMovement> VariableMovements { get; protected set; } = new List<IVariableMovement>();
public IVariableMovementCollection VariableMovementCollection
{
get => _variableMovementCollection;
set
{
_variableMovementCollection = value;
VariableMovements.Clear();
_defaultVariableMovement.VariableMovementAsset = value.DefaultVariableMovementsAsset;
_defaultVariableMovement.StateEnabled = true;
if (VariableMovements.Capacity < value.VariableMovementsAssets.Count)
VariableMovements.Capacity = value.VariableMovementsAssets.Count;
foreach (var asset in value.VariableMovementsAssets)
VariableMovements.Add(new SimpleVariableMovement(asset));
}
}
public IVariableMovement CurrentVariableMovement
{
get
{
if (!StateEnabled)
return _defaultVariableMovement;
foreach (IVariableMovement variableMovement in VariableMovements)
if (variableMovement.StateEnabled)
return variableMovement;
return _defaultVariableMovement;
}
}
}
}

View File

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

View File

@ -1,24 +0,0 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Syntriax.Modules.Movement.VariableMovement
{
public class VariableMovementBehaviourApplier : MonoBehaviour
{
private List<IMovement> movements = new List<IMovement>();
private IVariableMovementController variableMovementController = null;
private void Start()
{
movements = GetComponents<IMovement>().ToList();
variableMovementController = GetComponent<IVariableMovementController>();
}
private void Update()
{
foreach (var movement in movements)
movement.MovementMultiplier = variableMovementController.CurrentVariableMovement.VariableMovementAsset.MovementMultiplier;
}
}
}

View File

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