zoukankan      html  css  js  c++  java
  • 背包_InventoryGrid

    var bagName:String = "set a name for bag";
    var bagBtnName:String = "Bag button name here";
    var posBagBtn:Vector2 = Vector2(50,50);

    var slotSize: float = 40;
    var spacingBetweenSlots :float = 5;
    var defaultTextureSlot:Texture;
    var bagNumber:int;

    private var numSlot = 16;
    private var offsetBag : int ;
    private var showBag :boolean = false;
    private var bagPos:Vector2 ;
    private var indexOnController:int;

    private var tempTexture :Texture;

    private var ctrl:PositionController;

    function Start()
    {
       offsetBag = bagNumber * numSlot;
      ctrl = transform.parent.GetComponent(PositionController);
      indexOnController = 0;
    }

    function OnGUI()
    {
       if(GUI.Button(Rect(Screen.width - posBagBtn.x, Screen.height - posBagBtn.y,slotSize,slotSize),bagBtnName))
       {
          ToggleBag();
       }
      
       if(showBag)
       {
         Bag(bagPos.x,bagPos.y,185,200);
       }
    }

    function ToggleBag()
    {
       if(showBag)
       {
         HideBag();
       }
       else
       {
         ShowBag();
       }
    }

    function HideBag()
    {
      showBag = false;
      ctrl.FreePosition(indexOnController);
    }

    function ShowBag()
    {
      showBag = true;
      var info: Vector3 = ctrl.GetPositionAvailable();
      indexOnController = info.z;
      bagPos = Vector2(info.x,info.y);
    }

    function BagIsBeingDisplayed()
    {
       return showBag;
    }

    function Bag(posX:float,posY:float,sX:float,sY:float)
    {
      GUI.BeginGroup(Rect(Screen.width - posX,Screen.height - posY,sX,sY));
      GUI.Box(Rect(0,0,sX,sY),bagName);
     
      for(var i=0;i<4;i++)
      {
         for(var j=0;j<4;j++)
         {
            if(ctrl.GetObjectsInBag(offsetBag + (4*i)+j) == null)
            {
               tempTexture = defaultTextureSlot; 
      } 
      else
      {
       tempTexture = ctrl.GetObjectsInBag(offsetBag+(4*i)+j).GetComponent(ObjectInfo).iconTexture;
      }
      
      if(Input.GetMouseButtonDown(0))
      {
          ctrl.SetButtonClicked(false);
      }
      
            if(GUI.Button(Rect(slotSize*j+spacingBetweenSlots*(j+1) , slotSize*i+spacingBetweenSlots*(i+1)+15,
            slotSize,slotSize),tempTexture))
            {
          ctrl.SetButtonClicked(true);
                   
               if(ctrl.GetObjectsInBag(offsetBag+(4*i)+j) != null)
         {
            ctrl.mouseTextureHander.SetCursor(ctrl.GetObjectsInBag(offsetBag+(4*i)+j).GetComponent(ObjectInfo).iconTexture);
        
         ctrl.SetObjAttached(true);
         }
         //print((offsetBag+(4*i)+j));
         //print(ctrl.GetObjectsInBag((offsetBag+(4*i)+j)));
          MoveSlot((offsetBag+(4*i)+j));
            }
         }  
      }
     
      GUI.EndGroup(); 
    }

    function MoveSlot( indSlot : int)
    {
        //ctrl.SetLastSlotUsed(indSlot);
        if(ctrl.GetObjectsInBag(indSlot) == null && !ctrl.GetSlotMovedFlag())//移动图标到新的位置
        {
      ctrl.SetObjectInBag(indSlot, ctrl.GetObjectsInBag(ctrl.GetLastSlotUsed()));
      ReleaseSlot(ctrl.GetLastSlotUsed());
      ctrl.SetSlotMovedFlag(true);
      ctrl.mouseTextureHander.ReleaseCursorIcon();
      ctrl.SetObjAttached(false);
     }
     else if(ctrl.GetObjectsInBag(indSlot) != null && ctrl.GetObjectsInBag(ctrl.GetLastSlotUsed()) != null && !ctrl.GetSlotMovedFlag()) //转换两个图标
     {
         var tmpObj : GameObject = ctrl.GetObjectsInBag(indSlot);
      ctrl.SetObjectInBag(indSlot, ctrl.GetObjectsInBag(ctrl.GetLastSlotUsed()));
      ctrl.SetObjectInBag(ctrl.GetLastSlotUsed(),tmpObj);
      ctrl.SetSlotMovedFlag(true);
      ctrl.mouseTextureHander.ReleaseCursorIcon();
      ctrl.SetObjAttached(false);
     }
     else
     {
         ctrl.SetSlotMovedFlag(false);
     }
     ctrl.SetLastSlotUsed(indSlot);
    }

    function ReleaseSlot(index :int)
    {
        ctrl.SetObjectInBag( index, null);
    }

  • 相关阅读:
    LeetCode153 Find Minimum in Rotated Sorted Array. LeetCode162 Find Peak Element
    LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word
    LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number
    LeetCode191 Number of 1 Bits. LeetCode231 Power of Two. LeetCode342 Power of Four
    LeetCode225 Implement Stack using Queues
    LeetCode150 Evaluate Reverse Polish Notation
    LeetCode125 Valid Palindrome
    LeetCode128 Longest Consecutive Sequence
    LeetCode124 Binary Tree Maximum Path Sum
    LeetCode123 Best Time to Buy and Sell Stock III
  • 原文地址:https://www.cnblogs.com/softimagewht/p/2172975.html
Copyright © 2011-2022 走看看