Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.09 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityStandardAssets.Characters.FirstPerson;
  6.  
  7. public class CharController : MonoBehaviour
  8. {
  9.     public RectTransform[] craftTable;
  10.     public Sprite[] sprites;
  11.     public GameObject slotPrefab;
  12.     public RectTransform content;
  13.     FirstPersonController fpsc;
  14.     int selectedBlock = 0;
  15.     int[] resources;
  16.     public Transform marker;
  17.     public LandscapeGenerator lg;
  18.     public Text hint;
  19.     public RectTransform qp;
  20.     public RectTransform ip;
  21.     public RectTransform selector;
  22.     Camera cam;
  23.     RaycastHit hit = new RaycastHit();
  24.     public Transform weaponContainer;
  25.     void Start()
  26.     {
  27.         fpsc = GetComponent<FirstPersonController>();
  28.         cam = GetComponentInChildren<Camera>();
  29.         resources = new int[5];
  30.     }
  31.     void Update()
  32.     {
  33.         if (Input.GetKeyDown(KeyCode.Tab))
  34.         {
  35.             bool target = !ip.gameObject.activeInHierarchy;
  36.             ip.gameObject.SetActive(target);
  37.             fpsc.enabled = !target;
  38.             Cursor.visible = target;
  39.  
  40.             Time.timeScale = target ? 0 : 1; // тернарная операция
  41.             Cursor.lockState = target ? CursorLockMode.None : CursorLockMode.Locked;
  42.             hint.enabled = !target;
  43.  
  44.             if (target)
  45.             {
  46.                 for (int i = 0; i < resources.Length; i++) // Тип ресурса
  47.                 {
  48.                     for (int j = 0; j < resources[i]; j++) // количество i-ресурса
  49.                     {
  50.                         GameObject s = GameObject.Instantiate(
  51.                             slotPrefab,
  52.                             content
  53.                             );
  54.                         s.transform.GetChild(0)
  55.                             .GetComponent<Image>()
  56.                             .sprite = sprites[i];
  57.                     }
  58.                 }
  59.             }
  60.             else
  61.             {
  62.                 foreach(Transform t in content)
  63.                 {
  64.                     Destroy(t.gameObject);
  65.                 }
  66.                 /*
  67.                 for (int i = 0; i < content.childCount; i++)
  68.                 {
  69.                     Destroy(content.GetChild(i));
  70.                 }
  71.                 */
  72.                 ClearTable();
  73.             }
  74.  
  75.             int n = content.childCount;
  76.             int columns = (int)((content.rect.size.x - 4) / 68);
  77.             int rows = (n / columns) + 1;
  78.  
  79.             content.sizeDelta = new Vector2(
  80.                 content.sizeDelta.x,
  81.                 rows * 64 + (rows + 1) * 4
  82.             );
  83.  
  84.            
  85.         }
  86.         if (ip.gameObject.activeInHierarchy)
  87.             return;
  88.  
  89.         // transform.forward, right, up
  90.         Physics.Raycast(
  91.             cam.transform.position, // коорд начала луча
  92.             cam.transform.forward, // вектор направления луча
  93.             out hit, //
  94.             5.0f // расстояние взаимодействия
  95.             );
  96.         if (hit.collider != null)
  97.         {
  98.             if (hit.collider.gameObject.
  99.                 GetComponent<Weapon>() != null)
  100.             {
  101.                 hint.text = hit.collider.gameObject.
  102.                     GetComponent<Weapon>().Name;
  103.             }
  104.             else
  105.             {
  106.                 hint.text = hit.collider.gameObject.name;
  107.                 hint.text += "\n" +
  108.                     hit.collider.gameObject.GetComponent<HP>()?.GetHealth();
  109.             }
  110.             if (hit.collider.tag == "Block")
  111.             {
  112.                 marker.GetComponent<MeshRenderer>().enabled = true;
  113.                 marker.position = hit.collider.transform.position
  114.                     + hit.normal;
  115.             }
  116.             else
  117.             {
  118.                 marker.GetComponent<MeshRenderer>().enabled = false;
  119.             }
  120.  
  121.         }
  122.         else
  123.         {
  124.             hint.text = "";
  125.             marker.GetComponent<MeshRenderer>().enabled = false;
  126.         }
  127.  
  128.         if (Input.GetKeyDown(KeyCode.E))
  129.         {
  130.            
  131.             // object -> bool => true
  132.             // null   -> bool => false
  133.             if (hit.collider && hit.collider.gameObject.GetComponent<Weapon>())
  134.             {
  135.                 DropWeapon();
  136.  
  137.                 hit.collider.transform.SetParent(weaponContainer);
  138.                 weaponContainer.GetChild(0).localPosition = Vector3.zero;
  139.                 weaponContainer.GetChild(0).localRotation = Quaternion.identity;
  140.                 weaponContainer.GetChild(0).GetComponent<Animator>().enabled = true;
  141.                 weaponContainer.GetChild(0).GetComponent<Rigidbody>().isKinematic = true;
  142.             }
  143.         }
  144.  
  145.         if (Input.GetMouseButtonDown(0) && weaponContainer.childCount > 0)
  146.         {
  147.             weaponContainer.GetChild(0)
  148.                 .GetComponent<Animator>()
  149.                 .SetTrigger("attack");
  150.             if (hit.collider != null
  151.                 && hit.collider.gameObject.GetComponent<HP>() != null)
  152.             {
  153.                 hit.collider.gameObject.GetComponent<HP>().GetDamage(
  154.                     weaponContainer.GetChild(0)
  155.                     .GetComponent<Weapon>().Damage);
  156.             }
  157.         }
  158.         if (Input.GetKeyDown(KeyCode.G))
  159.         {
  160.             DropWeapon();
  161.         }
  162.         if (Input.GetMouseButtonDown(1))
  163.         {
  164.             //build block
  165.             GameObject.Instantiate(
  166.                 lg.prefabs[selectedBlock],
  167.                 marker.position,
  168.                 marker.rotation
  169.                 );
  170.         }
  171.         if (Input.GetKeyDown(KeyCode.Alpha1)) selectedBlock = 0;
  172.         if (Input.GetKeyDown(KeyCode.Alpha2)) selectedBlock = 1;
  173.         if (Input.GetKeyDown(KeyCode.Alpha3)) selectedBlock = 2;
  174.         if (Input.GetKeyDown(KeyCode.Alpha4)) selectedBlock = 3;
  175.         if (Input.GetKeyDown(KeyCode.Alpha5)) selectedBlock = 4;
  176.         if (Input.GetAxis("Mouse ScrollWheel") != 0)
  177.         {
  178.             if (Input.GetAxis("Mouse ScrollWheel") > 0) selectedBlock++;
  179.             if (Input.GetAxis("Mouse ScrollWheel") < 0) selectedBlock--;
  180.  
  181.             int k = lg.prefabs.Length - 1;
  182.             selectedBlock = (k + selectedBlock) % k;
  183.         }
  184.  
  185.         selector.SetParent(qp.GetChild(6 + selectedBlock));
  186.         selector.localPosition = Vector3.zero;
  187.     }
  188.     void DropWeapon()
  189.     {
  190.         if (weaponContainer.childCount == 0)
  191.             return;
  192.  
  193.         weaponContainer.GetChild(0)
  194.             .GetComponent<Rigidbody>().isKinematic = false;
  195.         weaponContainer.GetChild(0).GetComponent<Animator>().enabled = false;
  196.         weaponContainer.GetChild(0).SetParent(null);
  197.     }
  198.     public void ChangeMats(int id, int value)
  199.     {
  200.         resources[id] += value;
  201.         qp.GetChild(id)
  202.             .GetComponentInChildren<Text>()
  203.             .text = resources[id].ToString();
  204.     }
  205.     public void ClearTable()
  206.     {
  207.         foreach (RectTransform slot in craftTable)
  208.         {
  209.             if (slot.childCount > 0)
  210.                 Destroy(slot.GetChild(0).gameObject);
  211.         }
  212.     }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement