Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.Networking;
- using System.Collections.Generic;
- public class Player_Inventory : NetworkBehaviour {
- public Player Ply;
- public Item[] Items;
- public Item[] StartItems;
- [SerializeField]
- public List<Item_Slot> Inventory;
- public GameObject _CharakterManagerGo;
- void Start () {
- getPrefabs();
- Ply = GetComponent<Player>();
- Inventory = new List<Item_Slot>();
- for( int i = 0; i < StartItems.Length; i++ ) {
- CmdSetItem( StartItems[i].Item_Name, 1 );
- }
- }
- void getPrefabs() {
- Object[] Objs1 = Resources.LoadAll( "Prefabs/Props" );
- Items = new Item[Objs1.Length];
- int index = 0;
- for( int i = 0; i < Objs1.Length; i++ ) {
- GameObject ItemGo = Objs1[i] as GameObject;
- if( ItemGo.GetComponent<Item>() ) {
- Items[index] = ItemGo.GetComponent<Item>();
- index++;
- }
- }
- }
- public void TrimInventory() {
- List<Item_Slot> CleanInventory = new List<Item_Slot>();
- for( int i = 0; i < Inventory.Count; i++ ) {
- if( Inventory[i] != null ) {
- if( Inventory[i].ItemAmount > 0 ) {
- CleanInventory.Add( Inventory[i] );
- }
- }
- }
- Inventory = CleanInventory;
- }
- public Item GetItemByName( string _ItemName ) {
- for( int i = 0; i < Items.Length; i++ ) {
- if( Items[i] ) {
- if( Items[i].Item_Name.Trim().ToLower() == _ItemName.Trim().ToLower() ) {
- return Items[i];
- }
- }
- }
- return null;
- }
- [Command]
- public void CmdSetItem( string addItemName, int amount ) {
- RpcSetItem( addItemName, amount );
- }
- [ClientRpc]
- public void RpcSetItem( string addItemName, int amount ) {
- SetItem( addItemName, amount );
- }
- //Both
- public void SetItem( string addItemName, int amount ) {
- Item NewItem = GetItemByName( addItemName );
- if( !NewItem ) {
- return;
- }
- if( Inventory.Count > 0 ) {
- for( int i = 0; i < Inventory.Count; i++ ) {
- if( Inventory[i].Item ) {
- if( NewItem.Item_Name == Inventory[i].Item.Item_Name ) {
- Inventory[i].ItemAmount = amount;
- if( Inventory[i].ItemAmount <= 0 ) {
- Inventory[i] = null;
- }
- TrimInventory();
- return;
- }
- }
- }
- }
- Inventory.Add( new Item_Slot( NewItem, amount ) );
- TrimInventory();
- if( !_CharakterManagerGo ) {
- _CharakterManagerGo = GameObject.FindGameObjectWithTag( "CharakterManager" );
- }
- if( _CharakterManagerGo ) {
- _CharakterManagerGo.GetComponent<CharakterManager>().SavePlayer( gameObject );
- }
- if( isLocalPlayer ) {
- if( Ply ) {
- if( Ply.Hud ) {
- if( Ply.Hud ) {
- if( Ply.Hud.GetComponentInChildren<AddItemPopup>() ) {
- Ply.Hud.GetComponentInChildren<AddItemPopup>().CreatePopup( NewItem, " +" + amount );
- }
- }
- }
- }
- }
- }
- [Command]
- public void CmdEditItem( string addItemName, int amount ) {
- RpcEditItem( addItemName, amount );
- }
- [ClientRpc]
- public void RpcEditItem( string addItemName, int amount ) {
- EditItem( addItemName, amount );
- }
- //Both
- public void EditItem( string addItemName, int amount ) {
- Item NewItem = GetItemByName( addItemName );
- if( !NewItem ) {
- return;
- }
- bool has = false;
- if( Inventory.Count > 0 ) {
- for( int i = 0; i < Inventory.Count; i++ ) {
- if( Inventory[i].Item ) {
- if( NewItem.Item_Name == Inventory[i].Item.Item_Name ) {
- has = true;
- Inventory[i].ItemAmount += amount;
- if( Inventory[i].ItemAmount <= 0 ) {
- Inventory[i] = null;
- }
- if( amount <= 0 ) {
- TrimInventory();
- return;
- }
- break;
- }
- }
- }
- }
- if( !has ) {
- Inventory.Add( new Item_Slot( NewItem, amount ) );
- TrimInventory();
- }
- if( isLocalPlayer ) {
- if( Ply ) {
- if( Ply.Hud ) {
- if( Ply.Hud ) {
- if( Ply.Hud.GetComponentInChildren<AddItemPopup>() ) {
- Ply.Hud.GetComponentInChildren<AddItemPopup>().CreatePopup( NewItem," +" + amount );
- }
- }
- }
- }
- }
- TrimInventory();
- if( !_CharakterManagerGo ) {
- _CharakterManagerGo = GameObject.FindGameObjectWithTag( "CharakterManager" );
- }
- if( _CharakterManagerGo ) {
- _CharakterManagerGo.GetComponent<CharakterManager>().SavePlayer( gameObject );
- }
- }
- public void DropItem( string dropItemName ) {
- Ply.CmdSelectSlot(-1);
- Item NewItem = GetItemByName( dropItemName );
- if( !NewItem ) {
- return;
- }
- if( Inventory.Count > 0 ) {
- for( int i = 0; i < Inventory.Count; i++ ) {
- if( Inventory[i].Item ) {
- if( NewItem.Item_Name == Inventory[i].Item.Item_Name ) {
- if( Inventory[i].ItemAmount > 0 ) {
- Inventory[i].ItemAmount -= 1;
- if( Inventory[i].ItemAmount <= 0 ) {
- Inventory[i] = null;
- }
- CmdSpawnItem( NewItem.Item_Name );
- break;
- }
- }
- }
- }
- }
- TrimInventory();
- }
- [Command]
- public void CmdSpawnItem( string SpawnName ) {
- Item NewItem = GetItemByName( SpawnName );
- if( !NewItem ) {
- return;
- }
- GameObject ItemGo = (GameObject)Instantiate( NewItem.gameObject, Ply.HeadTransform. transform.position + ( transform.forward * 1.5f ) , transform.rotation );
- NetworkServer.Spawn( ItemGo );
- }
- public void RemoteInitInventory() {
- CmdRemoteInitInventory();
- }
- [Command]
- void CmdRemoteInitInventory() {
- if( !isServer ) {
- return;
- }
- if( Inventory.Count > 0 ) {
- for( int i = 0; i < Inventory.Count; i++ ) {
- if( Inventory[i].Item ) {
- CmdSetItem( Inventory[i].Item.Item_Name, Inventory[i].ItemAmount );
- }
- }
- }
- }
- }
- [System.Serializable]
- public class Item_Slot {
- public Item Item;
- public int ItemAmount;
- public Item_Slot( Item _Item, int _ItemAmount ) {
- Item = _Item;
- ItemAmount = _ItemAmount;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement