Compare commits

...

2 Commits

Author SHA1 Message Date
Syntriax ea79b92982 Update Readme 2022-11-18 12:18:35 +03:00
Syntriax fd30c829e8 Movement Definition Applier Added 2022-11-18 12:11:49 +03:00
3 changed files with 96 additions and 2 deletions

View File

@ -0,0 +1,73 @@
using System;
using Syntriax.Modules.Movement.Config;
using UnityEngine;
namespace Syntriax.Modules.Movement
{
public class MovementDefinitionApplier : MonoBehaviour
{
[Tooltip("The definition's name as defined in it's \"Name\" field")]
[SerializeField] private string startupDefinitionName = "";
private string appliedDefinitionName = "";
public Action<GameObject> OnMovementDefinitionApplied = null;
public Action<GameObject> OnMovementDefinitionRemoved = null;
private void Awake()
{
if (string.IsNullOrWhiteSpace(startupDefinitionName))
return;
LoadDefinition(startupDefinitionName);
}
public void LoadDefinition(string definitionName)
{
RemoveDefinition();
MovementDefinitionFactory.Instance.ApplyDefinitionToGameObject(gameObject, definitionName);
appliedDefinitionName = definitionName;
OnMovementDefinitionApplied?.Invoke(gameObject);
}
public void LoadDefinition(MovementDefinition definition)
{
RemoveDefinition();
MovementDefinitionFactory.Instance.ApplyDefinitionToGameObject(gameObject, definition);
appliedDefinitionName = definition.Name;
OnMovementDefinitionApplied?.Invoke(gameObject);
}
public void RemoveDefinition()
{
if (string.IsNullOrWhiteSpace(appliedDefinitionName))
return;
MovementDefinition appliedDefinition = MovementDefinitionFactory.Instance.Definitions[appliedDefinitionName];
if (appliedDefinition.MovementConfigs == null || appliedDefinition.MonoBehaviours == null)
return;
foreach (MovementConfig movementConfig in appliedDefinition.MovementConfigs)
{
Type type = MovementFactory.Instance.Types[movementConfig.TypeName];
if (TryGetComponent(type, out Component component))
Destroy(component);
}
foreach (string monoBehaviour in appliedDefinition.MonoBehaviours)
{
Type type = MovementFactory.Instance.Types[monoBehaviour];
if (TryGetComponent(type, out Component component))
Destroy(component);
}
appliedDefinition = default;
OnMovementDefinitionRemoved?.Invoke(gameObject);
}
[ContextMenu("Load Longer")] private void LoadLonger() => LoadDefinition("Example Movement Definition (Longer)");
[ContextMenu("Load Quick")] private void LoadQuick() => LoadDefinition("Example Movement Definition (Quick)");
[ContextMenu("Remove Definition")] private void UnloadDefinition() => RemoveDefinition();
}
}

View File

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

View File

@ -12,14 +12,24 @@ Or you can call ```git submodule init``` to get them automatically added (note t
## Getting Started 2D ## Getting Started 2D
### Quick ### Automaticaly
#### Custom The Editor
1. Find the button on the Unity Editor's top panel named `Syntriax`, Under `Modules/Movement` open the window `Definition Creator`
3. You can find two buttons for both `Quick` and `Longer` presets, or you can define your own, then press `Create` button
4. Add `MovementDefinitionApplier` component to your Player `GameObject`
5. Put the name of the definition you created in the first step to `Startup Definition Name` field
- You can also call `MovementDefinitionApplier.LoadDefinition` method manually to load them via anoher script.
### Manually
#### Quick
1. Clone the module to a folder in your Assets folder 1. Clone the module to a folder in your Assets folder
2. Add `MovementController` component to your Player `GameObject` 2. Add `MovementController` component to your Player `GameObject`
3. Add `GroundMovement1D` component to your Player `GameObject` 3. Add `GroundMovement1D` component to your Player `GameObject`
4. Connect your inputs to the `MovementController` ([Example with Input System](#input-system-example)) 4. Connect your inputs to the `MovementController` ([Example with Input System](#input-system-example))
### Longer #### Longer
1. Clone the module to a folder in your Assets folder 1. Clone the module to a folder in your Assets folder
2. Add `ToggleStateMonoBehaviour` component to your Player `GameObject` 2. Add `ToggleStateMonoBehaviour` component to your Player `GameObject`