HATEBIN
>
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class UnitActionSystem : MonoBehaviour { // This property can only be set by this class because of private set; // but it can be read by any other class because of get; // -Dino public static UnitActionSystem Instance { get; private set; } public event EventHandler OnSelectedUnitChanged; [SerializeField] private Unit selectedUnit; [SerializeField] private LayerMask unitLayerMask; private BaseAction selectedAction; private bool isBusy; private void Awake() { if(Instance != null) { Debug.LogError("There is more than one UnitActionSystem! " + transform + " - " + Instance); Destroy(gameObject); return; } Instance = this; } void Start() { SetSelectedUnit(selectedUnit); } // Update is called once per frame void Update() { if(isBusy) { return; } // Debug.Log(selectedAction.ToString()); HandleUnitSelection(); if(selectedUnit) { HandleSelectedAction(); } // we're going to try to handle everything with clicks. -Dino /* // If we get a hard coded input, we move to a location. -Dino if(Input.GetMouseButtonDown(1)) { if(selectedUnit != null) { GridPosition mouseGridPosition = LevelGrid.Instance.GetGridPosition(MouseWorld.GetPosition()); if(selectedUnit.GetMoveAction().IsValidActionGridPosition(mouseGridPosition)) { SetBusy(); selectedUnit.GetMoveAction().Move(mouseGridPosition, ClearBusy); } // selectedUnit.GetMoveAction().Move(MouseWorld.GetPosition()); } else { Debug.Log("No unit selected... idiot."); } } if(Input.GetButtonDown("Jump")) { SetBusy(); selectedUnit.GetSpinAction().Spin(ClearBusy); } */ } private void HandleSelectedAction() { if(Input.GetMouseButtonDown(0)) { GridPosition mouseGridPosition = LevelGrid.Instance.GetGridPosition(MouseWorld.GetPosition()); switch (selectedAction) { case MoveAction moveAction: if(moveAction.IsValidActionGridPosition(mouseGridPosition)) { SetBusy(); moveAction.Move(mouseGridPosition, ClearBusy); } break; case SpinAction spinAction: SetBusy(); selectedUnit.GetSpinAction().Spin(ClearBusy); spinAction.Spin(ClearBusy); break; default: break; } } } private void SetBusy() { isBusy = true; } private void ClearBusy() { isBusy = false; } private void HandleUnitSelection() { if(Input.GetMouseButtonDown(0)) { //raycast method -Dino Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out RaycastHit raycastHit, float.MaxValue, unitLayerMask)) { if(raycastHit.transform.TryGetComponent
(out Unit unit)) { SetSelectedUnit(unit); } } // fancy method that was fun to try. /* // If we click on the floor without selecting a unit... // the floor is still a game object, but it does not have a "unit" component. // This script will run, and set the selectedUnit field to the "unit" // component of the floor, which is 'null'. -Dino if(MouseWorld.GetObject().GetComponent
() != null) { SetSelectedUnit(MouseWorld.GetObject().GetComponent
()); } */ /* else { // To make this clean, we can set the selected unit to null by default // if we don't get a unit when clicking. Debug.Log("Setting selected unit to null deliberately"); selectedUnit = null; } */ // This code does the same thing as the conditional below. -Dino OnSelectedUnitChanged?.Invoke(this, EventArgs.Empty); // This code does the same thing as the line above. -Dino /* if(OnSelectedUnitChanged != null) { OnSelectedUnitChanged(this, EventArgs.Empty); } */ } } private void SetSelectedUnit(Unit unit) { selectedUnit = unit; SetSelectedAction(unit.GetMoveAction()); //SetSelectedAction(unit.GetSpinAction()); OnSelectedUnitChanged(this, EventArgs.Empty); } public void SetSelectedAction(BaseAction baseAction) { selectedAction = baseAction; } public Unit GetSelectedUnit() { return selectedUnit; } }