public void SetElectron(int x, int y, GamePieceData electron) { if (OnBoard(x, y)) { m_electronPositions[x + y * m_width] = electron; } }
void UpdateElecDiagonal(Vector2 piecePos, GamePiece piece, GamePieceData electron) { // // Diagonal checks helper tables // Vector2[] kDirectionToPossibleWires = { // corners: north-east, south-east, south-west, north-west new Vector2(0.0f, 3.0f), new Vector2(0.0f, 1.0f), new Vector2(1.0f, 2.0f), new Vector2(2.0f, 3.0f) }; Vector2[] kWireToDirections = { // corners: north-east, south-east, south-west, north-west new Vector2(0.0f, 1.0f), new Vector2(1.0f, 2.0f), new Vector2(2.0f, 3.0f), new Vector2(3.0f, 0.0f) }; for (int i = 0; i < 4; ++i) { // If electron is at position if (electron[i] != 0) { // For each possible diagonal for (int j = 0; j < 2; ++j) { // if diagonal exists int wireTest = (int)kDirectionToPossibleWires[i][j]; if (piece.d.m_wire[wireTest + 4] != 0) { // There is a wire! Hurray. Now to get the output direction... if (electron[4] != 0) { // If center electron is set, output to both directions outputToDirection(piecePos, (int)kWireToDirections[wireTest][0]); outputToDirection(piecePos, (int)kWireToDirections[wireTest][1]); } else { // Output to one direction only int outputDirection = (int)kWireToDirections[wireTest][0]; if (outputDirection == i) { outputDirection = (int)kWireToDirections[wireTest][1]; } outputToDirection(piecePos, outputDirection); } } } } } }
// Update electron movements on non diagonal tiles void UpdateElecSimple(Vector2 piecePos, GamePiece piece, GamePieceData electron) { for (int i = 0; i < 5; ++i) { // if electron and wire exist in position, or center electron and any wire (center is an exception) if (electron[i] != 0 && (i == 4 || piece.d.m_wire[i] != 0)) { for (int j = 0; j < 4; ++j) { // make sure electron isn't going backwards if (i != j && piece.d.m_wire[j] != 0) { // output to new tile outputToDirection(piecePos, j); } } } } }
// Use this for initialization void Start() { m_board = new GamePiece[m_width * m_height]; m_electricityAnimations = new ElectricityAnimationData[m_width * m_height]; m_electronPositions = new GamePieceData[m_width * m_height]; for (int i = 0; i < (m_width * m_height); ++i) { m_electronPositions[i] = new GamePieceData(); } // Get premade pieces SnapePiecesFromTransforms(); GamePiece[] pArr = GetComponentsInChildren <GamePiece> (); for (int i = 0; i < pArr.Length; i++) { if (Place(pArr[i], pArr[i].d.x, pArr[i].d.y) == false) { Destroy(pArr[i].gameObject); // Destroy on fail } } }
public void OnElectricTrigger() { GameBoard gameBoard = GetGameBoard(); if (gameBoard != null) { Debug.Log("Diode"); int transformedElectronEntry = (d.r / 90 + m_allowedElectronEntry) % 4; GamePieceData electron = gameBoard.GetElectron(d.x, d.y); Debug.Log(transformedElectronEntry.ToString()); for (int i = 0; i < 5; ++i) { // Remove all unallowed electrons! :) if (i != transformedElectronEntry) { electron[i] = 0; } gameBoard.SetElectron(d.x, d.y, electron); } } }
public void OnElectricTrigger() { GameBoard gameBoard = GetGameBoard(); if (gameBoard != null) { int transformedElectronPosition = (d.r / 90 + m_RelayControlElectronPosition) % 4; GamePieceData electron = gameBoard.GetElectron(d.x, d.y); if (electron[transformedElectronPosition] == 1) { Debug.Log("Toggling relay"); m_RelayActive = !m_RelayActive; } int electronPath0 = (transformedElectronPosition - 1) % 4; int electronPath1 = (transformedElectronPosition + 1) % 4; Debug.Log(electronPath0.ToString() + "," + electronPath1.ToString()); for (int i = 0; i < 5; ++i) { if (m_RelayActive) { if (i != electronPath0 && i != electronPath1) { electron[i] = 0; } } else { electron[i] = 0; } } gameBoard.SetElectron(d.x, d.y, electron); } }
// Update electrons logic (movement and event trigger to tiles for activation) void TickElec() { // Copy the data to avoid working on an updated tile GamePieceData[] electronPositionsCopy = m_electronPositions.Clone() as GamePieceData[]; // Zero the main data for (int i = 0; i < (m_width * m_height); ++i) { m_electronPositions[i] = GamePieceData.zero; } // Pos variable Vector2 piecePos = new Vector2(); for (int x = 0; x < m_width; ++x) { for (int y = 0; y < m_height; ++y) { // Set the pos variable piecePos.x = x; piecePos.y = y; // Get the piece and electron GamePiece piece = m_board[x + y * m_width]; GamePieceData electron = electronPositionsCopy[x + y * m_width]; // Safety check piece actually exists if (piece == null) { // No piece, reset electron for (int i = 0; i < 5; ++i) { electron[i] = 0; } continue; } // Process Simple directions (non-diagonals) UpdateElecSimple(piecePos, piece, electron); // Process Diagonal directions UpdateElecDiagonal(piecePos, piece, electron); } } // Trigger all game pieces with new electrons on them for (int i = 0; i < (m_width * m_height); ++i) { if (m_board[i] != null) // safety { GamePieceData elec = m_electronPositions[i]; for (int j = 0; j < 4; ++j) { // if any electron at all, send trigger once if (elec[j] != 0) { m_board[i].SendMessage("OnElectricTrigger"); break; } } } } }