Action
Example code
In the Action file, we define the action enum and the main action class.
Using an explicit namespace like MyGame.Inputs
is recommanded.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using EODE.Wonderland;
namespace MyProject {
public enum ActionType {
Unknown,
Main,
Secondary,
ControllerStatus
}
public class MonoAction : MonoBehaviour, IAction<ActionType> {
protected virtual ActionType Type => ActionType.Unknown;
void Start() {
// auto remove other same type actions
foreach (var act in GetComponents<MonoAction>()) {
if (act != this && act.Type == Type) {
act.StopIt();
act.SafeDestroy();
}
}
// set skill
GetComponent<Player>()?.InputsPlayer.SetAction(Type, this);
}
void OnDestroy() {
var player = GetComponent<Player>();
if (player != null && player.InputsPlayer.GetAction(Type) == (this as IAction<ActionType>))
GetComponent<Player>()?.InputsPlayer.SetAction(Type, null);
}
public virtual void DoIt() { }
public virtual void StopIt() { }
}
}
ActionType
It is a list of actions. Main action, secondary action, enable/disable controller...
MonoAction
In this example, we implement IAction in a MonoBehaviour. We want to set the action by adding the component directly on the player. After we can create a new skill like this :
using EODE.Wonderland.Example;
using UnityEngine;
public class Fireball : MonoAction {
protected override ActionType Type => ActionType.Main;
public override void DoIt() {
Debug.Log("Fireball cast");
}
public override void StopIt() {
Debug.Log("Fireball release");
}
}
In your implemented action you can add a cast system or create other spell types with a simple inheritance :
- IAction -> MonoAction -> InstantAction
- IAction -> MonoAction -> CastAndCooldownAction
- ...
IAction implementation is not necessary a MonoBehaviour, you will set manually the skill to the player.