Files
Generic/Assets/Scripts/EnemyController.cs

159 lines
4.3 KiB
C#
Raw Normal View History

2025-04-28 12:01:01 +01:00
using System.Collections.Generic;
using System.Collections;
2025-04-27 19:41:05 +01:00
using UnityEngine;
2025-04-28 12:01:01 +01:00
using System;
using Random = UnityEngine.Random;
using System.IO.Compression;
2025-04-27 19:41:05 +01:00
public class EnemyController : MonoBehaviour
{
2025-04-28 12:01:01 +01:00
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>();
2025-04-27 19:41:05 +01:00
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
2025-04-28 12:01:01 +01:00
SetUpDeck();
if(enemyAIType != AIType.placeFromDeck)
{
SetupHand();
}
2025-04-27 19:41:05 +01:00
}
// Update is called once per frame
void Update()
{
}
2025-04-28 12:01:01 +01:00
public void SetUpDeck()
{
activeCards.Clear();
int interations = 0;
List<CardScriptableObject> tempDeck = new List<CardScriptableObject>();
tempDeck.AddRange(deckToUse);
while (tempDeck.Count > 0 && interations < 500)
{
int selected = Random.Range(0, tempDeck.Count);
activeCards.Add(tempDeck[selected]);
tempDeck.RemoveAt(selected);
interations++;
}
}
public void StartAction()
{
StartCoroutine(EnemyActionCo());
}
IEnumerator EnemyActionCo()
{
if(activeCards.Count == 0)
{
SetUpDeck();
}
yield return new WaitForSeconds(.5f);
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 interations = 0;
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:
break;
case AIType.handDefensive:
break;
case AIType.handAttacking:
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);
}
2025-04-27 19:41:05 +01:00
}
2025-04-28 12:01:01 +01:00