Пример #1
0
        /// <summary>
        /// Method called by the SyncList operation over the Network when its content changes.
        /// This is an implementation for changes to the buffered Collectibles, updating their position and assignment.
        /// Parameters: type of operation, index of Collectible which received updates.
        /// </summary>
        public void OnCollectibleStateChanged(SyncListStruct <CollectibleState> .Operation op, int index)
        {
            //get reference by index
            CollectibleState state = collects[index];

            if (state.objId.Value <= 0)
            {
                return;
            }

            //get game object instance by network ID
            GameObject obj = ClientScene.FindLocalObject(state.objId);

            if (obj == null)
            {
                return;
            }

            //get Collectible component on that object
            Collectible colComp = obj.GetComponent <Collectible>();

            if (colComp == null)
            {
                return;
            }

            //targetId is assigned: handle pickup on the corresponding player
            //or position is not at origin: handle drop at that position
            //otherwise when the entry has changed it got returned to its spawn position
            if (state.targetId.Value > 0)
            {
                colComp.spawner.Pickup(state.targetId);
            }
            else if (state.pos != colComp.spawner.transform.position)
            {
                colComp.spawner.Drop(state.pos);
            }
            else
            {
                colComp.spawner.Return();
            }
        }
Пример #2
0
        public void Return()
        {
            //re-parent object to this spawner
            obj.transform.parent   = PoolManager.GetPool(obj).transform;
            obj.transform.position = transform.position;

            //reset carrier
            Collectible colItem = obj.GetComponent <Collectible>();

            if (colItem != null)
            {
                colItem.carrierId = -1;
                colItem.OnReturn();
            }

            //cancel return timer as the object is now back at its base position
            if (PhotonNetwork.IsMasterClient)
            {
                StopAllCoroutines();
            }
        }
Пример #3
0
        /// <summary>
        /// Returns the object back to this spawner's position. E.g. in Capture The Flag mode this
        /// can occur if a team collects its own flag, or a flag timed out after being dropped.
        /// </summary>
        public void Return()
        {
            //re-parent object to this spawner
            obj.transform.parent   = PoolManager.GetPool(obj).transform;
            obj.transform.position = transform.position;
            obj.transform.rotation = Quaternion.identity;

            //reset carrier
            Collectible colItem = obj.GetComponent <Collectible>();

            if (colItem != null)
            {
                colItem.carrierId = new NetworkInstanceId(0);
                colItem.OnReturn();
            }

            //cancel return timer as the object is now back at its base position
            if (isServer)
            {
                StopAllCoroutines();
            }
        }
Пример #4
0
        private ObjectSpawner SelectPowerup()
        {
            if (m_SheildSpawner && tankPlayer.shield < m_ShieldAmount)
            {
                float timeBeforeShieldSpawn = m_NextSpawnTime - Time.time;
                NavMesh.CalculatePath(tankPlayer.Position, m_SheildSpawner.transform.position, NavMesh.AllAreas, m_PathBuffer);

                float timeToShieldSpawner = PathLength(m_PathBuffer) / m_TankSpeed;

                if (Mathf.Abs(timeBeforeShieldSpawn - timeToShieldSpawner) <= 2f)
                {
                    return(m_SheildSpawner);
                }
            }

            Collectible[] collectibles = CheckSurroundingObjectsByType(m_ItemSensorRadius,
                                                                       (Collectible c) => c.gameObject.activeSelf);

            if (collectibles.Length > 0)
            {
                //SortByDist<Collectible> comparer = new SortByDist<Collectible>(tankPlayer);
                SortByAStarDist <Collectible> comparer = new SortByAStarDist <Collectible>(tankPlayer);

                //low health, prioritize finding health and sheild
                if (tankPlayer.health / (float)tankPlayer.maxHealth < 0.5)
                {
                    Collectible[] healthBoosts = Array.FindAll(collectibles, (Collectible c) => c.GetType() == typeof(PowerupHealth));
                    Collectible   shield       = Array.Find(collectibles, (Collectible c) => c.GetType() == typeof(PowerupShield));

                    if (healthBoosts.Length > 0)
                    {
                        Array.Sort(healthBoosts, comparer);

                        if (shield)
                        {
                            return(comparer.Compare(shield, healthBoosts[0]) < 0 ? shield.spawner : healthBoosts[0].spawner);
                        }
                        else
                        {
                            return(healthBoosts[0].spawner);
                        }
                    }
                    else
                    {
                        if (shield)
                        {
                            return(shield.spawner);
                        }
                    }
                }

                Array.Sort(collectibles, comparer);

                List <Collectible> tempList = new List <Collectible>(collectibles.Length);
                int i;
                for (i = 0; i < collectibles.Length; i++)
                {
                    if (collectibles[i].GetType() == typeof(PowerupShield))
                    {
                        PowerupShield shield = (PowerupShield)collectibles[i];
                        if (tankPlayer.shield == m_ShieldAmount)
                        {
                            continue;
                        }

                        if (!m_SheildSpawner)
                        {
                            m_SheildSpawner = shield.spawner;
                        }

                        //always prioritize sheild
                        return(shield.spawner);
                    }

                    //prevent getting stuck trying to pick up a health item when on full health
                    if (collectibles[i].GetType() == typeof(PowerupHealth) && tankPlayer.health == tankPlayer.maxHealth)
                    {
                        continue;
                    }

                    //prevent getting stuck trying to pick up an item the tank already has
                    if (collectibles[i].GetType() == typeof(PowerupBullet)) //TODO: hardcoded 1
                    {
                        PowerupBullet powerupBullet = (PowerupBullet)collectibles[i];

                        if (tankPlayer.ammo == powerupBullet.amount && tankPlayer.currentBullet == powerupBullet.bulletIndex)
                        {
                            continue;
                        }
                    }

                    tempList.Add(collectibles[i]);
                }

                return(tempList.Count > 0 ? tempList[0].spawner : null);
            }
            else
            {
                return(null);
            }
        }
