BuildSite

mail@pastecode.io avatar
unknown
csharp
a year ago
3.0 kB
3
Indexable
Never
using System;
using UnityEngine;

namespace BuildSite {

    public class Handler : MonoBehaviour
    {

        [Header("Prefabs")]
        [SerializeField] private Transform bunkerPf;
        [SerializeField] private Transform barrackPf;
        [SerializeField] private Transform diamondBackPf;
        [SerializeField] private Transform siegeTowerPf;

        [Header("Timer")]
        [SerializeField] private float constructTimer;

        private Transform currnetBuildingPf;
        private StateMachine.Handler stateMachine;
        private bool isConstructing;
        
        public event EventHandler onSoldBuilding;
        public event EventHandler onBeginConstructing;
        public event EventHandler onEndConstructing;

        private void Start() {
            SetUpStateMachine();

            Tick.instance.onTick += Tick_onTick;
        }

        private void Tick_onTick(object sender, EventArgs e) {
            stateMachine.Update();
        }

        private void OnDestroy() {
            Tick tick = Tick.instance;
            if (tick != null) {
                tick.onTick -= Tick_onTick;
            }
        }

        private void SetUpStateMachine() {
            stateMachine = new StateMachine.Handler();

            BuildSite.State.Idle idle = new BuildSite.State.Idle();
            BuildSite.State.Constructing constructing = new BuildSite.State.Constructing(this);
            BuildSite.State.Hidden hidden = new BuildSite.State.Hidden();

            stateMachine.AddTransition(idle, constructing, () => currnetBuildingPf != null);
            stateMachine.AddTransition(constructing, hidden, () => constructing.FinishedConstructing());

            stateMachine.SetState(idle);
        }

        public void ConstructBunker() {
            TryStartConstruct(bunkerPf);
        }

        private void TryStartConstruct(Transform pfTransform) {
            if (isConstructing) {
                return;
            }

            currnetBuildingPf = pfTransform;
        }

        public void ConstructBarrack() {
            TryStartConstruct(barrackPf);
        }

        public void ConstructDiamondBack() {
            TryStartConstruct(diamondBackPf);
        }

        public void ConstructSiegeTower() {
            TryStartConstruct(siegeTowerPf);
        }

        public void ClearCurrentBuildingPf() {
            currnetBuildingPf = null;
        }

        public void InvokeOnSoldBuilding() => onSoldBuilding?.Invoke(this, EventArgs.Empty);
        public void InvokeOnBeginConstructing() => onBeginConstructing?.Invoke(this, EventArgs.Empty);
        public void InvokeOnEndConstructing() => onEndConstructing?.Invoke(this, EventArgs.Empty);

        public void SetIsConstructing(bool value) => isConstructing = value;
        public Transform GetCurrentBuildingPf() => currnetBuildingPf;
        public float GetConstructTimer() => constructTimer;

    }
}