mission-demolition/Assets/Scripts/MissionDemolition.cs

116 lines
3.2 KiB
C#
Raw Normal View History

2024-02-19 21:26:17 -06:00
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
2024-02-22 02:08:26 -06:00
using UnityEngine.SceneManagement;
2024-02-19 21:26:17 -06:00
public enum GameMode {
idle,
playing,
levelEnd
}
public class MissionDemolition: MonoBehaviour {
static private MissionDemolition s; // a private Singleton
[Header("Set in Inspector")]
public Text uitLevel; // The UIText_Level Text
public Text uitShots; // The UIText_Shots Text
public Text uitButton; // The Text on UIButton_View
public Vector3 castlePos; // The place to put castles
public GameObject[] castles; // An array of the castles
[Header("Set Dynamically")]
public int level; // The current level
public int levelMax; // The number of levels
public int shotsTaken;
public GameObject castle; // The current castle
public GameMode mode = GameMode.idle;
public string showing = "Show Slingshot" ; // FollowCam mode
void Start() {
s = this; // Define the Singleton
level = 0;
levelMax = castles.Length;
StartLevel();
}
void StartLevel() {
// Get rid of the old castle if one exists
if (castle != null ) {
Destroy(castle);
}
// Destroy old projectiles if they exist
GameObject[] gos = GameObject.FindGameObjectsWithTag("Projectile");
foreach (GameObject pTemp in gos) {
Destroy(pTemp);
}
// Instantiate the new castle
castle = Instantiate<GameObject>(castles[level]);
castle.transform.position = castlePos;
shotsTaken = 0;
// Reset the camera
2024-02-22 02:08:26 -06:00
SwitchView("Show Both");
2024-02-19 21:26:17 -06:00
ProjectileLine.s.Clear();
// Reset the goal
Goal.goalMet = false;
UpdateGUI();
mode = GameMode.playing;
}
void UpdateGUI() {
// Show the data in the GUITexts
uitLevel.text = "Level: " + (level + 1) + "of " + levelMax;
uitShots.text = "Shots Taken: " + shotsTaken;
}
void Update() {
UpdateGUI();
// Check for level end
if ((mode == GameMode.playing) && Goal.goalMet) {
// Change mode to stop checking for level end
mode = GameMode.levelEnd;
// Zoom out
2024-02-22 02:08:26 -06:00
SwitchView("Show Both");
2024-02-19 21:26:17 -06:00
// Start the next level in 2 seconds
Invoke("NextLevel", 2f);
2024-02-19 21:26:17 -06:00
}
}
void NextLevel() {
level++;
if (level == levelMax) {
2024-02-22 02:08:26 -06:00
SceneManager.LoadScene("_Scene_1");
return;
2024-02-19 21:26:17 -06:00
}
StartLevel();
}
public void SwitchView(string eView = "") {
if (eView == "") {
eView = uitButton.text;
}
showing = eView;
switch (showing) {
case "Show Slingshot":
2024-02-19 21:26:17 -06:00
FollowCam.POI = null;
uitButton.text = "Show Castle";
break;
case "Show Castle":
2024-02-19 21:26:17 -06:00
FollowCam.POI = s.castle;
uitButton.text = "Show Both" ;
break;
case "Show Both":
2024-02-19 21:26:17 -06:00
FollowCam.POI = GameObject.Find("ViewBoth" );
uitButton.text = "Show Slingshot";
break;
}
}
// Static method that allows code anywhere to increment shotsTaken
public static void ShotFired() {
s.shotsTaken++;
}
}