背包_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);
}

posted @ 2011-09-10 08:47  softimagewht  阅读(270)  评论(0编辑  收藏  举报