Пример #1
0
 void FixedUpdate()
 {
     if (timer >= GameDefines.UnitRangedAttackTimer)
     {
         timer = 0;
         Vector2 position     = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         Vector2 thisPosition = transform.position;
         Spear   spear        = GameData.Instance.GetSpear();
         thisPosition.y     += 0.235f;
         spear.body.position = thisPosition;
         spear.location      = position + new Vector2(Random.Range(-GameDefines.ProjectileMaxOffset, GameDefines.ProjectileMaxOffset), Random.Range(-GameDefines.ProjectileMaxOffset, GameDefines.ProjectileMaxOffset));
         spear.FixedUpdate();
     }
     else
     {
         timer += Time.fixedDeltaTime * GameData.Instance.gameSpeed;
     }
 }
Пример #2
0
    public void FixedUpdate()
    {
        if (unit != null)
        {
            bool lookForANewEnemy = false;
            if (unit.enemy != null && unit.enemy.health <= 0)
            {
                unit.enemy = null;

                if (unit.task == UnitTasks.Attack)
                {
                    lookForANewEnemy = true;
                }

                //if ( unit.prevTask == UnitTasks.MoveAndAttack )
                unit.SetPreviousTask();
            }
            if (unit.enemyBuilding != null && unit.enemyBuilding.health <= 0)
            {
                unit.enemyBuilding = null;

                if (unit.task == UnitTasks.AttackBuilding)
                {
                    lookForANewEnemy = true;
                }

                //if ( unit.prevTask == UnitTasks.MoveAndAttack )
                unit.SetPreviousTask();
            }
            if (unit.health <= 0)
            {
                GameData.Instance.kingdoms.Find(m => m.id == unit.kingdomID).DestroyUnit(unit);
                return;
            }
            else if (lookForANewEnemy)
            {
                SearchForEnemies(GameDefines.UnitCheckForEnemiesPassiveTime, true);
            }
        }
        else
        {
            GameData.Instance.ReturnUnitBody(this);
            return;
        }

        moving = false;
        unit.currentMapPosition = MapGenerator.Instance.GetPositionOnMap(unit.position);

        if (MapGenerator.Instance.IsTileBuilding(unit.currentMapPosition.x, unit.currentMapPosition.y, out short tileType) &&
            tileType - (short)TileTypes.Buildings == (short)BuildingTypes.Wall && MapGenerator.Instance.GetKingdom(unit.currentMapPosition.x, unit.currentMapPosition.y) == unit.kingdomID)
        {
            unit.onWall = true;
        }
        else
        {
            unit.onWall = false;
        }

        if (unit.unitType != UnitTypes.Peasant && (unit.task == UnitTasks.AttackBuilding || unit.task == UnitTasks.Attack ||
                                                   unit.task == UnitTasks.MoveAndAttack || (unit.task == UnitTasks.Guard && inPosition) ||
                                                   (unit.task != UnitTasks.Guard && GameData.Instance.kingdoms.Find(m => m.id == unit.kingdomID).underAttack)))
        {
            SearchForEnemies(unit.task == UnitTasks.MoveAndAttack ? GameDefines.UnitCheckForEnemiesTime : GameDefines.UnitCheckForEnemiesPassiveTime);
        }

        if (unit.task != UnitTasks.Guard)
        {
            inPosition = false;
        }

        if (unit.task == UnitTasks.GatherResources)
        {
            Vector2 pos = MapGenerator.Instance.GetPositionOnWorld(unit.taskLocation);
            Movement(pos);

            if (Vector2.Distance(unit.position, pos) < MapGenerator.Instance.halfCellSize.magnitude)
            {
                MapGenerator.Instance.RemoveTileFromTileMap(unit.taskLocation.x, unit.taskLocation.y);
                //GameData.Instance.kingdoms.Find ( m => m.id == unit.kingdomID ).peasants.Remove ( unit );
                //GameData.Instance.ReturnUnitBody ( this );
                moving = false;

                if (unit.tileType == TileTypes.Tree || unit.tileType == TileTypes.Stone)
                {
                    if (unit.tileType == TileTypes.Tree)
                    {
                        log.gameObject.SetActive(true);
                    }
                    else
                    {
                        stone.gameObject.SetActive(true);
                    }

                    unit.ReturnToLoiter();
                    unit.task = UnitTasks.ReturnResource;
                    Building castle = GameData.Instance.kingdoms.Find(m => m.id == unit.kingdomID).buildings.Find(m => m.buildingType == BuildingTypes.Castle);
                    unit.taskLocation    = castle.position;
                    unit.taskLocation.y -= 1;
                }
                else
                {
                    unit.ReturnToLoiter();
                }
            }
        }
        else if (unit.task == UnitTasks.ReturnResource)
        {
            Vector2 pos = MapGenerator.Instance.GetPositionOnWorld(unit.taskLocation);
            Movement(pos);

            if (Vector2.Distance(unit.position, pos) < MapGenerator.Instance.halfCellSize.magnitude)
            {
                log.gameObject.SetActive(false);
                stone.gameObject.SetActive(false);
                unit.ReturnToLoiter();
                moving = false;
            }
        }
        else if (unit.task == UnitTasks.BuildBuilding)
        {
            Vector2 pos = MapGenerator.Instance.GetPositionOnWorld(unit.taskLocation);
            if (Vector2.Distance(unit.position, pos) < MapGenerator.Instance.halfCellSize.magnitude)
            {
                attackBuildingAnimation  = true;
                attackDirection          = pos;
                unit.building.buildPerc += 1 * GameData.Instance.gameSpeed;
                if (unit.building.buildPerc >= 100)
                {
                    GameData.Instance.kingdoms.Find(m => m.id == unit.kingdomID).buildersWorking--;

                    if (unit.building.buildingType == BuildingTypes.House)
                    {
                        GameData.Instance.kingdoms.Find(m => m.id == unit.kingdomID).populationCap += GameDefines.PopulationCapIncrease;
                    }

                    MapGenerator.Instance.RemoveTileFromTileMap(unit.taskLocation.x, unit.taskLocation.y);
                    MapGenerator.Instance.AddTile(unit.building.buildingType, unit.taskLocation);
                    unit.building.buildPerc = 100;
                    unit.ReturnToLoiter();
                    //unit.building = null;
                    //GameData.Instance.kingdoms.Find ( m => m.id == unit.kingdomID ).peasants.Remove ( unit );
                    //GameData.Instance.ReturnUnitBody ( this );
                }
            }
            else
            {
                Movement(pos);
            }
        }
        else if (unit.task == UnitTasks.Move)
        {
            Vector2 pos = MapGenerator.Instance.GetPositionOnWorld(unit.taskLocation);
            Movement(pos);

            if (Vector2.Distance(unit.position, pos) < MapGenerator.Instance.halfCellSize.magnitude)
            {
                unit.ReturnToLoiter();
                unit.task = UnitTasks.Stay;
                moving    = false;
            }
        }
        else if (unit.task == UnitTasks.MoveAndAttack)
        {
            Vector2 pos = MapGenerator.Instance.GetPositionOnWorld(unit.taskLocation);
            Movement(pos);

            if (Vector2.Distance(unit.position, pos) < MapGenerator.Instance.halfCellSize.magnitude)
            {
                unit.ReturnToLoiter();
                unit.task = UnitTasks.Stay;
                moving    = false;
            }
        }
        else if (unit.task == UnitTasks.Guard)
        {
            if (Vector2.Distance(unit.position, unit.taskLocationVector) > GameDefines.UnitSpeed)
            {
                Movement(unit.taskLocationVector);
            }
            else
            {
                if (!inPosition)
                {
                    SearchForEnemies(unit.task == UnitTasks.MoveAndAttack ? GameDefines.UnitCheckForEnemiesTime : GameDefines.UnitCheckForEnemiesPassiveTime, true);
                }

                unit.SetPosition(unit.taskLocationVector);
                inPosition = true;
            }
        }
        else if (unit.task == UnitTasks.Attack)
        {
            float distance        = unit.unitType == UnitTypes.Archer ? GameDefines.RangedDistance(unit.onWall) : GameDefines.UnitAttackDistance;
            float distanceToEnemy = Vector2.Distance(unit.position, unit.enemy.position);

            if (distanceToEnemy >= distance)
            {
                Movement(unit.enemy.position);
            }
            else
            {
                if (unit.unitType != UnitTypes.Archer)
                {
                    attackBuildingAnimation = true;
                    attackDirection         = unit.enemy.position;
                }
                else
                {
                    if (attackTimer >= GameDefines.UnitRangedAttackTimer)
                    {
                        attackTimer = 0;
                        Vector2 thisPosition = unit.position;
                        Spear   spear        = GameData.Instance.GetSpear();
                        thisPosition.y     += 0.235f;
                        spear.onWall        = unit.onWall;
                        spear.target        = unit.enemy;
                        spear.body.position = thisPosition;
                        spear.location      = unit.enemy.GetMovementVector(unit.position) + new Vector2(UnityEngine.Random.Range(-GameDefines.ProjectileMaxOffset, GameDefines.ProjectileMaxOffset),
                                                                                                        UnityEngine.Random.Range(-GameDefines.ProjectileMaxOffset, GameDefines.ProjectileMaxOffset));
                        spear.FixedUpdate();
                    }
                    else
                    {
                        attackTimer += Time.fixedDeltaTime * GameData.Instance.gameSpeed;
                    }
                }
            }
        }
        else if (unit.task == UnitTasks.AttackBuilding)
        {
            float   distance        = unit.unitType == UnitTypes.Archer ? GameDefines.RangedDistance(unit.onWall) : GameDefines.UnitAttackDistance + MapGenerator.Instance.halfCellSize.x;
            Vector2 enemyPosition   = unit.enemyBuilding.positionVector;
            float   distanceToEnemy = Vector2.Distance(unit.position, enemyPosition);

            if (distanceToEnemy > distance)
            {
                Movement(enemyPosition);
            }
            else
            {
                if (unit.unitType != UnitTypes.Archer)
                {
                    attackBuildingAnimation = true;
                    attackDirection         = enemyPosition;
                }
                else
                {
                    if (attackTimer >= GameDefines.UnitRangedAttackTimer)
                    {
                        attackTimer = 0;
                        Vector2 thisPosition = unit.position;
                        Spear   spear        = GameData.Instance.GetSpear();
                        thisPosition.y      += 0.235f;
                        spear.onWall         = unit.onWall;
                        spear.targetBuilding = unit.enemyBuilding;
                        spear.body.position  = thisPosition;
                        spear.location       = enemyPosition + new Vector2(UnityEngine.Random.Range(-GameDefines.ProjectileMaxOffset, GameDefines.ProjectileMaxOffset),
                                                                           UnityEngine.Random.Range(-GameDefines.ProjectileMaxOffset, GameDefines.ProjectileMaxOffset));
                        spear.FixedUpdate();
                    }
                    else
                    {
                        attackTimer += Time.fixedDeltaTime * GameData.Instance.gameSpeed;
                    }
                }
            }
        }
        else if (unit.task == UnitTasks.Nothing)
        {
            if (!Mathf.Approximately(loiterTimer, 0) && loiter < loiterTimer)
            {
                loiter += Time.fixedDeltaTime * GameData.Instance.gameSpeed;
            }
            else
            {
                if (unit.taskLocation == Vector2Int.zero)
                {
                    Kingdom kingdom = GameData.Instance.kingdoms.Find(m => m.id == unit.kingdomID);
                    unit.taskLocation = new Vector2Int(UnityEngine.Random.Range(kingdom.sX, kingdom.eX + (kingdom.eX < MapGenerator.Instance.mapSize - 2 ? 1 : 0)),
                                                       UnityEngine.Random.Range(kingdom.sY, kingdom.eY + (kingdom.eY < MapGenerator.Instance.mapSize - 2 ? 1 : 0)));
                }

                Vector2 pos = MapGenerator.Instance.GetPositionOnWorld(unit.taskLocation);
                if (Vector2.Distance(unit.position, pos) < MapGenerator.Instance.halfCellSize.magnitude)
                {
                    Kingdom kingdom = GameData.Instance.kingdoms.Find(m => m.id == unit.kingdomID);
                    unit.taskLocation = new Vector2Int(UnityEngine.Random.Range(kingdom.sX, kingdom.eX + (kingdom.eX < MapGenerator.Instance.mapSize - 2 ? 1 : 0)),
                                                       UnityEngine.Random.Range(kingdom.sY, kingdom.eY + (kingdom.eY < MapGenerator.Instance.mapSize - 2 ? 1 : 0)));
                    loiter      = 0;
                    loiterTimer = UnityEngine.Random.Range(GameDefines.UnitLoiterTime, GameDefines.UnitLoiterTimeMax);
                }
                else
                {
                    Movement(pos, GameDefines.UnitHalfSpeed);
                }
            }
        }

        HoppingAnimation();
        if (attackBuildingAnimation)
        {
            BuildingAttackAnimation();
        }
    }