You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.7 KiB
112 lines
3.7 KiB
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using UnityEngine.UI; |
|
|
|
/// <summary> |
|
/// This is attached to an empty game object in the battle scene. This class |
|
/// manages all aspects of the battle that occurs. |
|
/// </summary> |
|
public class BattleManager : MonoBehaviour { |
|
|
|
// The locations where enemies may appear within the scene. |
|
public GameObject[] EnemySpawnPoints; |
|
// The enemies that may be placed on the spawn points |
|
public GameObject[] EnemyPrefabs; |
|
// When enemies are placed they slide into place based on the curve |
|
public AnimationCurve SpawnAnimationCurve; |
|
// Reference to the actions that a player may perform |
|
public CanvasGroup theButtons; |
|
// The number of enemies in the scene |
|
private int enemyCount; |
|
|
|
// This enumeration defines the components of a turn |
|
enum BattlePhase |
|
{ |
|
PlayerAttack, |
|
EnemyAttack |
|
} |
|
// Track which phase of the turn we are on |
|
private BattlePhase phase; |
|
|
|
// Use this for initialization |
|
void Start () { |
|
// Calculate how many enemies will be in the scene (battle) |
|
enemyCount = Random.Range(1, EnemySpawnPoints.Length); |
|
// Spawn the enemies in |
|
StartCoroutine(SpawnEnemies()); |
|
// Set the beginning battle phase - player always has initiative in this game |
|
phase = BattlePhase.PlayerAttack; |
|
} |
|
|
|
void Update() |
|
{ |
|
// Control the display of the player action buttons. Only show when it is the |
|
// player's turn |
|
if (phase == BattlePhase.PlayerAttack) |
|
{ |
|
theButtons.alpha = 1; |
|
theButtons.interactable = true; |
|
theButtons.blocksRaycasts = true; |
|
} |
|
else |
|
{ |
|
theButtons.alpha = 0; |
|
theButtons.interactable = false; |
|
theButtons.blocksRaycasts = false; |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// A player action. This action leaves the battle scene and returns to the world. |
|
/// </summary> |
|
public void RunAway() |
|
{ |
|
GameState.justExitedBattle = true; |
|
NavigationManager.NavigateTo("Overworld"); |
|
} |
|
|
|
/// <summary> |
|
/// This method creates enemies from the prefabs and calls the method |
|
/// that moves the enemies to the spawn points. |
|
/// </summary> |
|
/// <returns></returns> |
|
IEnumerator SpawnEnemies() |
|
{ |
|
// Spawn enemies in over time |
|
for (int i = 0; i < enemyCount; i++) |
|
{ |
|
var newEnemy = (GameObject)Instantiate(EnemyPrefabs[0]); |
|
newEnemy.transform.position = new Vector3(10, -1, 0); |
|
|
|
yield return StartCoroutine(MoveCharacterToPoint(EnemySpawnPoints[i], newEnemy)); |
|
newEnemy.transform.parent = EnemySpawnPoints[i].transform; |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// Moves the character into position using an animation that slides the char into place. |
|
/// </summary> |
|
/// <param name="destination"></param> |
|
/// <param name="character"></param> |
|
/// <returns></returns> |
|
IEnumerator MoveCharacterToPoint(GameObject destination, GameObject character) |
|
{ |
|
float timer = 0f; |
|
var StartPosition = character.transform.position; |
|
if (SpawnAnimationCurve.length > 0) |
|
{ |
|
while (timer < SpawnAnimationCurve.keys[SpawnAnimationCurve.length - 1].time) |
|
{ |
|
character.transform.position = Vector3.Lerp(StartPosition, destination.transform.position, SpawnAnimationCurve.Evaluate(timer)); |
|
|
|
timer += Time.deltaTime; |
|
yield return new WaitForEndOfFrame(); |
|
} |
|
} |
|
else |
|
{ |
|
character.transform.position = destination.transform.position; |
|
} |
|
} |
|
}
|
|
|