Advertisement
noradninja

Enum example

Mar 26th, 2023
857
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.46 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Runtime.Serialization;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using TMPro;
  7.  
  8. public class Item_Enumerator : MonoBehaviour {
  9.  
  10. //enums
  11.     public enum pickupItem{
  12.  
  13.     //data members
  14.         flashlight,
  15.         battery,
  16.         key,
  17.         health,
  18.         stims
  19.  
  20.     }
  21.  
  22. //public vars
  23.     public int identifier;
  24.     public bool isActiveObject = false;
  25.     public pickupItem thisItem;
  26.     public int itemValue;
  27.     //public pickupText itemText;
  28.     public Collider player;
  29.     public Animator animator;
  30.     public GameObject playerObject;
  31.     public Material playerBody;
  32.     public Collider thisCollider;
  33.     public GameObject targetObject;
  34.     public Camera mainCamera;
  35.     public GameObject pickupTextObject;
  36.     public GameObject hilightIcon;
  37.     public RawImage iconImage;
  38.     public RawImage dialogBG;
  39.     public Color iconVisible = new Vector4(1,1,1,0.5f);
  40.     public Color colorOff = new Vector4(0,0,0,0);
  41.     public Color colorOn = new Vector4(1,1,1,0.75f);
  42.     public Color dialogOn = new Vector4(0,0,0,0.5f);
  43.     public TextMeshProUGUI OSDText;
  44.     public TextMeshProUGUI batteryText;
  45.     public Texture healedTexture;
  46.    
  47.     //private vars
  48.     private const string joystick1 = "joystick button ";
  49.     private int CROSS = 0;
  50.     private Vector3 screenPosition;
  51.     private Vector3 targetPos;
  52.     private string tempText;
  53.     private Dictionary<string, string> itemTexts;
  54.  
  55.     private int GetFirstDigit(int number)
  56.     {
  57.         while (true)
  58.         {
  59.             if (number < 10) return number;
  60.             number = (number - (number % 10)) / 10;
  61.         }
  62.     }
  63.  
  64.     // Use this for initialization
  65.     private void Start ()
  66.     {
  67.        
  68.         if (Application.isEditor){
  69.             CROSS = 2;
  70.         }
  71.         Physics.IgnoreCollision(thisCollider, player.GetComponent<Collider>(), true); //so the player doesn't step 'up' when walking near
  72.     ////DICTIONARY//////////////////////////////////////////////////////////////////
  73.         itemTexts = new Dictionary<string, string>(); //string itemType, string dialogText
  74.         //TODO: change this so we are parsing a CSV file for the dictionary entries to make changes much easier
  75.         itemTexts.Add("flashlight", "flashlight");
  76.         itemTexts.Add("battery", "batteries");
  77.         itemTexts.Add("health", "antiseptic");
  78.         itemTexts.Add("stims", "stimulants");  
  79.     ///////////////////////////////////////////////////////////////////////////////
  80.     batteryText.text =InventoryManager.batteryCount.ToString();
  81.  
  82.     ////Check lists to see if we are in it; if not, destroy
  83.         // Meds
  84.         if (GetFirstDigit(identifier) == 1 && SetScenes.MedsRemaining != null){
  85.             if(!SetScenes.MedsRemaining.Contains(identifier)){
  86.                 Destroy(targetObject);
  87.             }
  88.         }
  89.         //Batteries
  90.         if (GetFirstDigit(identifier) == 2 && SetScenes.BatteryRemaining != null){
  91.             if(!SetScenes.BatteryRemaining.Contains(identifier)){
  92.                 Destroy(targetObject);
  93.             }
  94.         }
  95.         //Light Weapons
  96.         if (GetFirstDigit(identifier) == 3 && SetScenes.LightWepRemaining != null){
  97.             if(!SetScenes.LightWepRemaining.Contains(identifier)){
  98.                 Destroy(targetObject);
  99.             }
  100.         }
  101.         //Heavy Weapons
  102.         if (GetFirstDigit(identifier) == 4 && SetScenes.HvyWepRemaining != null){
  103.             if(!SetScenes.HvyWepRemaining.Contains(identifier)){
  104.                 Destroy(targetObject);
  105.             }
  106.         }  
  107.  
  108.     }
  109.  
  110.     // Update is called once per frame
  111.     void Update (){
  112.         if (Input.GetButtonDown("Cross") && dialogBG.color == dialogOn && isActiveObject){
  113.        
  114.             switch (thisItem){
  115.  
  116.                 case pickupItem.flashlight :
  117.                     PlayerController.HasFlashlight = true;
  118.                     PlayerController.FlashlightOff = false;
  119.                     Destroy(targetObject);
  120.                 break;
  121.  
  122.                 case pickupItem.battery :
  123.                     // print("That's a battery");
  124.                     InventoryManager.batteryCount += 2;
  125.                     string currentCount =InventoryManager.batteryCount.ToString();
  126.                     batteryText.text = currentCount;
  127.                     Destroy(targetObject);
  128.                 break;
  129.  
  130.                 case pickupItem.key :
  131.                     // print("That's a key");
  132.                     //do key shit here
  133.                     InventoryManager.keyCount += 1;
  134.                     Destroy(targetObject);             
  135.                 break;
  136.  
  137.                 case pickupItem.health :
  138.                     // print("That's a medkit");
  139.                     InventoryManager.medCount += 1;
  140.                     Destroy(targetObject);
  141.                 break;
  142.  
  143.                 case pickupItem.stims :
  144.                     // print("That's a stimulant");
  145.                     InventoryManager.stimCount += 1;
  146.                     Destroy(targetObject);
  147.                 break;
  148.             }
  149.             ClearOsd();
  150.         }
  151.        
  152.     }
  153.  
  154.     private void OnGUI (){
  155.         if (isActiveObject){
  156.             SetPosition();
  157.         }
  158.     }
  159.  
  160.     private void OnTriggerEnter (Collider col)
  161.     {
  162.         if (col != player) return;
  163.         SetPosition();
  164.         isActiveObject = true;
  165.         iconImage.color = iconVisible;
  166.         OSDText.color = colorOn;
  167.         dialogBG.color = dialogOn;
  168.         itemTexts.TryGetValue(thisItem.ToString(), out tempText);
  169.         OSDText.text = ("Press X to pick up " +  tempText + ".");
  170.     }
  171.  
  172.     private void OnTriggerExit (Collider col){
  173.         if(col == player){
  174.             ClearOsd();
  175.         }
  176.     }
  177.  
  178.     private void ClearOsd() //restores OSD to default and resets isActiveObject flag
  179.     {
  180.         iconImage.color = colorOff;
  181.         OSDText.color = colorOff;
  182.         dialogBG.color = colorOff;
  183.         OSDText.text = ("");
  184.         isActiveObject = false;
  185.     }
  186.  
  187.     private void SetPosition()
  188.     {
  189.         targetPos = targetObject.transform.position; //get target's position in the world
  190.         screenPosition = mainCamera.WorldToScreenPoint(targetPos); //convert target position to screen space coordinates
  191.         hilightIcon.transform.position = screenPosition; //set the icon position to the converted screen position of the object
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement