Vous êtes sur la page 1sur 18

var inventoryIcon:Texture2D;//this is what will be displayed in the demos On Screen GUI.

var canGet=true;
var itemType:String;//This will let us equip the item to specific slots. Ex: Head, Shoulder, or
whatever we set up.
var BoneList:Transform[];
var stackable=false;//is it stackable?
var maxStack=99;//each stack can have 99 items stacked on it!
var stack=1;//i will set the stack to 1. since itself counts as 1.
var isEquipment=true;

function Awake(){
var Bones=GetComponentsInChildren(Transform);//keeping track of all the bones
var newarray=new Array(Bones);//that this item has. so we can parent them to the player.
BoneList=newarray.ToBuiltin(Transform);
}


function OnMouseDown(){//When you click an item
var getit=true;
var playersinv=FindObjectOfType(Inventory);//finding the players inv. I suggest when
making
//a game, you make a function that picks up an item within the player script. and
then have the inventory
//referneced from its own variable. OR since the playerscript would be attached to
the inv i suggest you
//do GetComponent(Inventory).AddItem, This way multiple players can have
inventorys.
if(canGet){//if its getable or hasnt been gotten.
if(stackable){
var locatedit:Item;
for(var t:Transform in playersinv.Contents){
if(t.name==this.transform.name){//if the item we wanna stack this
on has the same name
var i:Item=t.GetComponent(Item);
if(i.stack<i.maxStack){
locatedit=i;
}
}
}
if(locatedit!=null){//if we have a stack to stack it to!
getit=false;
locatedit.stack+=1;
Destroy(this.gameObject);
}
else{
getit=true;
}
}
if(getit){
playersinv.AddItem(this.transform);
MoveMeToThePlayer(playersinv.transform);//moves the object, to the
player
}
}
}






function MoveMeToThePlayer(theplayer:Transform){//This will basically make the item stay where
the player is
//as long as its in the players inventory. This can also be done multiple ways, but ill stick with an
easy one.
canGet=false;//now that we have it no need to be able to get it again.
transform.collider.isTrigger=true;//makes it undence.
var renderers=GetComponentsInChildren(Renderer);//get all the renderers in the object
for(var rend:Renderer in renderers){
rend.enabled=false;//turn off all renderers in this object.
}
if(transform.renderer!=null){
transform.renderer.enabled=false;//makes it invisible
}
transform.parent=theplayer;//makes the object parented to the player.
transform.localPosition=Vector3.zero;//now that the item is parented to the player
}
//////////////////////////////PLAYER
var WeaponSlots:Transform[];
var ArmorSlot:Item[];
var ArmorSlotName:String[];

private var csheet=false;
var positionsForCS:Rect[];
var cbutton:Texture2D;
var windowRect=Rect(100,100,200,300);//keeping track of our window.

var BoneList:Transform[];//this is used for parenting items to the player

function Awake(){
var Bones=GetComponentsInChildren(Transform);//We are collecting all the bones in the
children
var newarray=new Array(Bones);
BoneList=newarray.ToBuiltin(Transform);
}



function PlayAnim(anim:String){//This is going to play an animation for the player, and all of his
armor.

if(animation!=null){//If this gameObject has an animation component!
animation.Play(anim);//We play the animation!
}
}

function PairBones(i:Item){

for(var mybone:Transform in BoneList){//for all the bones in the player.
for(var b:Transform in i.BoneList){//and for all the bones in the item
if(b.name==mybone.name){//if the bone in the item has the same name as
the bone in the player
b.parent=mybone;//make the items bone parented to the players
bone
b.rotation=mybone.rotation;//and give it the same attributes.
b.localPosition=Vector3.zero;

}
}
}
}

function CheckSlot(tocheck:int){
var toreturn=false;
if(ArmorSlot[tocheck]!=null){
toreturn=true;
}
return toreturn;
}

function UseItem(i:Item,slot:int,autoequip:boolean){
if(i.isEquipment){
if(autoequip){
var index=0;
var equipto=0;
for(var a in ArmorSlotName){
if(a==i.itemType){
equipto=index;
}
index++;
}
EquipItem(i,equipto);
}
else{

if(i.itemType==ArmorSlotName[slot]){
EquipItem(i,slot);
}
}
}
}



function EquipItem(i:Item,slot:int){
if(i.itemType==ArmorSlotName[slot]){
if(CheckSlot(slot)){
UnequipItem(ArmorSlot[slot]);
ArmorSlot[slot]=null;
}
ArmorSlot[slot]=i;
var rends=i.GetComponentsInChildren(Renderer);
var rend:Renderer=i.GetComponent(Renderer);
if(rend){
i.enabled=true;
}
for(var r:Renderer in rends){
r.enabled=true;
}
i.transform.position=transform.position;
i.transform.rotation=transform.rotation;
PairBones(i);
}
}

function UnequipItem(i:Item){
for(var b:Transform in i.BoneList){
b.parent=i.transform;
}
var rends=i.GetComponentsInChildren(Renderer);
var rend:Renderer=i.GetComponent(Renderer);
if(rend){
i.enabled=false;
}
for(var r:Renderer in rends){
r.enabled=false;
}
}


