private void OnMouseDown() { //If this square contains a block that isn't carrying energy, clicking on the square will remove the block and cause it to follow the cursor. if (NodeBlock != null && Battle_Manager.liftedBlock == null && !NodeBlock.HasEnergy) { Battle_Manager.liftedBlock = NodeBlock; NodeBlock.Placed = false; NodeBlock.GetComponent <SpriteRenderer>().sortingOrder = 6; NodeBlock = null; //Doing this deselects any selected Action. if (Battle_Manager.selectedAction != null) { Battle_Manager.selectedAction.Deselect(); Battle_Manager.selectedAction = null; } } //If this square is empty and a block is following the cursor, place the block onto this square. else if (NodeBlock == null && Battle_Manager.liftedBlock != null) { NodeBlock = Battle_Manager.liftedBlock; NodeBlock.Placed = true; NodeBlock.GridPosition = GridPosition; Battle_Manager.liftedBlock = null; NodeBlock.transform.position = transform.position; NodeBlock.GetComponent <SpriteRenderer>().sortingOrder = 4; } }
//Compare each pulse with the block positioned above it (in the bottom row of the grid) //If the above block is a source node, create energy at that block. public void CreateEnergy(Pulse pulse) { GameObject[,] currentNodeGrid = Grid.nodeGrid; int pulseColumn = pulse.CurrentGridColumn; if (currentNodeGrid[pulseColumn, 4] != null) { Node_Block currentBlock = currentNodeGrid[pulseColumn, 4].GetComponent <Node_Block>(); //Proceed if the block is a source node and doesn't already contain energy if (currentBlock.IsSource && currentBlock.HasEnergy == false) { //TODO: Instantiate energy prefab, set position to correct block and set that block's hasEnergy and possessedEnergy GameObject newEnergyObject = Instantiate(energyObject, currentBlock.transform.position, Quaternion.identity); newEnergyObject.transform.position += new Vector3(0, -0.09f, 0); Energy newEnergy = newEnergyObject.GetComponent <Energy>(); newEnergy.GridPosition = new Vector2(pulseColumn, 4); currentBlock.PossessedEnergy = newEnergy; currentBlock.HasEnergy = true; Debug.Log("Creating energy in column " + pulseColumn); } } }
//Save data on each block in the grid. public void PopulateSaveData(Save_Data a_SaveData) { for (int i = 0; i < 20; i++) { for (int j = 0; j < 5; j++) { Debug.Log("Attempting block [" + i + ", " + j + "]"); Debug.Log(nodeGrid[i, j]); if (nodeGrid[i, j] != null) { //Pass necessary data from the block to the save data, then save it. Node_Block blockScript = nodeGrid[i, j].GetComponent <Node_Block>(); Save_Data.BlockData blockData = new Save_Data.BlockData(); blockData.path = blockScript.BlockPath; blockData.gridPosition = blockScript.GridPosition; blockData.rotation = blockScript.BlockRotation; a_SaveData.m_AllBlockData.m_BlockData[5 * i + j] = blockData; Debug.Log("[" + i + ", " + j + "] Saved block to data!"); a_SaveData.m_Score = 4; } //Set default data for empty squares. else { Save_Data.BlockData blockData = new Save_Data.BlockData(); blockData.path = ""; blockData.gridPosition = new Vector2(i, j); blockData.rotation = 0; a_SaveData.m_AllBlockData.m_BlockData[5 * i + j] = blockData; } } } }
//Remove blocks from all squares and delete those blocks. public void ClearAllBlocks() { for (int i = 0; i < 20; i++) { for (int j = 0; j < 5; j++) { //Remove reference to the block in the corresponding square and delete the block. Node_Block clearingBlock = squareGrid[i, j].GetComponent <Grid_Square>().NodeBlock; if (clearingBlock != null) { squareGrid[i, j].GetComponent <Grid_Square>().NodeBlock = null; Destroy(clearingBlock.gameObject); } } } }
//Send energy from one block to another. //This is only called after compatibility between the two blocks has been confirmed. public void SendEnergy(Node_Block sendingBlock, Node_Block receivingBlock, string entrance) { receivingBlock.PossessedEnergy = sendingBlock.PossessedEnergy; sendingBlock.PossessedEnergy = null; receivingBlock.HasEnergy = true; sendingBlock.HasEnergy = false; //Specify the entrance used by the energy for future animations and sends if (entrance == "A") { receivingBlock.ExitDirection = receivingBlock.EnterDirectionB; } else if (entrance == "B") { receivingBlock.ExitDirection = receivingBlock.EnterDirectionA; } else { Debug.Log("Entrance not specified. This isn't supposed to be possible..."); } }
//Transfer energy from this block to another that has been found and approved by AttemptSend public void AttemptSend(Node_Block targetBlock) { //Debug.Log("Target block exists, really attempting send..."); if (!targetBlock.HasEnergy) { if (targetBlock.EnterDirectionA == (Directions)(((int)ExitDirection + 2) % 4)) //Results in opposite direction from ExitDirection. { if (!targetBlock.IsSource && !targetBlock.IsReceiver) //Sources and receivers don't have dynamic ExitDirections. { targetBlock.ExitDirection = targetBlock.EnterDirectionB; } targetBlock.HasEnergy = true; targetBlock.PossessedEnergy = PossessedEnergy; PossessedEnergy.EnteredNewBlock(); PossessedEnergy = null; HasEnergy = false; } else if (targetBlock.EnterDirectionB == (Directions)(((int)ExitDirection + 2) % 4) && !targetBlock.IsSource && !targetBlock.IsReceiver) //Sources and receivers don't have a second EnterDirection. { targetBlock.ExitDirection = targetBlock.EnterDirectionA; targetBlock.HasEnergy = true; targetBlock.PossessedEnergy = PossessedEnergy; PossessedEnergy.EnteredNewBlock(); PossessedEnergy = null; HasEnergy = false; } else { //Send fails if targetBlock doesn't have the corresponding EnterDirection. DeleteEnergy(); } } else { //Send fails if targetBlock already has energy. DeleteEnergy(); } }
// Update is called once per frame void Update() { UpdatePulseCycle(); //End battle in victory if there are no enemies left, or defeat if the player's health reaches 0. //This currentyl results in infinite pulses being generated over time with nowhere to move. if (enemies.Count == 0) { pulseTravelSpeed = 0; foreach (GameObject enemy in enemies) { enemy.SetActive(false); } victoryText.SetActive(true); } else if (player.currentHealth <= 0) { pulseTravelSpeed = 0; foreach (GameObject enemy in enemies) { enemy.SetActive(false); } defeatText.SetActive(true); } //If an enemy is selected after an action, send the action to that enemy. //Otherwise, clear the selected enemy. if (selectedEnemy != null) { if (selectedAction != null) { Debug.Log("Enemy thas been targeted and attacked."); selectedAction.TargetEnemy(selectedEnemy); } else { selectedEnemy = null; } } //Right mouse button functions. if (Input.GetMouseButtonDown(1)) { //Rotate the lifted block. if (liftedBlock != null) { if (liftedBlock.HasEnergy == false) { liftedBlock.RotateClockwise(); } } //Deselect any selected action. if (selectedAction != null) { selectedAction.Deselect(); selectedAction = null; } } //Update the node grid whenever a block is placed or removed. if (lastLiftedBlock != liftedBlock) { grid.GetComponent <Grid>().PopulateNodeGrid(); } if (testSave == true) { grid.GetComponent <Grid>().PopulateSaveData(gridSaveData); SaveJsonData(this); testSave = false; } if (testLoad == true) { LoadJsonData(this); testLoad = false; } lastLiftedBlock = liftedBlock; }
//Determine whether a valid block exists to receive energy from a block that currently possesses energy //The receiving block must have an EnterDirection that is opposite to the sending block's ExitDirection. (left -> right, top -> bottom, etc) public void AttemptSend(Node_Block sendingBlock) { Node_Block receivingBlock; //If the sending block has an ExitDirection of up... if (sendingBlock.ExitDirection == Node_Block.Directions.Up) { //Set the receiving block to the block located above the sendingBlock in the nodeGrid. receivingBlock = nodeGrid[(int)sendingBlock.GridPosition.x, (int)sendingBlock.GridPosition.y - 1].GetComponent <Node_Block>(); //Check that this receivingBlock exists. (It may not if the square has been left empty or the sendingBlock has been placed on an edge.) if (receivingBlock != null) { //Finally, check whether the receivingBlock's EnterDirection is compatible with the sendingBlock's ExitDirection. if (receivingBlock.EnterDirectionA == Node_Block.Directions.Down) { SendEnergy(sendingBlock, receivingBlock, "A"); } else if (receivingBlock.EnterDirectionB == Node_Block.Directions.Down) { SendEnergy(sendingBlock, receivingBlock, "B"); } else { Debug.Log("Invalid entrance direction. Send failed."); } } else { Debug.Log("Target block does not exist. Send failed."); } } else if (sendingBlock.ExitDirection == Node_Block.Directions.Right) { receivingBlock = nodeGrid[(int)sendingBlock.GridPosition.x + 1, (int)sendingBlock.GridPosition.y].GetComponent <Node_Block>(); if (receivingBlock != null) { if (receivingBlock.EnterDirectionA == Node_Block.Directions.Left) { SendEnergy(sendingBlock, receivingBlock, "A"); } else if (receivingBlock.EnterDirectionB == Node_Block.Directions.Left) { SendEnergy(sendingBlock, receivingBlock, "B"); } else { Debug.Log("Invalid entrance direction. Send failed."); } } else { Debug.Log("Target block does not exist. Send failed."); } } else if (sendingBlock.ExitDirection == Node_Block.Directions.Down) { receivingBlock = nodeGrid[(int)sendingBlock.GridPosition.x, (int)sendingBlock.GridPosition.y + 1].GetComponent <Node_Block>(); if (receivingBlock != null) { if (receivingBlock.EnterDirectionA == Node_Block.Directions.Up) { SendEnergy(sendingBlock, receivingBlock, "A"); } else if (receivingBlock.EnterDirectionB == Node_Block.Directions.Up) { SendEnergy(sendingBlock, receivingBlock, "B"); } else { Debug.Log("Invalid entrance direction. Send failed."); } } else { Debug.Log("Target block does not exist. Send failed."); } } else if (sendingBlock.ExitDirection == Node_Block.Directions.Left) { receivingBlock = nodeGrid[(int)sendingBlock.GridPosition.x - 1, (int)sendingBlock.GridPosition.y].GetComponent <Node_Block>(); if (receivingBlock != null) { if (receivingBlock.EnterDirectionA == Node_Block.Directions.Right) { SendEnergy(sendingBlock, receivingBlock, "A"); } else if (receivingBlock.EnterDirectionB == Node_Block.Directions.Right) { SendEnergy(sendingBlock, receivingBlock, "B"); } else { Debug.Log("Invalid entrance direction. Send failed."); } } else { Debug.Log("Target block does not exist. Send failed."); } } }