public Node(Vector3 _worldPos, int _gridX, int _gridY) { WorldPos = _worldPos; GridPos = new Int2(_gridX, _gridY); ChemicalContainer = new ChemicalContainer(CHEM_AMOUNT_MAX); DebugChemicalContainer = new ChemicalContainer(CHEM_AMOUNT_MAX); }
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))); }
public Blob(Chemical _chemical, ChemicalContainer _container) { Container = _container; Chemical = _chemical; Amount = 0; }
void CacheChemicalData(Int2 _nodeGridPos) { int _nodeIndex = _nodeGridPos.y * chemAmountsAndTemperatureTex.width + _nodeGridPos.x; // TODO: need to sort Contents ChemicalContainer _chemicalContainer = GameGrid.GetInstance().TryGetNode(_nodeGridPos).ChemicalContainer; Chemical.Blob _chem_0 = _chemicalContainer.Contents[0]; Chemical.Blob _chem_1 = _chemicalContainer.Contents[1]; Chemical.Blob _chem_2 = _chemicalContainer.Contents[2]; // Vertex data // int _vertexIndexBL = ((_nodeGridPos.y) * GameGrid.TILE_COUNT.x + (_nodeGridPos.x)) * VERTICES_PER_TILE + VERTEX_INDEX_TOP_RIGHT; // int _vertexIndexBR = ((_nodeGridPos.y) * GameGrid.TILE_COUNT.x + (_nodeGridPos.x + 1)) * VERTICES_PER_TILE + VERTEX_INDEX_TOP_LEFT; // int _vertexIndexTL = ((_nodeGridPos.y + 1) * GameGrid.TILE_COUNT.x + (_nodeGridPos.x)) * VERTICES_PER_TILE + VERTEX_INDEX_BOTTOM_RIGHT; // int _vertexIndexTR = ((_nodeGridPos.y + 1) * GameGrid.TILE_COUNT.x + (_nodeGridPos.x + 1)) * VERTICES_PER_TILE + VERTEX_INDEX_BOTTOM_LEFT; float _amount_0 = _chem_0.Amount / (float)_chemicalContainer.MaxAmount; float _amount_1 = _chem_1.Amount / (float)_chemicalContainer.MaxAmount; float _amount_2 = _chem_2.Amount / (float)_chemicalContainer.MaxAmount; float _temperature = _chemicalContainer.Temperature / (float)Chemical.MAX_TEMPERATURE; // chemAmountsAndTemperature[_vertexIndexBL] = new Vector4(_amount_0, _amount_1, _amount_2, _temperature); // chemAmountsAndTemperature[_vertexIndexBR] = new Vector4(_amount_0, _amount_1, _amount_2, _temperature); // chemAmountsAndTemperature[_vertexIndexTL] = new Vector4(_amount_0, _amount_1, _amount_2, _temperature); // chemAmountsAndTemperature[_vertexIndexTR] = new Vector4(_amount_0, _amount_1, _amount_2, _temperature); float debugValue_0 = GameGrid.GetInstance().TryGetNode(_nodeGridPos).DebugChemicalContainer.Contents[0].Amount / (float)GameGrid.GetInstance().TryGetNode(_nodeGridPos).DebugChemicalContainer.MaxAmount; float debugValue_1 = GameGrid.GetInstance().TryGetNode(_nodeGridPos).DebugChemicalContainer.Contents[1].Amount / (float)GameGrid.GetInstance().TryGetNode(_nodeGridPos).DebugChemicalContainer.MaxAmount; float debugValue_2 = GameGrid.GetInstance().TryGetNode(_nodeGridPos).DebugChemicalContainer.Contents[2].Amount / (float)GameGrid.GetInstance().TryGetNode(_nodeGridPos).DebugChemicalContainer.MaxAmount; debugColors[_nodeIndex] = new Color(debugValue_0, debugValue_1, debugValue_2, 1); chemAmountsAndTemperature[_nodeIndex] = new Color(_amount_0, _amount_1, _amount_2, _temperature); // chemColorIndices[_vertexIndexBL] = new Vector3(_colorIndex_0, _colorIndex_1, _colorIndex_2); // chemColorIndices[_vertexIndexBR] = new Vector3(_colorIndex_0, _colorIndex_1, _colorIndex_2); // chemColorIndices[_vertexIndexTL] = new Vector3(_colorIndex_0, _colorIndex_1, _colorIndex_2); // chemColorIndices[_vertexIndexTR] = new Vector3(_colorIndex_0, _colorIndex_1, _colorIndex_2); float _colorIndex_0 = _chem_0.Chemical.GetColorIndex() / (float)ColorManager.COLOR_COUNT; float _colorIndex_1 = _chem_1.Chemical.GetColorIndex() / (float)ColorManager.COLOR_COUNT; float _colorIndex_2 = _chem_2.Chemical.GetColorIndex() / (float)ColorManager.COLOR_COUNT; chemColorIndices[_nodeIndex] = new Color(_colorIndex_0, _colorIndex_1, _colorIndex_2, 0.0f); // // Texture data int _stateCount = System.Enum.GetValues(typeof(Chemical.State)).Length; float _state_0 = _chem_0.GetStateAsFloat() / (float)(_stateCount - 1.0f); float _state_1 = _chem_1.GetStateAsFloat() / (float)(_stateCount - 1.0f); float _state_2 = _chem_2.GetStateAsFloat() / (float)(_stateCount - 1.0f); // Debug.Log(_chem_0.GetStateAsFloat() + ", " + _state_0); // chemStates[_vertexIndexBL] = new Vector3(_state_0, _state_1, _state_2); // chemStates[_vertexIndexBR] = new Vector3(_state_0, _state_1, _state_2); // chemStates[_vertexIndexTL] = new Vector3(_state_0, _state_1, _state_2); // chemStates[_vertexIndexTR] = new Vector3(_state_0, _state_1, _state_2); chemStates[_nodeIndex] = new Color(_state_0, _state_1, _state_2); // // if(_nodeGridPos.x == 30 && _nodeGridPos.y == 20) { // SuperDebug.MarkPoint(GameGrid.GetInstance().GetWorldPosFromNodeGridPos(_nodeGridPos), Color.cyan); // // float _stateR = GameGrid.GetInstance().TryGetNode(_nodeGridPos + Int2.Right).ChemicalContainer.Contents[0].GetStateAsFloat(); // // Debug.Log("state = " +(_state_0 * 3.0f) + ", stateR = " + _stateR); // } if (_chemicalContainer.IsIncandescent() || _chemicalContainer.HasLostIncandescence()) { LampManager.GetInstance().SetNodeIncandescenceDirty(_nodeGridPos); } // Color _incandescence_0 = _chem_0.GetIncandescence() * _amount_0; // Color _incandescence_1 = _chem_1.GetIncandescence() * _amount_1; // Color _incandescence_2 = _chem_2.GetIncandescence() * _amount_2; // // float _maxIncandescenceR = Mathf.Max(_incandescence_0.r, Mathf.Max(_incandescence_1.r, _incandescence_2.r)); // float _maxIncandescenceG = Mathf.Max(_incandescence_0.g, Mathf.Max(_incandescence_1.g, _incandescence_2.g)); // float _maxIncandescenceB = Mathf.Max(_incandescence_0.b, Mathf.Max(_incandescence_1.b, _incandescence_2.b)); // float _maxIncandescenceA = Mathf.Max(_incandescence_0.a, Mathf.Max(_incandescence_1.a, _incandescence_2.a)); // // Node _node = GameGrid.GetInstance().TryGetNode(_nodeGridPos); // Color32 _lighting = _node.GetLighting(); // // _lighting.r = (byte)Mathf.Max(_lighting.r, _maxIncandescenceR * 255.0f); // _lighting.g = (byte)Mathf.Max(_lighting.g, _maxIncandescenceG * 255.0f); // _lighting.b = (byte)Mathf.Max(_lighting.b, _maxIncandescenceB * 255.0f); // _lighting.a = (byte)Mathf.Max(_lighting.a, _maxIncandescenceA * 255.0f); // // _node.SetLighting(_lighting); }
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); } }
public override void UpdateDefault() { base.UpdateDefault(); if (b) { for (int x = 0; x < SIZE.x; x++) { for (int y = 0; y < SIZE.y; y++) { Int2 _nodeGridPos = new Int2(x, y); SpreadChemicalsAndTemperature(_nodeGridPos); ScheduleCacheChemicalData(_nodeGridPos); } } } else { for (int x = SIZE.x - 1; x >= 0; x--) { for (int y = SIZE.y - 1; y >= 0; y--) { Int2 _nodeGridPos = new Int2(x, y); SpreadChemicalsAndTemperature(_nodeGridPos); ScheduleCacheChemicalData(_nodeGridPos); } } } b = !b; float _total = 0.0f; float _temp = 0.0f; float min = Mathf.Infinity; float max = Mathf.NegativeInfinity; for (int x = 0; x < SIZE.x; x++) { for (int y = 0; y < SIZE.y; y++) { ChemicalContainer chemCont = nodeGrid[x, y].ChemicalContainer; int _a = chemCont.Contents[0].Amount; _total += _a; _temp += chemCont.Temperature; if (_a < min) { min = _a; } if (_a > max) { max = _a; } } } _temp /= (float)(SIZE.x * SIZE.y); Debug.LogFormat("Amount: {0}, Min Amount: {1}, Max Amount: {2}, Temperature: {3}", _total, min, max, _temp); }