예제 #1
0
        public static void Update(this Soldier soldier)
        {
            var gameManager = Core.Get <GameManager>();

            var playerLair = gameManager.GetPlayerLair(soldier.owner);

            if (soldier.state == SoldierState.Harvest)
            {
                if (soldier.hasFood)
                {
                    soldier.targetPosition = playerLair.GetPosition();

                    if (Vector3.Distance(soldier.GetPosition(), playerLair.GetPosition()) < SoldierConstants.FOOD_HARVEST_MIN_DISTANCE)
                    {
                        playerLair.foodCount += 1;

                        if (gameManager.onAddFoodInLair != null)
                        {
                            gameManager.onAddFoodInLair(soldier, playerLair);
                        }

                        soldier.hasFood = false;
                        soldier.behaviour.CarryNone();

                        if (gameManager.onFoodDrop != null)
                        {
                            gameManager.onFoodDrop(soldier);
                        }
                    }
                }
                else
                {
                    if (soldier.harvestUnitTarget != null && soldier.harvestPileTarget == null)
                    {
                        soldier.targetPosition = soldier.harvestUnitTarget.GetPosition();

                        if (Vector3.Distance(soldier.GetPosition(), soldier.harvestUnitTarget.GetPosition()) < SoldierConstants.FOOD_HARVEST_MIN_DISTANCE)
                        {
                            soldier.hasFood = true;
                            soldier.behaviour.CarryFood();
                            soldier.harvestUnitTarget.Destroy();

                            if (gameManager.onFoodPickup != null)
                            {
                                gameManager.onFoodPickup(soldier);
                            }
                        }
                    }
                    else if (soldier.harvestPileTarget != null)
                    {
                        soldier.targetPosition = soldier.harvestPileTarget.GetPosition();

                        if (soldier.harvestPileTarget.behaviour.GetFoodCount() == 0)
                        {
                            soldier.harvestPileTarget = null;
                        }
                        else if (Vector3.Distance(soldier.GetPosition(), soldier.harvestPileTarget.GetPosition()) < SoldierConstants.FOOD_HARVEST_MIN_DISTANCE)
                        {
                            if (soldier.harvestPileTarget.behaviour.GetFoodUnit())
                            {
                                soldier.hasFood = true;
                                soldier.behaviour.CarryFood();

                                if (gameManager.onFoodPickup != null)
                                {
                                    gameManager.onFoodPickup(soldier);
                                }
                            }
                            else
                            {
                                soldier.harvestPileTarget = null;
                            }
                        }
                    }
                    else
                    {
                        FoodPile nearestFoodPile = null;
                        FoodUnit nearestFoodUnit = null;

                        foreach (var foodPile in gameManager.foodPiles)
                        {
                            if (foodPile.behaviour.GetFoodCount() == 0)
                            {
                                continue;
                            }

                            if (nearestFoodPile == null || Vector3.Distance(foodPile.GetPosition(), soldier.GetPosition()) < Vector3.Distance(nearestFoodPile.GetPosition(), soldier.GetPosition()))
                            {
                                nearestFoodPile = foodPile;
                            }
                        }

                        foreach (var foodUnit in gameManager.foodUnits)
                        {
                            if (nearestFoodUnit == null || Vector3.Distance(foodUnit.GetPosition(), soldier.GetPosition()) < Vector3.Distance(nearestFoodUnit.GetPosition(), soldier.GetPosition()))
                            {
                                nearestFoodUnit = foodUnit;
                            }
                        }

                        var goWithPlayer   = false;
                        var goWithFoodPile = false;
                        var goWithFoodUnit = false;

                        if (nearestFoodPile != null && nearestFoodUnit != null)
                        {
                            if (
                                Vector3.Distance(soldier.GetPosition(), soldier.owner.GetPosition()) > Vector3.Distance(soldier.GetPosition(), nearestFoodPile.GetPosition()) ||
                                Vector3.Distance(soldier.GetPosition(), soldier.owner.GetPosition()) > Vector3.Distance(soldier.GetPosition(), nearestFoodUnit.GetPosition()))
                            {
                                if (Vector3.Distance(soldier.GetPosition(), nearestFoodPile.GetPosition()) < Vector3.Distance(soldier.GetPosition(), nearestFoodUnit.GetPosition()))
                                {
                                    goWithFoodPile = true;
                                }
                                else
                                {
                                    goWithFoodUnit = true;
                                }
                            }
                            else
                            {
                                goWithPlayer = true;
                            }
                        }
                        else if (nearestFoodPile != null)
                        {
                            if (Vector3.Distance(soldier.GetPosition(), nearestFoodPile.GetPosition()) < Vector3.Distance(soldier.GetPosition(), soldier.owner.GetPosition()))
                            {
                                goWithFoodPile = true;
                            }
                            else
                            {
                                goWithPlayer = true;
                            }
                        }
                        else if (nearestFoodUnit != null)
                        {
                            if (Vector3.Distance(soldier.GetPosition(), nearestFoodUnit.GetPosition()) < Vector3.Distance(soldier.GetPosition(), soldier.owner.GetPosition()))
                            {
                                goWithFoodUnit = true;
                            }
                            else
                            {
                                goWithPlayer = true;
                            }
                        }
                        else
                        {
                            goWithPlayer = true;
                        }

                        if (goWithPlayer)
                        {
                            soldier.state          = SoldierState.Protect;
                            soldier.targetPosition = soldier.GetPosition();

                            if (gameManager.onSoldierChangeState != null)
                            {
                                gameManager.onSoldierChangeState(soldier, SoldierState.Harvest);
                            }
                        }
                        else if (goWithFoodPile)
                        {
                            soldier.state             = SoldierState.Harvest;
                            soldier.harvestPileTarget = nearestFoodPile;

                            if (gameManager.onSoldierChangeState != null)
                            {
                                gameManager.onSoldierChangeState(soldier, SoldierState.Harvest);
                            }
                        }
                        else if (goWithFoodUnit)
                        {
                            soldier.state             = SoldierState.Harvest;
                            soldier.harvestPileTarget = null;
                            soldier.harvestUnitTarget = nearestFoodUnit;

                            if (gameManager.onSoldierChangeState != null)
                            {
                                gameManager.onSoldierChangeState(soldier, SoldierState.Harvest);
                            }
                        }
                    }
                }
            }
            else if (soldier.state == SoldierState.StealEgg)
            {
                if (soldier.stealEggTarget == null)
                {
                    soldier.state          = SoldierState.Protect;
                    soldier.targetPosition = soldier.GetPosition();

                    if (gameManager.onSoldierChangeState != null)
                    {
                        gameManager.onSoldierChangeState(soldier, SoldierState.StealEgg);
                    }
                }
                else if (soldier.hasEgg)
                {
                    soldier.targetPosition = playerLair.GetPosition();

                    if (Vector3.Distance(soldier.GetPosition(), playerLair.GetPosition()) < (SoldierEggConstants.HATCH_MIN_DISTANCE_LAIR / 1.3f))
                    {
                        soldier.hasEgg = false;
                        soldier.behaviour.CarryNone();
                        soldier.stealEggTarget.behaviour.gameObject.SetActive(true);
                        soldier.stealEggTarget.behaviour.transform.position = playerLair.GetPosition();

                        if (gameManager.onEggDrop != null)
                        {
                            gameManager.onEggDrop(soldier, soldier.stealEggTarget);
                        }

                        soldier.stealEggTarget = null;
                    }
                }
                else
                {
                    soldier.targetPosition = soldier.stealEggTarget.GetPosition();

                    if (!soldier.stealEggTarget.behaviour.gameObject.activeInHierarchy)
                    {
                        soldier.stealEggTarget = null;
                    }
                    else if (Vector3.Distance(soldier.stealEggTarget.GetPosition(), soldier.GetPosition()) < SoldierConstants.EGG_STEAL_MIN_DISTANCE)
                    {
                        soldier.hasEgg = true;
                        soldier.behaviour.CarryEgg();
                        soldier.stealEggTarget.behaviour.gameObject.SetActive(false);

                        if (gameManager.onEggPickup != null)
                        {
                            gameManager.onEggPickup(soldier, soldier.stealEggTarget);
                        }
                    }
                }
            }
            else if (soldier.state == SoldierState.Attack)
            {
                if (soldier.attackTarget == null)
                {
                    var nearEnemySoldiers = new List <Soldier>();

                    foreach (var enemySoldier in gameManager.soldiers)
                    {
                        if (enemySoldier.owner != soldier.owner && Vector3.Distance(soldier.GetPosition(), enemySoldier.GetPosition()) < SoldierConstants.ATTACK_AGRO_MIN_DISTANCE)
                        {
                            nearEnemySoldiers.Add(enemySoldier);
                        }
                    }

                    if (nearEnemySoldiers.Count > 0)
                    {
                        soldier.state        = SoldierState.Attack;
                        soldier.attackTarget = nearEnemySoldiers[0];

                        if (gameManager.onSoldierChangeState != null)
                        {
                            gameManager.onSoldierChangeState(soldier, SoldierState.Attack);
                        }
                    }
                    else
                    {
                        soldier.state          = SoldierState.Protect;
                        soldier.targetPosition = soldier.GetPosition();

                        if (gameManager.onSoldierChangeState != null)
                        {
                            gameManager.onSoldierChangeState(soldier, SoldierState.Attack);
                        }
                    }
                }
                else if (Vector3.Distance(soldier.attackTarget.GetPosition(), soldier.GetPosition()) > SoldierConstants.ATTACK_DISTANCE)
                {
                    soldier.targetPosition = soldier.attackTarget.GetPosition();
                }
                else
                {
                    soldier.targetPosition = soldier.GetPosition();

                    soldier.attackTarget.health -= SoldierConstants.DAMAGE_RATE_SOLDIERS * Time.deltaTime;

                    if (soldier.attackTarget.state == SoldierState.Attack || soldier.attackTarget.state == SoldierState.Protect)
                    {
                        soldier.health -= SoldierConstants.DAMAGE_RATE_SOLDIERS * Time.deltaTime;
                    }

                    if (soldier.attackTarget.health <= 0)
                    {
                        soldier.attackTarget.isDead = true;
                    }

                    if (soldier.health <= 0)
                    {
                        soldier.isDead = true;
                    }
                }
            }
            else // SoldierState.Protect
            {
                soldier.targetPosition = soldier.GetPlayerProtectPosition();

                foreach (var enemySoldier in gameManager.soldiers)
                {
                    if (enemySoldier.owner != soldier.owner)
                    {
                        if (Vector3.Distance(soldier.GetPosition(), enemySoldier.GetPosition()) <= (SoldierConstants.ATTACK_AGRO_MIN_DISTANCE / 1.5f))
                        {
                            soldier.state        = SoldierState.Attack;
                            soldier.attackTarget = enemySoldier;

                            if (gameManager.onSoldierChangeState != null)
                            {
                                gameManager.onSoldierChangeState(soldier, SoldierState.Protect);
                            }

                            break;
                        }
                    }
                }
            }

            soldier.behaviour.MoveTo(soldier.targetPosition);
        }
