public void SelectNewProxyObject(GameObject newProxy) { //if the current proxy object is null, the new proxy object becomes the current proxy object if (!proxyObject) { proxyObject = newProxy; CursorScript.CreateNewCursor(proxyObject.transform.position, cursorPrefab); } else { float currentDistance = (proxyObject.transform.position - transform.position).magnitude; float newDistance = (newProxy.transform.position - transform.position).magnitude; if (currentDistance < newDistance) { proxyObject = newProxy; CursorScript.CreateNewCursor(proxyObject.transform.position, cursorPrefab); } } //select the correct type of proxy object switch (proxyObject.tag.ToUpper()) { case "DOOR": proxyType = ProxyObjectType.DOOR; break; case "LEVER": proxyType = ProxyObjectType.LEVER; break; case "TELEPORTER": proxyType = ProxyObjectType.TELEPORTER; break; case "GRABPOINT": proxyType = ProxyObjectType.GRABPOINT; break; default: proxyType = ProxyObjectType.NONE; break; } }
// Update is called once per frame void Update() { //if the level selection popup is not shown if (!showLevelWindow) { //check if the mouse has been moved bool mouseMoved = (previousMousePosition != Input.mousePosition); //if the mouse has been moved if (mouseMoved) { bool selectedChanged = false; //get the colliders at the mouse Collider2D[] points = Physics2D.OverlapPointAll(Camera.main.ScreenToWorldPoint(Input.mousePosition)); //iterate through the colliders Collider2D country = null; foreach (Collider2D point in points) { //if the collider is a country if (point.tag == "Country") { //if this country is different from the selected one, select that country country = point; if (point.GetComponent <MenuCountryScript>().countryName != selectedCountry) { selectedCountry = point.GetComponent <MenuCountryScript>().countryName; selectedChanged = true; } //break out of the loop break; } } //if a country is selected if (country) { MenuCountryScript countryScript = country.gameObject.GetComponent <MenuCountryScript>(); //if the selection has changed, create a new cursor if (selectedChanged) { CursorScript.CreateNewCursor(countryScript.cursorPoint, cursorPrefab); selectedChanged = false; } } else { //if no country is selected, remove the cursor and set the selected country to none CursorScript.RemoveCursors(); selectedCountry = MenuCountryScript.CountryName.NONE; } } //if the mouse has been clicked if (Input.GetMouseButtonDown(0) && selectedCountry != MenuCountryScript.CountryName.NONE) { //open the level selection pop up switch (selectedCountry) { case MenuCountryScript.CountryName.USA: showLevelWindow = true; break; case MenuCountryScript.CountryName.RUSSIA: showLevelWindow = true; break; default: break; } } } else { if (Input.GetMouseButtonDown(0)) { if (Input.mousePosition.x > windowRect.x + windowRect.size.x || Input.mousePosition.x < windowRect.x || Input.mousePosition.y > windowRect.y + windowRect.size.y || Input.mousePosition.y < windowRect.y) { showLevelWindow = false; } } } //set the previous mouse position previousMousePosition = Input.mousePosition; }
public void UpdateInteraction() { //allow for player interactions if (Input.GetButtonDown("Action")) { //if the player is at a teleporter if (proxyType == ProxyObjectType.TELEPORTER && !isHanging) { if (!animState.IsName("PhasingOut") && !animState.IsName("PhasingIn")) { //set the animation to phase out animator.SetBool("isPhasing", true); //prevent the player from moving isRooted = true; rigidbody2D.gravityScale = 0.0f; teleporterIndex = 0; teleportColour = proxyObject.GetComponent <TeleportNodeScript>().colour; audio.PlayOneShot(teleportOutClip); } } //if the player is at a door and is grounded else if (proxyType == ProxyObjectType.DOOR && isGrounded) { //move the player to the door's exit Vector2 newPosition = proxyObject.GetComponent <DoorScript>().exit.transform.position; transform.position = newPosition; } else if (proxyType == ProxyObjectType.LEVER && isGrounded) { //switch the lever proxyObject.GetComponent <LeverScript>().Switch(); } //if the player is at a grab point and is not grounded else if (proxyType == ProxyObjectType.GRABPOINT && !isGrounded) { if (!animState.IsName("PhasingOut") && !animState.IsName("PhasingIn")) { //if the player is hanging already, release from the grab point if (isHanging) { isRooted = false; //if targetting another grab point, set it to the destination if (targetedGrabPoint) { grabDestination = targetedGrabPoint; } else { //if not targetting a grab point, stop hanging animator.SetBool("isHanging", false); isHanging = false; rigidbody2D.gravityScale = gravityScale; //remove the old grabber GameObject oldGrabber = GameObject.FindGameObjectWithTag("Grabber"); if (oldGrabber) { Destroy(oldGrabber); } //remove the old cursor CursorScript.RemoveCursors(); } } else { //if not currently hanging, set the proxy grab point to the destination and start hanging velocity = Vector2.zero; rigidbody2D.velocity = Vector2.zero; rigidbody2D.gravityScale = 0.0f; grabDestination = proxyObject; animator.SetBool("isHanging", true); isHanging = true; } } } } //if the player is hanging if (isHanging) { //if not yet at the destination if ((Vector2)transform.position != (Vector2)grabDestination.GetComponent <GrabPointScript>().hangPoint.position) { //move towards the destination velocity = (Vector2)grabDestination.GetComponent <GrabPointScript>().hangPoint.position - (Vector2)transform.position; velocity.Normalize(); velocity *= maxHorizontalSpeed; if (((Vector2)transform.position - (Vector2)grabDestination.GetComponent <GrabPointScript>().hangPoint.position).magnitude < 0.5f) { transform.position = (Vector2)grabDestination.GetComponent <GrabPointScript>().hangPoint.position; } //remove the old grabber GameObject oldGrabber = GameObject.FindGameObjectWithTag("Grabber"); if (oldGrabber) { Destroy(oldGrabber); } //create a new grabber GameObject grabber = (GameObject)GameObject.Instantiate(grabberPrefab, transform.position, Quaternion.identity); LineRenderer lr = grabber.GetComponent <LineRenderer>(); lr.SetPosition(0, grabber.transform.position); lr.SetPosition(1, grabDestination.transform.position); lr.SetWidth(0.1f, 0.1f); grabber.tag = "Grabber"; } else { //if destination reached, root the player velocity = Vector2.zero; rigidbody2D.velocity = Vector2.zero; isRooted = true; } //if the player is pressing right if (Input.GetAxis("Horizontal") > 0.0f) { //look for a grab point to the right of the player Collider2D[] colliders = Physics2D.OverlapPointAll((Vector2)grabDestination.transform.position + new Vector2(5.0f, 0.0f)); targetedGrabPoint = null; facingDirection.x = 1.0f; FaceRight(true); if (mindSeedSpawnPosition.x <= 0.0f) { mindSeedSpawnPosition.x *= -1.0f; } foreach (Collider2D collider in colliders) { //if grab point found if (collider.tag == "GrabPoint") { //update the cursor CursorScript.CreateNewCursor(collider.transform.position, cursorPrefab); //set the grab point to the target if (targetedGrabPoint != collider.gameObject) { targetedGrabPoint = collider.gameObject; } } } } //if the player is pressing left else if (Input.GetAxis("Horizontal") < 0.0f) { //look for a grab point to the left of the player Collider2D[] colliders = Physics2D.OverlapPointAll((Vector2)transform.position + new Vector2(-5.0f, 0.0f)); targetedGrabPoint = null; facingDirection.x = -1.0f; FaceRight(false); if (mindSeedSpawnPosition.x >= 0.0f) { mindSeedSpawnPosition.x *= -1.0f; } foreach (Collider2D collider in colliders) { //if grab point found if (collider.tag == "GrabPoint") { //update the cursor CursorScript.CreateNewCursor(collider.transform.position, cursorPrefab); //set the grab point to the target if (targetedGrabPoint != collider.gameObject) { targetedGrabPoint = collider.gameObject; } } } } else { //remove the cursor CursorScript.RemoveCursors(); targetedGrabPoint = null; } } else { //remove the old grabber GameObject oldGrabber = GameObject.FindGameObjectWithTag("Grabber"); if (oldGrabber) { Destroy(oldGrabber); } } }
//update teleportation controls public void UpdateTeleportation() { //allow the player to select a teleporter if (animState.IsName("PhasingOut")) { //if the player is invisible/intangible and has not started teleporting if (!GetComponent <Renderer>().enabled&& !GetComponent <TrailRenderer>().enabled) { //if the mouse has moved if (mouseMoved) { Collider2D[] colliders; //get the objects at the mouse point if (Application.platform == RuntimePlatform.PSMPlayer) { colliders = Physics2D.OverlapPointAll(Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position)); } else { colliders = Physics2D.OverlapPointAll(Camera.main.ScreenToWorldPoint(Input.mousePosition)); } //iterate through the colliders foreach (Collider2D other in colliders) { //if the mouse is at a teleporter of the correct colour, it is the selected teleporter if (other.tag == "Teleporter") { if (other.GetComponent <TeleportNodeScript>().colour == teleportColour) { selectedTeleporter = other.gameObject; } } } } //get all the teleporters GameObject[] teleporters = GameObject.FindGameObjectsWithTag("Teleporter"); List <GameObject> colourTeleporters = new List <GameObject>(); //select the first teleporter of the correct colour foreach (GameObject teleporter in teleporters) { if (teleporter.GetComponent <TeleportNodeScript>().colour == teleportColour) { colourTeleporters.Add(teleporter); } } //if the axis has changed if (previousHorizontalAxis == 0.0f && Input.GetAxis("Horizontal") != 0.0f) { //decrement the index if (Input.GetAxis("Horizontal") < 0.0f) { teleporterIndex--; } //increment the index if (Input.GetAxis("Horizontal") > 0.0f) { teleporterIndex++; } //ensure the index is within the bounds if (teleporterIndex >= colourTeleporters.Count) { teleporterIndex = 0; } if (teleporterIndex < 0) { teleporterIndex = (colourTeleporters.Count - 1); } //set the selected teleporter selectedTeleporter = colourTeleporters[teleporterIndex]; } //make sure the cursor is at the correct position if (selectedTeleporter) { CursorScript.CreateNewCursor(selectedTeleporter.transform.position, cursorPrefab); } //if the player presses action if ((Input.GetButtonDown("Action") || Input.GetMouseButtonDown(0) || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)) && selectedTeleporter) { //player is teleporting to the selected teleporter teleportEndPosition = selectedTeleporter.transform.position; teleportDirection = teleportEndPosition - (Vector2)transform.position; selectedTeleporter = null; TrailRenderer trail = GetComponent <TrailRenderer>(); trail.enabled = true; audio.loop = true; audio.clip = teleportFlyClip; audio.Play(); //remove the cursor CursorScript.RemoveCursors(); } } } //if the player is teleporting, move towards the target position if (teleportDirection != Vector2.zero) { transform.Translate(teleportDirection.normalized * teleportSpeed * Time.deltaTime); //if the player is close to the target position, set its position to the target if ((teleportEndPosition - (Vector2)transform.position).magnitude < 0.5f) { transform.position = teleportEndPosition; //start phasing in animator.SetBool("isTeleporting", true); teleportEndPosition = Vector2.zero; teleportDirection = Vector2.zero; GetComponent <TrailRenderer>().enabled = false; renderer.enabled = true; audio.Stop(); audio.PlayOneShot(teleportInClip); } } }