Beispiel #1
0
        /// <summary>
        /// Spawns units from this tower and sends them toward the target position. This doesn't actually have to be an attack as reinforcements work the same way.
        /// This coroutine waits for unitSpawnRate seconds between spawning units. The number to spawn is determined by the number of units in the tower when called
        /// multiplied by percentOfUnitsPerAttack
        /// </summary>
        /// <param name="targetPos">Position to send units toward</param>
        /// <returns></returns>
        public IEnumerator SpawnAttack(Vector3 targetPos)
        {
            //Debug.Log("spawn Attack, selected: " + selected);

            GameObject prefabToSpawn;

            //Select the proper unit prefab to spawn
            prefabToSpawn = (myOwner == ePlayer.Player1) ? Player1UnitPrefab : Player2UnitPrefab;

            //Calculate the point at which the units should spawn (just outside the tower in the proper direction)
            Vector3 vecToTarget = targetPos - transform.position;                                 //line between source and target
            Vector3 spawnPoint  = vecToTarget;                                                    //a point on the line to target just outside the collider of the tower and unit

            spawnPoint.Normalize();
            spawnPoint *= (transform.localScale.x + prefabToSpawn.transform.localScale.x);                          //could be more efficient math here but this is what I came up with to scale
            spawnPoint *= .5f;
            spawnPoint  = new Vector3(spawnPoint.x + transform.position.x, spawnPoint.y + transform.position.y, 0); //then translate

            int unitsToSend = (int)(units * percentOfUnitsPerAttack);                                               //Calculate the number of units to spawn

            ePlayer myOwnerWhenStarted = myOwner;                                                                   //Ownership could change while SpawnAttack is sleeping

            //Keep sending till all units are sent or the tower runs out of units or switches sides
            while (unitsToSend > 0 && units > unitsToSend && myOwner == myOwnerWhenStarted)
            {
                //Create a unit and decrement
                GameObject   go          = (GameObject)GameObject.Instantiate(prefabToSpawn, spawnPoint, Quaternion.LookRotation(Vector3.forward, vecToTarget));
                unitBehavior spawnedUnit = go.GetComponent <unitBehavior>(); //set the owner of the new unit
                spawnedUnit.destination = targetPos;
                spawnedUnit.myOwner     = myOwner;
                unitsToSend--;
                units--;
                yield return(new WaitForSeconds(unitSpawnRate)); //wait to spawn another
            }
        }
Beispiel #2
0
        void OnTriggerEnter2D(Collider2D otherCollider)
        {
            print("magnet hit stuff");
            if (alreadyCollidedWith.Contains(otherCollider.gameObject))
            {
                return;
            }

            string otherTag = otherCollider.gameObject.tag;

            //Ignore collisions with anything other than units or towers
            if (!otherTag.Contains("Unit") && !otherTag.Contains("Tower"))
            {
                return;
            }
            else
            {
                if (otherTag.Contains("Unit"))
                {
                    unitBehavior otherBehavior = otherCollider.gameObject.GetComponent <unitBehavior>();
                    GameObject   unitToLoad    = rogueUnit;
                    Vector3      loadAt        = otherBehavior.gameObject.transform.position;
                    Vector3      dest          = unitToLoad.GetComponent <unitBehavior>().destination = Vector3.zero;
                    Vector3      dir           = otherBehavior.gameObject.transform.rotation.eulerAngles;
                    dir = new Vector3(dir.x, dir.y, dir.z + 180);
                    GameObject.Destroy(otherCollider.gameObject);
                    GameObject flippedUnit = GameObject.Instantiate(unitToLoad, loadAt, Quaternion.Euler(dir)) as GameObject;
                    alreadyCollidedWith.Add(flippedUnit);
                }
                else if (otherCollider == target)   //Must be tower
                {
                    gameObject.GetComponent <Rigidbody2D>().isKinematic = true;
                    gameObject.GetComponent <Collider2D>().enabled      = false;
                    startTime = Time.time;
                    StartCoroutine(interfereWithTower(otherCollider.gameObject.GetComponent <Tower>()));
                }
            }
        }