private void MoveBlockToPosition(EnergyBlock block, Vector3 position) { //block.transform.position = position; // TODO - change to LeanTween movement LeanTween.move(block.gameObject, position, blockMoveTime) .setEase(blockMoveEasing) .setOnComplete(OnBlockMoveEnd); }
private void CheckBlockValid(EnergyBlock block, int[] validConnections) { if (block != null && !block.IsConnected && block.IsActive && CollectionContains(validConnections, block.BlockType)) { block.Connect(); UpdateConnections(block); } }
private void UpdateConnections(EnergyBlock block) { if (block == startBlock) { EnergyBlock rightBlock = GetBlockAtPosition(Vector2Int.zero); if (rightBlock != null && CollectionContains(startBlock.ValidConnectionsRight, rightBlock.BlockType)) { rightBlock.Connect(); UpdateConnections(rightBlock); } } else { EnergyBlock leftBlock = GetBlockAtPosition(block.GetCoordinates() + Vector2Int.left); EnergyBlock upBlock = GetBlockAtPosition(block.GetCoordinates() + Vector2Int.down); EnergyBlock rightBlock = GetBlockAtPosition(block.GetCoordinates() + Vector2Int.right); EnergyBlock downBlock = GetBlockAtPosition(block.GetCoordinates() + Vector2Int.up); if (block.BlockType == 1) { CheckBlockValid(leftBlock, block.ValidConnectionsLeft); CheckBlockValid(rightBlock, block.ValidConnectionsRight); } else if (block.BlockType == 2) { CheckBlockValid(upBlock, block.ValidConnectionsUp); CheckBlockValid(downBlock, block.ValidConnectionsDown); } else if (block.BlockType == 3) { CheckBlockValid(leftBlock, block.ValidConnectionsLeft); CheckBlockValid(upBlock, block.ValidConnectionsUp); } else if (block.BlockType == 4) { CheckBlockValid(upBlock, block.ValidConnectionsUp); CheckBlockValid(rightBlock, block.ValidConnectionsRight); } else if (block.BlockType == 5) { CheckBlockValid(rightBlock, block.ValidConnectionsRight); CheckBlockValid(downBlock, block.ValidConnectionsDown); } else if (block.BlockType == 6) { CheckBlockValid(leftBlock, block.ValidConnectionsLeft); CheckBlockValid(downBlock, block.ValidConnectionsDown); } } }
private EnergyBlock GetFreeBlock(EnergyBlock block, Vector2Int blockCoordinates) { // check left int leftIndex = blockCoordinates.x - 1; if (leftIndex >= 0 && leftIndex < gridSizeX) { if (!blockGrid[leftIndex, blockCoordinates.y].IsActive) { return(blockGrid[leftIndex, blockCoordinates.y]); } } // check up int upIndex = blockCoordinates.y + 1; if (upIndex >= 0 && upIndex < gridSizeY) { if (!blockGrid[blockCoordinates.x, upIndex].IsActive) { return(blockGrid[blockCoordinates.x, upIndex]); } } // check right int rightIndex = blockCoordinates.x + 1; if (rightIndex >= 0 && rightIndex < gridSizeX) { if (!blockGrid[rightIndex, blockCoordinates.y].IsActive) { return(blockGrid[rightIndex, blockCoordinates.y]); } } // check down int downIndex = blockCoordinates.y - 1; if (downIndex >= 0 && downIndex < gridSizeY) { if (!blockGrid[blockCoordinates.x, downIndex].IsActive) { return(blockGrid[blockCoordinates.x, downIndex]); } } return(null); }
private void SwapBlocks(EnergyBlock blockToMove, EnergyBlock swapBlock) { isBusy = true; blockGrid[blockToMove.GetCoordinates().x, blockToMove.GetCoordinates().y] = swapBlock; blockGrid[swapBlock.GetCoordinates().x, swapBlock.GetCoordinates().y] = blockToMove; Vector3 tempPosition = blockToMove.transform.position; MoveBlockToPosition(blockToMove, swapBlock.transform.position); MoveBlockToPosition(swapBlock, tempPosition); Vector2Int tempCoordinates = blockToMove.GetCoordinates(); blockToMove.SetCoordinates(swapBlock.GetCoordinates()); swapBlock.SetCoordinates(tempCoordinates); }
public override void OnComponentInteract(BaseComponent component, object data = null) { base.OnComponentInteract(component); if (isBusy) { return; } Vector2Int blockCoordinates = (Vector2Int)data; EnergyBlock blockToMove = blockGrid[blockCoordinates.x, blockCoordinates.y]; EnergyBlock freeBlock = GetFreeBlock(blockGrid[blockCoordinates.x, blockCoordinates.y], blockCoordinates); //print(freeBlock); if (freeBlock) { SwapBlocks(blockToMove, freeBlock); } }
private void updateEnergyBlock(EnergyBlock energyBlock) { String[] lines = energyBlock.getBlock().DetailedInfo.Split('\n'); foreach (EnergyProperty energyProperty in energyBlock.getblockType().EnergyProperties) { ulong value = parsePower(lines[energyProperty.Position].Substring(energyProperty.EnergyType.getKey().Length + 1)); //program.Echo("value: " + value); switch (energyProperty.EnergyType.getIndex()) { case 0: energyBlock.CurrentOutput = value; break; case 1: energyBlock.CurrentInput = value; break; case 2: energyBlock.StoredPower = value; break; case 3: energyBlock.MaxStoredPower = value; break; case 4: energyBlock.MaxOutput = value; break; case 5: energyBlock.RequiredInput = value; break; default: break; } //program.Echo(energyBlock.ToString()); } }
public override EquipmentBlock ReadJson( JsonReader reader, Type objectType, EquipmentBlock existingValue, bool hasExistingValue, JsonSerializer serializer) { var obj = JObject.Load(reader); var type = obj[nameof(EquipmentBlock.Type)].ToObject <EquipmentType>(); EquipmentBlock equipmentBlock; switch (type) { case EquipmentType.Energy: equipmentBlock = new EnergyBlock(); break; case EquipmentType.Gun: equipmentBlock = new GunBlock(); break; case EquipmentType.Engine: equipmentBlock = new EngineBlock(); break; case EquipmentType.Health: equipmentBlock = new HealthBlock(); break; default: throw new NotSupportedException($"Unknown equipment {obj}"); } serializer.Populate(obj.CreateReader(), equipmentBlock); return(equipmentBlock); }
public int initBlocks() { DateTime dateTime = DateTime.Now; //program.Echo("init blocks"); List <IMyFunctionalBlock> allBlocks = new List <IMyFunctionalBlock>(); program.GridTerminalSystem.GetBlocksOfType <IMyFunctionalBlock>(allBlocks); if (allBlocks.Count == scannedBlocks) { return(0); } reactors.Clear(); batteries.Clear(); solarPanels.Clear(); consumer.Clear(); powerBlocks.Clear(); foreach (IMyTerminalBlock block in allBlocks) { BlockType blockType; if (!blockTypes.ContainsKey(block.BlockDefinition.TypeIdString)) { blockType = initBlockType(block); blockTypes.Add(blockType.Typ, blockType); } else { try { blockTypes.TryGetValue(block.BlockDefinition.TypeIdString, out blockType); } catch (ArgumentNullException ex) { program.Echo("Error while getting block Type: " + ex.Message); continue; } } if (!blockType.Consumer && !blockType.Producer) { continue; } //program.Echo("Block type: " + blockType.Typ); EnergyBlock energyBlock; energyBlock = new EnergyBlock(blockType, block); powerBlocks.Add(energyBlock); updateEnergyBlock(energyBlock); if (BATTERY_TYPE_ID.Equals(blockType.Typ)) { //program.Echo("Block type: " + blockType.Typ); batteries.Add(energyBlock); } else if (blockType.Consumer) { //program.Echo("Block type: " + blockType.Typ); consumer.Add(energyBlock); } else if (REACTOR_TYPE_ID.Equals(blockType.Typ)) { //program.Echo("Block type: " + blockType.Typ); reactors.Add(energyBlock); } else if (SOLAR_PANEL_TYPE_ID.Equals(blockType.Typ)) { //program.Echo("Block type: " + blockType.Typ); reactors.Add(energyBlock); } else if (ENGINE_TYPE_ID.Equals(blockType.Typ)) { engines.Add(energyBlock); } } int newBlocks = allBlocks.Count - scannedBlocks; scannedBlocks = allBlocks.Count; //program.Echo(DateTime.Now.Subtract(dateTime).Milliseconds + ""); return(newBlocks); }