public void HandleOnSwitchChanged(bool value) { PipePiece pipe = GetComponent <PipePiece>(); foreach (ChangePipeEvent e in changePipeEvents) { if (value && e.addOrRemoveConnection == ChangePipeEvent.AddRemove.ADD || !value && e.addOrRemoveConnection == ChangePipeEvent.AddRemove.REMOVE) { if (e.makePad) { pipe.isPad = true; } pipe.AddConnection((int)e.direction); } else { if (e.makePad) { pipe.isPad = false; } pipe.RemoveConnection((int)e.direction); } } pipe.UpdateRender(); }
private void CreatePipe(int direction) { // Get the current selected pipe PipePiece connection = _targetPipe.AddConnection(direction); // Check to see if the connection succeeded if (connection == null) { // If it failed, Create a new pipe at the location, and try connecting again // calculate the location of the new pipe Vector2 pos = _targetPipe.transform.position; Vector2 nextPos = pos; if (direction == 0) { nextPos += Vector2.up; } else if (direction == 1) { nextPos += Vector2.right; } else if (direction == 2) { nextPos += Vector2.down; } else if (direction == 3) { nextPos += Vector2.left; } else { Debug.LogError("Invalid Direction"); return; } PipePiece prefab = Resources.Load <PipePiece>("Prefabs/pipe"); connection = PrefabUtility.InstantiatePrefab(prefab, _targetPipe.transform.parent) as PipePiece; if (connection == null) { return; } connection.transform.position = nextPos; // Hypothetically should not fail now connection = _targetPipe.AddConnection(direction); } // Set the next pipe to be the selected game object if (connection != null) { Selection.activeGameObject = connection.gameObject; } if (connection != null) { connection.UpdateRender(); EditorUtility.SetDirty(connection); } }
public static void FixPipes() { PipePiece.CalculateNeighborsOfAllPipes(); foreach (PipePiece p in FindObjectsOfType <PipePiece>()) { p.UpdateRender(); EditorUtility.SetDirty(p); } }
private void RemoveConnection(int direction) { PipePiece connection = _targetPipe.RemoveConnection(direction); if (connection != null) { connection.UpdateRender(); EditorUtility.SetDirty(connection); } }
void OnEnable() { _targetPipe = (PipePiece)target; _isPadProperty = serializedObject.FindProperty("isPad"); _pipeNProperty = serializedObject.FindProperty("pipeN"); _pipeEProperty = serializedObject.FindProperty("pipeE"); _pipeSProperty = serializedObject.FindProperty("pipeS"); _pipeWProperty = serializedObject.FindProperty("pipeW"); }
public PipePiece RemoveConnection(int direction) { PipePiece connection = GetPipe(direction); if (connection == null) { return(null); } if (direction == 0) { pipeN = null; } else if (direction == 1) { pipeE = null; } else if (direction == 2) { pipeS = null; } else if (direction == 3) { pipeW = null; } int invertDirection = (direction + 2 + 4) % 4; if (invertDirection == 0) { connection.pipeN = null; } else if (invertDirection == 1) { connection.pipeE = null; } else if (invertDirection == 2) { connection.pipeS = null; } else if (invertDirection == 3) { connection.pipeW = null; } UpdateRender(); connection?.UpdateRender(); return(connection); }
private IEnumerator OnDead(Vector3 direction) { Destroy(GetComponent <PlayerInput>()); _onPipe = null; _movingOnPipe = false; transform.Find("Body").gameObject.SetActive(false); transform.Find("Dead").gameObject.SetActive(true); const float launchSpeed = 15f; const float friction = 50f; float speed = launchSpeed; int rotateDirection = Random.value > 0.5f ? 1 : -1; const float rotateSpeed = 180; while (true) { speed = Mathf.MoveTowards(speed, 0, friction * Time.deltaTime); if (speed <= 0) { break; } transform.position += direction * speed * Time.deltaTime; transform.Rotate(Vector3.forward, rotateSpeed * rotateDirection * Time.deltaTime); yield return(null); } yield return(new WaitForSeconds(2.25f)); GameManager.ReloadLevel(); }
public void Move(Vector3 input) { if (Input.GetKeyDown(KeyCode.Space)) { if (_onPipe == null) { PipePiece[] pipes = FindObjectsOfType <PipePiece>(); foreach (PipePiece pipe in pipes) { if (!pipe.isPad) { continue; } Vector2 displacement = pipe.transform.position - transform.position; const float maxDistance = 1f; if (displacement.sqrMagnitude <= maxDistance * maxDistance) { transform.position = pipe.transform.position; _onPipe = pipe; _velocity = Vector3.zero; transform.Find("Body").rotation = Quaternion.AngleAxis(_shakeValue = 0, Vector3.forward); FindObjectOfType <CameraController>().AdjustZoomTOverTime(1, 0.75f); IgnoreCollisionsWithWorld(true); break; } } } else { if (!_movingOnPipe) { _onPipe = null; FindObjectOfType <CameraController>().AdjustZoomTOverTime(0, 0.75f); IgnoreCollisionsWithWorld(false); } } } if (_onPipe != null) { if (!_movingOnPipe) { if (input.sqrMagnitude >= 0.1f) { // convert input to a direction int dir; if (Mathf.Abs(input.x) > Mathf.Abs(input.y)) // horizontal { dir = input.x > 0 ? 1 : 3; } else { dir = input.y > 0 ? 0 : 2; } PipePiece nextPiece = _onPipe.GetPipe(dir); if (nextPiece != null) { _movingOnPipe = true; _onPipe = nextPiece; _pipeMoveDirection = dir; } } } else { Vector3 pos = transform.position; Vector3 targetPos = _onPipe.transform.position; Vector3 delta = targetPos - pos; Vector3 velocity = delta.normalized * pipeSpeed; Vector3 projectedInput = Vector3.Project(input, velocity); velocity += projectedInput * pipeSpeedModifier; pos = Vector3.MoveTowards(pos, targetPos, velocity.magnitude * Time.deltaTime); transform.position = pos; if (delta.sqrMagnitude <= 0.001f) { if (_onPipe.isPad) { _movingOnPipe = false; } else { bool foundNextPipe = false; // Get the next pipe for (int i = 0; i < 4; i++) { int dir = (_pipeMoveDirection + i) % 4; if (i == 2 && _onPipe.GetPipeType != PipePiece.PipeType.END) { continue; } PipePiece nextPiece = _onPipe.GetPipe(dir); if (nextPiece != null) { foundNextPipe = true; _onPipe = nextPiece; _pipeMoveDirection = dir; break; } } if (!foundNextPipe) { _onPipe = null; _movingOnPipe = false; Debug.Log("Error? did not find a pipe piece"); } } } } } else { // Swaying while walking float targetSway = (input.sqrMagnitude < 0.1) ? 0 : maxTilt; _shakeValue = Mathf.MoveTowards(_shakeValue, targetSway, acceleration * Time.deltaTime); float angle = Mathf.Sin(Time.time * tiltPerSecond) * _shakeValue; transform.Find("Body").rotation = Quaternion.AngleAxis(angle, Vector3.forward); // Movement code Vector3 targetVelocity = (input.Equals(Vector3.zero) ? Vector3.zero : input) * walkSpeed; _velocity = Vector3.MoveTowards(_velocity, targetVelocity, acceleration * Time.deltaTime); transform.position = transform.position + _velocity * Time.deltaTime; } }
public PipePiece AddConnection(int direction) { // Find the location of the next pipe Vector2 pos = transform.position; Vector2 nextPos = pos; if (direction == 0) { nextPos += Vector2.up; } else if (direction == 1) { nextPos += Vector2.right; } else if (direction == 2) { nextPos += Vector2.down; } else if (direction == 3) { nextPos += Vector2.left; } else { Debug.LogError("Invalid Direction"); return(null); } // Determine if a pipe exists at this position; PipePiece connection = null; foreach (PipePiece p in FindObjectsOfType <PipePiece>()) { if (p == this) { continue; } Vector2 pipePos = p.transform.position; Vector2 displacement = pipePos - nextPos; if (displacement.sqrMagnitude < 0.01f) // close enough to be the at the new location { connection = p; break; } } // If there is no pipe at the next location, return a null reference if (connection == null) { return(null); } // Join the connection with this pipe if (direction == 0) { pipeN = connection; connection.pipeS = this; } else if (direction == 1) { pipeE = connection; connection.pipeW = this; } else if (direction == 2) { pipeS = connection; connection.pipeN = this; } else if (direction == 3) { pipeW = connection; connection.pipeE = this; } UpdateRender(); connection?.UpdateRender(); return(connection); }
public static void CalculateNeighborsOfAllPipes() { PipePiece[] pipes = FindObjectsOfType <PipePiece>(); foreach (PipePiece pipe in pipes) { for (int i = 0; i < 4; i++) { // Check to see if there is a connection PipePiece check = pipe.GetPipe(i); if (check == null) { continue; } // Check to see if the connection is both ways int invertDirection = (i + 2 + 4) % 4; if (check.GetPipe(invertDirection) != pipe) { // Fix the connection if (i == 0) { pipe.pipeN = check; check.pipeS = pipe; } else if (i == 1) { pipe.pipeE = check; check.pipeW = pipe; } else if (i == 2) { pipe.pipeS = check; check.pipeN = pipe; } else if (i == 3) { pipe.pipeW = check; check.pipeE = pipe; } } #region Old Testing Code // else, see if we can find a pipe in the right location //Vector3 pos = pipe.transform.position; //Vector3 checkLocation = pos; //if (i == 0) // checkLocation += Vector3.up; //else if (i == 1) // checkLocation += Vector3.right; //else if (i == 2) // checkLocation += Vector3.down; //else if (i == 3) // checkLocation += Vector3.left; //foreach (PipePiece comparePipe in pipes) //{ // if (comparePipe == pipe) // continue; // Vector3 displacement = comparePipe.transform.position - checkLocation; // if (displacement.sqrMagnitude < 0.01f) // close enough to be the at the new location // { // // Join the nextPipe with this pipe // if (i == 0) // { // pipe.pipeN = comparePipe; // comparePipe.pipeS = pipe; // } // else if (i == 1) // { // pipe.pipeE = comparePipe; // comparePipe.pipeW = pipe; // } // else if (i == 2) // { // pipe.pipeS = comparePipe; // comparePipe.pipeN = pipe; // } // else if (i == 3) // { // pipe.pipeW = comparePipe; // comparePipe.pipeE = pipe; // } // break; // } //} #endregion } } }