using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Grunt_gun;
using Grunt_bite;
using System;
public class StateMachine : MonoBehaviour
{
private StateMachine stateMachine;
public Grunt_bite.BaseState biteActiveState;
public Grunt_gun.BaseState gunActiveState;
internal object activeState;
[SerializeField] private Path path;
private Enemy_1 enemy;
public void Initialize(Path path, EnemyType enemyType)
{
switch (enemyType)
{
case EnemyType.Grunt_bite:
ChangeBiteState(new Grunt_bite.PatrolState(path));
break;
case EnemyType.Grunt_gun:
ChangeGunState(new Grunt_gun.PatrolState(path));
break;
default:
Debug.LogError("Unsupported enemy type");
break;
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Debug.Log("Current active state: " + (gunActiveState != null ? gunActiveState.GetType().Name : "None"));
// Null checks performing actions on active states
if (biteActiveState != null)
{
biteActiveState.Perform();
}
if (gunActiveState != null)
{
gunActiveState.Perform();
}
}
public void ChangeBiteState(Grunt_bite.BaseState newState)
{
if (biteActiveState != null)
{
biteActiveState.Exit();
}
biteActiveState = newState;
if (biteActiveState != null)
{
biteActiveState.stateMachine = this;
if (biteActiveState is Grunt_bite.PatrolState patrolstate)
{
biteActiveState = new Grunt_bite.PatrolState(patrolstate.path, GetComponent<Enemy_2>());
biteActiveState.Enter();
}
else
{
Debug.LogError("newState to PatrolState failed");
}
}
}
public void ChangeGunState(Grunt_gun.BaseState newState)
{
//Debug.Log("Changing gun state to; " + newState.GetType().Name);
// Check if there is a currently active state
Grunt_gun.PatrolState patrolState = null;
if (gunActiveState != null)
{
gunActiveState.Exit();// Call the Exit method of the currently active state
}
gunActiveState = newState;// Set the new state as the active state
if (gunActiveState != null)// Check if the new state is not null
{
// Set the state machine and enemy references for the new state
gunActiveState.stateMachine = this;
gunActiveState.enemy = GetComponent<Enemy_1>();
// Check if the new state is a PatrolState
if (gunActiveState is Grunt_gun.PatrolState)
{
// Create a new instance of PatrolState with the same path and enemy
patrolState = gunActiveState as Grunt_gun.PatrolState;
gunActiveState = new Grunt_gun.PatrolState(patrolState.path, GetComponent<Enemy_1>());
}
// Check if the new state is an AttackState
else if (GetComponent<Enemy_1>().CanSeePlayer())
{
gunActiveState = new Grunt_gun.AttackState(enemy);
gunActiveState.Enter();
}
else // check if the new state is an AttackState
{
patrolState = gunActiveState as Grunt_gun.PatrolState;
gunActiveState = new Grunt_gun.PatrolState(patrolState.path, GetComponent<Enemy_1>());
gunActiveState.Enter(); // Enter AttackState
}
//if (stateMachine != null && stateMachine.gunActiveState != null)
//{
//Enemy_1 enemy = GetComponent<Enemy_1>();
//if (enemy.CanSeePlayer())
//{
//Debug.Log("Player detected in " + gunActiveState.GetType().Name + ". Transitioning to AttackState");
//stateMachine.ChangeGunState(new Grunt_gun.AttackState(GetComponent<Enemy_1>()));
//}
//}
//if (gunActiveState.GetType() == typeof(Grunt_gun.AttackState))
//{
//Grunt_gun.AttackState attackState = (Grunt_gun.AttackState)gunActiveState;
//if (attackState.CanSeePlayer())
//{
//Debug.Log("Player detected in " + gunActiveState.GetType().Name + ". Transitioning to AttackState");
//stateMachine.ChangeGunState(new Grunt_gun.AttackState(GetComponent<Enemy_1>()));
//}
//}
//if (gunActiveState != null && gunActiveState.CanSeePlayer())
//{
//Debug.Log("player detected in" + gunActiveState.GetType().Name +".transitioning to attackState");
//StateMachine stateMachine = new StateMachine();
//stateMachine.ChangeGunState(new Grunt_gun.AttackState(GetComponent<Enemy_1>()));
//}
}
}
}
public enum EnemyType
{
Grunt_bite,
Grunt_gun,
}