Exemple #1
0
        /// <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();
            }
        }
Exemple #2
0
        //sets inRange list for player detection
        IEnumerator DetectPlayers()
        {
            //wait for initialization
            yield return(new WaitForEndOfFrame());

            //detection logic
            while (true)
            {
                //empty list on each iteration
                inRange.Clear();

                //casts a sphere to detect other player objects within the sphere radius
                Collider[] cols = Physics.OverlapSphere(transform.position, range, LayerMask.GetMask("Player"));
                //loop over players found within bot radius
                for (int i = 0; i < cols.Length; i++)
                {
                    //get other Player component
                    //only add the player to the list if its not in this team
                    SinglePlayer p = cols [i].gameObject.GetComponent <SinglePlayer> ();
                    if (p.teamIndex != teamIndex && !inRange.Contains(cols [i].gameObject))
                    {
                        inRange.Add(cols [i].gameObject);
                    }
                }

                //wait a second before doing the next range check
                yield return(new WaitForSeconds(1));
            }
        }
        /// <summary>
        /// Only for this player: sets the death text stating the killer on death.
        /// </summary>
        public void DisplayDeath(bool skipAd = false)
        {
            //get the player component that killed us
            SinglePlayer other = localPlayer.killedBy.GetComponent <SinglePlayer>();

            //increase local death counter for this game
            ui.killCounter[1].text = (int.Parse(ui.killCounter[1].text) + 1).ToString();
            ui.killCounter[1].GetComponent <Animator>().Play("Animation");

            //set the death text
            //and start waiting for the respawn delay immediately
            ui.SetDeathText(other.myName, teamsForSinglePlayer[other.teamIndex]);
            StartCoroutine(SpawnRoutine());
        }
Exemple #4
0
        //check what was hit on collisions
        void OnTriggerEnter(Collider col)
        {
            //cache corresponding gameobject that was hit
            GameObject obj = col.gameObject;
            //try to get a player component out of the collided gameobject
            SinglePlayer player = obj.GetComponent <SinglePlayer>();

            //we actually hit a player
            //do further checks
            if (player != null)
            {
                //ignore ourselves & disable friendly fire (same team index)
                if (player.gameObject == owner || player.gameObject == null)
                {
                    return;
                }
                else if (player.teamIndex == owner.GetComponent <SinglePlayer>().teamIndex)
                {
                    return;
                }

                //create clips and particles on hit
                if (hitFX)
                {
                    PoolManager.Spawn(hitFX, transform.position, Quaternion.identity);
                }
                if (hitClip)
                {
                    AudioManager.Play3D(hitClip, transform.position);
                }

                //on the player that was hit, set the killing player to the owner of this bullet
                //maybe this owner really killed the player, but that check is done in the Player script
                player.killedBy = owner;
            }

            //apply bullet damage to the collided player
            if (player)
            {
                player.TakeDamage(this);
            }
            //despawn gameobject
            PoolManager.Despawn(gameObject);
        }
        //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;
        }
Exemple #6
0
        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));
            }
        }