예제 #2
0
 public static Vector3 GetPosition(this FoodPile foodPile)
 {
     return(foodPile.behaviour.transform.position);
 }
예제 #3
0
        public static void Reset(this GameManager gameManager)
        {
            gameManager.players.Clear();
            gameManager.soldiers.Clear();
            gameManager.eggs.Clear();
            gameManager.lairs.Clear();
            gameManager.foodPiles.Clear();
            gameManager.foodUnits.Clear();

            var players   = Object.FindObjectsOfType(typeof(PlayerBehaviour)) as PlayerBehaviour[];
            var lairs     = Object.FindObjectsOfType(typeof(LairBehaviour)) as LairBehaviour[];
            var foodPiles = Object.FindObjectsOfType(typeof(FoodPileBehaviour)) as FoodPileBehaviour[];
            var ias       = 0;

            foreach (var player in players)
            {
                if (player.IsBot)
                {
                    ias++;
                }
            }

            Core.Log.Debug("Found: {0} Players ({1} Bots), {2} Lairs, {3} FoodPiles", players.Length, ias, lairs.Length, foodPiles.Length);

            foreach (var playerBehaviour in players)
            {
                var player = new Player();
                player.lastEggTime = Time.fixedTime;
                player.behaviour   = playerBehaviour;

                if (player.behaviour.IsBot)
                {
                    player.ia = Baka.Instantiate(player);
                }

                gameManager.players.Add(player);

                playerBehaviour.playerData = player;
            }

            foreach (var player in gameManager.players)
            {
                for (int i = 0; i < GameConstants.START_SOLDIER_COUNT; ++i)
                {
                    SoldierExtension.Instantiate(player, player.GetPosition());
                }

                foreach (var soldier in gameManager.soldiers)
                {
                    soldier.state = SoldierState.Protect;
                    soldier.behaviour.transform.position = soldier.GetPlayerProtectPosition();
                }
            }

            foreach (var lairBehaviour in lairs)
            {
                var lair = new Lair();
                lair.behaviour = lairBehaviour;
                lair.foodCount = LairConstants.START_FOOD_COUNT;

                foreach (var player in gameManager.players)
                {
                    if (player.behaviour == lairBehaviour.owner)
                    {
                        lair.owner = player;
                        break;
                    }
                }

                gameManager.lairs.Add(lair);

                lairBehaviour.lairData = lair;
            }

            foreach (var foodPileBehaviour in foodPiles)
            {
                var foodPile = new FoodPile();
                foodPile.behaviour = foodPileBehaviour;

                gameManager.foodPiles.Add(foodPile);

                foodPileBehaviour.foodPileData = foodPile;
            }
        }