Beispiel #1
0
        private void SpawnAllTanks()
        {
            // For all the tanks...
            for (int i = 0; i < m_Tanks.Length; i++)
            {
                // ... create them, set their player number and references needed for control.
                m_Tanks[i].m_Instance =
                    Instantiate(m_TankPrefab, m_Tanks[i].m_SpawnPoint.position, m_Tanks[i].m_SpawnPoint.rotation) as GameObject;
                m_Tanks[i].m_PlayerNumber = i + 1;
                m_Tanks[i].Setup();
            }

            // set them to target each other -- for now assume only 2 tanks
            // if we want to set this up for more, will need to address later.
            // Note that we would also need to create agents which can handle more than 1 enemy as well
            if (m_Tanks.Length >= 2)
            {
                GameObject tank1  = m_Tanks[0].m_Instance;
                GameObject tank2  = m_Tanks[1].m_Instance;
                ITankAgent agent1 = tank1.GetComponent <ITankAgent>();
                ITankAgent agent2 = tank2.GetComponent <ITankAgent>();
                if (agent1 != null && agent2 != null)
                {
                    agent1.SetTarget(tank2);
                    agent2.SetTarget(tank1);
                }
            }
        }
Beispiel #2
0
        public void Setup()
        {
            // Get references to the components.
            m_Movement         = m_Instance.GetComponent <TankMovement> ();
            m_Shooting         = m_Instance.GetComponent <TankShooting> ();
            m_Health           = m_Instance.GetComponent <TankHealth>();
            m_CanvasGameObject = m_Instance.GetComponentInChildren <Canvas> ().gameObject;
            tankAgent          = m_Instance.GetComponent <ITankAgent>();

            // Set the player numbers to be consistent across the scripts.
            m_Instance.tag            = "Tank" + m_PlayerNumber.ToString();
            m_Movement.m_PlayerNumber = m_PlayerNumber;
            m_Shooting.m_PlayerNumber = m_PlayerNumber;
            if (tankAgent != null)
            {
                tankAgent.SetPlayerNumber(m_PlayerNumber);
            }

            // Create a string using the correct color that says 'PLAYER 1' etc based on the tank's color and the player's number.
            m_ColoredPlayerText = "<color=#" + ColorUtility.ToHtmlStringRGB(m_PlayerColor) + ">PLAYER " + m_PlayerNumber + "</color>";

            // Get all of the renderers of the tank.
            MeshRenderer[] renderers = m_Instance.GetComponentsInChildren <MeshRenderer> ();

            // Go through all the renderers...
            for (int i = 0; i < renderers.Length; i++)
            {
                // ... set their material color to the color specific to this tank.
                renderers[i].material.color = m_PlayerColor;
            }
        }
Beispiel #3
0
 private void Awake()
 {
     agent = GetComponent <ITankAgent>();
     if (agent == null)
     {
         agentControl = false;
         Debug.unityLogger.LogWarning("TankMovement", "No agent found");
     }
 }
Beispiel #4
0
        void Start()
        {
            outputUITexts[0] = GameObject.Find("AgentText1")?.GetComponent <Text>();
            outputUITexts[1] = GameObject.Find("AgentText2")?.GetComponent <Text>();
            agent            = GetComponent <ITankAgent>();
            int textIndex = agent.GetPlayerNumber() - 1;

            outputText = outputUITexts[textIndex];
        }
        void Start()
        {
            agent = GetComponent <ITankAgent>();
            if (cameraSensor == null)
            {
                cameraSensor = GetComponent <CameraSensor>();
            }

            // wire up to UI images normal/segmented based on player 1 or 2
            // yeah this isn't pretty, but we just need something quick.
            int playerNumber = agent.GetPlayerNumber() - 1;

            // might not need to use this, might be better off manually wiring up for visual agents

            normalRenderTexture    = normalRTs[playerNumber];
            segmentedRenderTexture = segmentedRTs[playerNumber];

            normalCam.targetTexture = normalRenderTexture;
            cameraSensor.Camera     = normalCam;
        }
Beispiel #6
0
        private void OnTriggerEnter(Collider other)
        {
            // Collect all the colliders in a sphere from the shell's current position to a radius of the explosion radius.
            Collider[] colliders = Physics.OverlapSphere(transform.position, m_ExplosionRadius, m_TankMask);

            // Go through all the colliders...
            for (int i = 0; i < colliders.Length; i++)
            {
                // ... and find their rigidbody.
                Rigidbody targetRigidbody = colliders[i].GetComponent <Rigidbody> ();

                // If they don't have a rigidbody, go on to the next collider.
                if (!targetRigidbody)
                {
                    continue;
                }

                // Add an explosion force.
                targetRigidbody.AddExplosionForce(m_ExplosionForce, transform.position, m_ExplosionRadius);

                // Find the TankHealth script associated with the rigidbody.
                TankHealth targetHealth = targetRigidbody.GetComponent <TankHealth> ();

                // If there is no TankHealth script attached to the gameobject, go on to the next collider.
                if (!targetHealth)
                {
                    continue;
                }

                // Calculate the amount of damage the target should take based on it's distance from the shell.
                float damage = CalculateDamage(targetRigidbody.position);

                // Deal this damage to the tank.
                targetHealth.TakeDamage(damage);

                // add to damage report, so we can send to listensers
                // note that tank might damage itself, so we need to add all damages
                ITankAgent agent = targetRigidbody.GetComponent <ITankAgent>();
                if (agent == null)
                {
                    continue;
                }

                if (damageReport.ContainsKey(agent.GetPlayerNumber()))
                {
                    // if multiple parts of the tank are hit, we need to add up all the damage
                    damageReport[agent.GetPlayerNumber()] += damage;
                }
                else
                {
                    damageReport.Add(agent.GetPlayerNumber(), damage);
                }
            }

            // once all damages are calculated, send out damage report
            if (OnExplosion != null)
            {
                OnExplosion.Invoke(this, damageReport);
            }

            // Unparent the particles from the shell.
            m_ExplosionParticles.transform.parent = null;

            // Play the particle system.
            m_ExplosionParticles.Play();

            // Play the explosion sound effect.
            // m_ExplosionAudio.Play();

            // Once the particles have finished, destroy the gameobject they are on.
            ParticleSystem.MainModule mainModule = m_ExplosionParticles.main;
            Destroy(m_ExplosionParticles.gameObject, mainModule.duration);

            // Destroy the shell.
            Destroy(gameObject);
        }