public void OnTouch(Collider2D obj) { if (Elements[progress] == obj) { obj.GetComponent <Renderer>().material.color = Color.green; AudioManager.playGoodStep(); progress++; if (progress == Elements.Count) { if (!NextGate.IsOpen) { NextGate.Open(); Help.Open(); } } } else { StartCoroutine(WrongStep()); Player.Instance.Die(); progress = 0; for (int i = 0; i < Elements.Count; i++) { Elements[i].GetComponent <CollisionDetection>().Reset(); } print("Wrong step!"); } }
public void OnTouch(Collider2D obj) { if (Elements[progress] == obj) { AudioManager.playGoodStep(); obj.GetComponent <Renderer>().material.color = lightGreen; if (progress % 2 > 0) { bool earlier = obj.transform.localPosition.x < Elements[progress - 1].transform.localPosition.x; bool smaller = int.Parse(obj.gameObject.tag) < int.Parse(Elements[progress - 1].gameObject.tag); if ((smaller && !earlier) || (!smaller && earlier)) { StartCoroutine(SwapText()); Vector2 temp = new Vector2(obj.transform.position.x, obj.transform.position.y); obj.transform.position = Elements[progress - 1].transform.position; Elements[progress - 1].transform.position = temp; SortCollisionDetection eScript = Elements[progress - 1].GetComponent <SortCollisionDetection>(); eScript.wasTouched = true; eScript.OnCollisionLeave.AddListener(() => { eScript.wasTouched = false; eScript.OnCollisionLeave.RemoveAllListeners(); }); } obj.GetComponent <Renderer>().sharedMaterial.color = Color.white; Elements[progress - 1].GetComponent <Renderer>().sharedMaterial.color = Color.white; Comparison.SetActive(true); Num1.text = Elements[progress - 1].gameObject.tag.ToString(); Num2.text = obj.gameObject.tag.ToString(); } else { } progress++; if (progress == Elements.Count) { Comparison.SetActive(false); if (!NextGate.IsOpen) { Num1 = null; Num2 = null; NextGate.Open(); Help.Open(); } } } else { StartCoroutine(WrongStep()); Player.Instance.Die(); progress = 0; Num1.text = null; Num2.text = null; Comparison.SetActive(false); for (int i = 0; i < Elements.Count; i++) { Elements[i].GetComponent <SortCollisionDetection>().Reset(); } print("Wrong step!"); } }
public void Load() { //https://www.sitepoint.com/saving-and-loading-player-game-data-in-unity/ if (Directory.Exists("Saves") == false || File.Exists("Saves/save.binary") == false) { return; } BinaryFormatter formatter = new BinaryFormatter(); FileStream saveFile = File.Open("Saves/save.binary", FileMode.Open); localSaveModel = (SaveModel)formatter.Deserialize(saveFile); saveFile.Close(); GameManager.instance.player.transform.position = localSaveModel.Player.Position.GetVector(); GameManager.instance.player.transform.eulerAngles = localSaveModel.Player.Rotation.GetVector(); GameManager.instance.player.playerValues.health = localSaveModel.Player.Health; GameManager.instance.player.playerValues.maxHealth = localSaveModel.Player.MaxHealth; GameManager.instance.coinCount = localSaveModel.Player.Coins; LevelManager.instance.hasGateKey = localSaveModel.HasKey; if (LevelManager.instance.hasGateKey) { UIManager.instance.ShowKeyImage(); } else { UIManager.instance.HideKeyImage(); } foreach (ObjectModel model in localSaveModel.MovableObjects) { if (MovableObjects.ContainsKey(model.Id)) { GameObject go = MovableObjects[model.Id]; go.transform.position = model.Position.GetVector(); go.transform.eulerAngles = model.Rotation.GetVector(); go.SetActive(model.IsActive); } } foreach (ObjectModel model in localSaveModel.Haybales) { if (MovableObjects.ContainsKey(model.Id)) { GameObject go = Haybales[model.Id]; go.transform.position = model.Position.GetVector(); go.transform.eulerAngles = model.Rotation.GetVector(); go.SetActive(model.IsActive); } } foreach (ObjectModel model in localSaveModel.Checkpoints) { if (Checkpoints.ContainsKey(model.Id)) { GameObject go = Checkpoints[model.Id]; go.transform.position = model.Position.GetVector(); go.transform.eulerAngles = model.Rotation.GetVector(); go.SetActive(model.IsActive); } } foreach (FallingObjectModel model in localSaveModel.FallingObjects) { if (FallingObjects.ContainsKey(model.Id)) { FallingObject obj = FallingObjects[model.Id]; obj.transform.position = model.Position.GetVector(); obj.transform.eulerAngles = model.Rotation.GetVector(); obj.HasFallen = model.HasFallen; obj.gameObject.SetActive(model.IsActive); } } foreach (FallingObjectModel model in localSaveModel.TrapObjects) { if (TrapObjects.ContainsKey(model.Id)) { Breakable obj = TrapObjects[model.Id]; obj.transform.position = model.Position.GetVector(); obj.transform.eulerAngles = model.Rotation.GetVector(); obj.Broke = model.HasFallen; obj.gameObject.SetActive(model.IsActive); } } foreach (EnemyModel model in localSaveModel.Enemies) { if (Enemies.ContainsKey(model.Id)) { Peasant obj = Enemies[model.Id]; obj.transform.position = model.Position.GetVector(); obj.transform.eulerAngles = model.Rotation.GetVector(); if (model.IsStunned) { // obj.PlayerDash(Vector3.zero); obj.isDying = true; } else { obj.ShouldGoAlive = true; } obj.gameObject.SetActive(model.IsActive); } } foreach (GateModel model in localSaveModel.Gates) { if (Gates.ContainsKey(model.Id)) { GateScript obj = Gates[model.Id]; obj.transform.position = model.Position.GetVector(); obj.transform.eulerAngles = model.Rotation.GetVector(); if (model.IsOpen) { obj.Open(); UIManager.instance.HideKeyImage(); } obj.gameObject.SetActive(model.IsActive); } } foreach (ObjectModel model in localSaveModel.Dashables) { if (Dashables.ContainsKey(model.Id)) { Dashable obj = Dashables[model.Id]; obj.transform.position = model.Position.GetVector(); obj.transform.eulerAngles = model.Rotation.GetVector(); obj.gameObject.SetActive(model.IsActive); } } foreach (ObjectModel model in localSaveModel.Coins) { if (Coins.ContainsKey(model.Id)) { CoinPickup obj = Coins[model.Id]; obj.transform.position = model.Position.GetVector(); obj.transform.eulerAngles = model.Rotation.GetVector(); obj.gameObject.SetActive(model.IsActive); } } }