コード例 #1
0
ファイル: Inventory.cs プロジェクト: Ultraporing/The-Deity
        /// <summary>
        /// Remove a certain amount of Resource from the inventory
        /// </summary>
        /// <param name="resourceType">Type of the Resource</param>
        /// <param name="amount">Amount to remove</param>
        /// <returns>Amount removed</returns>
        public int RemoveResource(ResourceType resourceType, int amount)
        {
            int          removed  = 0;
            int          toRemove = amount;
            ResourceSlot rs       = null;

            while ((rs = FindNextSlotWithResource(resourceType)) != null && toRemove != 0)
            {
                int leftToRemove = (int)rs.Amount - toRemove;
                if (leftToRemove < 0)
                {
                    toRemove  = amount - Math.Abs(leftToRemove);
                    removed  += toRemove;
                    rs.Amount = 0;
                    toRemove  = 0;
                }
                else
                {
                    rs.Amount = (uint)Mathf.Clamp(rs.Amount - toRemove, 0, uint.MaxValue);
                    removed  += toRemove;
                    toRemove  = 0;
                }
            }

            return(removed);
        }
コード例 #2
0
ファイル: Inventory.cs プロジェクト: Ultraporing/The-Deity
        /// <summary>
        /// Add a certain amount of Resource into the inventory
        /// </summary>
        /// <param name="resourceType">Type of the Resource</param>
        /// <param name="amount">Amount to add</param>
        /// <returns>Amount added</returns>
        public int AddResource(ResourceType resourceType, int amount)
        {
            int          added = 0;
            int          toAdd = amount;
            ResourceSlot rs    = null;

            while ((rs = FindNextSlotWithSpace(resourceType)) != null && toAdd != 0)
            {
                int leftToAdd = (int)rs.Amount + toAdd;
                if (leftToAdd > MaxAmountPerSlot)
                {
                    toAdd     = leftToAdd - MaxAmountPerSlot;
                    added    += (int)(MaxAmountPerSlot - rs.Amount);
                    rs.Amount = (uint)MaxAmountPerSlot;
                    toAdd     = 0;
                }
                else
                {
                    rs.Amount += (uint)Mathf.Clamp(rs.Amount + toAdd, 0, MaxAmountPerSlot);;
                    added     += toAdd;
                    toAdd      = 0;
                }
            }

            return(added);
        }
コード例 #3
0
 /// <summary>
 /// 为资源表增加一个资源
 /// </summary>
 /// <param name="typeKey">资源类型</param>
 /// <param name="slot">资源信息块</param>
 /// <returns>操作成功与否</returns>
 private bool AddResource(string typeKey, ResourceSlot slot)
 {
     lock (this.resourceTable)
     {
         if (this.resourceTable.ContainsKey(typeKey))
         {
             if (!this.resourceTable[typeKey].ContainsKey(slot.ResourceName))
             {
                 this.resourceTable[typeKey][slot.ResourceName] = slot;
                 return(true);
             }
             if (
                 String.Compare(this.resourceTable[typeKey][slot.ResourceName].Version, slot.Version,
                                StringComparison.Ordinal) < 0)
             {
                 this.resourceTable[typeKey][slot.ResourceName] = slot;
                 LogUtils.LogLine(
                     String.Format(
                         "Resource already exist with later version, to be replaced, type: {0}, name: {1}",
                         typeKey, slot.ResourceName), "ResourceManager", LogLevel.Warning);
                 return(true);
             }
             LogUtils.LogLine(
                 String.Format(
                     "Resource already exist without later version, to be ignored, type: {0}, name: {1}",
                     typeKey, slot.ResourceName), "ResourceManager", LogLevel.Warning);
             return(false);
         }
         LogUtils.LogLine(String.Format("Resource type not exist, type: {0}", typeKey),
                          "ResourceManager", LogLevel.Warning);
         return(false);
     }
 }
コード例 #4
0
    /// <summary>
    /// Updates the appearance of the selected slot and updates the selected slot value
    /// This is deactivated if a buff is in progress
    /// </summary>
    /// <param name="newSlot">Most recently selected resource slot</param>
    public void setSelectedSlot(ResourceSlot newSlot)
    {
        selectedSlot.Value.OnDeslected();
        selectedSlot.Value = newSlot;
        newSlot.OnSelected();

        if (!isBuffInProgress)
        {
            isButtonBuilding.Value = true;
        }
    }
コード例 #5
0
        public FlagState()
        {
            FlagPaths.GotDirty          += (object sender, EventArgs args) => { MarkPropertyAsDirty(nameof(FlagPaths)); };
            OtherEndpoints.GotDirty     += (object sender, EventArgs args) => { MarkPropertyAsDirty(nameof(OtherEndpoints)); };
            OtherEndpointPaths.GotDirty += (object sender, EventArgs args) => { MarkPropertyAsDirty(nameof(OtherEndpointPaths)); };
            Slots.GotDirty += (object sender, EventArgs args) => { MarkPropertyAsDirty(nameof(Slots)); };

            for (int i = 0; i < Global.FLAG_MAX_RES_COUNT; ++i)
            {
                Slots[i] = new ResourceSlot();
            }
        }