function OnGUI(){
if(cbutton!=null){
if(GUI.Button(Rect(0,Screen.height-40,40,40),cbutton)){
//make a icon button that will open and close the character sheet.
if(csheet){
csheet=false;
}
else{
csheet=true;
}
}
}
if(csheet){
windowRect=GUI.Window (1, windowRect, DisplayCSheetWindow, "Character
Sheet");

}
}





function DisplayCSheetWindow(windowID:int){
GUI.DragWindow (Rect (0,0, 10000, 20));
var index=0;
for(var a in ArmorSlot){
if(a==null){
if(GUI.Button(positionsForCS[index],"")){
var id=GetComponent(InventoryDisplayBag);
if(id.itemBeingDragged!=null){
EquipItem(id.itemBeingDragged,index);
id.ClearDraggedItem();
}
}
}
else{
if(GUI.Button(positionsForCS[index],ArmorSlot[index].inventoryIcon)){
var id2=GetComponent(InventoryDisplayBag);
if(id2.itemBeingDragged!=null){
EquipItem(id2.itemBeingDragged,index);
id2.ClearDraggedItem();
}
else{
UnequipItem(ArmorSlot[index]);
ArmorSlot[index]=null;
id2.ClearDraggedItem();
}
}
}
index++;
}
}
//////////////////////// INVENTORY
var Contents:Transform[];
//Although in the demo I am going to identify items as its own script *Item* I will still
//identify and add them by thier transform to allow more of a versitile of ways to program a game
//using this simple type of script :P.


function AddItem(Item:Transform){//Add an item to the inventory.
var newContents=new Array(Contents);
newContents.Add(Item);
Debug.Log(Item.name+" Has been added to inventroy");
Contents=newContents.ToBuiltin(Transform);// array to unity builtin array
}
function RemoveItem(Item:Transform){//Removed an item from the inventory.
var newContents=new Array(Contents);
var index=0;
var shouldend=false;
for(var i:Transform in newContents){
if(i==Item){
Debug.Log(Item.name+" Has been removed from inventroy");
newContents.RemoveAt(index);
shouldend=true;
//No need to continue running through the loop since we found our item.
}
index++;//keep track of what index the item is and remove it.
if(shouldend){
Contents=newContents.ToBuiltin(Transform);
return;
}
}
}



function DebugInfo(){ //A simple debug. Will tell you everything that is in the inventory.
Debug.Log("Inventory Debug - Contents");
items=0;
for(var i:Transform in Contents){
items++;
Debug.Log(i.name);
}
Debug.Log("Inventory contains "+items+" Item(s)");
}
///////////////////////////////// INVENTORY DISPLAY
//This type of inventory display will be a bag. similair to WoW.
var itemBeingDragged:Item;//This refers to the "Item" script. you WILL need
//this script in your game to use this line of code.
var draggedItemPosition:Vector2;//Where on the screen we are dragging our item.
var draggedItemSize:Vector2;//The size of the item icon we are dragging.

var backDrop:Texture2D;
private var windowPosition:Vector2=Vector2(200,200);//where on the screen the window will
appear.
//this can easily be and should be updated on the fly incase the screen size changes or what not.
private var windowSize:Vector2=Vector2(108.0,130.0);//the size of the window the bag will be
displayed.
var itemIconSize:Vector2=Vector2(32.0,32.0);//The size of the item icons
var updateListDelay=1.0;//This will be used to updated the inventory on screen, rather then
//updating it every time OnGUI is called. if you prefer you can directly get what in the list. but i
//dont like having multiple GetComponents >.<.
var lastUpdate=0.0;//last time we updated the display.
var UpdatedList:Transform[];//The inv list with a late delay.
var associatedInventory:Inventory;//var to keep track of your inv
var displayInventory=false;//if inv is opened
var invSkin:GUISkin;//the GUI skin.
var windowRect=Rect(200,200,108,130);//keeping track of our window.
var bagIcon:Texture2D;//this texture will be used for our switch
var offsetX=6;//This will leave so many pixels between the edge of the window. left and right
var offsetY=6;//This will leave so many pixels between the edge of the window. top and bottom

function Awake(){
//On this line im hard coding the position. So the bag is displayed Above the button.
//You can remove this to have it appear where you want it.
windowRect=Rect(Screen.width-windowSize.x,Screen.height-40-
windowSize.y,windowSize.x,windowSize.y);
associatedInventory=GetComponent(Inventory);//keepin track of the inventory script
}
function UpdateInventoryList(){//update the delayed inv list
UpdatedList=associatedInventory.Contents;
}
function Update(){
if(Input.GetKeyDown(KeyCode.Escape)){//Pressed escape
ClearDraggedItem();//get rid of the dragged item.
}
}

function OnGUI(){
GUI.skin=invSkin;
if(itemBeingDragged!=null){//if we are in faCt dragging an item!
//Once again Im going to use a button to show where the item is being dragged
around.
//The reason for this is because buttons are easier to see and will still show on
screen if
//no inventoryIcon is assigned! If youd like you can change this to
GUI.DrawTexture() !

GUI.Button(Rect(draggedItemPosition.x,draggedItemPosition.y,draggedItemSize.x,dragged
ItemSize.y),itemBeingDragged.inventoryIcon);
}
if(bagIcon!=null){
if(GUI.Button(Rect(Screen.width-40,Screen.height-40,40,40),bagIcon)){
//make a bag icon button that will open and close the inventory.
if(displayInventory){
displayInventory=false;//inv off
//since we close inventory we will clear the dragged item
ClearDraggedItem();
}
else{
displayInventory=true;//inv on
}
}
}
if(displayInventory){//If the inventory is opened up.
//we will make a window that shows whats in the inventory.
windowRect=GUI.Window (0, windowRect, DisplayInventoryWindow,
"Inventory");
//and update the position and size variables from the windows variables.
//We will use these variables later on to determine where the inventory icons
belong
//and for later uses. This could be done by directly pulling the variabled from
windowRect
//but i prefer storing the variables as vector2's.
windowPosition=Vector2(windowRect.x,windowRect.y);
windowSize=Vector2(windowRect.width,windowRect.height);
}
}

function FixedUpdate(){//I will update the display inventory here at a set delay to limit cpu usage
if(itemBeingDragged!=null){//Making the dragged icon update its position
//as you can see im giving a 15 pixel space away from the mouse pointer to allow
me to be able
//to click stuff and not hit the button we are dragging
draggedItemPosition.y=Screen.height-Input.mousePosition.y+15;
draggedItemPosition.x=Input.mousePosition.x+15;
}
if(Time.time>lastUpdate){
lastUpdate=Time.time+updateListDelay;
UpdateInventoryList();
}
}



function DisplayInventoryWindow(windowID:int){
//GUI.DragWindow (Rect (0,0, 10000, 20)); //Do we want the window to be able to be
dragged?
var currentX=0+offsetX;//where to put the first items
var currentY=18+offsetY;//Im setting the start y position to 18 to give room for the title
bar on the window
//Draw the backdrop in the windowposition and the size of the windowsize.
if(backDrop!=null){//if we have a backDrop to display, we display it!

GUI.DrawTexture(Rect(0,0,windowSize.x,windowSize.y),backDrop,ScaleMode.StretchToFill
);
}
for(var i:Transform in UpdatedList){//we start a loop for whats in our list.
var item=i.GetComponent(Item);//we know that all objects in this list are items,
cus we
//will make sure nothing else can go in here, RIGHT? :P
//directly call accocialtedInventory.Contents but i prefer not to since its more work
for you and the pc.
//I use a button since its easier to be able to click it and then make a drop down
menu to delete or move

if(GUI.Button(Rect(currentX,currentY,itemIconSize.x,itemIconSize.y),item.inventoryIcon)){
//THIS HERE IS A DEMO ELEMENT! IF YOU DECIDE NOT TO USE THE DEMO
ASSETS
//YOU WILL NEED TO REMOVE THIS OR REPLACE IT WITH YOUR OWN
STUFF.
var dragitem=true;//Incase we stop dragging an item we dont wanna
redrag a new one.
if(itemBeingDragged==item){//Basically. we clicked the item, then clicked
it again
GetComponent(Player).UseItem(item,0,true);//so we use it. if its
equipment we autoequip it (true)
ClearDraggedItem();//stop dragging what we are dragging
dragitem=false;//dont redrag what we stopped dragging
}
if(dragitem){
itemBeingDragged=item;//set the item being dragged.
draggedItemSize=itemIconSize;//We set the dragged icon size to
our item button size
//and the position it should be at (mouseposition)
draggedItemPosition.y=Screen.height-Input.mousePosition.y-
15;//im taking away 15 pixels so the button isnt directly on the top of the cursor
draggedItemPosition.x=Input.mousePosition.x+15;
}
}
if(item.stackable){

GUI.Label(Rect(currentX,currentY,itemIconSize.x,itemIconSize.y),""+item.stack,"Stacks");
//This will show a number showing how many items a stack has IF it has a
stack
//Im using a GUISkin with this and its going to use the custom "Stacks"
//this way i can choose where to put the number at ex: Top left top right
bottom right.
}
currentX+=itemIconSize.x;
if(currentX+itemIconSize.x+offsetX>windowSize.x){
//if the next item icon will be to large for the window..... and the offsetX (to keep it
looking neat)
currentX=offsetX;//we move it back to its startpoint wich is 0 + our offsetX.
currentY+=itemIconSize.y;//and down a row.
if(currentY+itemIconSize.y+offsetY>windowSize.y){//if the row is down to
far. we quit the loop
//I also am going to unclude a offset here. So items wont be crouding the
window line.
return;
}
}
}
}


function ClearDraggedItem(){//If we are dragging an item, we will clear it and not be anymore.
itemBeingDragged=null;
}

Vous aimerez peut-être aussi