dimanche 24 octobre 2021

Pass Method as Parameter using C# with Parameters - Using the Command Pattern

I'm new to passing delegates and was wondering if the following code is possible to do?

CommandQueue.AddCommand(
    new CommandItem(
        group.MyGroupActionMovement(
            new Vector3(-1000,-1000,-1000))));

ok to elaborate I'm going to do a code dump of my bad attempt at the command pattern...

So I'm trying to create a queue of commands. Starting with just the movement command. It takes a vector3 as a parameter. Other Commands will have different parameters like enums and game objects.

This is supposed to be my interface for commands/actions but, I've been messing with it to see what I can do.

public class IGroupAction: MonoBehaviour
{
    public delegate bool Del(Vector3 destination);

    public virtual bool Execute()
    {
        return true;
    }
}

This is supposed to be the command/action unit/item

public class GroupActionItem
    {
     public IGroupAction MyGroupAction;

     public GroupActionItem(IGroupAction.Del action)
    {
            
    }
       
    public bool PerformAction()
    {
      return MyGroupAction.Execute();
    }
}

This is the command pattern that I've completed so far

public class GroupAction
{
    public List<GroupActionItem> Actions;

    public GroupAction()
    {
        Actions = new List<GroupActionItem>();
    }
        
    public void AddAction(GroupActionItem groupActionItem)
    {
        Actions.Add(groupActionItem);
    }
        
    public void ClearActions()
    {
        Actions.Clear();
    }

    public void PerformActions()
    {
        if (Actions.Count >= 1)
        {
            if(Actions[0].PerformAction())
            {
                Actions.Remove(Actions[0]);
            }
        }
    }
}

This is where I execute the movement command

public class GroupActionMovement : IGroupAction
{
    public override bool Execute()
    {
        return Movement();
    }

    public bool Movement(Vector3 destination)
    {
        return true;
    }
}

public class Group : MonoBehaviour
{
    public GroupActionMovement MyGroupActionMovement;
}

This is the execution of the coded behavior.

public class Player : MonoBehaviour
{
    
    public Dictionary<Group,GroupAction> PlayerGroupsActions;
    public List<Group> Groups;
    
    void Start()
    {
        PlayerGroupsActions = new Dictionary<Group, GroupAction>();
        Groups = GetComponentsInChildren<Group>().ToList();
        foreach (Group group in Groups)
        {
            GroupAction playerGroupActions = new GroupAction();
            PlayerGroupsActions.Add(group,playerGroupActions);
        }
    }
    
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            //remove the foreach
            foreach (Group group in Groups)
            {
                //directly apply the group
                PlayerGroupsActions.TryGetValue(group, out GroupAction playerGroupActions);
                if (playerGroupActions != null)
                {
                    playerGroupActions.AddAction(new GroupActionItem(group.MyGroupActionMovement.Movement));
                }
            }
        }

        foreach (Group group in Groups)
        {
            PlayerGroupsActions.TryGetValue(group, out GroupAction playerFormationActions);
            if (playerFormationActions != null)
            {
                if (playerFormationActions.Actions.Count != 0)
                {
                    playerFormationActions.PerformActions();
                }
            }
        }
    }
}

I'm sorry if this isn't the best explanation of what is going on or, that a code dump is not the best way to explain what i'm doing.

Aucun commentaire:

Enregistrer un commentaire