Advertisement
szymski

Untitled

Dec 27th, 2015
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.89 KB | None | 0 0
  1. class TINV_SubInventory {
  2.  
  3.     var Id = "NOT_DEFINED";
  4.     var Width, Height;
  5.     var Items = { };
  6.     var Slots = { }; // Slots point at items
  7.  
  8.     var Player; // Serverside only, set by inventory constructor
  9.  
  10.     /*
  11.         Constructor
  12.     */
  13.     function TINV_SubInventory(id, width, height) {
  14.         this.Id = id;
  15.         this.Width, this.Height = width, height;
  16.  
  17.         for(var x = 1; x <= width; x++) {
  18.             this.Slots[x] = { };
  19.         }
  20.     }
  21.  
  22.     /*
  23.         Updates slots. Should be called after every modification.
  24.     */
  25.     function UpdateSlots() {
  26.         for(var x = 1; x <= this.Width; x++)
  27.             this.Slots[x] = { };
  28.    
  29.         foreach(var item in this.Items) {
  30.             for(var itemX = item.SlotX; itemX <= item.SlotX + item.Width - 1; itemX++)
  31.                 for(var itemY = item.SlotY; itemY <= item.SlotY + item.Height - 1; itemY++)
  32.                     this.Slots[itemX][itemY] = item;
  33.         }
  34.     }
  35.  
  36.     /*
  37.         Determines, if the player can access/modify the inventory at the moment.
  38.     */
  39.     function CanAccess(ply) {
  40.         return true;
  41.     }
  42.  
  43.     /*
  44.         Item functions
  45.     */
  46.  
  47.     /*
  48.         Removes specified item object from inventory
  49.     */
  50.     function RemoveItem(item) {
  51.         table.RemoveByValue(this.Items, item);
  52.         this:UpdateSlots();
  53.     }
  54.  
  55.     /*
  56.         Returns true, if there's a place for an item with specific size on specific position
  57.     */
  58.     function HasPlaceOn(x, y, width, height) {
  59.         var ok = true;
  60.  
  61.         for(var itemX = x; itemX <= x + width - 1; itemX++) {
  62.             if(!ok) break;
  63.  
  64.             for(var itemY = y; itemY <= y + height - 1; itemY++)
  65.                 if(itemX < 1 || itemY < 1 || itemX > this.Width || itemY > this.Height || this.Slots[itemX][itemY]) { // We found an item on this slot, no place on this position
  66.                     return false;
  67.                 }
  68.         }
  69.  
  70.         return true;
  71.     }
  72.  
  73.     /*
  74.         Returns true, slotX, slotY, if there's a place for an item with specific size, otherwise, returns false.
  75.     */
  76.     function HasPlace(width, height) {
  77.         for(var y = 1; y <= this.Height - height + 1; y++)
  78.             for(var x = 1; x <= this.Width - width + 1; x++) {
  79.                 var ok = true;
  80.  
  81.                 for(var itemX = x; itemX <= x + width - 1; itemX++) {
  82.                     if(!ok) break;
  83.  
  84.                     for(var itemY = y; itemY <= y + height - 1; itemY++)
  85.                         if(itemX < 1 || itemY < 1 || itemX > this.Width || itemY > this.Height || this.Slots[itemX][itemY]) { // We found an item on this slot, no place on this position
  86.                             ok = false;
  87.                             break;
  88.                         }
  89.                 }
  90.  
  91.                 if(ok)
  92.                     return true, x, y;
  93.             }
  94.  
  95.         return false;
  96.     }
  97.  
  98.     /*
  99.         Adds item to inventory - first free slot.
  100.     */
  101.     function AddItem(item) {
  102.         if(!item)
  103.             error("TINV Error: Trying to add a null item!");
  104.  
  105.         var ok, x, y = this:HasPlace(item.Width, item.Height);
  106.  
  107.         if(ok)
  108.             print(string.format("There's a place for item on X: %s, Y: %s", x, y));
  109.         else {
  110.             print("No place for your item :(");
  111.             return false;
  112.         }
  113.  
  114.         this.Items[#this.Items+1] = item;
  115.         item:UpdateSubInventory(this, x, y);
  116.         this:UpdateSlots();
  117.  
  118.         if(SERVER)
  119.             TINV.UpdatePlayerSubInventory(this.Player, this);
  120.  
  121.         return true;
  122.     }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement