Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ############################# CAMERA CONTROLLER #######################
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CameraController : MonoBehaviour
- {
- [SerializeField] private float mouseSensitivity = 100;
- private Transform playerBody;
- private float xRotation = 0;
- // Start is called before the first frame update
- void Start()
- {
- playerBody = transform.parent;
- Cursor.lockState = CursorLockMode.Locked;
- }
- // Update is called once per frame
- void Update()
- {
- CameraRotation();
- }
- private void CameraRotation()
- {
- float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
- float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
- xRotation -= mouseY;
- xRotation = Mathf.Clamp(xRotation, -80f, 80f);
- transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
- playerBody.Rotate(Vector3.up * mouseX);
- }
- }
- // ############################ CLOCK ##########################
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Clock : PickUp
- {
- [SerializeField] private bool addTime = true;
- [SerializeField] private uint timeToAdd = 10;
- public override void Picked()
- {
- int sign = addTime ? 1 : -1;
- GameManager.Instance.AddTime( sign * (int)timeToAdd );
- base.Picked();
- }
- }
- // ############################ ColorToPrefab ##################
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [System.Serializable]
- public class ColorToPrefab
- {
- public Color color;
- public GameObject prefab;
- }
- // ########################### CRYSTAL #################################
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Crystal : PickUp
- {
- [SerializeField] private int points = 1;
- public override void Picked()
- {
- GameManager.Instance.AddPoints(points);
- base.Picked();
- }
- }
- // ############################ DOOR ###########################
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Door : MonoBehaviour
- {
- public Transform closePosition;
- public Transform openPosition;
- public Transform door;
- public bool open = false;
- float speed = 5;
- // Start is called before the first frame update
- void Start()
- {
- door.position = closePosition.position;
- }
- // Update is called once per frame
- private void Update()
- {
- if (open && Vector3.Distance(door.position, openPosition.position) > 0.001f)
- {
- door.position = Vector3.MoveTowards(door.position, openPosition.position, Time.deltaTime * speed);
- }
- }
- public void Open()
- {
- open = true;
- }
- }
- // ################################# EditorButton #####################
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- [CustomEditor(typeof(LevelGenerator))]
- public class EditorButton : Editor
- {
- public override void OnInspectorGUI()
- {
- DrawDefaultInspector();
- LevelGenerator generator = (LevelGenerator) target;
- if (GUILayout.Button("Create Labirynth"))
- {
- generator.GenerateLabirynth();
- }
- }
- }
- // ################################## FREEZE #######################
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Freeze : PickUp
- {
- [SerializeField] private int freezeTime;
- public override void Picked()
- {
- GameManager.Instance.FreezeTime(freezeTime);
- base.Picked();
- }
- }
- // ############################# GameManager ##############################
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class GameManager : MonoBehaviour
- {
- // PUBLIC
- public static GameManager Instance;
- public int points = 0;
- // PRIVATE SERIALIZE FIELD
- [SerializeField] private int timeToEnd = 100;
- // PRIVATE
- private bool win = false;
- [SerializeField] private int greenKeys = 0;
- [SerializeField] private int goldKeys = 0;
- [SerializeField] private int redKeys = 0;
- private AudioSource audioSource;
- public AudioClip resumeClip;
- public AudioClip pauseClip;
- public AudioClip winClip;
- public AudioClip loseClip;
- private void Awake()
- {
- if (Instance == null) Instance = this;
- }
- // Start is called before the first frame update
- void Start()
- {
- audioSource = GetComponent<AudioSource>();
- Time.timeScale = 1f;
- InvokeRepeating(nameof(Stoper), 3, 1);
- }
- // Update is called once per frame
- void Update()
- {
- if (timeToEnd < 1 && Input.GetKeyDown(KeyCode.R) )
- {
- ReloadGame();
- }
- }
- private void Stoper()
- {
- timeToEnd--;
- Debug.Log($"Time left: {timeToEnd}");
- if (timeToEnd < 1)
- {
- timeToEnd = 0;
- EndGame();
- }
- }
- public void EndGame()
- {
- CancelInvoke(nameof(Stoper));
- if(win)
- {
- Debug.Log("You Win! Reload?");
- PlayClip(winClip);
- }
- else
- {
- Debug.Log("You Lose!!! Reload?");
- PlayClip(loseClip);
- }
- Time.timeScale = 0f;
- }
- public void ReloadGame()
- {
- SceneManager.LoadScene(0);
- }
- public void AddPoints(int pointsToAdd = 1)
- {
- points += pointsToAdd;
- }
- public void FreezeTime(int time)
- {
- CancelInvoke(nameof(Stoper));
- InvokeRepeating(nameof(Stoper), time, 1f);
- }
- public void AddTime(int timeToAdd)
- {
- timeToEnd += timeToAdd;
- }
- public void AddKey(KeyColor color, int amount = 1)
- {
- if (color == KeyColor.Red)
- {
- redKeys += amount;
- }
- else if (color == KeyColor.Green)
- {
- greenKeys += amount;
- }
- else if (color == KeyColor.Gold)
- {
- goldKeys += amount;
- }
- }
- public bool TryUsingKey(KeyColor color)
- {
- if (color == KeyColor.Red && redKeys > 0)
- {
- redKeys--;
- return true;
- }
- else if (color == KeyColor.Green && greenKeys > 0)
- {
- greenKeys--;
- return true;
- }
- else if (color == KeyColor.Gold && goldKeys > 0)
- {
- goldKeys--;
- return true;
- }
- else
- {
- return false;
- }
- }
- public void PlayClip(AudioClip clip)
- {
- audioSource.clip = clip;
- audioSource.Play();
- }
- }
- // ################################ Key #####################################
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public enum KeyColor
- {
- Gold,
- Green,
- Red
- }
- public class Key : PickUp
- {
- [SerializeField] private KeyColor color;
- public override void Picked()
- {
- GameManager.Instance.AddKey(color);
- base.Picked();
- }
- }
- // ############################### LevelGenerator ################################
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class LevelGenerator : MonoBehaviour
- {
- [SerializeField] private Texture2D map;
- [SerializeField] private ColorToPrefab[] colorMappings;
- [SerializeField] private float offset = 5f;
- private void GenerateTile(int x, int z)
- {
- Color pixelColor = map.GetPixel(x, z);
- if (pixelColor.a == 0)
- return;
- foreach(ColorToPrefab mapping in colorMappings)
- {
- if (mapping.color.Equals(pixelColor))
- {
- Vector3 position = new Vector3(x, 0, z) * offset;
- Instantiate(mapping.prefab, position, Quaternion.identity, transform);
- break;
- }
- }
- }
- public void GenerateLabirynth()
- {
- for (int x = 0; x < map.width; x++)
- {
- for (int z = 0; z < map.height; z++)
- {
- GenerateTile(x, z);
- }
- }
- }
- }
- // ######################## Lock #######################
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Lock : MonoBehaviour
- {
- [SerializeField] private Door[] doors;
- [SerializeField] private KeyColor myColor;
- private bool locked = true;
- private Animator key;
- private bool iCanOpen = false;
- // Start is called before the first frame update
- void Start()
- {
- key = GetComponent<Animator>();
- }
- // Update is called once per frame
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.E) && iCanOpen && locked)
- {
- if (GameManager.Instance.TryUsingKey(myColor))
- {
- key.SetBool("Unlock", true);
- }
- }
- }
- public void UseKey()
- {
- foreach (Door door in doors)
- {
- door.Open();
- }
- }
- private void OnTriggerEnter(Collider other)
- {
- if (other.tag == "Player")
- {
- iCanOpen = true;
- Debug.Log("You Can Use Lock");
- }
- }
- private void OnTriggerExit(Collider other)
- {
- if (other.tag == "Player")
- {
- iCanOpen = false;
- Debug.Log("You Can not Use Lock");
- }
- }
- }
- // ############################### PickUp ###########################
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PickUp : MonoBehaviour
- {
- public AudioClip clip;
- public virtual void Picked()
- {
- //Debug.Log("Pickup podniesiony");
- GameManager.Instance.PlayClip(clip);
- Destroy(gameObject);
- }
- }
- // ###################### PlayerController ###############################
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- [SerializeField] private float speed = 12f;
- [SerializeField] private Transform groundCheck;
- [SerializeField] private LayerMask groundMask;
- private CharacterController characterController;
- // Start is called before the first frame update
- void Start()
- {
- characterController = GetComponent<CharacterController>();
- }
- // Update is called once per frame
- void Update()
- {
- PlayerMove();
- }
- private void PlayerMove()
- {
- float x = Input.GetAxis("Horizontal");
- float z = Input.GetAxis("Vertical");
- Vector3 move = transform.right * x + transform.forward * z;
- ModifySpeed();
- characterController.Move(move * speed * Time.deltaTime);
- }
- private void ModifySpeed()
- {
- speed = 12;
- RaycastHit hit;
- if(Physics.Raycast(
- groundCheck.position,
- Vector3.down,
- out hit,
- 0.4f,
- groundMask // lista warstwa traktowanych jako podłoga
- ))
- {
- string tag = hit.collider.gameObject.tag;
- if (tag == "High")
- {
- speed = 24f;
- }
- else if (tag == "Low")
- {
- speed = 3f;
- }
- else
- {
- speed = 12f;
- }
- }
- }
- private void OnControllerColliderHit(ControllerColliderHit hit)
- {
- // if(hit.gameObject.CompareTag("PickUp"))
- if (hit.gameObject.tag == "PickUp")
- {
- hit.gameObject.GetComponent<PickUp>().Picked();
- }
- }
- }
- // ############################### PlayerController ##############################
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Portal : MonoBehaviour
- {
- [SerializeField] Portal otherPortal;
- [SerializeField] Material material;
- public Camera myCamera;
- public Transform renderSurface;
- public Transform portalCollider;
- private GameObject player;
- private PortalTeleport portalTeleport;
- private PortalCamera portalCamera;
- private void Awake()
- {
- player = GameObject.FindGameObjectWithTag("Player");
- portalTeleport = portalCollider.GetComponent<PortalTeleport>();
- portalTeleport.player = player.transform;
- portalTeleport.receiver = otherPortal.portalCollider;
- portalCamera = myCamera.GetComponent<PortalCamera>();
- portalCamera.playerCamera = player.GetComponentInChildren<Camera>().transform;
- portalCamera.portal = transform;
- portalCamera.otherPortal = otherPortal.transform;
- renderSurface.GetComponent<MeshRenderer>().material = Instantiate(material);
- if(myCamera.targetTexture != null)
- {
- myCamera.targetTexture.Release();
- }
- myCamera.targetTexture = new RenderTexture(Screen.width, Screen.height, 24);
- }
- private void Start()
- {
- renderSurface.GetComponent<MeshRenderer>().material.mainTexture =
- otherPortal.myCamera.targetTexture;
- }
- }
- // ############################### PortalCamera ##############################
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PortalCamera : MonoBehaviour
- {
- public Transform playerCamera;
- public Transform portal;
- public Transform otherPortal;
- void Update()
- {
- Vector3 playerOffsetFromPortal = playerCamera.position - otherPortal.position;
- transform.position = portal.position + playerOffsetFromPortal;
- //transform.forward = playerCamera.forward;
- transform.rotation = playerCamera.rotation;
- }
- }
- // #################### PoratalTeleport ######################
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PortalTeleport : MonoBehaviour
- {
- public Transform player;
- public Transform receiver;
- private bool playerIsOverlapping = false;
- private void OnTriggerEnter(Collider other)
- {
- if (other.tag == "Player")
- {
- playerIsOverlapping = true;
- Debug.Log("Player entered portal");
- }
- }
- private void OnTriggerExit(Collider other)
- {
- if (other.tag == "Player")
- {
- playerIsOverlapping = false;
- Debug.Log("Player left portal");
- }
- }
- private void FixedUpdate()
- {
- Teleportation();
- }
- private void Teleportation()
- {
- if (!playerIsOverlapping) return;
- Vector3 portalToPlayer = player.position - transform.position;
- float dotProduct = Vector3.Dot(transform.up, portalToPlayer);
- if (dotProduct < 0f)
- {
- player.position = receiver.position + portalToPlayer;
- playerIsOverlapping = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement