/// <summary> /// Calculate damage to be taken by the Player, /// triggers score increase and respawn workflow on death. /// </summary> public void TakeDamage(BulletSP bullet) { int totalDamage = bullet.damage - tankArmor; //substract health by damage health -= totalDamage; OnHealthChange(health); //bullet killed the player if (health <= 0) { //the game is already over so don't do anything if (GameManagerSinglePlayer.GetInstance().gameOver) { return; } //get killer and increase score for that team SinglePlayer other = bullet.owner.GetComponent <SinglePlayer>(); GameManagerSinglePlayer.GetInstance().score[other.teamIndex]++; GameManagerSinglePlayer.GetInstance().ui.OnTeamScoreChanged(other.teamIndex); //the maximum score has been reached now if (GameManagerSinglePlayer.GetInstance().IsGameOver()) { //tell client the winning team GameOver(other.teamIndex); return; } //the game is not over yet, reset runtime values health = maxHealth; OnHealthChange(health); Respawn(); } }
void Start() { //get corresponding team and colorise renderers in team color TeamForSinglePlayer team = GameManagerSinglePlayer.GetInstance().teamsForSinglePlayer[teamIndex]; for (int i = 0; i < renderers.Length; i++) { renderers[i].material = team.material; } //set name in label label.text = myName; OnHealthChange(health); if (GameManagerSinglePlayer.GetInstance().localPlayer != null) { return; } //set a global reference to the local player GameManagerSinglePlayer.GetInstance().localPlayer = this; isLocalPlayer = true; //get components and set camera target rb = GetComponent <Rigidbody>(); camFollow = Camera.main.GetComponent <FollowTarget>(); camFollow.target = turret; }
void FixedUpdate() { //don't execute anything if the game is over already, //but termine the agent and path finding routines if (GameManagerSinglePlayer.GetInstance().IsGameOver()) { agent.isStopped = true; StopAllCoroutines(); enabled = false; return; } //don't continue if this bot is marked as dead if (isDead) { return; } //no enemy players are in range if (inRange.Count == 0) { //if this bot reached the the random point on the navigation mesh, //then calculate another random point on the navmesh on continue moving around //with no other players in range, the AI wanders from team spawn to team spawn if (Vector3.Distance(transform.position, targetPoint) < agent.stoppingDistance) { int teamCount = GameManagerSinglePlayer.GetInstance().teamsForSinglePlayer.Length; RandomPoint(GameManagerSinglePlayer.GetInstance().teamsForSinglePlayer [Random.Range(0, teamCount)].spawn.position, range, out targetPoint); } } else { //if we reached the targeted point, calculate a new point around the enemy //this simulates more fluent "dancing" movement to avoid being shot easily if (Vector3.Distance(transform.position, targetPoint) < agent.stoppingDistance) { RandomPoint(inRange [0].transform.position, range * 2, out targetPoint); } //shooting loop for (int i = 0; i < inRange.Count; i++) { RaycastHit hit; //raycast to detect visible enemies and shoot at their current position if (Physics.Linecast(transform.position, inRange [i].transform.position, out hit)) { //get current enemy position and rotate this turret Vector3 lookPos = inRange [i].transform.position; turret.LookAt(lookPos); turret.eulerAngles = new Vector3(0, turret.eulerAngles.y, 0); //find shot direction and shoot there Vector3 shotDir = lookPos - transform.position; Shoot(new Vector2(shotDir.x, shotDir.z)); break; } } } }
/// <summary> /// This is when the respawn delay is over /// </summary> public virtual void Respawn() { //toggle visibility for player gameobject (on/off) gameObject.SetActive(!gameObject.activeInHierarchy); bool isActive = gameObject.activeInHierarchy; //the player has been killed if (!isActive) { //detect whether the current user was responsible for the kill //yes, that's my kill: increase local kill counter if (killedBy == GameManagerSinglePlayer.GetInstance().localPlayer.gameObject) { GameManagerSinglePlayer.GetInstance().ui.killCounter[0].text = (int.Parse(GameManagerSinglePlayer.GetInstance().ui.killCounter[0].text) + 1).ToString(); GameManagerSinglePlayer.GetInstance().ui.killCounter[0].GetComponent <Animator>().Play("Animation"); } if (explosionFX) { //spawn death particles locally using pooling and colorize them in the player's team color GameObject particle = PoolManager.Spawn(explosionFX, transform.position, transform.rotation); ParticleColor pColor = particle.GetComponent <ParticleColor>(); if (pColor) { pColor.SetColor(GameManagerSinglePlayer.GetInstance().teamsForSinglePlayer[teamIndex].material.color); } } //play sound clip on player death if (explosionClip) { AudioManager.Play3D(explosionClip, transform.position); } } //further changes only affect the local player if (!isLocalPlayer) { return; } //local player got respawned so reset states if (isActive == true) { ResetPosition(); } else { //local player was killed, set camera to follow the killer camFollow.target = killedBy.transform; //disable input disableInput = true; //display respawn window (only for local player) GameManagerSinglePlayer.GetInstance().DisplayDeath(); } }
//initialize variables IEnumerator Start() { //wait until the network is ready while (GameManagerSinglePlayer.GetInstance() == null || GameManagerSinglePlayer.GetInstance().localPlayer == null) { yield return(null); } //play background music AudioManager.PlayMusic(0); gamePaused = false; }
//the actual respawn routine IEnumerator ActualRespawn() { //stop AI updates isDead = true; inRange.Clear(); agent.isStopped = true; //detect whether the current user was responsible for the kill //yes, that's my kill: increase local kill counter if (killedBy == GameManagerSinglePlayer.GetInstance().localPlayer.gameObject) { GameManagerSinglePlayer.GetInstance().ui.killCounter[0].text = (int.Parse(GameManagerSinglePlayer.GetInstance().ui.killCounter [0].text) + 1).ToString(); GameManagerSinglePlayer.GetInstance().ui.killCounter[0].GetComponent <Animator> ().Play("Animation"); } if (explosionFX) { //spawn death particles locally using pooling and colorise them in the player's team color GameObject particle = PoolManager.Spawn(explosionFX, transform.position, transform.rotation); ParticleColor pColor = particle.GetComponent <ParticleColor> (); if (pColor) { pColor.SetColor(GameManagerSinglePlayer.GetInstance().teamsForSinglePlayer [teamIndex].material.color); } } //play sound clip on player death if (explosionClip) { AudioManager.Play3D(explosionClip, transform.position); } //toggle visibility for all rendering parts (off) ToggleComponents(false); //wait global respawn delay until reactivation yield return(new WaitForSeconds(GameManagerSinglePlayer.GetInstance().respawnTime)); //toggle visibility again (on) ToggleComponents(true); //respawn and continue with pathfinding targetPoint = GameManagerSinglePlayer.GetInstance().GetSpawnPosition(teamIndex); transform.position = targetPoint; agent.Warp(targetPoint); agent.isStopped = false; isDead = false; }
/// <summary> /// Repositions in team area and resets camera & input variables. /// This should only be called for the local player. /// </summary> public void ResetPosition() { if (!isLocalPlayer) { return; } //start following the local player again camFollow.target = turret; disableInput = false; //get team area and reposition it there transform.position = GameManagerSinglePlayer.GetInstance().GetSpawnPosition(teamIndex); //reset forces modified by input rb.velocity = Vector3.zero; rb.angularVelocity = Vector3.zero; transform.rotation = Quaternion.identity; }
//initialize variables void Awake() { tankSelected = PlayerPrefs.GetInt("TankSelected"); instance = this; if (size.Count != teamsForSinglePlayer.Length) { for (int i = 0; i < teamsForSinglePlayer.Length; i++) { size.Add(0); score.Add(0); } } //call the hooks manually for the first time, for each team for (int i = 0; i < teamsForSinglePlayer.Length; i++) { ui.OnTeamSizeChanged(i); } for (int i = 0; i < teamsForSinglePlayer.Length; i++) { ui.OnTeamScoreChanged(i); } //get the team value for this player int teamIndex = GameManagerSinglePlayer.GetInstance().GetTeamFill(); //get spawn position for this team and instantiate the player there Vector3 startPos = GameManagerSinglePlayer.GetInstance().GetSpawnPosition(teamIndex); playerPrefab = (GameObject)Instantiate(PlayerPrefabList[tankSelected], startPos, Quaternion.identity);//playerPrefab, startPos, Quaternion.identity); //assign name and team to Player component SinglePlayer p = playerPrefab.GetComponent <SinglePlayer>(); p.myName = playerName; p.teamIndex = teamIndex; }
IEnumerator Start() { //wait a second for all script to initialize yield return(new WaitForSeconds(0.25f)); //loop over bot count for (int i = 0; i < maxBots; i++) { //randomly choose bot from array of bot prefabs int randIndex = Random.Range(0, prefabs.Length); GameObject obj = (GameObject)GameObject.Instantiate(prefabs [randIndex], Vector3.zero, Quaternion.identity); //let the local host determine the team assignment SinglePlayer p = obj.GetComponent <SinglePlayer> (); p.teamIndex = GameManagerSinglePlayer.GetInstance().GetTeamFill(); //increase corresponding team size GameManagerSinglePlayer.GetInstance().size[p.teamIndex]++; GameManagerSinglePlayer.GetInstance().ui.OnTeamSizeChanged(p.teamIndex); yield return(new WaitForSeconds(0.25f)); } }
//called before SyncVar updates void Start() { //get components and set camera target camFollow = Camera.main.GetComponent <FollowTarget> (); agent = GetComponent <NavMeshAgent> (); agent.speed = moveSpeed; //get corresponding team and colorise renderers in team color targetPoint = GameManagerSinglePlayer.GetInstance().GetSpawnPosition(teamIndex); agent.Warp(targetPoint); TeamForSinglePlayer team = GameManagerSinglePlayer.GetInstance().teamsForSinglePlayer [teamIndex]; for (int i = 0; i < renderers.Length; i++) { renderers [i].material = team.material; } //set name in label myName = label.text = "Bot" + System.String.Format("{0:0000}", Random.Range(1, 9999)); //start enemy detection routine StartCoroutine(DetectPlayers()); }
//called on game end providing the winning team void GameOver(int teamIndex) { //display game over window GameManagerSinglePlayer.GetInstance().DisplayGameOver(teamIndex); }
/// <summary> /// This is an implementation for changes to the team score, updating the text values. /// Parameters: index of team which received updates. /// </summary> public void OnTeamScoreChanged(int index) { teamScore[index].text = GameManagerSinglePlayer.GetInstance().score[index].ToString(); teamScore[index].GetComponent <Animator>().Play("Animation"); }
/// <summary> /// This is an implementation for changes to the team fill, updating the slider values. /// Parameters: index of team which received updates. /// </summary> public void OnTeamSizeChanged(int index) { teamSize[index].value = GameManagerSinglePlayer.GetInstance().size[index]; }