Advertisement
Guest User

Untitled

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