Пример #5
0
        protected override void OnUpdate()
        {
            /*
             * if (!isMark) birthPoint = tankPlayer.transform.position;
             * chooseTankTime += Time.deltaTime;
             * if (chooseTankTime > 0.5f)
             * {
             *  chooseTankTime = 0;
             *  base.OnUpdate();
             *  float minDistance = 100000f;
             *  GameObject[] allplayer = GameObject.FindGameObjectsWithTag("Player");
             *  foreach (var pl in allplayer)
             *  {
             *      var comp = pl.GetComponent<BasePlayer>();
             *      if (comp.teamIndex != tankPlayer.teamIndex && comp.IsAlive)
             *      {
             *          var position = (tankPlayer.transform.position - comp.transform.position).magnitude;
             *          Debug.Log("position=" + position);
             *          if (position > minDistance) continue;
             *          minDistance = position;
             *          Debug.Log("TankTeamis "+comp.teamIndex);
             *          lockPlayer = comp;
             *
             *      }
             *  }
             * }
             * //Debug.Log("lockPlayerTeamis " + lockPlayer.teamIndex);
             *
             * if (lockPlayer != null)
             * {
             *  tankPlayer.MoveTo(lockPlayer.Position);
             *  Debug.Log("RemainingDistance: "+tankPlayer.agent.remainingDistance);
             *  //if (tankPlayer.agent.remainingDistance < 20f) tankPlayer.agent.isStopped = true;
             *  //else tankPlayer.agent.isStopped = false;
             *  //if (tankPlayer.agent.remainingDistance < 20f) tankPlayer.MoveTo(birthPoint);
             *  if (tankPlayer.bShootable)
             *  {
             *
             *      tankPlayer.AimAndShoot(lockPlayer.Position+lockPlayer.transform.forward.normalized*5);
             *  }
             * }
             *
             * }
             */



            BasePlayer lockPlayer;

            //GameObject lockPowerUp;

            base.OnUpdate();
            float min = 100000f;

            GameObject[] allCollectible = GameObject.FindGameObjectsWithTag("Powerup");
            var          minCollectible = allCollectible[0];

            foreach (var pu in allCollectible)
            {
                Collectible type = pu.GetComponent <Collectible>();
                if (type.type == CollectibleType.PowerupHealth && tankPlayer.health == 10)
                {
                    continue;
                }
                if (type.type == CollectibleType.powerupShield && tankPlayer.shield == 3)
                {
                    continue;
                }
                if ((tankPlayer.Position - pu.transform.position).magnitude < min)
                {
                    min            = (tankPlayer.Position - pu.transform.position).magnitude;
                    minCollectible = pu;
                }
            }
            tankPlayer.MoveTo(minCollectible.transform.position);
            tankPlayer.RotateTurret(-tankPlayer.transform.forward.x, -tankPlayer.transform.forward.z);
            if (tankPlayer.bShootable)
            {
                tankPlayer.Shoot();

                //tankPlayer.AimAndShoot(-tankPlayer.transform.forward.normalized);
            }
        }