public ChemicalContainer(int _maxAmount) { MaxAmount = Mathf.Min(_maxAmount, MAX_POSSIBLE_AMOUNT); Chemical[] _allChemicals = ChemicalManager.GetInstance().GetAllChemicals(); Contents = new Chemical.Blob[_allChemicals.Length]; for (int i = 0; i < Contents.Length; i++) { Contents[i] = new Chemical.Blob(_allChemicals[i], this); } }
public void GetThreeMostPrevalentChemicals(out Chemical.Blob _chem0, out Chemical.Blob _chem1, out Chemical.Blob _chem2) { List <Chemical.Blob> _chems = new List <Chemical.Blob>(Contents.Length); for (int i = 0; i < Contents.Length; i++) { _chems.Add(Contents[i]); } _chems.Sort((x, y) => x.Amount.CompareTo(y.Amount)); _chem0 = _chems[0]; _chem1 = _chems[1]; _chem2 = _chems[2]; }
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 UpdateLate() { foreach (KeyValuePair <Int2, NodeDetails> _nodeDetails in nodesToUpdate) { Node _node = GameGrid.GetInstance().TryGetNode(_nodeDetails.Key); RoomManager.Room _room = RoomManager.GetInstance().GetRoom(_node.RoomIndex); if (_room == null) { continue; } Color32 _lightColor = new Color32(0, 0, 0, 0); if (_nodeDetails.Value.HasDirtyLighting) { for (int _lampIndex = 0; _lampIndex < _room.Lamps.Count; _lampIndex++) { Lamp _lamp = _room.Lamps[_lampIndex]; if (_node.RoomIndex != _lamp.GetRoomIndex()) { Debug.LogErrorFormat("Somehow Node({0}) found {1} despite being in different rooms ({2} and {3})!", _node.GridPos, _lamp.name, _node.RoomIndex, _lamp.GetRoomIndex()); continue; } Int2 _distance = _node.GridPos - _lamp.GetNode().GridPos; if (Mathf.Abs(_distance.x) > _lamp.GetRadius() || Mathf.Abs(_distance.y) > _lamp.GetRadius()) { continue; } float _pathLength; bool _hasFoundPath = Task.FindPath.TryGetPathLength(_node, _lamp.GetNode(), out _pathLength); if (!_hasFoundPath) { continue; } if (_pathLength > _lamp.GetRadius()) { continue; } float _lightFromLamp = 1.0f - _pathLength / (float)_lamp.GetRadius(); _lightColor = new Color32( (byte)(_lamp.GetColor().r *_lightFromLamp), (byte)(_lamp.GetColor().g *_lightFromLamp), (byte)(_lamp.GetColor().b *_lightFromLamp), (byte)(255 * _lightFromLamp) ); } } if (_nodeDetails.Value.HasDirtyIncandescence && _node.ChemicalContainer.IsIncandescent()) { Chemical.Blob _chem_0 = _node.ChemicalContainer.Contents[0]; Chemical.Blob _chem_1 = _node.ChemicalContainer.Contents[1]; Chemical.Blob _chem_2 = _node.ChemicalContainer.Contents[2]; Color _incandescence = _node.ChemicalContainer.GetIncandescence(); _lightColor.r = (byte)Mathf.Max(_lightColor.r, _incandescence.r * 255.0f); _lightColor.g = (byte)Mathf.Max(_lightColor.g, _incandescence.g * 255.0f); _lightColor.b = (byte)Mathf.Max(_lightColor.b, _incandescence.b * 255.0f); _lightColor.a = (byte)Mathf.Max(_lightColor.a, _incandescence.a * 255.0f); } _node.SetLighting(_lightColor); } for (int i = 0; i < wallNodesToUpdate.Count; i++) { Node _node = GameGrid.GetInstance().TryGetNode(wallNodesToUpdate[i]); _node.SetLightingBasedOnNeighbors(); } nodesToUpdate.Clear(); }