Commit d1ed5ac0 authored by Markus Schlösser's avatar Markus Schlösser
Browse files

Refactored code files

parent 22dba28e
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine;
public class CannonController : MonoBehaviour
{
public class CannonController : MonoBehaviour {
public GameObject cannonBarrel;
public GameObject fire;
private GameObject currentFire;
......@@ -12,21 +9,16 @@ public class CannonController : MonoBehaviour
[SerializeField] private float shotsPerSeconds;
[SerializeField] private float speed = 10f;
[SerializeField] private Animator anim;
void FixedUpdate ()
{
if (cooldown <= 0)
{
void FixedUpdate() {
if (cooldown <= 0) {
anim.SetTrigger("Fire");
}
else
{
} else {
cooldown = Mathf.Max(0, cooldown - Time.deltaTime);
}
}
public void Fire()
{
public void Fire() {
currentFire = Instantiate(fire, cannonBarrel.transform.position, cannonBarrel.transform.rotation);
cooldown = 1f / (shotsPerSeconds);
// Drehung
......
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
using UnityEngine;
public class ChallagerController : MonoBehaviour
{
public class ChallagerController : MonoBehaviour {
private PlayerActor pa;
public float waitTime;
private Queue<Vector3> oldPosition= new Queue<Vector3>();
// Use this for initialization
void Start () {
pa= GetComponent<PlayerActor>();
private readonly Queue<Vector3> oldPosition = new Queue<Vector3>();
waitTime = 2.0f;
private void Start() {
pa = GetComponent<PlayerActor>();
waitTime = 2.0f;
}
// Update is called once per frame
void FixedUpdate () {
if (waitTime <= 0f)
{
oldPosition.Enqueue(pa.transform.position);
transform.position = oldPosition.Dequeue();
}
else
{
waitTime -= Time.deltaTime;
oldPosition.Enqueue(pa.transform.position);
}
private void FixedUpdate() {
if (waitTime <= 0f) {
oldPosition.Enqueue(pa.transform.position);
transform.position = oldPosition.Dequeue();
}
else {
waitTime -= Time.deltaTime;
oldPosition.Enqueue(pa.transform.position);
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using Soraphis;
using UnityEditor;
using Soraphis;
using UnityEngine;
using UnityEngine.Experimental.PlayerLoop;
using UnityEngine.SceneManagement;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class GameManager : SingletonBehaviour<GameManager>
{
public class GameManager : SingletonBehaviour<GameManager> {
private int currentLevel = 1; //Current level number, expressed in game as "Day 1".
public int HighestLevel { get; private set; }
public int LastLevel{ get; private set; } //Die Anzahl aller existierenden Level (Maximale Anzahl Level im Spiel)
public int LastLevel { get; private set; } //Die Anzahl aller existierenden Level (Maximale Anzahl Level im Spiel)
private GameObject PauseMenu =null ; //Damit wird das PauseMenu gespeichert
public bool inLevel { get; set; }
public bool challenger { get; set; }
private GameObject PauseMenu;
private LoadSave ls = new LoadSave();
public bool IsInLevel { get; set; }
//Awake is always called before any Start functions
public bool IsChallengerModeEnabled { get; set; }
private void Awake()
{
challenger = false;
inLevel = false;
private void Awake() {
IsChallengerModeEnabled = false;
IsInLevel = false;
HighestLevel = 1;
LastLevel = SceneManager.sceneCountInBuildSettings-1;
if (ls.DoesSaveFileExist())
{
int tmp = 1;
LastLevel = SceneManager.sceneCountInBuildSettings - 1;
ls.Load(out currentLevel, out tmp);
if (LoadSave.DoesSaveFileExist) {
LoadSave.Load(out currentLevel, out int tmp);
HighestLevel = tmp;
}
}
// Update nicht benötigt da alle veränderungen über Methodenaufrufe laufen
public void LoadLevel(int lvl) //lädt das ausgewählte lvl abhängig seiner Nummer
{
//lädt das ausgewählte lvl abhängig seiner Nummer
public void LoadLevel(int lvl) {
currentLevel = lvl;
string load;
if (lvl < 10)
if (lvl < 10) {
load = "Level0" + lvl;
else
{
} else {
load = "Level" + lvl;
}
inLevel = true;
SceneManager.LoadScene(load,LoadSceneMode.Single);
IsInLevel = true;
SceneManager.LoadScene(load, LoadSceneMode.Single);
Time.timeScale = 1.0f;
}
public void ContinueGame() //Continue im Hauptmenu
{
//Continue im Hauptmenu
public void ContinueGame() {
LoadLevel(currentLevel);
}
public void CloseGame() // beendet das Spiel
{
/// <summary>
/// Exits the Application
/// </summary>
public void CloseGame() {
#if UNITY_EDITOR
EditorApplication.isPlaying = false;
#endif
Application.Quit();
}
public void LevelFinished() // wenn man ein Level beendet hat
public void LevelFinished() // wenn man ein Level beendet hat
{
if(currentLevel<LastLevel)
currentLevel++;
if (currentLevel < LastLevel) {
currentLevel++;
}
if (currentLevel > HighestLevel)
if (currentLevel > HighestLevel) {
HighestLevel++;
}
int tmp = HighestLevel;
ls.Safe(currentLevel,tmp);
LoadSave.Save(currentLevel, tmp);
LoadLevel(currentLevel);
}
public void ResetLevel() // Neustarte bzw. neu lade das Leven/ die scene
{
// Neustarte bzw. neu lade das Leven/ die scene
public void ResetLevel() {
LoadLevel(currentLevel);
}
public void PauseLevel() // pausiert das lvl indem das Gameobject PauseMenu aktiv wird
{
if (PauseMenu==null) // erstellt das PauseMenu wenn es noch keines gibt
{
PauseMenu=Instantiate(Resources.Load("PauseMenu",typeof(GameObject)))as GameObject;
// pausiert das lvl indem das Gameobject PauseMenu aktiv wird
public void PauseLevel() {
// Lazy Initialize pause menu
if (PauseMenu == null) {
PauseMenu = Instantiate(Resources.Load("PauseMenu", typeof(GameObject))) as GameObject;
}
Time.timeScale = 0.0f;
PauseMenu.SetActive(true);
}
public bool PauseMenuActive()
{
if (PauseMenu != null)
{
public bool PauseMenuActive() {
if (PauseMenu != null) {
return PauseMenu.activeSelf;
}
else
{
else {
return false;
}
}
public void ContinueLevel() // weiter button im PauseMenu ; Pause Menu wird inaktiv
{
// weiter button im PauseMenu ; Pause Menu wird inaktiv
public void ContinueLevel() {
Time.timeScale = 1.0f;
PauseMenu.SetActive(false);
}
public void GoMainMenu() // Das Lvl wird beendet und man geht ins Hauptmenu
{
Destroy(PauseMenu); // Da im hauptmenu kein PauseMenu gebraucht wird wird es zerstört und wieder erstellt wenn man ein lvl startet
// Das Lvl wird beendet und man geht ins Hauptmenu
public void GoMainMenu() {
// Da im hauptmenu kein PauseMenu gebraucht wird wird es zerstört und wieder erstellt wenn man ein lvl startet
Destroy(PauseMenu);
PauseMenu = null;
inLevel = false;
SceneManager.LoadScene("MainMenu",LoadSceneMode.Single); // aufruf des Mainmenu
IsInLevel = false;
// aufruf des Mainmenu
SceneManager.LoadScene("MainMenu", LoadSceneMode.Single);
}
public void ResetProgress() // Man löscht seinen gesamten Fortschritt und hat wieder keine Lvl freigeschaltet
{
/// <summary>
/// Resets Progress so no levels are unlocked
/// </summary>
public void ResetProgress() {
currentLevel = 1;
HighestLevel = 1;
int highestLevel = HighestLevel;
ls.Safe(currentLevel, highestLevel);
LoadSave.Save(currentLevel, highestLevel);
}
public void SetChallengerOn()
{
challenger = true;
}
public void SetChallangerOff()
{
challenger = false;
}
}
\ No newline at end of file
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine;
public class InLevelController : MonoBehaviour {
private GameManager gm;
private void Awake()
{
gm = GameManager.Instance;
gm.inLevel = true;
}
private void Start()
{
if (gm.challenger)
{
GameObject go = Instantiate(Resources.Load<GameObject>("ChallengerModus"));
private void Start() {
if (GameManager.Instance.IsChallengerModeEnabled) {
Instantiate(Resources.Load<GameObject>("ChallengerModus"));
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (!gm.PauseMenuActive())
{
gm.PauseLevel();
}
else
{
gm.ContinueLevel();
private void Update() {
if (Input.GetKeyDown(KeyCode.Escape)) {
if (!GameManager.Instance.PauseMenuActive()) {
GameManager.Instance.PauseLevel();
} else {
GameManager.Instance.ContinueLevel();
}
}
}
public void LevelFinished()
{
gm.LevelFinished();
public void LevelFinished() {
GameManager.Instance.LevelFinished();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine;
using UnityEngine.Events;
public class InteractibleTrigger : MonoBehaviour, IInteractible {
......@@ -30,8 +28,4 @@ public class InteractibleTrigger : MonoBehaviour, IInteractible {
onInteract.Invoke();
}
}
public void Test() {
Debug.Log("Test");
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.PlayerLoop;
using UnityEngine;
using UnityEngine.UI;
public class LevelSelectController : MonoBehaviour
{
private GameManager gm;
// Use this for initialization
void Start ()
{
gm = GameManager.Instance;
public class LevelSelectController : MonoBehaviour {
private void Start() {
ActivateAllPages();
UpdateLevelSelect();
DisablePagesAfterFirst();
}
// Locks all currently not unlocked levels
public void UpdateLevelSelect()
{
if(gm == null)
gm = GameManager.Instance;
int highestLevel = gm.HighestLevel;
int lastLevel = gm.LastLevel;
for (int i = 1; i <= 18; i++)
{
String buttonName = "ButtonLevel" + i;
if (GameObject.Find(buttonName) != null)
{
/// <summary>
/// Locks all currently not unlocked levels
/// </summary>
public void UpdateLevelSelect() {
int highestLevel = GameManager.Instance.HighestLevel;
int lastLevel = GameManager.Instance.LastLevel;
for (int i = 1; i <= 18; i++) {
string buttonName = "ButtonLevel" + i;
if (GameObject.Find(buttonName) != null) {
GameObject button = GameObject.Find(buttonName);
Image image = button.transform.Find("Image").GetComponent<Image>();
// Level unlocked
if (i <= highestLevel)
{
String loadStr = i < 10 ? "LevelSelect/Level0" + i : "LevelSelect/Level" + i;
if (i <= highestLevel) {
string loadStr = i < 10 ? "LevelSelect/Level0" + i : "LevelSelect/Level" + i;
image.sprite = Resources.Load(loadStr, typeof(Sprite)) as Sprite;
button.GetComponent<Button>().interactable = true;
}
// Level locked
else if (i <= lastLevel)
{
else if (i <= lastLevel) {
image.sprite = Resources.Load("LevelSelect/lockedTEMP", typeof(Sprite)) as Sprite;
button.GetComponent<Button>().interactable = false;
}
// Level doesn't exist
else
{
else {
button.SetActive(false);
// Deactivates NextPageButton because there isnt' a next page
GameObject nextPage = button.transform.parent.Find("ButtonNextPage").gameObject;
nextPage.SetActive(false);
// Next page can't be opened so buttons on it don't have to be changed
if (i % 6 == 0)
return;
......@@ -63,35 +47,34 @@ public class LevelSelectController : MonoBehaviour
}
}
//Disables all pages except the first one
private void DisablePagesAfterFirst()
{
/// <summary>
/// Disables all pages except the first one
/// </summary>
private void DisablePagesAfterFirst() {
bool firstPage = true;
foreach(GameObject page in GameObject.FindGameObjectsWithTag("LevelSelectPage"))
{
if(firstPage)
foreach (GameObject page in GameObject.FindGameObjectsWithTag("LevelSelectPage")) {
if (firstPage) {
firstPage = false;
else
} else {
page.SetActive(false);
}
}
}
//Ensures all pages are active before loading "locked" images
private void ActivateAllPages()
{
foreach(GameObject page in GameObject.FindGameObjectsWithTag("LevelSelectPage"))
{
/// <summary>
/// Ensures all pages are active before loading "locked" images
/// </summary>
private void ActivateAllPages() {
foreach (GameObject page in GameObject.FindGameObjectsWithTag("LevelSelectPage")) {
page.SetActive(true);
}
}
public void LoadLevel(int level)
{
gm.LoadLevel(level);
public void LoadLevel(int level) {
GameManager.Instance.LoadLevel(level);
}
public void OpenLevelSelect()
{
public void OpenLevelSelect() {
ActivateAllPages();
UpdateLevelSelect();
DisablePagesAfterFirst();
......
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.IO;
using UnityEngine;
[System.Serializable]
public class LoadSave
{
public static class LoadSave {
public static bool DoesSaveFileExist => File.Exists(SaveFile);
private int currentLevelSafe,highestLevelSafe;
private static string SaveFile => $"{Application.persistentDataPath}/save.json";
private struct SaveData
{
private struct SaveData {
public int currentLevelSafe;
public int highestLevelSafe;
}
public void Safe(int currentLevel, int highestLevel)
{
public static void Save(int currentLevel, int highestLevel) {
SaveData data = new SaveData {
currentLevelSafe = currentLevel,
highestLevelSafe = highestLevel
highestLevelSafe = highestLevel
};
StreamWriter sw = new StreamWriter(SaveFile);
sw.WriteLine(JsonUtility.ToJson(data));
sw.Close();
}
public void Load(out int currentLevel, out int highestLevel)
{
public static void Load(out int currentLevel, out int highestLevel) {
StreamReader sr = new StreamReader(SaveFile);
SaveData save = JsonUtility.FromJson<SaveData>(sr.ReadToEnd());
sr.Close();
currentLevel = save.currentLevelSafe;
highestLevel = save.highestLevelSafe;
}