Ejemplo n.º 1
0
    static int GetChemicalAmountToTransfer(ChemicalContainer _source, ChemicalContainer _target)
    {
        int _totalAmountSource = Mathf.Clamp(_source.GetAmountTotal(), 0, _source.MaxAmount);
        int _totalAmountTarget = Mathf.Clamp(_target.GetAmountTotal(), 0, _target.MaxAmount);
        int _totalAmountDiff   = _totalAmountSource - _totalAmountTarget;

        int _maxAmountAbleToGet = _totalAmountDiff;
        int _maxAmountAbleToSet = _target.MaxAmount - _totalAmountTarget;

        return(Mathf.RoundToInt(0.5f * Mathf.Min(_maxAmountAbleToSet, _maxAmountAbleToGet)));
    }
Ejemplo n.º 2
0
    void SpreadChemicalsAndTemperature(Int2 _nodeGridPos)
    {
        Node _sourceRead  = TryGetNode(_nodeGridPos, _atStartFrame: true);
        Node _sourceWrite = TryGetNode(_nodeGridPos);

        Node _neighborLRead = TryGetNode(_nodeGridPos + Int2.Left, true);
        Node _neighborTRead = TryGetNode(_nodeGridPos + Int2.Up, true);
        Node _neighborRRead = TryGetNode(_nodeGridPos + Int2.Right, true);
        Node _neighborBRead = TryGetNode(_nodeGridPos + Int2.Down, true);

        List <Node> _sortedNodes = new List <Node>();

        int _sourceAmountTotal = _sourceRead.ChemicalContainer.GetAmountTotal();

        if (_neighborLRead != null && !_neighborLRead.IsWall)
        {
            _sortedNodes.Add(_neighborLRead);
        }
        if (_neighborTRead != null && !_neighborTRead.IsWall)
        {
            _sortedNodes.Add(_neighborTRead);
        }
        if (_neighborRRead != null && !_neighborRRead.IsWall)
        {
            _sortedNodes.Add(_neighborRRead);
        }
        if (_neighborBRead != null && !_neighborBRead.IsWall)
        {
            _sortedNodes.Add(_neighborBRead);
        }

        _sortedNodes.Sort((_x, _y) => {
            ChemicalContainer _xCC = _x.ChemicalContainer;
            ChemicalContainer _yCC = _y.ChemicalContainer;
            return(_xCC.GetAmountTotal() < _yCC.GetAmountTotal() ? 0 : 1);
        });

        int _sourceAmountTotalRemaining = _sourceAmountTotal;

        for (int _nodeIndex = 0; _nodeIndex < _sortedNodes.Count; _nodeIndex++)
        {
            Node _targetRead        = _sortedNodes[_nodeIndex];
            Node _targetWrite       = TryGetNode(_targetRead.GridPos);
            int  _targetAmountTotal = _targetRead.ChemicalContainer.GetAmountTotal();

            float _sourceReadTemp  = _sourceRead.ChemicalContainer.Temperature;
            float _sourceWriteTemp = _sourceWrite.ChemicalContainer.Temperature;
            float _targetReadTemp  = _targetRead.ChemicalContainer.Temperature;
            float _targetWriteTemp = _targetWrite.ChemicalContainer.Temperature;

            if (_sourceAmountTotal > _targetAmountTotal)
            {
                int _transferAmountTotal = GetTotalChemAmountToTransfer(ref _sourceAmountTotalRemaining, _sourceRead, _sourceWrite, _targetRead, _targetWrite);

                if (_transferAmountTotal > 0)
                {
                    _targetWriteTemp = Mathf.Lerp(_targetWriteTemp, _sourceReadTemp, _transferAmountTotal / (float)_targetAmountTotal * 0.5f);


                    for (int _chemIndex = 0; _chemIndex < _sourceRead.ChemicalContainer.Contents.Length; _chemIndex++)
                    {
                        Chemical.Blob _sourceChemBlob   = _sourceRead.ChemicalContainer.Contents[_chemIndex];
                        int           _sourceChemAmount = _sourceChemBlob.Amount;
                        if (_sourceChemAmount <= 0)
                        {
                            continue;
                        }

                        int _chemTransferAmount = Mathf.FloorToInt(_transferAmountTotal * (_sourceChemAmount / (float)_sourceAmountTotal) * _sourceChemBlob.GetAmountTransferRate());
                        if (_chemTransferAmount <= 0)
                        {
                            continue;
                        }

                        _sourceWrite.ChemicalContainer.Contents[_chemIndex].SubtractAmount(_chemTransferAmount);
                        _targetWrite.ChemicalContainer.Contents[_chemIndex].AddAmount(_chemTransferAmount);
                    }
                }
            }

            if (_sourceAmountTotal > 0 && _targetAmountTotal > 0 && _sourceReadTemp > _targetReadTemp)
            {
                float _sourceTempLoss, _targetTempGain;
                GetTemperatureToTransfer(_sourceRead, _sourceWrite, _targetRead, _targetWrite, out _sourceTempLoss, out _targetTempGain);
                _sourceWriteTemp -= _sourceTempLoss;
                _targetWriteTemp += _targetTempGain;
            }

            _sourceWrite.ChemicalContainer.SetTemperature(_sourceWriteTemp);
            _targetWrite.ChemicalContainer.SetTemperature(_targetWriteTemp);
        }
    }