// load the game public void load() { // Do we have the save? if (PlayerPrefs.HasKey("save")) { state = OtherMethod.deserialize <SaveState>(PlayerPrefs.GetString("save")); } GameManager.instance.saveFile = state; }
//Save the whole state of this saveState public void saveNewGame() { state = new SaveState(true); // dont update the username and password when making new game if (!"".Equals(GameManager.instance.saveFile.username)) { state.username = GameManager.instance.saveFile.username; state.password = GameManager.instance.saveFile.password; } PlayerPrefs.SetString("save", OtherMethod.serialize <SaveState>(state)); GameManager.instance.saveFile = state; }
// Shuffling the answer private void Start() { isSendingTruck = false; nextForTutorial = false; foreach (QuestionsAnswers question in easyQuestions) { question.setAnswers(OtherMethod.shuffle <Answer>(question.getAnswers(), epoch)); } foreach (QuestionsAnswers question in mediumQuestions) { question.setAnswers(OtherMethod.shuffle <Answer>(question.getAnswers(), epoch)); } foreach (QuestionsAnswers question in hardQuestions) { question.setAnswers(OtherMethod.shuffle <Answer>(question.getAnswers(), epoch)); } currentRightAnswer = 0; if (GameManager.instance.isPractice || GameManager.instance.isTutorial) { getNewQuestion(true); } if (!GameManager.instance.isPractice && !GameManager.instance.isTutorial) { timer = 10f; hasDonePrepare = false; // disable all answer and question possibleAnswers[0].transform.parent.parent.gameObject.SetActive(false); questionUI.transform.parent.parent.gameObject.SetActive(false); } else if (!GameManager.instance.isTutorial) { hasDonePrepare = true; warningText.gameObject.SetActive(false); } else { hasDonePrepare = true; } // game event controller GameEvents.current.onTruckDestroyedEnter += OnTruckDestroyed; }
// save the game when updated public void saveAndUpdate() { state = GameManager.instance.saveFile; PlayerPrefs.SetString("save", OtherMethod.serialize <SaveState>(state)); }
// Update is called once per frame after normal update void Update() { if (GameManager.instance.isPaused) { cancel(); } // for touch if (Input.touchCount > 0 && GameManager.instance.isSelectTrap && !isReadyToSpawn && !OtherMethod.onUiPressed(Input.GetTouch(0).position)) { Touch touch = Input.GetTouch(0); // If it is the first time to build then make the trap if (!isReadyToBuild && !OtherMethod.onUiPressed(touch.position)) { switch (GameManager.instance.selectedTrap) { case 0: currentTrap = Instantiate(trapBomb, this.transform); currentTrapType = TrapType.BOMB_TRAP; break; case 1: currentTrap = Instantiate(trapTime, this.transform); currentTrapType = TrapType.TIME_TRAP; break; case 2: currentTrap = Instantiate(trapFreeze, this.transform); currentTrapType = TrapType.FREEZE_TRAP; break; default: currentTrap = Instantiate(trapBomb, this.transform); currentTrapType = TrapType.BOMB_TRAP; break; } currentTrap.SetActive(false); SpriteRenderer renderer = currentTrap.GetComponent <SpriteRenderer>(); renderer.color = new Color(1, 1, 1, 150f / 256f); isReadyToBuild = true; } if (touch.phase == TouchPhase.Began && !OtherMethod.onUiPressed(touch.position)) { Vector2 touchPosition; touchPosition = mainCamera.ScreenToWorldPoint(touch.position); // checking if the place is open field int x, y; GridCustom.getXYFromPosition(touchPosition, out x, out y); x += GridCustom.offsetX; y += GridCustom.offsetY; if (GridCustom.cells[x, y].cellContent == CellContent.PATH) { currentTrap.SetActive(true); isReadyToSpawn = true; canSpawn = false; currentTrap.transform.position = new Vector3(touch.position.x, touch.position.y, 0); } } } if (Input.touchCount > 0 && isReadyToBuild && isReadyToSpawn && !OtherMethod.onUiPressed(Input.GetTouch(0).position)) { Touch touch = Input.GetTouch(0); Vector2 touchPosition; touchPosition = mainCamera.ScreenToWorldPoint(touch.position); int x, y; GridCustom.getXYFromPosition(touchPosition, out x, out y); x += GridCustom.offsetX; y += GridCustom.offsetY; // if the player touch the area again then spawn the trap if (touch.phase == TouchPhase.Ended && GameManager.instance.isSelectTrap) { if (isReadyToSpawn && isReadyToBuild) { canSpawn = true; } else if (canSpawn) { // change the color SpriteRenderer renderer = currentTrap.GetComponent <SpriteRenderer>(); renderer.color = new Color(1, 1, 1, 1); // activate it TrapsBehaviour trap = currentTrap.GetComponent <TrapsBehaviour>(); trap.activate(); // pay the trap GameManager.instance.pay(currentTrapType); trapButton.disableButton(GameManager.instance.selectedTrap); // reset variable currentTrap = null; currentTrapType = TrapType.BOMB_TRAP; isReadyToBuild = false; isReadyToSpawn = false; canSpawn = false; desc.gameObject.SetActive(false); GameEvents.current.TowerOrTrapBuild(); if (GameManager.instance.gameStart) { generator.StartCoroutine(generator.openGenerator()); kotoTower.StartCoroutine(kotoTower.openKotoTower()); } return; } } else if (touch.phase == TouchPhase.Began && Vector3.Distance(currentTrap.transform.position, new Vector3(touchPosition.x, touchPosition.y, 0f)) > 0.5f) { isReadyToSpawn = false; canSpawn = false; } } // for mouse (debug) if (Input.GetMouseButton(0) && GameManager.instance.isSelectTrap && !OtherMethod.onUiPressed(Input.mousePosition)) { // If it is the first time to build then make the trap if (!isReadyToBuild) { switch (GameManager.instance.selectedTrap) { case 0: currentTrap = Instantiate(trapBomb, this.transform); currentTrapType = TrapType.BOMB_TRAP; break; case 1: currentTrap = Instantiate(trapTime, this.transform); currentTrapType = TrapType.TIME_TRAP; break; case 2: currentTrap = Instantiate(trapFreeze, this.transform); currentTrapType = TrapType.FREEZE_TRAP; break; default: currentTrap = Instantiate(trapBomb, this.transform); currentTrapType = TrapType.BOMB_TRAP; break; } currentTrap.SetActive(false); SpriteRenderer renderer = currentTrap.GetComponent <SpriteRenderer>(); renderer.color = new Color(1, 1, 1, 150f / 256f); isReadyToBuild = true; } Vector2 touchPosition; touchPosition = mainCamera.ScreenToWorldPoint(Input.mousePosition); // checking if the place is open field int x, y; GridCustom.getXYFromPosition(touchPosition, out x, out y); x += GridCustom.offsetX; y += GridCustom.offsetY; if (Vector3.Distance(currentTrap.transform.position, touchPosition) > 0.5f) { isReadyToSpawn = false; } if (GridCustom.cells[x, y].cellContent == CellContent.PATH && !isReadyToSpawn) { currentTrap.SetActive(true); currentTrap.transform.position = new Vector3(touchPosition.x, touchPosition.y, -0); } } // if the player touch the area again then spawn the trap, if its the first time then just be ready to spawn if (Input.GetMouseButtonUp(0) && GameManager.instance.isSelectTrap && !OtherMethod.onUiPressed(Input.mousePosition)) { if (!isReadyToSpawn && isReadyToBuild) { isReadyToSpawn = true; } else if (isReadyToBuild) { // change the color SpriteRenderer renderer = currentTrap.GetComponent <SpriteRenderer>(); renderer.color = new Color(1, 1, 1, 1); // activate it TrapsBehaviour trap = currentTrap.GetComponent <TrapsBehaviour>(); trap.activate(); // pay the trap GameManager.instance.pay(currentTrapType); trapButton.disableButton(GameManager.instance.selectedTrap); // reset variable currentTrap = null; currentTrapType = TrapType.BOMB_TRAP; isReadyToBuild = false; isReadyToSpawn = false; desc.gameObject.SetActive(false); GameEvents.current.TowerOrTrapBuild(); if (GameManager.instance.gameStart) { generator.StartCoroutine(generator.openGenerator()); kotoTower.StartCoroutine(kotoTower.openKotoTower()); } return; } } }
// for touches void detectTouches() { // for touch if (Input.touchCount > 0 && GameManager.instance.isSelectTower && !isReadyToSpawn && !OtherMethod.onUiPressed(Input.GetTouch(0).position)) { Touch touch = Input.GetTouch(0); // If it is the first time to build then make the tower if (!isReadyToBuild) { switch (GameManager.instance.selectedTower) { case 0: currentTower = Instantiate(towerMachineGun, this.transform); currentTowerType = TowerType.MACHINE_GUN; break; case 1: currentTower = Instantiate(towerSniper, this.transform); currentTowerType = TowerType.SNIPER; break; case 2: currentTower = Instantiate(towerElectric, this.transform); currentTowerType = TowerType.ELECTRIC; break; default: currentTower = Instantiate(towerMachineGun, this.transform); currentTowerType = TowerType.MACHINE_GUN; break; } currentTower.SetActive(false); SpriteRenderer renderer = currentTower.GetComponent <SpriteRenderer>(); renderer.color = new Color(1, 1, 1, 150f / 256f); isReadyToBuild = true; } if (touch.phase == TouchPhase.Began) { Vector2 touchPosition; touchPosition = mainCamera.ScreenToWorldPoint(touch.position); // checking if the place is open field int x, y; GridCustom.getXYFromPosition(touchPosition, out x, out y); x += GridCustom.offsetX; y += GridCustom.offsetY; if (GridCustom.cells[x, y].cellContent == CellContent.OPEN_FIELD) { currentTower.SetActive(true); isReadyToSpawn = true; canSpawn = false; currentTower.transform.position = GridCustom.getWorldSpace(x, y) + new Vector3(0.5f, 0.5f, 0); currX = x; currY = y; } } } if (Input.touchCount > 0 && isReadyToBuild && isReadyToSpawn && !OtherMethod.onUiPressed(Input.GetTouch(0).position)) { Touch touch = Input.GetTouch(0); Vector2 touchPosition; touchPosition = mainCamera.ScreenToWorldPoint(touch.position); int x, y; GridCustom.getXYFromPosition(touchPosition, out x, out y); x += GridCustom.offsetX; y += GridCustom.offsetY; // if the player touch the area again then spawn the tower if (touch.phase == TouchPhase.Ended && GameManager.instance.isSelectTower) { if (isReadyToSpawn && isReadyToBuild) { canSpawn = true; } else if (canSpawn) { // change the color SpriteRenderer renderer = currentTower.GetComponent <SpriteRenderer>(); renderer.color = new Color(1, 1, 1, 1); // activate it TowerBehaviour tower = currentTower.GetComponent <TowerBehaviour>(); tower.activate(); // block the area TowerGridBlocker gridBlocker = currentTower.GetComponent <TowerGridBlocker>(); gridBlocker.changeGridStatus(); // pay the tower GameManager.instance.pay(currentTowerType); towerButton.disableButton(GameManager.instance.selectedTower); // reset variable currentTower = null; currentTowerType = TowerType.MACHINE_GUN; isReadyToBuild = false; isReadyToSpawn = false; canSpawn = false; currX = -1; currY = -1; desc.gameObject.SetActive(false); GameEvents.current.TowerOrTrapBuild(); if (GameManager.instance.gameStart) { generator.StartCoroutine(generator.openGenerator()); kotoTower.StartCoroutine(kotoTower.openKotoTower()); } return; } } else if (touch.phase == TouchPhase.Began && !(currX == x && currY == y)) { isReadyToSpawn = false; canSpawn = false; } } }