Files
Generic/Assets/Scripts/EnemyController.cs

285 lines
9.2 KiB
C#

using System.Collections.Generic;
using System.Collections;
using UnityEngine;
using System;
using Random = UnityEngine.Random;
public class EnemyController : MonoBehaviour
{
public static EnemyController instance;
// Awake is called when the script instance is being loaded
void Awake()
{
{
instance = this;
}
}
public List<CardScriptableObject> deckToUse = new List<CardScriptableObject>();
public Card cardToSpawn;
public Transform cardSpawnPoint;
public enum AIType { placeFromDeck, handRandomPlace, handDefensive, handAttacking };
public AIType enemyAIType;
public int startHandSize;
private List<CardScriptableObject> activeCards = new List<CardScriptableObject>();
private List<CardScriptableObject> cardsInHand = new List<CardScriptableObject>();
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
SetUpDeck();
if(enemyAIType != AIType.placeFromDeck)
{
SetupHand();
}
}
// Update is called once per frame
void Update()
{
}
public void SetUpDeck()
{
activeCards.Clear();
int iterations = 0;
List<CardScriptableObject> tempDeck = new List<CardScriptableObject>();
tempDeck.AddRange(deckToUse);
while (tempDeck.Count > 0 && iterations < 500)
{
int selected = Random.Range(0, tempDeck.Count);
activeCards.Add(tempDeck[selected]);
tempDeck.RemoveAt(selected);
iterations++;
}
}
public void StartAction()
{
StartCoroutine(EnemyActionCo());
}
IEnumerator EnemyActionCo()
{
if(activeCards.Count == 0)
{
SetUpDeck();
}
yield return new WaitForSeconds(.5f);
if(enemyAIType != AIType.placeFromDeck)
{
for(int i = 0; i < BattleController.instance.cardsToDrawPerTurn; i++)
{
cardsInHand.Add(activeCards[0]);
activeCards.RemoveAt(0);
if(activeCards.Count == 0)
{
SetUpDeck();
}
}
}
List<CardPlacePoint> cardPoints = new List<CardPlacePoint>();
cardPoints.AddRange(CardPointsController.instance.enemyCardPoints);
int randomPoint = Random.Range(0, cardPoints.Count);
CardPlacePoint selectedPoint = cardPoints[randomPoint];
if(enemyAIType == AIType.placeFromDeck || enemyAIType == AIType.handRandomPlace)
{
cardPoints.Remove(selectedPoint);
while(selectedPoint.activeCard != null && cardPoints.Count > 0)
{
randomPoint = Random.Range(0, cardPoints.Count);
selectedPoint = cardPoints[randomPoint];
cardPoints.RemoveAt(randomPoint);
}
}
CardScriptableObject selectedCard = null;
int iterations = 0;
List<CardPlacePoint> preferredPoints = new List<CardPlacePoint>();
List<CardPlacePoint> secondaryPoints = new List<CardPlacePoint>();
switch (enemyAIType)
{
case AIType.placeFromDeck:
if(selectedPoint.activeCard == null)
{
Card newCard = Instantiate(cardToSpawn, cardSpawnPoint.position, cardSpawnPoint.rotation);
newCard.cardSO = activeCards[0];
activeCards.RemoveAt(0);
newCard.SetupCard();
newCard.MoveToPoint(selectedPoint.transform.position, selectedPoint.transform.rotation);
selectedPoint.activeCard = newCard;
newCard.assignedPlace = selectedPoint;
}
break;
case AIType.handRandomPlace:
selectedCard = SelectedCardToPlay();
iterations = 50;
while(selectedCard != null && iterations > 0 && selectedPoint.activeCard == null)
{
PlayCard(selectedCard, selectedPoint);
selectedCard = SelectedCardToPlay();
iterations--;
yield return new WaitForSeconds(CardPointsController.instance.timeBetweenAttacks);
while(selectedPoint.activeCard != null && cardPoints.Count > 0)
{
randomPoint = Random.Range(0, cardPoints.Count);
selectedPoint = cardPoints[randomPoint];
cardPoints.RemoveAt(randomPoint);
}
}
break;
case AIType.handDefensive:
selectedCard = SelectedCardToPlay();
preferredPoints.Clear();
secondaryPoints.Clear();
for(int i = 0; i < cardPoints.Count; i++)
{
if(cardPoints[i].activeCard == null)
{
if(CardPointsController.instance.playerCardPoints[i].activeCard != null)
{
preferredPoints.Add(cardPoints[i]);
}
else
{
secondaryPoints.Add(cardPoints[i]);
}
}
}
iterations = 50;
while(selectedCard != null && iterations > 0 && preferredPoints.Count + secondaryPoints.Count > 0)
{
if(preferredPoints.Count > 0)
{
int selectPoint = Random.Range(0, preferredPoints.Count);
selectedPoint = preferredPoints[selectPoint];
preferredPoints.RemoveAt(selectPoint);
}
else
{
int selectPoint = Random.Range(0, secondaryPoints.Count);
selectedPoint = secondaryPoints[selectPoint];
secondaryPoints.RemoveAt(selectPoint);
}
PlayCard(selectedCard, selectedPoint);
selectedCard = SelectedCardToPlay();
iterations--;
yield return new WaitForSeconds(CardPointsController.instance.timeBetweenAttacks);
}
break;
case AIType.handAttacking:
selectedCard = SelectedCardToPlay();
preferredPoints.Clear();
secondaryPoints.Clear();
for(int i = 0; i < cardPoints.Count; i++)
{
if(cardPoints[i].activeCard == null)
{
if(CardPointsController.instance.playerCardPoints[i].activeCard == null)
{
preferredPoints.Add(cardPoints[i]);
}
else
{
secondaryPoints.Add(cardPoints[i]);
}
}
}
iterations = 50;
while(selectedCard != null && iterations > 0 && preferredPoints.Count + secondaryPoints.Count > 0)
{
if(preferredPoints.Count > 0)
{
int selectPoint = Random.Range(0, preferredPoints.Count);
selectedPoint = preferredPoints[selectPoint];
preferredPoints.RemoveAt(selectPoint);
}
else
{
int selectPoint = Random.Range(0, secondaryPoints.Count);
selectedPoint = secondaryPoints[selectPoint];
secondaryPoints.RemoveAt(selectPoint);
}
PlayCard(selectedCard, selectedPoint);
selectedCard = SelectedCardToPlay();
iterations--;
yield return new WaitForSeconds(CardPointsController.instance.timeBetweenAttacks);
}
break;
}
yield return new WaitForSeconds(.5f);
BattleController.instance.AdvanceTurn();
}
void SetupHand()
{
for(int i = 0; i < startHandSize; i++)
{
if(activeCards.Count == 0)
{
SetUpDeck();
}
cardsInHand.Add(activeCards[0]);
activeCards.RemoveAt(0);
}
}
public void PlayCard(CardScriptableObject cardSO, CardPlacePoint placePoint)
{
Card newCard = Instantiate(cardToSpawn, cardSpawnPoint.position, cardSpawnPoint.rotation);
newCard.cardSO = cardSO;
newCard.SetupCard();
newCard.MoveToPoint(placePoint.transform.position, placePoint.transform.rotation);
placePoint.activeCard = newCard;
newCard.assignedPlace = placePoint;
cardsInHand.Remove(cardSO);
BattleController.instance.SpendEnemyMana(cardSO.manaCost);
}
CardScriptableObject SelectedCardToPlay()
{
CardScriptableObject cardToPlay = null;
List<CardScriptableObject> cardsToPlay = new List<CardScriptableObject>();
foreach(CardScriptableObject card in cardsInHand)
{
if(card.manaCost <= BattleController.instance.enemyMana)
{
cardsToPlay.Add(card);
}
}
if(cardsToPlay.Count > 0)
{
int selected = Random.Range(0, cardsToPlay.Count);
cardToPlay = cardsToPlay[selected];
}
return cardToPlay;
}
}