void Update() { if (_state == State.Counting) { _startTimer += Time.deltaTime; Vector3 clScale = _counterLabel.gameObject.transform.localScale; clScale.x -= Time.deltaTime; clScale.y -= Time.deltaTime; if (_startTimer > 0.9f && _startTimer < 1.1f) { clScale.x = 1f; clScale.y = 1f; _counterLabel.text = "2"; } if (_startTimer > 1.9f && _startTimer < 2.1f) { clScale.x = 1f; clScale.y = 1f; _counterLabel.text = "1"; } if (_startTimer > 2.9f && _startTimer < 3.1f) { clScale.x = 1f; clScale.y = 1f; _counterLabel.text = "Spook!"; } if (_startTimer > 4f) { for (int i = 0; i < _npcs.Count; i++) { _npcs[i].GetComponent <Npc>().SetPause(_paused); } _counterLabel.gameObject.SetActive(false); _state = State.Playing; } _counterLabel.gameObject.transform.localScale = clScale; } else if (_state == State.Playing) { if (!_paused) { // Check if the player has touched an actuator if (Input.GetMouseButtonDown(0)) { // Throw the ray Vector2 origin = _mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1f)); Vector2 direction = new Vector2(0.1f, 0.1f); RaycastHit2D[] hit = Physics2D.RaycastAll(origin, direction, 0.1f); if (_zoomed == ZoomState.ZoomedIn) { // Get the ray origin if (hit != null) { for (int i = 0; i < hit.Length; i++) { GameObject go = hit[i].transform.gameObject; Actuator act = go.GetComponent <Actuator>(); // If it hits on an actuator, make it react if (act && act.GetRoom() == _room) { act.OnAction(); } } } } else if (_zoomed == ZoomState.ZoomedOut) { for (int i = 0; i < hit.Length; i++) { GameObject go = hit[i].transform.gameObject; // If it hits on an actuator, make it react if (go && go.gameObject.transform.parent) { if (go.gameObject.transform.parent.name.Contains("Habitaciones")) { ZoomIn(go.name, go.transform.position); break; } } } } } _timer -= Time.deltaTime; // Update scare UI bar and timer // @To-Do _scareSlider.fillAmount = _scareLevel / _maxPoints; _timerLabel.text = ((int)_timer).ToString(); // Check if the player has lost if (_timer < 0 || (_difficulty != GameDifficulty.TimeAttack && _scareLevel > _maxPoints)) { if (_difficulty == GameDifficulty.TimeAttack) { _timeAttackScore = (int)_scareLevel; } else { if (_scareLevel < _maxPoints) { _win = false; } else { _win = true; } } if (_win) { _state = State.WinFinish; Node exitNode = _exitWaypoint.GetComponent <Node>(); foreach (Npc n in _npcs) { n.SetDesiredNode(exitNode); n.SetAnimState(Npc.AnimState.Frightened); } } else { _state = State.Finished; } } } } else if (_state == State.Finished) { TogglePause(true); _endWindow.SetActive(true); int ownBest = SettingsController.instance.GetOwnBestScore(); if (_timeAttackScore > ownBest) { SettingsController.instance.SetOwnBestScore(_timeAttackScore); } if (_difficulty != GameDifficulty.TimeAttack) { GameObject.Find("EndMenu/Loading").SetActive(false); if (!_win) { GameObject.Find("EndMenu/Header").GetComponent <UnityEngine.UI.Text>().text = "You lose..."; } else { GameObject.Find("EndMenu/Header").GetComponent <UnityEngine.UI.Text>().text = "You win!"; } } else { GameObject.Find("EndMenu/Points").GetComponent <UnityEngine.UI.Text>().text = "Score: " + _timeAttackScore + " points (your best: " + SettingsController.instance.GetOwnBestScore() + " points)"; GameObject.Find("EndMenu/Header").GetComponent <UnityEngine.UI.Text>().text = "Time's up!"; } _state = State.FinishedIdle; } else if (_state == State.WinFinish) { bool end = true; Node exitNode = _exitWaypoint.GetComponent <Node>(); foreach (Npc n in _npcs) { bool is_stoped = n.GetLogicState() == Npc.LogicState.Stand; if (is_stoped) { n.SetPause(true); } end &= is_stoped; } if (end) { _state = State.Finished; } } }
private void DoToggleDoor() { Vector3 dir = _currentTarget.transform.position - transform.position; Vector2 origin = transform.position + dir.normalized; bool onDoorIn = false; bool onDoorOut = false; Actuator actIn = null; Actuator actOut = null; RaycastHit2D[] hit = Physics2D.RaycastAll(origin, dir, 1f); Debug.DrawRay(new Vector3(origin.x, origin.y, 1f), dir, Color.green); for (int i = 0; i < hit.Length; i++) { GameObject go = hit[i].transform.gameObject; actIn = go.GetComponent <Actuator>(); if (actIn) { onDoorIn = true; break; } } origin = transform.position - dir.normalized; hit = Physics2D.RaycastAll(origin, -dir, 1f); for (int i = 0; i < hit.Length; i++) { GameObject go = hit[i].transform.gameObject; actOut = go.GetComponent <Actuator>(); if (actOut) { onDoorOut = true; break; } } if (onDoorIn && !actIn.GetToggled()) { actIn.OnAction(false); } else if (onDoorOut && actOut.GetToggled() && _animState == AnimState.Frightened) { actOut.OnAction(false); } if (onDoorOut || onDoorIn) { Color c = GetComponent <SpriteRenderer>().color; c.a = 0.5f; GetComponent <SpriteRenderer>().color = c; } else { Color c = GetComponent <SpriteRenderer>().color; c.a = 1f; GetComponent <SpriteRenderer>().color = c; } }