コード例 #6
0
    public void SetButtonResource(ViligerController vilController, Resource res)
    {
        GameObject button       = Instantiate(buttonPrefab, unitsButtonsParent);
        Button     buttonScript = button.GetComponent <Button>();

        ResourceSlot slot = button.AddComponent <ResourceSlot>();

        slot.SetContent(vilController, res, playerManager);
        buttonScript.onClick.AddListener(slot.OnTouch);


        resourceSlot = slot;



        unitsButtonsParent.gameObject.SetActive(true);
    }
コード例 #7
0
        /// <summary>
        /// 根据PST向量载入资源到字典
        /// </summary>
        /// <param name="threadID">线程ID</param>
        private void InitDictionaryByPST(object threadID)
        {
            int tid = (int)threadID;

            while (true)
            {
                string pstPath;
                lock (this.pendingPst)
                {
                    if (this.pendingPst.Count != 0)
                    {
                        pstPath = this.pendingPst.Dequeue();
                    }
                    else
                    {
                        break;
                    }
                }
                LogUtils.AsyncLogLine(String.Format("Loading PST Resource From \"{0}\" At thread {1}", pstPath, tid),
                                      "ResourceManager", this.consoleMutex, LogLevel.Normal);
                // 开始处理文件
                FileStream   fs = new FileStream(pstPath, FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                // 读取头部信息
                string header = sr.ReadLine();
                if (header == null)
                {
                    LogUtils.AsyncLogLine(String.Format("Jump null header PST Resource From \"{0}\" At thread {1}", pstPath, tid),
                                          "ResourceManager", this.consoleMutex, LogLevel.Normal);
                    continue;
                }
                var headerItem = header.Split('@');
                if (headerItem.Length != GlobalConfigContext.PackHeaderItemNum && headerItem[0] != GlobalConfigContext.PackHeader)
                {
                    LogUtils.AsyncLogLine(String.Format("Ignored Pack (Bad Header): {0}", pstPath), "ResourceManager", this.consoleMutex, LogLevel.Warning);
                    continue;
                }
                try
                {
                    var fileCount    = Convert.ToInt32(headerItem[1]);
                    var resourceType = headerItem[2];
                    var keyItem      = headerItem[3].Split('?');
                    var version      = keyItem.Length != 2 ? "0" : keyItem[1];
                    var key          = keyItem[0];
                    if (key != GlobalConfigContext.GAME_KEY)
                    {
                        LogUtils.AsyncLogLine(String.Format("Ignored Pack (Key Failed): {0}", pstPath), "ResourceManager", this.consoleMutex, LogLevel.Warning);
                        continue;
                    }
                    // 通过检验的包才载入资源字典
                    this.AddResouceTable(resourceType);
                    int lineEncounter = 0;
                    while (lineEncounter < fileCount)
                    {
                        lineEncounter++;
                        var lineitem = sr.ReadLine()?.Split(':');
                        if (lineitem == null)
                        {
                            LogUtils.AsyncLogLine(String.Format("Ignored Pack of null body: {0}", pstPath), "ResourceManager", this.consoleMutex, LogLevel.Warning);
                            continue;
                        }
                        if (lineitem[0] == GlobalConfigContext.PackEOF)
                        {
                            LogUtils.AsyncLogLine(String.Format("Stop PST caching because encountered EOF: {0}", pstPath), "ResourceManager", this.consoleMutex, LogLevel.Warning);
                            break;
                        }
                        if (lineitem.Length != 3)
                        {
                            LogUtils.AsyncLogLine(String.Format("Igonred line(Bad lineitem): {0}, In file: {1}", lineEncounter, pstPath), "ResourceManager", this.consoleMutex, LogLevel.Warning);
                            continue;
                        }
                        string       srcName   = lineitem[0];
                        long         srcOffset = Convert.ToInt64(lineitem[1]);
                        long         srcLength = Convert.ToInt64(lineitem[2]);
                        ResourceSlot resSlot   = new ResourceSlot()
                        {
                            ResourceName = srcName,
                            BindingFile  = pstPath.Substring(0, pstPath.Length - GlobalConfigContext.PackPostfix.Length),
                            Position     = srcOffset,
                            Length       = srcLength,
                            Version      = version
                        };
                        this.AddResource(resourceType, resSlot);
                    }
                    LogUtils.AsyncLogLine(String.Format("Finish Dictionary Init From \"{0}\" At thread {1}", pstPath, tid), "ResourceManager", this.consoleMutex, LogLevel.Normal);
                }
                catch (Exception ex)
                {
                    LogUtils.AsyncLogLine(ex.ToString(), "ResourceManager / CLR", this.consoleMutex, LogLevel.Error);
                }
                sr.Close();
                fs.Close();
            }
            // 递增回到等待
            ++this.threadFinishCounter;
            LogUtils.AsyncLogLine(String.Format("At ResMana thread {0}, Waiting for callback", tid), "ResourceManager", this.consoleMutex, LogLevel.Important);
        }