Skip to content

Commit

Permalink
Merge pull request #3 from Chizaruu/1-turn-system-not-working-as-inte…
Browse files Browse the repository at this point in the history
…nded

Added Queue<Actor> Generic
  • Loading branch information
Chizaruu authored Feb 6, 2023
2 parents 0c5de09 + 932f39e commit 21171b9
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;

Expand All @@ -9,10 +10,10 @@ public class GameManager : MonoBehaviour {
[Header("Time")]
[SerializeField] private float baseTime = 0.075f;
[SerializeField] private float delayTime; //Read-only
private Queue<Actor> actorQueue;

[Header("Entities")]
[SerializeField] private bool isPlayerTurn = true; //Read-only
[SerializeField] private int actorNum = 0; //Read-only
[SerializeField] private List<Entity> entities;
[SerializeField] private List<Actor> actors;

Expand All @@ -38,33 +39,33 @@ private void OnSceneLoaded(Scene scene, LoadSceneMode mode) {
if (sceneState is not null) {
LoadState(sceneState.GameState, true);
} else {
actorQueue = new Queue<Actor>();
entities = new List<Entity>();
actors = new List<Actor>();
}
}

private void StartTurn() {
if (actors[actorNum].GetComponent<Player>()) {
Actor actor = actorQueue.Peek();
if(actor.GetComponent<Player>()) {
isPlayerTurn = true;
} else {
if (actors[actorNum].AI != null) {
actors[actorNum].AI.RunAI();
if (actor.AI != null) {
actor.AI.RunAI();
} else {
Action.WaitAction();
}
}
}

public void EndTurn() {
if (actors[actorNum].GetComponent<Player>()) {
Actor actor = actorQueue.Dequeue();

if (actor.GetComponent<Player>()) {
isPlayerTurn = false;
}

if (actorNum == actors.Count - 1) {
actorNum = 0;
} else {
actorNum++;
}
actorQueue.Enqueue(actor);

StartCoroutine(TurnDelay());
}
Expand Down Expand Up @@ -96,16 +97,22 @@ public void RemoveEntity(Entity entity) {
public void AddActor(Actor actor) {
actors.Add(actor);
delayTime = SetTime();
actorQueue.Enqueue(actor);
}

public void InsertActor(Actor actor, int index) {
actors.Insert(index, actor);
delayTime = SetTime();
actorQueue.Enqueue(actor);
}

public void RemoveActor(Actor actor) {
if(actor.GetComponent<Player>()) {
return;
}
actors.Remove(actor);
delayTime = SetTime();
actorQueue = new Queue<Actor>(actorQueue.Where(x => x != actor));
}

public void RefreshPlayer() {
Expand Down Expand Up @@ -199,9 +206,11 @@ public void Reset(bool canRemovePlayer) {
if (canRemovePlayer) {
entities.Clear();
actors.Clear();
actorQueue.Clear();
} else {
entities.RemoveRange(1, entities.Count - 1);
actors.RemoveRange(1, actors.Count - 1);
actorQueue = new Queue<Actor>(actorQueue.Where(x => x.GetComponent<Player>()));
}
}
}
Expand Down

0 comments on commit 21171b9

Please sign in to comment.