Advertisement
drakon-firestone

Skrypty

Dec 20th, 2023
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.45 KB | None | 0 0
  1. // ############################# CAMERA CONTROLLER #######################
  2.  
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6.  
  7. public class CameraController : MonoBehaviour
  8. {
  9. [SerializeField] private float mouseSensitivity = 100;
  10.  
  11. private Transform playerBody;
  12.  
  13. private float xRotation = 0;
  14.  
  15. // Start is called before the first frame update
  16. void Start()
  17. {
  18. playerBody = transform.parent;
  19. Cursor.lockState = CursorLockMode.Locked;
  20. }
  21.  
  22. // Update is called once per frame
  23. void Update()
  24. {
  25. CameraRotation();
  26. }
  27.  
  28. private void CameraRotation()
  29. {
  30. float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
  31. float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
  32.  
  33. xRotation -= mouseY;
  34. xRotation = Mathf.Clamp(xRotation, -80f, 80f);
  35.  
  36. transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
  37.  
  38. playerBody.Rotate(Vector3.up * mouseX);
  39. }
  40. }
  41.  
  42.  
  43.  
  44. // ############################ CLOCK ##########################
  45. using System.Collections;
  46. using System.Collections.Generic;
  47. using UnityEngine;
  48.  
  49. public class Clock : PickUp
  50. {
  51. [SerializeField] private bool addTime = true;
  52.  
  53. [SerializeField] private uint timeToAdd = 10;
  54.  
  55. public override void Picked()
  56. {
  57. int sign = addTime ? 1 : -1;
  58.  
  59. GameManager.Instance.AddTime( sign * (int)timeToAdd );
  60.  
  61. base.Picked();
  62. }
  63. }
  64.  
  65. // ############################ ColorToPrefab ##################
  66.  
  67. using System.Collections;
  68. using System.Collections.Generic;
  69. using UnityEngine;
  70.  
  71. [System.Serializable]
  72. public class ColorToPrefab
  73. {
  74. public Color color;
  75. public GameObject prefab;
  76. }
  77.  
  78.  
  79. // ########################### CRYSTAL #################################
  80. using System.Collections;
  81. using System.Collections.Generic;
  82. using UnityEngine;
  83.  
  84. public class Crystal : PickUp
  85. {
  86. [SerializeField] private int points = 1;
  87.  
  88.  
  89. public override void Picked()
  90. {
  91. GameManager.Instance.AddPoints(points);
  92. base.Picked();
  93. }
  94.  
  95. }
  96.  
  97. // ############################ DOOR ###########################
  98. using System.Collections;
  99. using System.Collections.Generic;
  100. using UnityEngine;
  101.  
  102. public class Door : MonoBehaviour
  103. {
  104. public Transform closePosition;
  105. public Transform openPosition;
  106. public Transform door;
  107.  
  108. public bool open = false;
  109. float speed = 5;
  110.  
  111.  
  112. // Start is called before the first frame update
  113. void Start()
  114. {
  115. door.position = closePosition.position;
  116. }
  117.  
  118.  
  119. // Update is called once per frame
  120. private void Update()
  121. {
  122. if (open && Vector3.Distance(door.position, openPosition.position) > 0.001f)
  123. {
  124. door.position = Vector3.MoveTowards(door.position, openPosition.position, Time.deltaTime * speed);
  125. }
  126. }
  127.  
  128.  
  129. public void Open()
  130. {
  131. open = true;
  132. }
  133.  
  134. }
  135.  
  136. // ################################# EditorButton #####################
  137. using System.Collections;
  138. using System.Collections.Generic;
  139. using UnityEngine;
  140. using UnityEditor;
  141.  
  142. [CustomEditor(typeof(LevelGenerator))]
  143. public class EditorButton : Editor
  144. {
  145.  
  146. public override void OnInspectorGUI()
  147. {
  148. DrawDefaultInspector();
  149.  
  150. LevelGenerator generator = (LevelGenerator) target;
  151.  
  152. if (GUILayout.Button("Create Labirynth"))
  153. {
  154. generator.GenerateLabirynth();
  155. }
  156.  
  157. }
  158.  
  159. }
  160.  
  161. // ################################## FREEZE #######################
  162. using System.Collections;
  163. using System.Collections.Generic;
  164. using UnityEngine;
  165.  
  166. public class Freeze : PickUp
  167. {
  168. [SerializeField] private int freezeTime;
  169.  
  170. public override void Picked()
  171. {
  172. GameManager.Instance.FreezeTime(freezeTime);
  173.  
  174. base.Picked();
  175. }
  176.  
  177.  
  178. }
  179.  
  180.  
  181. // ############################# GameManager ##############################
  182. using System.Collections;
  183. using System.Collections.Generic;
  184. using UnityEngine;
  185. using UnityEngine.SceneManagement;
  186.  
  187. public class GameManager : MonoBehaviour
  188. {
  189. // PUBLIC
  190. public static GameManager Instance;
  191.  
  192. public int points = 0;
  193.  
  194. // PRIVATE SERIALIZE FIELD
  195.  
  196. [SerializeField] private int timeToEnd = 100;
  197.  
  198. // PRIVATE
  199.  
  200. private bool win = false;
  201.  
  202. [SerializeField] private int greenKeys = 0;
  203. [SerializeField] private int goldKeys = 0;
  204. [SerializeField] private int redKeys = 0;
  205.  
  206. private AudioSource audioSource;
  207.  
  208. public AudioClip resumeClip;
  209. public AudioClip pauseClip;
  210. public AudioClip winClip;
  211. public AudioClip loseClip;
  212.  
  213. private void Awake()
  214. {
  215. if (Instance == null) Instance = this;
  216. }
  217.  
  218.  
  219. // Start is called before the first frame update
  220. void Start()
  221. {
  222. audioSource = GetComponent<AudioSource>();
  223.  
  224. Time.timeScale = 1f;
  225.  
  226. InvokeRepeating(nameof(Stoper), 3, 1);
  227. }
  228.  
  229. // Update is called once per frame
  230. void Update()
  231. {
  232. if (timeToEnd < 1 && Input.GetKeyDown(KeyCode.R) )
  233. {
  234. ReloadGame();
  235. }
  236. }
  237.  
  238. private void Stoper()
  239. {
  240. timeToEnd--;
  241. Debug.Log($"Time left: {timeToEnd}");
  242.  
  243. if (timeToEnd < 1)
  244. {
  245. timeToEnd = 0;
  246. EndGame();
  247. }
  248. }
  249.  
  250. public void EndGame()
  251. {
  252. CancelInvoke(nameof(Stoper));
  253.  
  254. if(win)
  255. {
  256. Debug.Log("You Win! Reload?");
  257. PlayClip(winClip);
  258. }
  259. else
  260. {
  261. Debug.Log("You Lose!!! Reload?");
  262. PlayClip(loseClip);
  263. }
  264. Time.timeScale = 0f;
  265. }
  266.  
  267. public void ReloadGame()
  268. {
  269. SceneManager.LoadScene(0);
  270. }
  271.  
  272.  
  273. public void AddPoints(int pointsToAdd = 1)
  274. {
  275. points += pointsToAdd;
  276. }
  277.  
  278. public void FreezeTime(int time)
  279. {
  280. CancelInvoke(nameof(Stoper));
  281. InvokeRepeating(nameof(Stoper), time, 1f);
  282. }
  283.  
  284. public void AddTime(int timeToAdd)
  285. {
  286. timeToEnd += timeToAdd;
  287. }
  288.  
  289. public void AddKey(KeyColor color, int amount = 1)
  290. {
  291. if (color == KeyColor.Red)
  292. {
  293. redKeys += amount;
  294. }
  295. else if (color == KeyColor.Green)
  296. {
  297. greenKeys += amount;
  298. }
  299. else if (color == KeyColor.Gold)
  300. {
  301. goldKeys += amount;
  302. }
  303.  
  304. }
  305.  
  306.  
  307. public bool TryUsingKey(KeyColor color)
  308. {
  309. if (color == KeyColor.Red && redKeys > 0)
  310. {
  311. redKeys--;
  312. return true;
  313. }
  314. else if (color == KeyColor.Green && greenKeys > 0)
  315. {
  316. greenKeys--;
  317. return true;
  318. }
  319. else if (color == KeyColor.Gold && goldKeys > 0)
  320. {
  321. goldKeys--;
  322. return true;
  323. }
  324. else
  325. {
  326. return false;
  327. }
  328. }
  329.  
  330.  
  331. public void PlayClip(AudioClip clip)
  332. {
  333. audioSource.clip = clip;
  334. audioSource.Play();
  335. }
  336. }
  337.  
  338. // ################################ Key #####################################
  339. using System.Collections;
  340. using System.Collections.Generic;
  341. using UnityEngine;
  342.  
  343. public enum KeyColor
  344. {
  345. Gold,
  346. Green,
  347. Red
  348. }
  349.  
  350. public class Key : PickUp
  351. {
  352. [SerializeField] private KeyColor color;
  353.  
  354. public override void Picked()
  355. {
  356. GameManager.Instance.AddKey(color);
  357.  
  358. base.Picked();
  359. }
  360. }
  361.  
  362.  
  363. // ############################### LevelGenerator ################################
  364. using System.Collections;
  365. using System.Collections.Generic;
  366. using UnityEngine;
  367.  
  368. public class LevelGenerator : MonoBehaviour
  369. {
  370. [SerializeField] private Texture2D map;
  371.  
  372. [SerializeField] private ColorToPrefab[] colorMappings;
  373.  
  374. [SerializeField] private float offset = 5f;
  375.  
  376.  
  377. private void GenerateTile(int x, int z)
  378. {
  379. Color pixelColor = map.GetPixel(x, z);
  380.  
  381. if (pixelColor.a == 0)
  382. return;
  383.  
  384. foreach(ColorToPrefab mapping in colorMappings)
  385. {
  386. if (mapping.color.Equals(pixelColor))
  387. {
  388. Vector3 position = new Vector3(x, 0, z) * offset;
  389. Instantiate(mapping.prefab, position, Quaternion.identity, transform);
  390. break;
  391. }
  392. }
  393.  
  394. }
  395.  
  396. public void GenerateLabirynth()
  397. {
  398. for (int x = 0; x < map.width; x++)
  399. {
  400. for (int z = 0; z < map.height; z++)
  401. {
  402. GenerateTile(x, z);
  403. }
  404. }
  405. }
  406. }
  407.  
  408.  
  409. // ######################## Lock #######################
  410. using System.Collections;
  411. using System.Collections.Generic;
  412. using UnityEngine;
  413.  
  414. public class Lock : MonoBehaviour
  415. {
  416. [SerializeField] private Door[] doors;
  417. [SerializeField] private KeyColor myColor;
  418. private bool locked = true;
  419. private Animator key;
  420.  
  421. private bool iCanOpen = false;
  422.  
  423.  
  424. // Start is called before the first frame update
  425. void Start()
  426. {
  427. key = GetComponent<Animator>();
  428. }
  429.  
  430. // Update is called once per frame
  431. void Update()
  432. {
  433. if (Input.GetKeyDown(KeyCode.E) && iCanOpen && locked)
  434. {
  435. if (GameManager.Instance.TryUsingKey(myColor))
  436. {
  437. key.SetBool("Unlock", true);
  438. }
  439. }
  440. }
  441.  
  442. public void UseKey()
  443. {
  444. foreach (Door door in doors)
  445. {
  446. door.Open();
  447. }
  448. }
  449.  
  450. private void OnTriggerEnter(Collider other)
  451. {
  452. if (other.tag == "Player")
  453. {
  454. iCanOpen = true;
  455. Debug.Log("You Can Use Lock");
  456. }
  457. }
  458. private void OnTriggerExit(Collider other)
  459. {
  460. if (other.tag == "Player")
  461. {
  462. iCanOpen = false;
  463. Debug.Log("You Can not Use Lock");
  464. }
  465. }
  466.  
  467. }
  468.  
  469.  
  470. // ############################### PickUp ###########################
  471.  
  472. using System.Collections;
  473. using System.Collections.Generic;
  474. using UnityEngine;
  475.  
  476. public class PickUp : MonoBehaviour
  477. {
  478. public AudioClip clip;
  479.  
  480. public virtual void Picked()
  481. {
  482. //Debug.Log("Pickup podniesiony");
  483. GameManager.Instance.PlayClip(clip);
  484. Destroy(gameObject);
  485. }
  486.  
  487.  
  488. }
  489.  
  490.  
  491. // ###################### PlayerController ###############################
  492. using System.Collections;
  493. using System.Collections.Generic;
  494. using UnityEngine;
  495.  
  496. public class PlayerController : MonoBehaviour
  497. {
  498. [SerializeField] private float speed = 12f;
  499. [SerializeField] private Transform groundCheck;
  500. [SerializeField] private LayerMask groundMask;
  501.  
  502. private CharacterController characterController;
  503.  
  504. // Start is called before the first frame update
  505. void Start()
  506. {
  507. characterController = GetComponent<CharacterController>();
  508. }
  509.  
  510. // Update is called once per frame
  511. void Update()
  512. {
  513. PlayerMove();
  514. }
  515.  
  516. private void PlayerMove()
  517. {
  518. float x = Input.GetAxis("Horizontal");
  519. float z = Input.GetAxis("Vertical");
  520.  
  521. Vector3 move = transform.right * x + transform.forward * z;
  522.  
  523. ModifySpeed();
  524. characterController.Move(move * speed * Time.deltaTime);
  525. }
  526.  
  527. private void ModifySpeed()
  528. {
  529. speed = 12;
  530.  
  531. RaycastHit hit;
  532.  
  533. if(Physics.Raycast(
  534. groundCheck.position,
  535. Vector3.down,
  536. out hit,
  537. 0.4f,
  538. groundMask // lista warstwa traktowanych jako podłoga
  539. ))
  540. {
  541.  
  542. string tag = hit.collider.gameObject.tag;
  543.  
  544. if (tag == "High")
  545. {
  546. speed = 24f;
  547. }
  548. else if (tag == "Low")
  549. {
  550. speed = 3f;
  551. }
  552. else
  553. {
  554. speed = 12f;
  555. }
  556. }
  557. }
  558.  
  559.  
  560. private void OnControllerColliderHit(ControllerColliderHit hit)
  561. {
  562. // if(hit.gameObject.CompareTag("PickUp"))
  563. if (hit.gameObject.tag == "PickUp")
  564. {
  565. hit.gameObject.GetComponent<PickUp>().Picked();
  566. }
  567. }
  568.  
  569.  
  570.  
  571. }
  572.  
  573. // ############################### PlayerController ##############################
  574. using System.Collections;
  575. using System.Collections.Generic;
  576. using UnityEngine;
  577.  
  578. public class Portal : MonoBehaviour
  579. {
  580. [SerializeField] Portal otherPortal;
  581. [SerializeField] Material material;
  582.  
  583. public Camera myCamera;
  584.  
  585. public Transform renderSurface;
  586. public Transform portalCollider;
  587.  
  588. private GameObject player;
  589. private PortalTeleport portalTeleport;
  590. private PortalCamera portalCamera;
  591.  
  592. private void Awake()
  593. {
  594. player = GameObject.FindGameObjectWithTag("Player");
  595.  
  596. portalTeleport = portalCollider.GetComponent<PortalTeleport>();
  597. portalTeleport.player = player.transform;
  598. portalTeleport.receiver = otherPortal.portalCollider;
  599.  
  600. portalCamera = myCamera.GetComponent<PortalCamera>();
  601. portalCamera.playerCamera = player.GetComponentInChildren<Camera>().transform;
  602. portalCamera.portal = transform;
  603. portalCamera.otherPortal = otherPortal.transform;
  604.  
  605. renderSurface.GetComponent<MeshRenderer>().material = Instantiate(material);
  606. if(myCamera.targetTexture != null)
  607. {
  608. myCamera.targetTexture.Release();
  609. }
  610.  
  611. myCamera.targetTexture = new RenderTexture(Screen.width, Screen.height, 24);
  612.  
  613.  
  614. }
  615.  
  616. private void Start()
  617. {
  618. renderSurface.GetComponent<MeshRenderer>().material.mainTexture =
  619. otherPortal.myCamera.targetTexture;
  620. }
  621. }
  622.  
  623. // ############################### PortalCamera ##############################
  624.  
  625. using System.Collections;
  626. using System.Collections.Generic;
  627. using UnityEngine;
  628.  
  629. public class PortalCamera : MonoBehaviour
  630. {
  631. public Transform playerCamera;
  632. public Transform portal;
  633. public Transform otherPortal;
  634.  
  635. void Update()
  636. {
  637. Vector3 playerOffsetFromPortal = playerCamera.position - otherPortal.position;
  638. transform.position = portal.position + playerOffsetFromPortal;
  639.  
  640. //transform.forward = playerCamera.forward;
  641. transform.rotation = playerCamera.rotation;
  642. }
  643.  
  644. }
  645.  
  646.  
  647. // #################### PoratalTeleport ######################
  648.  
  649. using System.Collections;
  650. using System.Collections.Generic;
  651. using UnityEngine;
  652.  
  653. public class PortalTeleport : MonoBehaviour
  654. {
  655.  
  656. public Transform player;
  657. public Transform receiver;
  658.  
  659. private bool playerIsOverlapping = false;
  660.  
  661. private void OnTriggerEnter(Collider other)
  662. {
  663. if (other.tag == "Player")
  664. {
  665. playerIsOverlapping = true;
  666. Debug.Log("Player entered portal");
  667. }
  668. }
  669.  
  670. private void OnTriggerExit(Collider other)
  671. {
  672. if (other.tag == "Player")
  673. {
  674. playerIsOverlapping = false;
  675. Debug.Log("Player left portal");
  676. }
  677. }
  678.  
  679. private void FixedUpdate()
  680. {
  681. Teleportation();
  682. }
  683.  
  684. private void Teleportation()
  685. {
  686. if (!playerIsOverlapping) return;
  687.  
  688. Vector3 portalToPlayer = player.position - transform.position;
  689.  
  690. float dotProduct = Vector3.Dot(transform.up, portalToPlayer);
  691.  
  692. if (dotProduct < 0f)
  693. {
  694. player.position = receiver.position + portalToPlayer;
  695.  
  696. playerIsOverlapping = false;
  697. }
  698.  
  699. }
  700.  
  701.  
  702. }
  703.  
  704.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement