Search Unity

What would be my best approach for an authorization of actions?

Discussion in 'Scripting' started by Squabbler, Oct 11, 2013.

  1. Squabbler

    Squabbler

    Joined:
    Jan 11, 2013
    Posts:
    11
    Quickies
    • I'm making a simple 2D bomberman-type game, but the player can only move horizontally and "throws" bombs vertically.
    • The entire game will be point and click (for mobile reasons).

    Basic Idea: I'm trying to understand how the flow of authorizing actions works.

    All clicks or taps are being handled with a simple Singleton RaycastHandler script that is attached to the camera. This script just simply checks for a hit and fires an OnClick method in the hit game object's class, like an event listener essentially.

    The player will have 3 different actions: Idle, Move, Throw. However some are unable to be performed simultaneously or at all if another action is in progress (ex: can't move if you are throwing, but you can throw if you are moving - the moving stops temporarily until the throw action is completed, then resumes).

    I have a Singleton ActionAuthorizer class that I'm currently working on. But I'm having trouble wrapping my head around the order of operations. For example, and the simplest version, the player by default starts in Idle. If I click on the plane somewhere, I want it to authorize the action before the player moves.

    I'm expecting the class to return either true or false to allow the action. How would I access this from say my PlayerMovement script (attached to the plane the character is restricted to) where I can simply use an if statement to authorize? Am I even going about this the correct way?

    Edit: Figured this part out
    Code (csharp):
    1.  
    2. if (ActionAuthorizer.Instance.Move()) {
    3.   // permission to move granted
    4.   // fire the action
    5. }
    What if I need to return to a previous action that has to wait for a higher priority action? Or in simpler terms, pause an action while another one is performed, then return to continue the paused action. EX: player is running to the right but fires the action to throw a bomb. Running has to pause while the bomb action is completed, then resume running to the point originally assigned.

    Also, below is the ActionAuthorizer class so far.

    Thanks!

    ActionAuthorizer.cs -- EDIT: Updated
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ActionAuthorizer : MonoBehaviour {
    6.  
    7.   // Singleton
    8.   private static ActionAuthorizer instance;
    9.  
    10.   public static ActionAuthorizer Instance {
    11.  
    12.     get {
    13.       return instance ?? (instance = GameObject.FindObjectOfType(typeof(ActionAuthorizer)) as ActionAuthorizer);
    14.     }
    15.    
    16.   }
    17.  
    18.   // player action options
    19.   private enum PlayerAction {Idle, Move, Throw};
    20.  
    21.   // set the default action to idle
    22.   private PlayerAction currentAction = PlayerAction.Idle;
    23.  
    24.  
    25.   // Use this for initialization
    26.   void Start () {}
    27.    
    28.   // Update is called once per frame
    29.   void Update () {}
    30.  
    31.   // authorize move
    32.   public bool Move() {
    33.    
    34.     if (currentAction != PlayerAction.Throw) {
    35.       currentAction = PlayerAction.Move;
    36.       return true;  
    37.     }
    38.  
    39.     return false;
    40.    
    41.   }
    42.  
    43. }
    44.  
     
    Last edited: Oct 11, 2013