Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.54 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.Networking;
  4. using System.Collections.Generic;
  5.  
  6. public class Player_Inventory : NetworkBehaviour {
  7.  
  8.     public Player Ply;
  9.     public Item[] Items;
  10.     public Item[] StartItems;
  11.  
  12.     [SerializeField]
  13.     public List<Item_Slot> Inventory;
  14.  
  15.     public GameObject _CharakterManagerGo;
  16.  
  17.  
  18.     void Start () {
  19.         getPrefabs();
  20.         Ply = GetComponent<Player>();
  21.         Inventory = new List<Item_Slot>();
  22.  
  23.         for( int i = 0; i < StartItems.Length; i++ ) {
  24.             CmdSetItem( StartItems[i].Item_Name, 1 );
  25.         }
  26.  
  27.     }
  28.    
  29.     void getPrefabs() {
  30.         Object[] Objs1 = Resources.LoadAll( "Prefabs/Props" );
  31.         Items = new Item[Objs1.Length];
  32.         int index = 0;
  33.         for( int i = 0; i < Objs1.Length; i++ ) {
  34.             GameObject ItemGo = Objs1[i] as GameObject;
  35.             if( ItemGo.GetComponent<Item>() ) {
  36.                 Items[index] = ItemGo.GetComponent<Item>();
  37.                 index++;
  38.             }
  39.            
  40.         }
  41.  
  42.     }
  43.  
  44.     public void TrimInventory() {
  45.         List<Item_Slot> CleanInventory = new List<Item_Slot>();
  46.         for( int i = 0; i < Inventory.Count; i++ ) {
  47.             if( Inventory[i] != null ) {
  48.                 if( Inventory[i].ItemAmount > 0 ) {
  49.                     CleanInventory.Add( Inventory[i] );
  50.                 }
  51.             }
  52.         }
  53.         Inventory = CleanInventory;
  54.     }
  55.  
  56.     public Item GetItemByName( string _ItemName ) {
  57.         for( int i = 0; i < Items.Length; i++ ) {
  58.             if( Items[i] ) {
  59.                 if( Items[i].Item_Name.Trim().ToLower() == _ItemName.Trim().ToLower() ) {
  60.                     return Items[i];
  61.                 }
  62.             }
  63.         }
  64.         return null;
  65.     }
  66.  
  67.     [Command]
  68.     public void CmdSetItem( string addItemName, int amount ) {
  69.         RpcSetItem( addItemName, amount );
  70.     }
  71.     [ClientRpc]
  72.     public void RpcSetItem( string addItemName, int amount ) {
  73.         SetItem( addItemName, amount );
  74.     }
  75.     //Both
  76.     public void SetItem( string addItemName, int amount ) {
  77.         Item NewItem = GetItemByName( addItemName );
  78.         if( !NewItem ) {
  79.             return;
  80.         }
  81.         if( Inventory.Count > 0 ) {
  82.             for( int i = 0; i < Inventory.Count; i++ ) {
  83.                 if( Inventory[i].Item ) {
  84.                     if( NewItem.Item_Name == Inventory[i].Item.Item_Name ) {
  85.                         Inventory[i].ItemAmount = amount;
  86.                         if( Inventory[i].ItemAmount <= 0 ) {
  87.                             Inventory[i] = null;
  88.                         }
  89.                         TrimInventory();
  90.                         return;
  91.                     }
  92.                 }
  93.             }            
  94.         }
  95.         Inventory.Add( new Item_Slot( NewItem, amount ) );
  96.         TrimInventory();
  97.         if( !_CharakterManagerGo ) {
  98.             _CharakterManagerGo = GameObject.FindGameObjectWithTag( "CharakterManager" );
  99.         }
  100.         if( _CharakterManagerGo ) {
  101.             _CharakterManagerGo.GetComponent<CharakterManager>().SavePlayer( gameObject );
  102.         }
  103.  
  104.         if( isLocalPlayer ) {
  105.             if( Ply ) {
  106.                 if( Ply.Hud ) {
  107.                     if( Ply.Hud ) {
  108.                         if( Ply.Hud.GetComponentInChildren<AddItemPopup>() ) {
  109.                             Ply.Hud.GetComponentInChildren<AddItemPopup>().CreatePopup( NewItem, " +" + amount );
  110.                         }
  111.                     }
  112.                 }
  113.             }
  114.         }
  115.     }
  116.  
  117.  
  118.     [Command]
  119.     public void CmdEditItem( string addItemName, int amount ) {
  120.         RpcEditItem( addItemName, amount );
  121.     }
  122.     [ClientRpc]
  123.     public void RpcEditItem( string addItemName, int amount ) {
  124.         EditItem( addItemName, amount );
  125.     }
  126.     //Both
  127.     public void EditItem( string addItemName, int amount ) {
  128.         Item NewItem = GetItemByName( addItemName );
  129.         if( !NewItem ) {
  130.             return;
  131.         }
  132.         bool has = false;
  133.         if( Inventory.Count > 0 ) {
  134.             for( int i = 0; i < Inventory.Count; i++ ) {
  135.                 if( Inventory[i].Item ) {
  136.                     if( NewItem.Item_Name == Inventory[i].Item.Item_Name ) {
  137.                         has = true;
  138.                         Inventory[i].ItemAmount += amount;
  139.                         if( Inventory[i].ItemAmount <= 0 ) {
  140.                             Inventory[i] = null;
  141.                         }
  142.                         if( amount <= 0 ) {
  143.                             TrimInventory();
  144.                             return;
  145.                         }
  146.                         break;
  147.                     }
  148.                 }
  149.             }
  150.         }
  151.         if( !has ) {
  152.             Inventory.Add( new Item_Slot( NewItem, amount ) );
  153.             TrimInventory();
  154.         }
  155.  
  156.         if( isLocalPlayer ) {
  157.             if( Ply ) {
  158.                 if( Ply.Hud ) {
  159.                     if( Ply.Hud ) {
  160.                         if( Ply.Hud.GetComponentInChildren<AddItemPopup>() ) {
  161.                             Ply.Hud.GetComponentInChildren<AddItemPopup>().CreatePopup( NewItem," +" + amount );
  162.                         }
  163.                     }
  164.                 }
  165.             }
  166.         }
  167.         TrimInventory();
  168.         if( !_CharakterManagerGo ) {
  169.             _CharakterManagerGo = GameObject.FindGameObjectWithTag( "CharakterManager" );
  170.         }
  171.         if( _CharakterManagerGo ) {
  172.             _CharakterManagerGo.GetComponent<CharakterManager>().SavePlayer( gameObject );
  173.         }
  174.     }
  175.  
  176.     public void DropItem( string dropItemName ) {
  177.         Ply.CmdSelectSlot(-1);
  178.         Item NewItem = GetItemByName( dropItemName );
  179.         if( !NewItem ) {
  180.             return;
  181.         }
  182.         if( Inventory.Count > 0 ) {
  183.             for( int i = 0; i < Inventory.Count; i++ ) {
  184.                 if( Inventory[i].Item ) {
  185.                     if( NewItem.Item_Name == Inventory[i].Item.Item_Name ) {
  186.                         if( Inventory[i].ItemAmount > 0 ) {
  187.                             Inventory[i].ItemAmount -= 1;
  188.                             if( Inventory[i].ItemAmount <= 0 ) {
  189.                                 Inventory[i] = null;
  190.                             }
  191.                             CmdSpawnItem( NewItem.Item_Name );
  192.                             break;
  193.                         }                      
  194.                     }
  195.                 }
  196.             }
  197.         }
  198.         TrimInventory();
  199.     }
  200.  
  201.     [Command]
  202.     public void CmdSpawnItem( string SpawnName ) {
  203.         Item NewItem = GetItemByName( SpawnName );
  204.         if( !NewItem ) {
  205.             return;
  206.         }
  207.         GameObject ItemGo = (GameObject)Instantiate( NewItem.gameObject, Ply.HeadTransform. transform.position + ( transform.forward * 1.5f ) , transform.rotation );
  208.         NetworkServer.Spawn( ItemGo );
  209.     }
  210.  
  211.     public void RemoteInitInventory() {
  212.         CmdRemoteInitInventory();
  213.     }
  214.     [Command]
  215.     void CmdRemoteInitInventory() {
  216.         if( !isServer ) {
  217.             return;
  218.         }
  219.         if( Inventory.Count > 0 ) {
  220.             for( int i = 0; i < Inventory.Count; i++ ) {
  221.                 if( Inventory[i].Item ) {
  222.                     CmdSetItem( Inventory[i].Item.Item_Name, Inventory[i].ItemAmount );                    
  223.                 }
  224.             }
  225.         }
  226.     }
  227.  
  228. }
  229.  
  230. [System.Serializable]
  231. public class Item_Slot {
  232.     public Item Item;
  233.     public int ItemAmount;
  234.  
  235.     public Item_Slot( Item _Item, int _ItemAmount ) {
  236.         Item = _Item;
  237.         ItemAmount = _ItemAmount;
  238.     }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement