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.
78 lines
2.3 KiB
78 lines
2.3 KiB
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using UnityEngine.SceneManagement; |
|
|
|
/// <summary> |
|
/// This script is attached to "Zone" objects in the world scene. When the player collides with |
|
/// the zone, there is a chance of a battle. This script will load that battle scene when a |
|
/// battle occurs. |
|
/// </summary> |
|
public class RandomBattle : MonoBehaviour { |
|
|
|
public int battleProbability; |
|
int encounterChance = 100; |
|
public int secondsBetweenBattles; |
|
public string battleSceneName; |
|
|
|
/// <summary> |
|
/// When a player object enters the 'zone' there is a chance for a battle |
|
/// </summary> |
|
/// <param name="col"></param> |
|
void OnTriggerEnter2D(Collider2D col) |
|
{ |
|
// Prevents a loop condition between the world and the battle scene |
|
if (!GameState.justExitedBattle) |
|
{ |
|
// Roll the dice |
|
encounterChance = Random.Range(1, 100); |
|
// Check the DC :) |
|
if (encounterChance > battleProbability) |
|
{ |
|
StartCoroutine(RecalculateChance()); |
|
} |
|
} |
|
else |
|
{ |
|
StartCoroutine(RecalculateChance()); |
|
GameState.justExitedBattle = false; |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// Continue to roll the dice for a random encounter while the player is in the zone |
|
/// </summary> |
|
/// <returns></returns> |
|
IEnumerator RecalculateChance() |
|
{ |
|
while (encounterChance > battleProbability) |
|
{ |
|
yield return new WaitForSeconds(secondsBetweenBattles); |
|
encounterChance = Random.Range(1, 100); |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// As long as the player is in the zone, there is a chance of a battle. When |
|
/// that chance hits, load the battle scene. |
|
/// </summary> |
|
/// <param name="col"></param> |
|
void OnTriggerStay2D(Collider2D col) |
|
{ |
|
if (encounterChance <= battleProbability) |
|
{ |
|
Debug.Log("Battle"); |
|
SceneManager.LoadScene(battleSceneName); |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// When the player leaves the zone, clean up. |
|
/// </summary> |
|
/// <param name="col"></param> |
|
void OnTriggerExit2D(Collider2D col) |
|
{ |
|
encounterChance = 100; |
|
StopCoroutine(RecalculateChance()); |
|
} |
|
}
|
|
|