diff --git a/Platformer/Assets/Scripts/Background/Asteroid.cs b/Platformer/Assets/Scripts/Background/Asteroid.cs index 48dcf77..cf88bab 100644 --- a/Platformer/Assets/Scripts/Background/Asteroid.cs +++ b/Platformer/Assets/Scripts/Background/Asteroid.cs @@ -10,99 +10,103 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; +using Viewport; using Random = UnityEngine.Random; -public class Asteroid : MonoBehaviour +namespace Background { - [SerializeField] private List asteroid_list; /*SerializeField show im Unity inspector https://stackoverflow.com/a/53192557 */ + public class Asteroid : MonoBehaviour + { + [SerializeField] private List asteroid_list; /*SerializeField show im Unity inspector https://stackoverflow.com/a/53192557 */ - [SerializeField] private int number; /*number of asteroids that are to be placed*/ - [SerializeField] private bool viewport; - [SerializeField] private bool deathwall; + [SerializeField] private int number; /*number of asteroids that are to be placed*/ + [SerializeField] private bool viewport; + [SerializeField] private bool deathwall; - [SerializeField] private bool position; - [SerializeField] private Vector3 pos; + [SerializeField] private bool position; + [SerializeField] private Vector3 pos; - /*Delegate is a container for one or many Methods that can be used as a variable*/ - delegate Vector3 DelegateRandom(); /*Delegate declaration*/ - private DelegateRandom delegate_random; + /*Delegate is a container for one or many Methods that can be used as a variable*/ + delegate Vector3 DelegateRandom(); /*Delegate declaration*/ + private DelegateRandom delegate_random; - private void Awake() - { - if (viewport.Equals(deathwall) && viewport.Equals(position)) + private void Awake() { - Debug.LogException(new Exception("Either Viewport or Deathwall or Position")); + if (viewport.Equals(deathwall) && viewport.Equals(position)) + { + Debug.LogException(new Exception("Either Viewport or Deathwall or Position")); + } } - } - - // Start is called before the first frame update - private void Start() - { - StartCoroutine(Wait(0.01f)); - } - - private IEnumerator Wait(float t) - { - yield return new WaitForSeconds(t); - - InitPlace(number); - } - - private void InitPlace(int num) - { - if (viewport) + // Start is called before the first frame update + private void Start() { - delegate_random = VectorRandViewport; + StartCoroutine(Wait(0.01f)); } - else if (deathwall) + + + private IEnumerator Wait(float t) { - delegate_random = VectorRandY; + yield return new WaitForSeconds(t); + + InitPlace(number); } - else if (position) + + private void InitPlace(int num) { - delegate_random = VectorViewport; + if (viewport) + { + delegate_random = VectorRandViewport; + } + else if (deathwall) + { + delegate_random = VectorRandY; + } + else if (position) + { + delegate_random = VectorViewport; + } + PlaceAsteroid(delegate_random,num); } - PlaceAsteroid(delegate_random,num); - } - private void PlaceAsteroid(DelegateRandom del, int num) /* Method actually Placing Asteroid with the desired position declared in delegate_random*/ - { - for (int i = 0; num > i; i++) + private void PlaceAsteroid(DelegateRandom del, int num) /* Method actually Placing Asteroid with the desired position declared in delegate_random*/ { - Transform chosen_asteroid = asteroid_list[Random.Range(0, asteroid_list.Count)]; - Instantiate(chosen_asteroid, del.Invoke() , Quaternion.identity, transform); + for (int i = 0; num > i; i++) + { + Transform chosen_asteroid = asteroid_list[Random.Range(0, asteroid_list.Count)]; + Instantiate(chosen_asteroid, del.Invoke() , Quaternion.identity, transform); + } } - } - /* Three Methods for the user of the Unity Inspector to set the position of Asteroids*/ - private Vector3 VectorRandViewport() /*Random position respective to the Player and the Viewport*/ - { - float w = Random.Range(-(ScreenViewport.GetWidth()/2), ScreenViewport.GetWidth()/2); - float h = Random.Range(-(ScreenViewport.GetHeight()/2), ScreenViewport.GetHeight()/2); + /* Three Methods for the user of the Unity Inspector to set the position of Asteroids*/ + private Vector3 VectorRandViewport() /*Random position respective to the Player and the Viewport*/ + { + float w = Random.Range(-(ScreenViewport.GetWidth()/2), ScreenViewport.GetWidth()/2); + float h = Random.Range(-(ScreenViewport.GetHeight()/2), ScreenViewport.GetHeight()/2); - Vector3 rand = new Vector3(w, h, 0); - Vector3 center = CameraView.GetScreenPos(); - Vector3 v = center + rand; - return v; - } + Vector3 rand = new Vector3(w, h, 0); + Vector3 center = CameraView.GetScreenPos(); + Vector3 v = center + rand; + return v; + } - private Vector3 VectorRandY() /*Random position respective to the Player and the Viewport but only along the y axis*/ - { - float h = Random.Range(-(ScreenViewport.GetHeight()/2), ScreenViewport.GetHeight()/2); + private Vector3 VectorRandY() /*Random position respective to the Player and the Viewport but only along the y axis*/ + { + float h = Random.Range(-(ScreenViewport.GetHeight()/2), ScreenViewport.GetHeight()/2); - Vector3 rand = new Vector3(0, h, 0); - //Debug.Log(h); - //Vector3 center = new Vector3(0,CameraView.GetScreenPos().y,0); - //Vector3 v = center + rand; - return rand; - } + Vector3 rand = new Vector3(0, h, 0); + //Debug.Log(h); + //Vector3 center = new Vector3(0,CameraView.GetScreenPos().y,0); + //Vector3 v = center + rand; + return rand; + } - private Vector3 VectorViewport() /*Manual position respective to the Viewport (in Viewportspace)*/ - { - Vector3 v = CameraView.GetCamera().ViewportToWorldPoint(pos); - return v; + private Vector3 VectorViewport() /*Manual position respective to the Viewport (in Viewportspace)*/ + { + Vector3 v = CameraView.GetCamera().ViewportToWorldPoint(pos); + return v; + } } } diff --git a/Platformer/Assets/Scripts/Background/AsteroidMovement.cs b/Platformer/Assets/Scripts/Background/AsteroidMovement.cs index 78ff3a0..a846e05 100644 --- a/Platformer/Assets/Scripts/Background/AsteroidMovement.cs +++ b/Platformer/Assets/Scripts/Background/AsteroidMovement.cs @@ -7,55 +7,59 @@ using UnityEngine; +using Viewport; using Random = UnityEngine.Random; -public class AsteroidMovement : MonoBehaviour +namespace Background { - private Animator animatior; - private float anim_rotation_factor = 5f; + public class AsteroidMovement : MonoBehaviour + { + private Animator animatior; + private float anim_rotation_factor = 5f; - private float velocity; - private float max_velocity = 2.5f; + private float velocity; + private float max_velocity = 2.5f; - private Transform trans; - private Vector3 pos; + private Transform trans; + private Vector3 pos; - private void Awake() - { - animatior = gameObject.GetComponent(); - trans = gameObject.GetComponent(); - pos = gameObject.GetComponent().position; - - } - - // Start is called before the first frame update - private void Start() - { - velocity = Random.Range(-max_velocity, max_velocity); - - if (velocity > 0) + private void Awake() { - animatior.speed = velocity * anim_rotation_factor; /* speed of the animation depends on the velocity with which the asteroid moves regulated*/ - animatior.SetFloat("rotation", -1); /* "rotation" is a parameter created in the Unity AnimationController and used under speed as a multiplier to change the rotation based on direction*/ + animatior = gameObject.GetComponent(); + trans = gameObject.GetComponent(); + pos = gameObject.GetComponent().position; + } - else + + // Start is called before the first frame update + private void Start() { - animatior.speed = velocity * -anim_rotation_factor; - animatior.SetFloat("rotation", 1); + velocity = Random.Range(-max_velocity, max_velocity); + + if (velocity > 0) + { + animatior.speed = velocity * anim_rotation_factor; /* speed of the animation depends on the velocity with which the asteroid moves regulated*/ + animatior.SetFloat("rotation", -1); /* "rotation" is a parameter created in the Unity AnimationController and used under speed as a multiplier to change the rotation based on direction*/ + } + else + { + animatior.speed = velocity * -anim_rotation_factor; + animatior.SetFloat("rotation", 1); + } } - } - // Update is called once per frame - private void Update() - { - ScreenViewport.OutBoundary(trans); + // Update is called once per frame + private void Update() + { + ScreenViewport.OutBoundary(trans); - var position = trans.position; - position = position + new Vector3(velocity * Time.deltaTime, 0, 0); + var position = trans.position; + position = position + new Vector3(velocity * Time.deltaTime, 0, 0); - transform.position = position; + transform.position = position; + } } } \ No newline at end of file diff --git a/Platformer/Assets/Scripts/Background/Octopus.cs b/Platformer/Assets/Scripts/Background/Octopus.cs index e4e6a64..640db7a 100644 --- a/Platformer/Assets/Scripts/Background/Octopus.cs +++ b/Platformer/Assets/Scripts/Background/Octopus.cs @@ -6,31 +6,34 @@ */ +using Global; +using Player; using UnityEngine; -public class Octopus : MonoBehaviour +namespace Background { - [SerializeField] private GameObject octopus; - private GameObject octopus_instance; - - private Vector3 pos; - - private void Awake() - { - pos = new Vector3(PlayerSpawn.edge_death, GameController.GetStartPos().y, GameController.GetStartPos().z); /* position dependent on StartPos and Deathwall */ - } - - // Start is called before the first frame update - private void Start() + public class Octopus : MonoBehaviour { - octopus_instance = Instantiate(octopus, pos, Quaternion.identity,transform); /* creates the object everytime the script is executed */ + [SerializeField] private GameObject octopus; + private GameObject octopus_instance; + + private Vector3 pos; + + private void Awake() + { + pos = new Vector3(PlayerSpawn.edge_death, GameController.GetStartPos().y, GameController.GetStartPos().z); /* position dependent on StartPos and Deathwall */ + } + + // Start is called before the first frame update + private void Start() + { + octopus_instance = Instantiate(octopus, pos, Quaternion.identity,transform); /* creates the object everytime the script is executed */ + } + + // Update is called once per frame + private void Update() + { + octopus_instance.transform.position = new Vector3(PlayerSpawn.edge_death, PlayerSpawn.Lerp(octopus_instance.transform.position.y ,PlayerController.GetPlayerPos().y), 0); + } } - - // Update is called once per frame - private void Update() - { - octopus_instance.transform.position = new Vector3(PlayerSpawn.edge_death, PlayerSpawn.Lerp(octopus_instance.transform.position.y ,PlayerController.GetPlayerPos().y), 0); - } - - } diff --git a/Platformer/Assets/Scripts/Background/Planet.cs b/Platformer/Assets/Scripts/Background/Planet.cs index fbd46a4..178c0d7 100644 --- a/Platformer/Assets/Scripts/Background/Planet.cs +++ b/Platformer/Assets/Scripts/Background/Planet.cs @@ -5,45 +5,50 @@ * Moves the Planet with a parallax effect */ +using Global; +using Viewport; using UnityEngine; -public class Planet : MonoBehaviour +namespace Background { - [SerializeField] private Transform planet; - private Transform cam_trans; - private Vector3 last_cam_pos; - public Vector3 pos; + public class Planet : MonoBehaviour + { + [SerializeField] private Transform planet; + private Transform cam_trans; + private Vector3 last_cam_pos; + public Vector3 pos; - [SerializeField] private float parallax; + [SerializeField] private float parallax; - void Awake() - { - cam_trans = CameraView.GetCamera().transform; - } + void Awake() + { + cam_trans = CameraView.GetCamera().transform; + } - // Start is called before the first frame update - void Start() - { - Instantiate(planet, Vector3.zero, Quaternion.identity, transform); - last_cam_pos = cam_trans.position - GameController.GetStartPos() - pos; - } + // Start is called before the first frame update + void Start() + { + Instantiate(planet, Vector3.zero, Quaternion.identity, transform); + last_cam_pos = cam_trans.position - GameController.GetStartPos() - pos; + } - // Update is called once per frame - void Update() - { - Parallax(5.0f); - } + // Update is called once per frame + void Update() + { + Parallax(5.0f); + } - private void Parallax(float t) - { - new WaitForSeconds(t); /* parallex starts after 5 sec, because player has to settle on plattform */ + private void Parallax(float t) + { + new WaitForSeconds(t); /* parallex starts after 5 sec, because player has to settle on plattform */ - ScreenViewport.OutBoundary(transform); + ScreenViewport.OutBoundary(transform); - Vector3 delta_move = cam_trans.position - last_cam_pos; + Vector3 delta_move = cam_trans.position - last_cam_pos; - transform.position += (delta_move * parallax); - last_cam_pos = cam_trans.position; - } + transform.position += (delta_move * parallax); + last_cam_pos = cam_trans.position; + } + } } diff --git a/Platformer/Assets/Scripts/Global/GameController.cs b/Platformer/Assets/Scripts/Global/GameController.cs index e45711c..d4e5b3e 100644 --- a/Platformer/Assets/Scripts/Global/GameController.cs +++ b/Platformer/Assets/Scripts/Global/GameController.cs @@ -7,52 +7,44 @@ using UnityEngine; -public class GameController : MonoBehaviour +namespace Global { - public static bool game_is_paused; + public class GameController : MonoBehaviour + { + public static bool game_is_paused; - [SerializeField] private Vector3 start_pos; - private static Vector3 start_pos_s; + [SerializeField] private Vector3 start_pos; + private static Vector3 start_pos_s; - private void Awake() - { - GameObject[] objs = GameObject.FindGameObjectsWithTag("GameController"); - if (objs.Length > 1) + private void Awake() { - Destroy(gameObject); - } - DontDestroyOnLoad(gameObject); + GameObject[] objs = GameObject.FindGameObjectsWithTag("GameController"); + if (objs.Length > 1) + { + Destroy(gameObject); + } + DontDestroyOnLoad(gameObject); - game_is_paused = false; + game_is_paused = false; - start_pos_s = start_pos; - } - - // Start is called before the first frame update - private void Start() - { + start_pos_s = start_pos; + } - } - // Update is called once per frame - void Update() - { - - } - - public static void TimeStop() - { - Time.timeScale = 0f; - game_is_paused = true; - } - public static void TimeStart() - { - Time.timeScale = 1f; - game_is_paused = false; - } + public static void TimeStop() + { + Time.timeScale = 0f; + game_is_paused = true; + } + public static void TimeStart() + { + Time.timeScale = 1f; + game_is_paused = false; + } - public static Vector3 GetStartPos() - { - return start_pos_s; + public static Vector3 GetStartPos() + { + return start_pos_s; + } } } diff --git a/Platformer/Assets/Scripts/Global/Highscore.cs b/Platformer/Assets/Scripts/Global/Highscore.cs index 3fcd9b0..c155efd 100644 --- a/Platformer/Assets/Scripts/Global/Highscore.cs +++ b/Platformer/Assets/Scripts/Global/Highscore.cs @@ -6,50 +6,54 @@ */ +using Player; using UnityEngine; -public class Highscore : MonoBehaviour +namespace Global { - private Vector3 start_pos; - private Vector3 player_pos; - - private static float highscore; - private float distance; - private float last_distance; - - private void Awake() + public class Highscore : MonoBehaviour { - start_pos = GameController.GetStartPos(); - player_pos = PlayerController.GetPlayerPos(); - } + private Vector3 start_pos; + private Vector3 player_pos; + + private static float highscore; + private float distance; + private float last_distance; - // Start is called before the first frame update - private void Start() - { - player_pos = PlayerController.GetPlayerPos(); - } + private void Awake() + { + start_pos = GameController.GetStartPos(); + player_pos = PlayerController.GetPlayerPos(); + } - // Update is called once per frame - private void Update() - { - player_pos = PlayerController.GetPlayerPos(); - - if (highscore <= distance) + // Start is called before the first frame update + private void Start() { - last_distance = distance; + player_pos = PlayerController.GetPlayerPos(); } - - //distance = Vector3.Distance(new Vector3(player_pos.x,0 ,0), Vector3.zero); - distance = PlayerController.GetPlayerPos().x; - if (distance > last_distance) + // Update is called once per frame + private void Update() { - highscore = distance; + player_pos = PlayerController.GetPlayerPos(); + + if (highscore <= distance) + { + last_distance = distance; + } + + //distance = Vector3.Distance(new Vector3(player_pos.x,0 ,0), new Vector3(start_pos.x,0,0)); /* fails when going left */ + distance = player_pos.x; + + if (distance > last_distance) + { + highscore = distance; + } } - } - public static float GetHighscore() - { - return highscore; + public static float GetHighscore() + { + return highscore; + } } } diff --git a/Platformer/Assets/Scripts/Global/PolygonCollider.cs b/Platformer/Assets/Scripts/Global/PolygonCollider.cs index e8e95bc..ae869d2 100644 --- a/Platformer/Assets/Scripts/Global/PolygonCollider.cs +++ b/Platformer/Assets/Scripts/Global/PolygonCollider.cs @@ -9,16 +9,18 @@ using System.Collections.Generic; using UnityEngine; -public class PolygonCollider : MonoBehaviour +namespace Global { - private PolygonCollider2D polygon_collider; - private Sprite sprite; + public class PolygonCollider : MonoBehaviour + { + private PolygonCollider2D polygon_collider; + private Sprite sprite; - // Update is called once per frame - private void Update() - { - /*new_sprite = GetComponent().sprite; Inefficient but working + // Update is called once per frame + private void Update() + { + /*new_sprite = GetComponent().sprite; Inefficient but working if (new_sprite.Equals(old_sprite)) { @@ -30,16 +32,17 @@ private void Update() } old_sprite = new_sprite;*/ - polygon_collider = GetComponent(); - sprite = GetComponent().sprite; - polygon_collider.pathCount = 0; - polygon_collider.pathCount = sprite.GetPhysicsShapeCount(); + polygon_collider = GetComponent(); + sprite = GetComponent().sprite; + polygon_collider.pathCount = 0; + polygon_collider.pathCount = sprite.GetPhysicsShapeCount(); - List path = new List(); - for (int i = 0; i < polygon_collider.pathCount; i++) { - path.Clear(); - sprite.GetPhysicsShape(i, path); - polygon_collider.SetPath(i, path.ToArray()); + List path = new List(); + for (int i = 0; i < polygon_collider.pathCount; i++) { + path.Clear(); + sprite.GetPhysicsShape(i, path); + polygon_collider.SetPath(i, path.ToArray()); + } } } } diff --git a/Platformer/Assets/Scripts/Menu/DeathMenu.cs b/Platformer/Assets/Scripts/Menu/DeathMenu.cs deleted file mode 100644 index 5a9e707..0000000 --- a/Platformer/Assets/Scripts/Menu/DeathMenu.cs +++ /dev/null @@ -1,155 +0,0 @@ -using System; -using System.IO; -using TMPro; -using UnityEngine; - -public class DeathMenu : MonoBehaviour -{ - - - [SerializeField] private GameObject helpButton, valueSaved, valueNotSaved, deathCluster; - - [SerializeField] private TextMeshPro score; - - [SerializeField] private TextMeshProUGUI textBox; - [SerializeField] private TextMeshProUGUI valueNotSavedText; - - private string[] currentSplitLine; - - private string path = HighscoreMenu.path; - - private int rank, buttonCheck, fail; - private static int loc_highscore; - - private StreamReader reader; - private StreamWriter writer; - - private readonly char split = HighscoreMenu.split; - - private void Awake() - { - buttonCheck = 0; - valueNotSavedText.text = "Value not saved!"; - path = @"highscore.txt"; - } - - // Start is called before the first frame update - private void Start() - { - if (!File.Exists(path)) - { - using (var init = File.CreateText(path)) - { - for (var i = 0; i < 10; i++) init.WriteLine("Init" + split + "0"); - } - - Debug.Log("dataPath : " + path); - } - - try - { - reader = new StreamReader(path); - } - catch - { - fail = 1; - valueNotSavedText.text = "No highscore file accessible!"; - valueNotSaved.SetActive(true); - } - - - - helpButton.SetActive(false); - loc_highscore = Convert.ToInt32(Highscore.GetHighscore()); - score.text = "Score: " + loc_highscore; - - - - if (fail != 1) - { - for (var i = 0; i < 10; i++) - { - var currentLine = reader.ReadLine(); - - if (currentLine != null) - { - currentSplitLine = currentLine.Split(split); - try - { - for (var j = 0; j < 2; j++) HighscoreMenu.HighscoreList(j, i, currentSplitLine[j]); - } - catch - { - valueNotSavedText.text = "Highscore file corrupt!"; - valueNotSaved.SetActive(true); - fail = 1; - } - } - } - reader.Close(); - - - - for (var i = 0; i < 10; i++) - if (HighscoreMenu.HighscoreList(0, i) == "Init") - { - i = NewHighscore(i); - buttonCheck = 1; /* overrides the initial highscore */ - } - else if (loc_highscore > int.Parse(HighscoreMenu.HighscoreList(1, i))) - { - i = NewHighscore(i); - } - } - } - - - private int NewHighscore(int i) - { - deathCluster.SetActive(true); - rank = i; - i = 9; - return i; - } - - public void SubmitHighscore() - { - var fail = 0; - try - { - writer = new StreamWriter(path, false); - } - catch - { - fail = 1; - valueNotSavedText.text = "No highscore file accessible!"; - valueNotSaved.SetActive(true); - } - - - - if (fail == 0) - { - if (buttonCheck == 0) - for (var i = 8; i >= rank; i--) - { - HighscoreMenu.HighscoreList(0, i + 1, HighscoreMenu.HighscoreList(0, i)); - HighscoreMenu.HighscoreList(1, i + 1, HighscoreMenu.HighscoreList(1, i)); - } - - HighscoreMenu.HighscoreList(0, rank, textBox.text); - HighscoreMenu.HighscoreList(1, rank, loc_highscore.ToString()); - for (var i = 0; i < 10; i++) - { - if (HighscoreMenu.HighscoreList(0, i) != null && HighscoreMenu.HighscoreList(1, i) != null) - { - writer.WriteLine(HighscoreMenu.HighscoreList(0, i) + split + HighscoreMenu.HighscoreList(1, i)); - } - } - - writer.Close(); - valueSaved.SetActive(true); - buttonCheck = 1; - } - } -} \ No newline at end of file diff --git a/Platformer/Assets/Scripts/Menu/HighscoreMenu.cs b/Platformer/Assets/Scripts/Menu/HighscoreMenu.cs deleted file mode 100644 index 564efda..0000000 --- a/Platformer/Assets/Scripts/Menu/HighscoreMenu.cs +++ /dev/null @@ -1,116 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using TMPro; -using UnityEngine; - - -public class HighscoreMenu : MonoBehaviour -{ - [SerializeField] private List textList; - [SerializeField] private GameObject error; - [SerializeField] private TextMeshProUGUI errorText; - - private static string[,] highscoreList = new string[2,10]; - - private string[] currentSplitLine; - private int empty = 0; - - public static char split = ''; - public static string path; - - private StreamReader reader; - private StreamWriter writer; - - private int fail = 0; - - - private void Awake() - { - path = @"highscore.txt"; - } - - // Start is called before the first frame update - void Start() - { - if (!File.Exists(path)) - { - using (var init = File.CreateText(path)) - { - for (var i = 0; i < 10; i++) - { - init.WriteLine("Init"+split+"0"); - } - } - - Debug.Log("dataPath : " + path); - } - - try - { - reader = new StreamReader(path); - } - catch - { - fail = 1; - errorText.text = "No highscore file accessible!"; - error.SetActive(true); - } - - - - if (fail != 1) - { - for (var i = 0; i < 10; i++) - { - var currentLine = reader.ReadLine(); - - if (currentLine != null) - { - currentSplitLine = currentLine.Split(split); - try - { - for (var j = 0; j < 2; j++) HighscoreList(j, i, currentSplitLine[j]); - } - catch - { - errorText.text = "Highscore file corrupt!"; - error.SetActive(true); - fail = 1; - } - } - } - reader.Close(); - - - - for (int i = 0; i < 10; i++) - { - if (HighscoreList(0, i) != "Init") - { - textList[i].text = (i+1)+". "+HighscoreList(0, i) + " " + HighscoreList(1, i); - } - else - { - empty++; - } - } - - if (empty == 10) - { - errorText.text = "No Highscores Yet, Play to Fill!"; - error.SetActive(true); - } - } - } - - - public static string HighscoreList(int x, int y) - { - return highscoreList[x,y]; - } - public static void HighscoreList(int x, int y, string b) - { - highscoreList[x, y] = b; - } -} diff --git a/Platformer/Assets/Scripts/Menu/Menu.cs b/Platformer/Assets/Scripts/Menu/Menu.cs deleted file mode 100644 index f77708d..0000000 --- a/Platformer/Assets/Scripts/Menu/Menu.cs +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Script: Menu - * Author: Philip Noack - * Last Change: 12.06.21 - * Source: https://www.youtube.com/watch?v=zc8ac_qUXQY - * Play and Quit Button in Menu - */ - - -using UnityEngine; -using UnityEngine.SceneManagement; - -public class Menu : MonoBehaviour -{ - public string scenename; /*name of the gamescene*/ - - public void StartScene() - { - SceneManager.LoadScene(scenename); /*load the scene*/ - GameController.TimeStart(); - } - - public void QuitGame() - { - Debug.Log("QuitTest"); /*only for debugging*/ - Application.Quit(); /*close the game*/ - } - -} \ No newline at end of file diff --git a/Platformer/Assets/Scripts/Menu/MusicControl.cs b/Platformer/Assets/Scripts/Menu/MusicControl.cs deleted file mode 100644 index 88614db..0000000 --- a/Platformer/Assets/Scripts/Menu/MusicControl.cs +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Script: MusicControl - * Author: Philip Noack - * Last Change: 12.06.21 - * Load the soundtrack object in other scenes - */ - - -using UnityEngine; - -public class MusicControl : MonoBehaviour -{ - private void Awake() - { - GameObject[] objs = GameObject.FindGameObjectsWithTag("Soundtrack"); /*the soundtrack will not destroyed after change the scene and dont load twice, if the player change the scene again*/ - if (objs.Length > 1) - Destroy(this.gameObject); - DontDestroyOnLoad(this.gameObject); - } -} diff --git a/Platformer/Assets/Scripts/Menu/PauseMenu.cs b/Platformer/Assets/Scripts/Menu/PauseMenu.cs deleted file mode 100644 index eba399a..0000000 --- a/Platformer/Assets/Scripts/Menu/PauseMenu.cs +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Script: PauseMenu - * Author: Johannes Wilhelm, Philip Noack (PN) - * Last Change: 21.06.2021 - * bind ESC, controls status HelpUI and load Help- and Pausemenu - */ - -using System; -using System.Collections.Generic; -using UnityEngine; - -public class PauseMenu : MonoBehaviour -{ - /*public GameObject ui;*/ - public List ui; - - [SerializeField] private GameObject helpButton; - [SerializeField] private GameObject helpUI; - - public bool bind_to_key; - public KeyCode key; - - - private void Awake() - { - helpButton = GameObject.FindWithTag("HelpButton"); - helpUI = GameObject.FindWithTag("HelpUI"); - } - - private void Start() - { - foreach (var game_object in ui) - { - game_object.SetActive(false); - } - } - - // Update is called once per frame - private void Update() //PN - { - if (helpUI.activeSelf) //check if the HelpUi is active - { - bind_to_key = false; //ESC will ignore - } - else - { - bind_to_key = true; //ESC can be used normally - } - - if (bind_to_key == true) - { - KeyBind(); - } - } - - private void KeyBind() - { - { - if (Input.GetKeyDown(key)) - { - if (GameController.game_is_paused) { - ShowHelp(); - Resume(); - } - else { - Pause(); - HideHelp(); - } - } - } - } - - public void Resume() - { - foreach (var game_object in ui) - { - game_object.SetActive(false); - } - ShowHelp(); - GameController.TimeStart(); - } - - public void Pause() - { - foreach (var game_object in ui) - { - game_object.SetActive(true); - } - - GameController.TimeStop(); - } - - private void HideHelp() //hide Helpbutton if Pausemenu is active PN - { - helpButton.SetActive(false); - } - - private void ShowHelp() //show Helpbutton if Pausemenu isn´t active PN - { - helpButton.SetActive(true); - } -} - - - \ No newline at end of file diff --git a/Platformer/Assets/Scripts/Menu/Rocket.cs b/Platformer/Assets/Scripts/Menu/Rocket.cs deleted file mode 100644 index 5591f97..0000000 --- a/Platformer/Assets/Scripts/Menu/Rocket.cs +++ /dev/null @@ -1,110 +0,0 @@ -/* -* Script: Rocket -* Author: Philipp Scheffler -* Last Change: 21.06.21 -* Controls the rocket in menu.scene -*/ - -using UnityEngine; -using UnityEngine.UI; - - -public class Rocket : MonoBehaviour -{ - private Vector3 direction; - private Vector3 startposition; - - private Rigidbody r_rigidbody; - public Camera cam; - public Sprite[] rocket = new Sprite[4]; /*array with all used images*/ - - private float movingspeed = 10.0f; - private float border; - private int currentstate,direction_dec; - - - - void Awake() - { - cam = CameraView.GetCamera(); - r_rigidbody = GetComponent(); /*assign rigidbody*/ - Time.timeScale = 1.0f; - } - // Start is called before the first frame update - void Start() - { - r_rigidbody = GetComponent(); /*assign rigidbody*/ - ResetValues(); - direction_dec = 0; - } - - // Update is called once per frame - - void FixedUpdate() - { - r_rigidbody.velocity = direction * movingspeed; /*set velocity*/ - if (currentstate != 0 && currentstate%20==0) /*animation change every 20 frames*/ - { - gameObject.GetComponent().sprite = rocket[currentstate/20]; - currentstate++; - if ((currentstate-1) / 20 == 3) /*if the last image is shown, begin with the first*/ - { - currentstate = 0; - } - } - else - { - currentstate++; - } - /*if (r_rigidbody.position.y > border direction_dec == 2 ^ direction_dec == 0) - { - Debug.Log(true); - }*/ - - if (r_rigidbody.position.y > border & direction_dec < 2 ^ r_rigidbody.position.y < border & direction_dec >=2) - { - ResetValues(); - - if (direction_dec == 0) - { - direction.x = -direction.x; - startposition.x = -startposition.x; - gameObject.GetComponent().rectTransform.rotation = Quaternion.Euler(0, 0, -90); - direction_dec++; - } - - else if (direction_dec == 1) - { - direction = new Vector3(-direction.x, -direction.y, 0); - startposition = new Vector3(-startposition.x, -startposition.y, 0); - gameObject.GetComponent().rectTransform.rotation = Quaternion.Euler(0, 0, -180); - border = -border; - direction_dec++; - } - - else if (direction_dec == 2) - { - direction.y = -direction.y; - startposition.y = -startposition.y; - gameObject.GetComponent().rectTransform.rotation = Quaternion.Euler(0, 0, 90); - border = -border; - direction_dec++; - } - - else - { - direction_dec = 0; - gameObject.GetComponent().rectTransform.rotation = Quaternion.Euler(0, 0, 0); - } - - gameObject.GetComponent().rectTransform.anchoredPosition3D = startposition; /*reset image*/ - } - } - - void ResetValues() - { - startposition = (new Vector3(1440f, -1080f, 0)); - direction = new Vector3(-1.0f, 1.0f, 0.0f); /*set direction*/ - border=cam.ScreenToWorldPoint(new Vector3(0,2160,0)).y; - } -} \ No newline at end of file diff --git a/Platformer/Assets/Scripts/Menu/SettingsMenu.cs b/Platformer/Assets/Scripts/Menu/SettingsMenu.cs deleted file mode 100644 index d0f5ca1..0000000 --- a/Platformer/Assets/Scripts/Menu/SettingsMenu.cs +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Script: SettingsMenu - * Author: Philip Noack - * Last Change: 15.06.21 - * Source: https://answers.unity.com/questions/1463609/screenresolutions-returning-duplicates.html - * https://www.youtube.com/watch?v=YOaYQrN1oYQ - * https://www.youtube.com/watch?v=BX8IyTmkiMY - * https://www.youtube.com/watch?v=5onggHOiZaw - * Volume and Resolution control in OptionsMenu - */ - -using System.Collections.Generic; -using UnityEngine; -using UnityEngine.Audio; -using UnityEngine.UI; -using System.Linq; - - -public class SettingsMenu : MonoBehaviour -{ - public AudioMixer mixer; - public Slider slider; - public Dropdown resolutions_dropdown; - - private Resolution mon_resolution; - private Resolution[] resolutions; - public Toggle fullscreen_toggle; - - private int index = 0; - - - void Start() - { - slider.value = PlayerPrefs.GetFloat("MusicVolume", 0.5f); /*get current slider value*/ - - mon_resolution = Screen.currentResolution; - resolutions = Screen.resolutions.Select(resolution => new Resolution { width = resolution.width, height = resolution.height }).Distinct().ToArray(); /*filter for unique height and width results, before all resolutions were twice (source)*/ - - - /* chosen resolution ends up blurry, therefore removal of the feature, uncomment the next lines to activate the feature */ - /* - resolutions_dropdown.ClearOptions(); - List options = new List(); //new string for dropdownmenu - - int currentResolutionIndex = 0; - for (int i = 0; i < resolutions.Length; i++) - { - int aspectratio = resolutions[i].width % resolutions[i].height; //check if calculated resolution has an aspect ratio from 16:9 (16 mod 9 = 7) - if (aspectratio % 7 == 0) - { - string option = resolutions[i].width + "x" + resolutions[i].height; //add resolutions to dropdownmenu - options.Add(option); - } - - if (resolutions[i].width == Screen.width && resolutions[i].height == Screen.height) //current display resolution, will be automatically game resolution - { - currentResolutionIndex = i; - } - } - resolutions_dropdown.AddOptions(options); - resolutions_dropdown.value = currentResolutionIndex; - resolutions_dropdown.RefreshShownValue(); - */ - fullscreen_toggle.isOn = Screen.fullScreen; /* set the Fullscreen Tooglto the right setting when starting */ - - } - - - private void SetVolume (float sliderValue) - { - mixer.SetFloat("Volume", Mathf.Log10(sliderValue) * 20); /*calculate new volume (log function because the audio mixer value is logarithmic*/ - PlayerPrefs.SetFloat("MusicVolume", sliderValue); /*save volume settings*/ - } - - private void SetResolution(int resolutionIndex) - { - index = resolutionIndex; - - Resolution resolution = resolutions[resolutionIndex]; - - if (Screen.fullScreen == true) - { - Screen.SetResolution(resolution.width,resolution.height,FullScreenMode.Windowed); - Screen.SetResolution(resolution.width,resolution.height,FullScreenMode.ExclusiveFullScreen); - } - else - { - Screen.SetResolution(resolution.width,resolution.height,FullScreenMode.Windowed); - } - } - - private void SetFullscreen(bool isFullscreen) - { - Resolution resolution = resolutions[index]; - - if (isFullscreen) - { - Screen.SetResolution(mon_resolution.width, mon_resolution.height,FullScreenMode.FullScreenWindow); - } - else - { - Screen.SetResolution(resolution.width, resolution.height, FullScreenMode.Windowed); - } - } -} diff --git a/Platformer/Assets/Scripts/Menu.meta b/Platformer/Assets/Scripts/Menus.meta similarity index 100% rename from Platformer/Assets/Scripts/Menu.meta rename to Platformer/Assets/Scripts/Menus.meta diff --git a/Platformer/Assets/Scripts/Menus/DeathMenu.cs b/Platformer/Assets/Scripts/Menus/DeathMenu.cs new file mode 100644 index 0000000..4ed5fe8 --- /dev/null +++ b/Platformer/Assets/Scripts/Menus/DeathMenu.cs @@ -0,0 +1,164 @@ +/* +* Script: DeathMenu +* Author: Philipp Scheffler +* Last Change: 03.08.21 +* Read existing highscores, check for new highscore, write new highscore if necessary +*/ + +using System; +using System.IO; +using Global; +using TMPro; +using UnityEngine; + +namespace Menus +{ + public class DeathMenu : MonoBehaviour + { + [SerializeField] private GameObject helpButton, valueSaved, valueNotSaved, deathCluster; //create all gameObjects + + [SerializeField] private TextMeshPro score; //create textObject + + [SerializeField] private TextMeshProUGUI valueNotSavedText, textBox; //create textObjects + + private string[] currentSplitLine; + + private string path = HighscoreMenu.path; + + private int rank, buttonCheck, fail; + private static int loc_highscore; + + private StreamReader reader; + private StreamWriter writer; + + private readonly char split = HighscoreMenu.split; //import seperator from highscoreMenu + + private void Awake() + { + buttonCheck = 0; + valueNotSavedText.text = "Value not saved!"; + path = @"highscore.txt"; //set path for highscore file + } + + // Start is called before the first frame update + private void Start() + { + if (!File.Exists(path)) //check if highscore file already exists + { + using (var init = File.CreateText(path)) //create it at specified path + { + for (var i = 0; i < 10; i++) init.WriteLine("Init" + split + "0"); //fill it with initial values + init.Close(); //close StreamWriter + } + } + + try + { + reader = new StreamReader(path); + } + catch + { + //catch error if file is not readable and display it + fail = 1; + valueNotSavedText.text = "No highscore file accessible!"; + valueNotSaved.SetActive(true); + } + + + + helpButton.SetActive(false); + loc_highscore = Convert.ToInt32(Highscore.GetHighscore()); //import current highscore from game + score.text = "Score: " + loc_highscore; + + + + if (fail != 1) //if file was not readable before, skip reading + { + for (var i = 0; i < 10; i++) //highscore board from 1-10 + { + var currentLine = reader.ReadLine(); + + if (currentLine != null) + { + currentSplitLine = currentLine.Split(split); //split the line into name and matching highscore + try + { + for (var j = 0; j < 2; j++) HighscoreMenu.HighscoreList(j, i, currentSplitLine[j]); //try writing the contents of the file to the function HighscoreMenu.HighscoreList / array highscoreList + } + catch + { + //if the file is corrupt + valueNotSavedText.text = "Highscore file corrupt!"; + valueNotSaved.SetActive(true); + fail = 1; + } + } + } + reader.Close(); + + + + for (var i = 0; i < 10; i++) + if (HighscoreMenu.HighscoreList(0, i) == "Init") //check for initial values + { + i = NewHighscore(i); + buttonCheck = 1; //override the initial highscore + } + else if (loc_highscore > int.Parse(HighscoreMenu.HighscoreList(1, i))) //check if reached score is a highscore + { + i = NewHighscore(i); + } + } + } + + + private int NewHighscore(int i) //called, if a new highscore was set + { + deathCluster.SetActive(true); //set textbox, submit button, highscore text to active + rank = i; //reached rank + i = 9; //to exit the for loop + return i; + } + + public void SubmitHighscore() //write reached highscore into highscore file + { + var fail = 0; + try + { + writer = new StreamWriter(path, false); //override the current file + } + catch + { + fail = 1; + valueNotSavedText.text = "No highscore file accessible!"; + valueNotSaved.SetActive(true); + } + + + + if (fail == 0) + { + if (buttonCheck == 0) //check if button was already pressed after death + for (var i = 8; i >= rank; i--) + { + HighscoreMenu.HighscoreList(0, i + 1, HighscoreMenu.HighscoreList(0, i)); //shift every entry in the highscoreList array behind the highscore + HighscoreMenu.HighscoreList(1, i + 1, HighscoreMenu.HighscoreList(1, i)); + } + + HighscoreMenu.HighscoreList(0, rank, textBox.text); //write name into highscoreList array + HighscoreMenu.HighscoreList(1, rank, loc_highscore.ToString()); //write highscore into highscoreList array + for (var i = 0; i < 10; i++) + { + if (HighscoreMenu.HighscoreList(0, i) != null && HighscoreMenu.HighscoreList(1, i) != null) //check if the entries in the array are null + { + writer.WriteLine(HighscoreMenu.HighscoreList(0, i) + split + HighscoreMenu.HighscoreList(1, i)); //write array content of each line with seperator into file + } + } + + writer.Close(); + valueSaved.SetActive(true); //output success message + buttonCheck = 1; //set the variable that the button was already pressed to 1 + } + } + } +} \ No newline at end of file diff --git a/Platformer/Assets/Scripts/Menu/DeathMenu.cs.meta b/Platformer/Assets/Scripts/Menus/DeathMenu.cs.meta similarity index 100% rename from Platformer/Assets/Scripts/Menu/DeathMenu.cs.meta rename to Platformer/Assets/Scripts/Menus/DeathMenu.cs.meta diff --git a/Platformer/Assets/Scripts/Menus/HighscoreMenu.cs b/Platformer/Assets/Scripts/Menus/HighscoreMenu.cs new file mode 100644 index 0000000..6d0498f --- /dev/null +++ b/Platformer/Assets/Scripts/Menus/HighscoreMenu.cs @@ -0,0 +1,122 @@ +/* +* Script: HighscoreMenu +* Author: Philipp Scheffler +* Last Change: 03.08.21 +* Read highscore file and display top ten scoreboard +*/ + +using System.Collections.Generic; +using System.IO; +using TMPro; +using UnityEngine; + +namespace Menus +{ + public class HighscoreMenu : MonoBehaviour + { + [SerializeField] private List textList; + [SerializeField] private GameObject error; + [SerializeField] private TextMeshProUGUI errorText; + + private static string[,] highscoreList = new string[2,10]; //array, where highscore names and values are saved + + private string[] currentSplitLine; + private int empty = 0; + + public static char split = ''; //cryptic seperator to prevent accidental split + public static string path; + + private StreamReader reader; + private StreamWriter writer; + + private int fail = 0; + + + private void Awake() + { + path = @"highscore.txt"; //set path of highscore file + } + + // Start is called before the first frame update + void Start() + { + if (!File.Exists(path)) //check if highscore file already exists + { + using (var init = File.CreateText(path)) //create it at specified path + { + for (var i = 0; i < 10; i++) init.WriteLine("Init"+split+"0"); //fill it with initial values + init.Close(); + } + } + + try + { + reader = new StreamReader(path); + } + catch + { + //catch error if file is not readable and display it + fail = 1; + errorText.text = "No highscore file accessible!"; + error.SetActive(true); + } + + + + if (fail != 1) //if file was not readable before, skip reading + { + for (var i = 0; i < 10; i++) //highscore board from 1-10 + { + var currentLine = reader.ReadLine(); + + if (currentLine != null) + { + currentSplitLine = currentLine.Split(split); //split the line into name and matching highscore + try + { + for (var j = 0; j < 2; j++) HighscoreList(j, i, currentSplitLine[j]); //try writing the contents of the file to the function HighscoreMenu.HighscoreList / array highscoreList + } + catch + { + //if the file is corrupt + errorText.text = "Highscore file corrupt!"; + error.SetActive(true); + fail = 1; + } + } + } + reader.Close(); + + + + for (int i = 0; i < 10; i++) + { + if (HighscoreList(0, i) != "Init") //check for initial values + { + textList[i].text = (i+1)+". "+HighscoreList(0, i) + " " + HighscoreList(1, i); //display highscores + } + else + { + empty++; //variable to check if whole table is empty / initial + } + } + + if (empty == 10) + { + errorText.text = "No Highscores Yet, Play to Fill!"; + error.SetActive(true); + } + } + } + + + public static string HighscoreList(int x, int y) //function to return array values + { + return highscoreList[x,y]; + } + public static void HighscoreList(int x, int y, string b) //function to save array values + { + highscoreList[x, y] = b; + } + } +} diff --git a/Platformer/Assets/Scripts/Menu/HighscoreMenu.cs.meta b/Platformer/Assets/Scripts/Menus/HighscoreMenu.cs.meta similarity index 100% rename from Platformer/Assets/Scripts/Menu/HighscoreMenu.cs.meta rename to Platformer/Assets/Scripts/Menus/HighscoreMenu.cs.meta diff --git a/Platformer/Assets/Scripts/Menus/Menu.cs b/Platformer/Assets/Scripts/Menus/Menu.cs new file mode 100644 index 0000000..ce0094b --- /dev/null +++ b/Platformer/Assets/Scripts/Menus/Menu.cs @@ -0,0 +1,33 @@ +/* + * Script: Menu + * Author: Philip Noack + * Last Change: 12.06.21 + * Source: https://www.youtube.com/watch?v=zc8ac_qUXQY + * Play and Quit Button in Menu + */ + + +using Global; +using UnityEngine; +using UnityEngine.SceneManagement; + +namespace Menus +{ + public class Menu : MonoBehaviour + { + public string scenename; /*name of the gamescene*/ + + public void StartScene() + { + SceneManager.LoadScene(scenename); /*load the scene*/ + GameController.TimeStart(); + } + + public void QuitGame() + { + Debug.Log("QuitTest"); /*only for debugging*/ + Application.Quit(); /*close the game*/ + } + + } +} \ No newline at end of file diff --git a/Platformer/Assets/Scripts/Menu/Menu.cs.meta b/Platformer/Assets/Scripts/Menus/Menu.cs.meta similarity index 100% rename from Platformer/Assets/Scripts/Menu/Menu.cs.meta rename to Platformer/Assets/Scripts/Menus/Menu.cs.meta diff --git a/Platformer/Assets/Scripts/Menus/MusicControl.cs b/Platformer/Assets/Scripts/Menus/MusicControl.cs new file mode 100644 index 0000000..7035dc5 --- /dev/null +++ b/Platformer/Assets/Scripts/Menus/MusicControl.cs @@ -0,0 +1,23 @@ +/* + * Script: MusicControl + * Author: Philip Noack + * Last Change: 12.06.21 + * Load the soundtrack object in other scenes + */ + + +using UnityEngine; + +namespace Menus +{ + public class MusicControl : MonoBehaviour + { + private void Awake() + { + GameObject[] objs = GameObject.FindGameObjectsWithTag("Soundtrack"); /*the soundtrack will not destroyed after change the scene and dont load twice, if the player change the scene again*/ + if (objs.Length > 1) + Destroy(this.gameObject); + DontDestroyOnLoad(this.gameObject); + } + } +} diff --git a/Platformer/Assets/Scripts/Menu/MusicControl.cs.meta b/Platformer/Assets/Scripts/Menus/MusicControl.cs.meta similarity index 100% rename from Platformer/Assets/Scripts/Menu/MusicControl.cs.meta rename to Platformer/Assets/Scripts/Menus/MusicControl.cs.meta diff --git a/Platformer/Assets/Scripts/Menus/PauseMenu.cs b/Platformer/Assets/Scripts/Menus/PauseMenu.cs new file mode 100644 index 0000000..147c3dc --- /dev/null +++ b/Platformer/Assets/Scripts/Menus/PauseMenu.cs @@ -0,0 +1,108 @@ +/* + * Script: PauseMenu + * Author: Johannes Wilhelm, Philip Noack (PN) + * Last Change: 21.06.2021 + * bind ESC, controls status HelpUI and load Help- and Pausemenu + */ + +using System.Collections.Generic; +using Global; +using UnityEngine; + +namespace Menus +{ + public class PauseMenu : MonoBehaviour + { + /*public GameObject ui;*/ + public List ui; + + [SerializeField] private GameObject helpButton; + [SerializeField] private GameObject helpUI; + + public bool bind_to_key; + public KeyCode key; + + + private void Awake() + { + helpButton = GameObject.FindWithTag("HelpButton"); + helpUI = GameObject.FindWithTag("HelpUI"); + } + + private void Start() + { + foreach (var game_object in ui) + { + game_object.SetActive(false); + } + } + + // Update is called once per frame + private void Update() //PN + { + if (helpUI.activeSelf) //check if the HelpUi is active + { + bind_to_key = false; //ESC will ignore + } + else + { + bind_to_key = true; //ESC can be used normally + } + + if (bind_to_key == true) + { + KeyBind(); + } + } + + private void KeyBind() + { + { + if (Input.GetKeyDown(key)) + { + if (GameController.game_is_paused) { + ShowHelp(); + Resume(); + } + else { + Pause(); + HideHelp(); + } + } + } + } + + public void Resume() + { + foreach (var game_object in ui) + { + game_object.SetActive(false); + } + ShowHelp(); + GameController.TimeStart(); + } + + public void Pause() + { + foreach (var game_object in ui) + { + game_object.SetActive(true); + } + + GameController.TimeStop(); + } + + private void HideHelp() //hide Helpbutton if Pausemenu is active PN + { + helpButton.SetActive(false); + } + + private void ShowHelp() //show Helpbutton if Pausemenu isn´t active PN + { + helpButton.SetActive(true); + } + } +} + + + \ No newline at end of file diff --git a/Platformer/Assets/Scripts/Menu/PauseMenu.cs.meta b/Platformer/Assets/Scripts/Menus/PauseMenu.cs.meta similarity index 100% rename from Platformer/Assets/Scripts/Menu/PauseMenu.cs.meta rename to Platformer/Assets/Scripts/Menus/PauseMenu.cs.meta diff --git a/Platformer/Assets/Scripts/Menus/Rocket.cs b/Platformer/Assets/Scripts/Menus/Rocket.cs new file mode 100644 index 0000000..3ecba72 --- /dev/null +++ b/Platformer/Assets/Scripts/Menus/Rocket.cs @@ -0,0 +1,110 @@ +/* +* Script: Rocket +* Author: Philipp Scheffler +* Last Change: 27.07.21 +* Control flying rocket in menu.scene +*/ + +using UnityEngine; +using UnityEngine.UI; +using Viewport; + +namespace Menus +{ + public class Rocket : MonoBehaviour + { + private Vector3 direction; + private Vector3 startposition; + + private Rigidbody r_rigidbody; + public Camera cam; + public Sprite[] rocket = new Sprite[4]; //array with all used images + + private float movingspeed = 10.0f; + private float border; + private int currentstate,direction_dec; + + + + void Awake() + { + cam = CameraView.GetCamera(); + r_rigidbody = GetComponent(); //assign rigidbody + Time.timeScale = 1.0f; + } + // Start is called before the first frame update + void Start() + { + r_rigidbody = GetComponent(); //assign rigidbody + ResetValues(); + direction_dec = 0; + } + + // Update is called once per frame + + void FixedUpdate() + { + r_rigidbody.velocity = direction * movingspeed; //set velocity + if (currentstate != 0 && currentstate%20==0) //animation change every 20 frames + { + gameObject.GetComponent().sprite = rocket[currentstate/20]; //show the matching image + currentstate++; + if ((currentstate-1) / 20 == 3) //if the last image is shown, begin with the first + { + currentstate = 0; + } + } + else + { + currentstate++; + } + + if (r_rigidbody.position.y > border & direction_dec < 2 ^ r_rigidbody.position.y < border & direction_dec >=2) //check border dependent on the direction + { + //if border is reached + ResetValues(); + + if (direction_dec == 0) //down right to up left + { + direction.x = -direction.x; + startposition.x = -startposition.x; + gameObject.GetComponent().rectTransform.rotation = Quaternion.Euler(0, 0, -90); + direction_dec++; + } + + else if (direction_dec == 1) //down left to up right + { + direction = new Vector3(-direction.x, -direction.y, 0); + startposition = new Vector3(-startposition.x, -startposition.y, 0); + gameObject.GetComponent().rectTransform.rotation = Quaternion.Euler(0, 0, -180); + border = -border; + direction_dec++; + } + + else if (direction_dec == 2) //up left to down right + { + direction.y = -direction.y; + startposition.y = -startposition.y; + gameObject.GetComponent().rectTransform.rotation = Quaternion.Euler(0, 0, 90); + border = -border; + direction_dec++; + } + + else //up right do down left + { + direction_dec = 0; + gameObject.GetComponent().rectTransform.rotation = Quaternion.Euler(0, 0, 0); + } + + gameObject.GetComponent().rectTransform.anchoredPosition3D = startposition; //reset image + } + } + + void ResetValues() + { + startposition = (new Vector3(1440f, -1080f, 0)); //set startposition + direction = new Vector3(-1.0f, 1.0f, 0.0f); //set direction + border=cam.ScreenToWorldPoint(new Vector3(0,2160,0)).y; //set border + } + } +} \ No newline at end of file diff --git a/Platformer/Assets/Scripts/Menu/Rocket.cs.meta b/Platformer/Assets/Scripts/Menus/Rocket.cs.meta similarity index 100% rename from Platformer/Assets/Scripts/Menu/Rocket.cs.meta rename to Platformer/Assets/Scripts/Menus/Rocket.cs.meta diff --git a/Platformer/Assets/Scripts/Menus/SettingsMenu.cs b/Platformer/Assets/Scripts/Menus/SettingsMenu.cs new file mode 100644 index 0000000..ce1724b --- /dev/null +++ b/Platformer/Assets/Scripts/Menus/SettingsMenu.cs @@ -0,0 +1,106 @@ +/* + * Script: SettingsMenu + * Author: Philip Noack + * Last Change: 15.06.21 + * Source: https://answers.unity.com/questions/1463609/screenresolutions-returning-duplicates.html + * https://www.youtube.com/watch?v=YOaYQrN1oYQ + * https://www.youtube.com/watch?v=BX8IyTmkiMY + * https://www.youtube.com/watch?v=5onggHOiZaw + * Volume and Resolution control in OptionsMenu + */ + +using System.Linq; +using UnityEngine; +using UnityEngine.Audio; +using UnityEngine.UI; + +namespace Menus +{ + public class SettingsMenu : MonoBehaviour + { + public AudioMixer mixer; + public Slider slider; + public Dropdown resolutions_dropdown; + + private Resolution mon_resolution; + private Resolution[] resolutions; + public Toggle fullscreen_toggle; + + private int index = 0; + + + void Start() + { + slider.value = PlayerPrefs.GetFloat("MusicVolume", 0.5f); /*get current slider value*/ + + mon_resolution = Screen.currentResolution; + resolutions = Screen.resolutions.Select(resolution => new Resolution { width = resolution.width, height = resolution.height }).Distinct().ToArray(); /*filter for unique height and width results, before all resolutions were twice (source)*/ + + + /* chosen resolution ends up blurry, therefore removal of the feature, uncomment the next lines to activate the feature */ + /* + resolutions_dropdown.ClearOptions(); + List options = new List(); //new string for dropdownmenu + + int currentResolutionIndex = 0; + for (int i = 0; i < resolutions.Length; i++) + { + int aspectratio = resolutions[i].width % resolutions[i].height; //check if calculated resolution has an aspect ratio from 16:9 (16 mod 9 = 7) + if (aspectratio % 7 == 0) + { + string option = resolutions[i].width + "x" + resolutions[i].height; //add resolutions to dropdownmenu + options.Add(option); + } + + if (resolutions[i].width == Screen.width && resolutions[i].height == Screen.height) //current display resolution, will be automatically game resolution + { + currentResolutionIndex = i; + } + } + resolutions_dropdown.AddOptions(options); + resolutions_dropdown.value = currentResolutionIndex; + resolutions_dropdown.RefreshShownValue(); + */ + fullscreen_toggle.isOn = Screen.fullScreen; /* set the Fullscreen Tooglto the right setting when starting */ + + } + + + private void SetVolume (float sliderValue) + { + mixer.SetFloat("Volume", Mathf.Log10(sliderValue) * 20); /*calculate new volume (log function because the audio mixer value is logarithmic*/ + PlayerPrefs.SetFloat("MusicVolume", sliderValue); /*save volume settings*/ + } + + private void SetResolution(int resolutionIndex) + { + index = resolutionIndex; + + Resolution resolution = resolutions[resolutionIndex]; + + if (Screen.fullScreen == true) + { + Screen.SetResolution(resolution.width,resolution.height,FullScreenMode.Windowed); + Screen.SetResolution(resolution.width,resolution.height,FullScreenMode.ExclusiveFullScreen); + } + else + { + Screen.SetResolution(resolution.width,resolution.height,FullScreenMode.Windowed); + } + } + + private void SetFullscreen(bool isFullscreen) + { + Resolution resolution = resolutions[index]; + + if (isFullscreen) + { + Screen.SetResolution(mon_resolution.width, mon_resolution.height,FullScreenMode.FullScreenWindow); + } + else + { + Screen.SetResolution(resolution.width, resolution.height, FullScreenMode.Windowed); + } + } + } +} diff --git a/Platformer/Assets/Scripts/Menu/SettingsMenu.cs.meta b/Platformer/Assets/Scripts/Menus/SettingsMenu.cs.meta similarity index 100% rename from Platformer/Assets/Scripts/Menu/SettingsMenu.cs.meta rename to Platformer/Assets/Scripts/Menus/SettingsMenu.cs.meta diff --git a/Platformer/Assets/Scripts/Player/EntityMovement.cs b/Platformer/Assets/Scripts/Player/EntityMovement.cs index 3972606..52e8a9c 100644 --- a/Platformer/Assets/Scripts/Player/EntityMovement.cs +++ b/Platformer/Assets/Scripts/Player/EntityMovement.cs @@ -1,5 +1,5 @@ /* - * Script: Entity Movement + * Script: EntityMovement * Author: Vincent Becker * Source: https://github.com/Brackeys/2D-Character-Controller * Last Change: 01.06.21 @@ -10,83 +10,86 @@ using UnityEngine; using UnityEngine.Events; -public class EntityMovement : MonoBehaviour +namespace Player { - private const float groundedradius = .2f; /* Radius of the overlap circle to determine if grounded */ - [SerializeField] private float jumpforce = 400f; /* SerializeField show variable in Inspector */ - [Range(0, .3f)] [SerializeField] private float movementsmoothing = .05f; - [SerializeField] private bool aircontrol; - [SerializeField] private LayerMask whatisground; - [SerializeField] private Transform groundcheck; + public class EntityMovement : MonoBehaviour + { + private const float groundedradius = .2f; /* Radius of the overlap circle to determine if grounded */ + [SerializeField] private float jumpforce = 400f; /* SerializeField show variable in Inspector */ + [Range(0, .3f)] [SerializeField] private float movementsmoothing = .05f; + [SerializeField] private bool aircontrol; + [SerializeField] private LayerMask whatisground; + [SerializeField] private Transform groundcheck; - [Header("Events")] /* Structured view in the Inspector */ [Space] - public UnityEvent OnLandEvent; + [Header("Events")] /* Structured view in the Inspector */ [Space] + public UnityEvent OnLandEvent; - private bool facingright = true; /* for determining which way the player is currently facing */ - private bool grounded; - private Rigidbody2D v_rigidbody2D; - private Vector3 velocity = Vector3.zero; + private bool facingright = true; /* for determining which way the player is currently facing */ + private bool grounded; + private Rigidbody2D v_rigidbody2D; + private Vector3 velocity = Vector3.zero; - //Awake is called when the script is being loaded - private void Awake() - { - groundcheck = GameObject.Find("Groundcheck").GetComponent(); - v_rigidbody2D = GetComponent(); + //Awake is called when the script is being loaded + private void Awake() + { + groundcheck = GameObject.Find("Groundcheck").GetComponent(); + v_rigidbody2D = GetComponent(); - if (OnLandEvent == null) - OnLandEvent = new UnityEvent(); - } + if (OnLandEvent == null) + OnLandEvent = new UnityEvent(); + } - //used for physics calculations, FixedUpdate gets called 50 times per second regardless of Fps - private void FixedUpdate() - { - var wasgrounded = grounded; - grounded = false; + //used for physics calculations, FixedUpdate gets called 50 times per second regardless of Fps + private void FixedUpdate() + { + var wasgrounded = grounded; + grounded = false; + + /* The entity is grounded if a circlecast to the groundcheck position hits anything designated as ground */ + var colliders = Physics2D.OverlapCircleAll(groundcheck.position, groundedradius, whatisground); + for (var i = 0; i < colliders.Length; i++) + if (colliders[i].gameObject != gameObject) + { + grounded = true; + if (!wasgrounded) + OnLandEvent.Invoke(); + } + } - /* The entity is grounded if a circlecast to the groundcheck position hits anything designated as ground */ - var colliders = Physics2D.OverlapCircleAll(groundcheck.position, groundedradius, whatisground); - for (var i = 0; i < colliders.Length; i++) - if (colliders[i].gameObject != gameObject) + public void Move(float move, bool jump) + { + if (grounded || aircontrol) { - grounded = true; - if (!wasgrounded) - OnLandEvent.Invoke(); + Vector3 targetVelocity = new Vector2(move * 10f, v_rigidbody2D.velocity.y); + /* Smooth and apply the velocity to the entity */ + v_rigidbody2D.velocity = + Vector3.SmoothDamp(v_rigidbody2D.velocity, targetVelocity, ref velocity, movementsmoothing); + + if (move > 0 && !facingright) + Flip(); + else if (move < 0 && facingright) Flip(); } - } - public void Move(float move, bool jump) - { - if (grounded || aircontrol) + if (grounded && jump) + { + grounded = false; + v_rigidbody2D.AddForce(new Vector2(0f, jumpforce)); + } + } + + private void Flip() { - Vector3 targetVelocity = new Vector2(move * 10f, v_rigidbody2D.velocity.y); - /* Smooth and apply the velocity to the entity */ - v_rigidbody2D.velocity = - Vector3.SmoothDamp(v_rigidbody2D.velocity, targetVelocity, ref velocity, movementsmoothing); + /* Switch the way the entity is labelled as facing */ + facingright = !facingright; - if (move > 0 && !facingright) - Flip(); - else if (move < 0 && facingright) Flip(); + var theScale = transform.localScale; + theScale.x *= -1; + transform.localScale = theScale; } - if (grounded && jump) + [Serializable] public class BoolEvent : UnityEvent { - grounded = false; - v_rigidbody2D.AddForce(new Vector2(0f, jumpforce)); } } - - private void Flip() - { - /* Switch the way the entity is labelled as facing */ - facingright = !facingright; - - var theScale = transform.localScale; - theScale.x *= -1; - transform.localScale = theScale; - } - - [Serializable] public class BoolEvent : UnityEvent - { - } } \ No newline at end of file diff --git a/Platformer/Assets/Scripts/Player/PlayerController.cs b/Platformer/Assets/Scripts/Player/PlayerController.cs index c6fd707..90290ce 100644 --- a/Platformer/Assets/Scripts/Player/PlayerController.cs +++ b/Platformer/Assets/Scripts/Player/PlayerController.cs @@ -1,5 +1,5 @@ /* - * Script: Player Controller + * Script: PlayerController * Author: Vincent Becker * Last Change: 26.07.21 * Extension of EntityMovement, Control of player movement @@ -8,41 +8,44 @@ using UnityEngine; -public class PlayerController : MonoBehaviour +namespace Player { - public EntityMovement controller; + public class PlayerController : MonoBehaviour + { + public EntityMovement controller; - private static Vector3 pos; - public float runspeed = 40f; - private float horizontalmove = 0f; - private bool jump = false; + private static Vector3 pos; + public float runspeed = 40f; + private float horizontalmove = 0f; + private bool jump = false; - private void Awake() - { - controller = GetComponent(); - pos = GetComponent().position; - } + private void Awake() + { + controller = GetComponent(); + pos = GetComponent().position; + } - private void Update() - { - controller.Move(horizontalmove * Time.fixedDeltaTime, jump); - jump = false; + private void Update() + { + controller.Move(horizontalmove * Time.fixedDeltaTime, jump); + jump = false; - horizontalmove = Input.GetAxisRaw("Horizontal") * runspeed; + horizontalmove = Input.GetAxisRaw("Horizontal") * runspeed; - if (Input.GetButtonDown("Jump")) - { - jump = true; + if (Input.GetButtonDown("Jump")) + { + jump = true; + } } - } - private void FixedUpdate() - { - pos = GetComponent().position; - } + private void FixedUpdate() + { + pos = GetComponent().position; + } - public static Vector3 GetPlayerPos() - { - return pos; + public static Vector3 GetPlayerPos() + { + return pos; + } } } \ No newline at end of file diff --git a/Platformer/Assets/Scripts/Player/PlayerMovement.cs b/Platformer/Assets/Scripts/Player/PlayerMovement.cs index 9d196a5..e4fbf86 100644 --- a/Platformer/Assets/Scripts/Player/PlayerMovement.cs +++ b/Platformer/Assets/Scripts/Player/PlayerMovement.cs @@ -1,5 +1,5 @@ /* - * Script: Player Movement + * Script: PlayerMovement * Author: Felix Schneider * Last Change: 20.05.21 * Not used anymore @@ -8,39 +8,42 @@ using UnityEngine; -public class PlayerMovement : MonoBehaviour +namespace Player { - public new Rigidbody2D rigidbody; - public new Transform transform; - public float force; - - private void Awake() + public class PlayerMovement : MonoBehaviour { - rigidbody = GetComponent(); - transform = GetComponent(); - } - - // Update is called once per frame - private void FixedUpdate() - { - if (Input.GetKey(KeyCode.W)) - { - rigidbody.AddForce(transform.up * force); - } - - if (Input.GetKey(KeyCode.S)) - { - rigidbody.AddForce(transform.up * -force); - } + public new Rigidbody2D rigidbody; + public new Transform transform; + public float force; - if (Input.GetKey(KeyCode.A)) + private void Awake() { - rigidbody.AddForce(transform.right * -force); + rigidbody = GetComponent(); + transform = GetComponent(); } - if (Input.GetKey(KeyCode.D)) + // Update is called once per frame + private void FixedUpdate() { - rigidbody.AddForce(transform.right * force); + if (Input.GetKey(KeyCode.W)) + { + rigidbody.AddForce(transform.up * force); + } + + if (Input.GetKey(KeyCode.S)) + { + rigidbody.AddForce(transform.up * -force); + } + + if (Input.GetKey(KeyCode.A)) + { + rigidbody.AddForce(transform.right * -force); + } + + if (Input.GetKey(KeyCode.D)) + { + rigidbody.AddForce(transform.right * force); + } } } } diff --git a/Platformer/Assets/Scripts/Player/PlayerSpawn.cs b/Platformer/Assets/Scripts/Player/PlayerSpawn.cs index 41ba43f..d817bdc 100644 --- a/Platformer/Assets/Scripts/Player/PlayerSpawn.cs +++ b/Platformer/Assets/Scripts/Player/PlayerSpawn.cs @@ -6,86 +6,87 @@ */ +using Global; using UnityEngine; -public class PlayerSpawn : MonoBehaviour +namespace Player { - [SerializeField] private GameObject player; - [SerializeField] private GameObject death; - private GameObject player_instance; + public class PlayerSpawn : MonoBehaviour + { + [SerializeField] private GameObject player; + [SerializeField] private GameObject death; + private GameObject player_instance; - public float falldeath; /* 0 is bottom of start chunk */ + public float falldeath; /* 0 is bottom of start chunk */ - public EdgeCollider2D deathwall; /* just to visualize */ + public EdgeCollider2D deathwall; /* just to visualize */ - [SerializeField] private float diff; - public static float edge_death; - private float last_edge_death; + public static float edge_death; + private float last_edge_death; - [SerializeField] private float linear_speed; - private static float lerp_speed = 1f; + [SerializeField] private float linear_speed; + private static float lerp_speed = 1f; - private void Awake() - { - GameController.TimeStart(); - player_instance = Instantiate(player,GameController.GetStartPos(), Quaternion.identity, transform); - deathwall = GetComponentInChildren(); + private void Awake() + { + GameController.TimeStart(); + player_instance = Instantiate(player,GameController.GetStartPos(), Quaternion.identity, transform); + deathwall = GetComponentInChildren(); - } + } - // Start is called before the first frame update - private void Start() - { - edge_death = 0.1f; - } + // Start is called before the first frame update + private void Start() + { + edge_death = 0.1f; + } - // Update is called once per frame - private void Update() - { - if (!GameController.game_is_paused) + // Update is called once per frame + private void Update() { - if (player_instance.transform.position.y < falldeath || player_instance.transform.position.x < edge_death) + if (!GameController.game_is_paused) { - GameController.TimeStop(); - death.SetActive(true); + if (player_instance.transform.position.y < falldeath || player_instance.transform.position.x < edge_death) + { + GameController.TimeStop(); + death.SetActive(true); + } } - } - //Deathwall - linear_speed = 3f * Time.deltaTime; - FunktionDeathwall(); - SetDeathwall(); - } + //Deathwall + linear_speed = 3f * Time.deltaTime; /* deltaTime ensures framerate independence */ + FunktionDeathwall(); + SetDeathwall(); + } - private void SetDeathwall() - { - Vector2 point_1 = new Vector2(edge_death, 100); - Vector2 point_2 = new Vector2(edge_death, -100); + private void SetDeathwall() + { + Vector2 point_1 = new Vector2(edge_death, 100); + Vector2 point_2 = new Vector2(edge_death, -100); - Vector2 [] pointArray = new Vector2[] {point_1,point_2,point_1}; + Vector2 [] pointArray = new Vector2[] {point_1,point_2,point_1}; - deathwall.points = pointArray; - } + deathwall.points = pointArray; + } - private void FunktionDeathwall() - { - edge_death = edge_death + linear_speed; - - if (PlayerController.GetPlayerPos().x - edge_death > 24f) /* Deathwall has a rubberband effect stays tries to not let the player advance more than 24f before the Dathwall */ + private void FunktionDeathwall() { - edge_death = Lerp(edge_death, PlayerController.GetPlayerPos().x - 24f); - Debug.Log("Lerp"); - } + edge_death = edge_death + linear_speed; - } + if (PlayerController.GetPlayerPos().x - edge_death > 24f) /* Deathwall has a rubberband effect stays tries to not let the player advance more than 24f before the Dathwall */ + { + edge_death = Lerp(edge_death, PlayerController.GetPlayerPos().x - 24f); + } + } - public static float Lerp(float last_pos, float pos) /* lets the octopus trail behind the player on one axis */ - { - float value; - value = Mathf.Lerp(last_pos, pos, Time.deltaTime * lerp_speed); - return value; + public static float Lerp(float last_pos, float pos) /* lets the octopus trail behind the player on one axis */ + { + float value; + value = Mathf.Lerp(last_pos, pos, Time.deltaTime * lerp_speed); + return value; + } } } diff --git a/Platformer/Assets/Scripts/Screen/CameraView.cs b/Platformer/Assets/Scripts/Screen/CameraView.cs deleted file mode 100644 index fbeccf7..0000000 --- a/Platformer/Assets/Scripts/Screen/CameraView.cs +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Script: Camera View - * Author: Felix Schneider - * Last Change: 10.06.21 - * Sets Camera to the position of the player - */ - - -using UnityEngine; - -public class CameraView : MonoBehaviour -{ - [SerializeField] private Camera cam; - private static Vector3 cam_pos; - - private void Awake() - { - cam = GameObject.Find("CameraPlayer").GetComponent(); /* Finds the GameObject CameraPlayer with the Component Camera */ - } - - // Start is called before the first frame update - void Start() - { - SetCamera(); - GetCameraPos(); - } - - // LateUpdate is called after Update - void Update() - { - SetCamera(); /* Has to be set because player moves every frame */ - GetCameraPos(); - } - - private void SetCamera() - { - var player_pos = PlayerController.GetPlayerPos(); - var camera_pos = cam.transform; - camera_pos.position = new Vector3(player_pos.x, player_pos.y, camera_pos.position.z); /* Camera follows Player exactly */ - } - - private void GetCameraPos() - { - cam_pos = cam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, - cam.transform.position.z)); - } - - public static Camera GetCamera() - { - return Camera.main; - } - - public static Vector3 GetScreenPos() - { - return cam_pos; - } -} diff --git a/Platformer/Assets/Scripts/Screen/ScreenViewport.cs b/Platformer/Assets/Scripts/Screen/ScreenViewport.cs deleted file mode 100644 index c547f40..0000000 --- a/Platformer/Assets/Scripts/Screen/ScreenViewport.cs +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Script: ScreenViewport - * Author: Felix Schneider - * Last Change: 10.06.21 - * Handles the ScreenViewport for other Objects to be Controlled - */ - -using UnityEngine; - -public class ScreenViewport : MonoBehaviour -{ - [SerializeField] private EdgeCollider2D edge_collider; - [SerializeField] private Camera cam; /* show private attribute in inspector */ - - private Vector3 pos; /* will store recent player position */ - - private static float width; - private static float hight; - - private static float left_edge; - private static float right_edge; - private static float top_edge; - private static float bottom_edge; - - private static float ext_edge; - - // Awake - private void Awake() - { - edge_collider = GameObject.Find("Boundary").GetComponent(); - cam = CameraView.GetCamera(); - pos = PlayerController.GetPlayerPos(); - - FindDimensions(); - } - - // Start is called before the first frame update - private void Start() - { - FindDimensions(); - ext_edge = 32f; - } - - // Update is called once per frame - private void Update() - { - pos = PlayerController.GetPlayerPos(); - - FindDimensions(); - SetEdges(); - SetBoundary(); - } - - private void FindDimensions() - { - width = 1 / (cam.WorldToViewportPoint(new Vector3(1, 1, 0) + pos).x - 0.5f); //Viewport Transformation - hight = 1 / (cam.WorldToViewportPoint(new Vector3(1, 1, 0) + pos).y - 0.5f); - } - - private void SetEdges() //set edges with camera pos --> Camera Viewport defines edges - { - left_edge = (-width/ 2) + pos.x; - right_edge = (width/ 2) + pos.x; - - top_edge = (hight / 2) + pos.y; - bottom_edge = (-hight / 2) + pos.y; - } - - private void SetBoundary() //with edgepoints boundary can be set - { - Vector2 point_1 = new Vector2(right_edge, top_edge); - Vector2 point_2 = new Vector2(right_edge, bottom_edge); - Vector2 point_3 = new Vector2(left_edge , bottom_edge); - Vector2 point_4 = new Vector2(left_edge, top_edge); - Vector2 [] pointArray = new Vector2[] {point_1,point_2,point_3,point_4,point_1}; - - edge_collider.points = pointArray; - } - - public static float GetWidth() - { - return width; - } - public static float GetHeight() - { - return hight; - } - - - public static void OutBoundary(Transform trans) /* sets the given Transform to another position respective to the Viewport */ - { - var pos = trans.position; - - if (pos.x < left_edge - ext_edge) - { - pos = new Vector3(right_edge + ext_edge, pos.y, pos.z); - } - - if (pos.x > right_edge + ext_edge) - { - pos = new Vector3(left_edge - ext_edge, pos.y, pos.z); - } - trans.position = pos; - } -} - diff --git a/Platformer/Assets/Scripts/Screen.meta b/Platformer/Assets/Scripts/Viewport.meta similarity index 100% rename from Platformer/Assets/Scripts/Screen.meta rename to Platformer/Assets/Scripts/Viewport.meta diff --git a/Platformer/Assets/Scripts/Viewport/CameraView.cs b/Platformer/Assets/Scripts/Viewport/CameraView.cs new file mode 100644 index 0000000..29e9fca --- /dev/null +++ b/Platformer/Assets/Scripts/Viewport/CameraView.cs @@ -0,0 +1,60 @@ +/* + * Script: Camera View + * Author: Felix Schneider + * Last Change: 10.06.21 + * Sets Camera to the position of the player + */ + + +using Player; +using UnityEngine; + +namespace Viewport +{ + public class CameraView : MonoBehaviour + { + [SerializeField] private Camera cam; + private static Vector3 cam_pos; + + private void Awake() + { + cam = GameObject.Find("CameraPlayer").GetComponent(); /* Finds the GameObject CameraPlayer with the Component Camera */ + } + + // Start is called before the first frame update + void Start() + { + SetCamera(); + GetCameraPos(); + } + + // LateUpdate is called after Update + void Update() + { + SetCamera(); /* Has to be set because player moves every frame */ + GetCameraPos(); + } + + private void SetCamera() + { + var player_pos = PlayerController.GetPlayerPos(); + var camera_pos = cam.transform; + camera_pos.position = new Vector3(player_pos.x, player_pos.y, camera_pos.position.z); /* Camera follows Player exactly */ + } + + private void GetCameraPos() + { + cam_pos = cam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, - cam.transform.position.z)); + } + + public static Camera GetCamera() + { + return Camera.main; + } + + public static Vector3 GetScreenPos() + { + return cam_pos; + } + } +} diff --git a/Platformer/Assets/Scripts/Screen/CameraView.cs.meta b/Platformer/Assets/Scripts/Viewport/CameraView.cs.meta similarity index 100% rename from Platformer/Assets/Scripts/Screen/CameraView.cs.meta rename to Platformer/Assets/Scripts/Viewport/CameraView.cs.meta diff --git a/Platformer/Assets/Scripts/Viewport/ScreenViewport.cs b/Platformer/Assets/Scripts/Viewport/ScreenViewport.cs new file mode 100644 index 0000000..3694cab --- /dev/null +++ b/Platformer/Assets/Scripts/Viewport/ScreenViewport.cs @@ -0,0 +1,110 @@ +/* + * Script: ScreenViewport + * Author: Felix Schneider + * Last Change: 10.06.21 + * Handles the ScreenViewport for other Objects to be Controlled + */ + +using Player; +using UnityEngine; + +namespace Viewport +{ + public class ScreenViewport : MonoBehaviour + { + [SerializeField] private EdgeCollider2D edge_collider; + [SerializeField] private Camera cam; /* show private attribute in inspector */ + + private Vector3 pos; /* will store recent player position */ + + private static float width; + private static float hight; + + private static float left_edge; + private static float right_edge; + private static float top_edge; + private static float bottom_edge; + + private static float ext_edge; + + // Awake + private void Awake() + { + edge_collider = GameObject.Find("Boundary").GetComponent(); + cam = CameraView.GetCamera(); + pos = PlayerController.GetPlayerPos(); + + FindDimensions(); + } + + // Start is called before the first frame update + private void Start() + { + FindDimensions(); + ext_edge = 32f; + } + + // Update is called once per frame + private void Update() + { + pos = PlayerController.GetPlayerPos(); + + FindDimensions(); + SetEdges(); + SetBoundary(); + } + + private void FindDimensions() + { + width = 1 / (cam.WorldToViewportPoint(new Vector3(1, 1, 0) + pos).x - 0.5f); //Viewport Transformation + hight = 1 / (cam.WorldToViewportPoint(new Vector3(1, 1, 0) + pos).y - 0.5f); + } + + private void SetEdges() //set edges with camera pos --> Camera Viewport defines edges + { + left_edge = (-width/ 2) + pos.x; + right_edge = (width/ 2) + pos.x; + + top_edge = (hight / 2) + pos.y; + bottom_edge = (-hight / 2) + pos.y; + } + + private void SetBoundary() //with edgepoints boundary can be set + { + Vector2 point_1 = new Vector2(right_edge, top_edge); + Vector2 point_2 = new Vector2(right_edge, bottom_edge); + Vector2 point_3 = new Vector2(left_edge , bottom_edge); + Vector2 point_4 = new Vector2(left_edge, top_edge); + Vector2 [] pointArray = new Vector2[] {point_1,point_2,point_3,point_4,point_1}; + + edge_collider.points = pointArray; + } + + public static float GetWidth() + { + return width; + } + public static float GetHeight() + { + return hight; + } + + + public static void OutBoundary(Transform trans) /* sets the given Transform to another position respective to the Viewport */ + { + var pos = trans.position; + + if (pos.x < left_edge - ext_edge) + { + pos = new Vector3(right_edge + ext_edge, pos.y, pos.z); + } + + if (pos.x > right_edge + ext_edge) + { + pos = new Vector3(left_edge - ext_edge, pos.y, pos.z); + } + trans.position = pos; + } + } +} + diff --git a/Platformer/Assets/Scripts/Screen/ScreenViewport.cs.meta b/Platformer/Assets/Scripts/Viewport/ScreenViewport.cs.meta similarity index 100% rename from Platformer/Assets/Scripts/Screen/ScreenViewport.cs.meta rename to Platformer/Assets/Scripts/Viewport/ScreenViewport.cs.meta diff --git a/Platformer/Assets/Scripts/World/MovingPlatform.cs b/Platformer/Assets/Scripts/World/MovingPlatform.cs index 757813b..0f786fb 100644 --- a/Platformer/Assets/Scripts/World/MovingPlatform.cs +++ b/Platformer/Assets/Scripts/World/MovingPlatform.cs @@ -9,30 +9,33 @@ using UnityEngine; -public class MovingPlatform : MonoBehaviour +namespace World { - [SerializeField] private Transform targetA, targetB; - [Range(0, .1f)] [SerializeField] private float speed; - private bool switching = false; - - private void FixedUpdate() + public class MovingPlatform : MonoBehaviour { - if (!switching) - { - transform.position = Vector3.MoveTowards(transform.position, targetA.position, speed); - } - else if (switching) - { - transform.position = Vector3.MoveTowards(transform.position, targetB.position, speed); - } - if (transform.position == targetA.position) - { - switching = true; - } - else if (transform.position == targetB.position) + [SerializeField] private Transform targetA, targetB; + [Range(0, .1f)] [SerializeField] private float speed; + private bool switching = false; + + private void FixedUpdate() { - switching = false; - } - } + if (!switching) + { + transform.position = Vector3.MoveTowards(transform.position, targetA.position, speed); + } + else if (switching) + { + transform.position = Vector3.MoveTowards(transform.position, targetB.position, speed); + } + if (transform.position == targetA.position) + { + switching = true; + } + else if (transform.position == targetB.position) + { + switching = false; + } + } + } } diff --git a/Platformer/Assets/Scripts/World/MovingPlayer.cs b/Platformer/Assets/Scripts/World/MovingPlayer.cs index fcd4773..d5f2ad4 100644 --- a/Platformer/Assets/Scripts/World/MovingPlayer.cs +++ b/Platformer/Assets/Scripts/World/MovingPlayer.cs @@ -8,34 +8,37 @@ using UnityEngine; -public class MovingPlayer : MonoBehaviour +namespace World { - [SerializeField] - private Vector3 velocity; - private bool moving; - - private void OnCollisionEnter2D(Collision2D collision) + public class MovingPlayer : MonoBehaviour { - if (collision.gameObject.CompareTag("Player")) + [SerializeField] + private Vector3 velocity; + private bool moving; + + private void OnCollisionEnter2D(Collision2D collision) { - moving = true; - collision.collider.transform.SetParent(transform); + if (collision.gameObject.CompareTag("Player")) + { + moving = true; + collision.collider.transform.SetParent(transform); + } } - } - private void OnCollisionExit2D(Collision2D collision) - { - if (collision.gameObject.CompareTag("Player")) + private void OnCollisionExit2D(Collision2D collision) { - collision.collider.transform.SetParent(null); - } + if (collision.gameObject.CompareTag("Player")) + { + collision.collider.transform.SetParent(null); + } - } - void FixedUpdate() - { - if (moving) + } + void FixedUpdate() { - transform.position += (velocity * Time.deltaTime); + if (moving) + { + transform.position += (velocity * Time.deltaTime); + } } } } diff --git a/Platformer/Assets/Scripts/World/WorldGenerator.cs b/Platformer/Assets/Scripts/World/WorldGenerator.cs index f31292f..a435842 100644 --- a/Platformer/Assets/Scripts/World/WorldGenerator.cs +++ b/Platformer/Assets/Scripts/World/WorldGenerator.cs @@ -7,13 +7,16 @@ using System.Collections.Generic; +using Player; +using Viewport; using UnityEngine; using Random = UnityEngine.Random; public class WorldGenerator : MonoBehaviour { [SerializeField] private Transform chunk_start; - [SerializeField] private List chunk_list; + [SerializeField] private List chunk_list; /* Chunks are prefabs and are initialised via the Unity Inspector */ + [Range(0, 10f)] [SerializeField] private int start_chunks; [Range(0, 500f)] [SerializeField] private int chunk_visibility; diff --git a/Platformer/Logs/ApiUpdaterCheck.txt b/Platformer/Logs/ApiUpdaterCheck.txt index 4c12445..9ebd2ab 100644 --- a/Platformer/Logs/ApiUpdaterCheck.txt +++ b/Platformer/Logs/ApiUpdaterCheck.txt @@ -422,3 +422,43 @@ C# parse time : 191ms candidates check time : 46ms console write time : 0ms +[api-updater (non-obsolete-error-filter)] 05.08.2021 13:43:41 : Starting C:/Program Files/Unity/Hub/Editor/2020.3.5f1/Editor/Data/Tools/ScriptUpdater/APIUpdater.NonObsoleteApiUpdaterDetector.exe +[api-updater (non-obsolete-error-filter)] +---------------------------------- +jit/startup time : 653,3063ms +moved types parse time: 56ms +candidates parse time : 1ms +C# parse time : 326ms +candidates check time : 95ms +console write time : 0ms + +[api-updater (non-obsolete-error-filter)] 05.08.2021 13:47:23 : Starting C:/Program Files/Unity/Hub/Editor/2020.3.5f1/Editor/Data/Tools/ScriptUpdater/APIUpdater.NonObsoleteApiUpdaterDetector.exe +[api-updater (non-obsolete-error-filter)] +---------------------------------- +jit/startup time : 98,0696ms +moved types parse time: 79ms +candidates parse time : 1ms +C# parse time : 221ms +candidates check time : 98ms +console write time : 0ms + +[api-updater (non-obsolete-error-filter)] 05.08.2021 13:49:52 : Starting C:/Program Files/Unity/Hub/Editor/2020.3.5f1/Editor/Data/Tools/ScriptUpdater/APIUpdater.NonObsoleteApiUpdaterDetector.exe +[api-updater (non-obsolete-error-filter)] +---------------------------------- +jit/startup time : 95,0173ms +moved types parse time: 56ms +candidates parse time : 1ms +C# parse time : 203ms +candidates check time : 67ms +console write time : 0ms + +[api-updater (non-obsolete-error-filter)] 05.08.2021 13:50:18 : Starting C:/Program Files/Unity/Hub/Editor/2020.3.5f1/Editor/Data/Tools/ScriptUpdater/APIUpdater.NonObsoleteApiUpdaterDetector.exe +[api-updater (non-obsolete-error-filter)] +---------------------------------- +jit/startup time : 92,0855ms +moved types parse time: 56ms +candidates parse time : 1ms +C# parse time : 220ms +candidates check time : 54ms +console write time : 0ms + diff --git a/Platformer/highscore.txt b/Platformer/highscore.txt index 2c91f4d..0a799dd 100644 --- a/Platformer/highscore.txt +++ b/Platformer/highscore.txt @@ -1,9 +1,11 @@ +Felix​526 Felix​382 Felix​253 Felix​206 ​114 Felix​56 Felix​50 +​29 Felix Schneider​24 Init0 Init0