Example #1
0
    void OnDrawGizmosSelected()
    {
        if (unit == null)
        {
            return;
        }

        if (unit.enemy != null)
        {
            GameData.Instance.circleIndicator1.position = unit.enemy.unitMB.body.position;
            GameData.Instance.circleIndicator1.GetComponent <SpriteRenderer> ().sortingOrder = unit.enemy.unitMB.spriteRenderer.sortingOrder - 1;
        }

        GameData.Instance.circleIndicator2.localScale = new Vector3(GameDefines.UnitFieldOfView * 2f, GameDefines.UnitFieldOfView * 2f, 1f);
        GameData.Instance.circleIndicator2.position   = unit.position;
        GameData.Instance.circleIndicator2.GetComponent <SpriteRenderer> ().sortingOrder = spriteRenderer.sortingOrder - 2;
        GameData.Instance.circleIndicator3.localScale = unit.unitType == UnitTypes.Archer ? new Vector3(GameDefines.RangedDistance(unit.onWall) * 2f, GameDefines.RangedDistance(unit.onWall) * 2f, 1f) :
                                                        new Vector3(GameDefines.UnitAttackDistance * 2f, GameDefines.UnitAttackDistance * 2f, 1f);
        GameData.Instance.circleIndicator3.position = unit.position;
        GameData.Instance.circleIndicator3.GetComponent <SpriteRenderer> ().sortingOrder = spriteRenderer.sortingOrder - 1;
    }
Example #2
0
    public void FixedUpdate()
    {
        if (!landed)
        {
            if (Mathf.Approximately(startingDistance, 0))
            {
                startingDistance = Vector2.Distance(body.position, location);
                position         = body.position;
                lastPosition     = body.position;
                startingAtan     = Mathf.Atan2(location.y - position.y, location.x - position.x);
                maxHeight        = GameDefines.ProjectileHeight * (startingDistance / GameDefines.RangedDistance(onWall)) + Mathf.Abs(location.y - position.y) / 2f;
            }

            float currentDistance = Vector2.Distance(position, location);

            if (currentDistance <= GameDefines.ProjectileSpeed * GameData.Instance.gameSpeed)
            {
                landed        = true;
                body.position = location;
                float angle = Mathf.Atan2(location.y - lastPosition.y, location.x - lastPosition.x) * Mathf.Rad2Deg;
                body.eulerAngles = new Vector3(0, 0, angle - 90);
                DamageTarget();
            }
            else
            {
                position.x     += GameDefines.ProjectileSpeed * GameData.Instance.gameSpeed * Mathf.Cos(startingAtan);
                position.y     += GameDefines.ProjectileSpeed * GameData.Instance.gameSpeed * Mathf.Sin(startingAtan);
                currentDistance = Vector2.Distance(position, location);
                Vector2 tempPosition = position;
                float   distancePerc = currentDistance / startingDistance;
                tempPosition.y += Mathf.Lerp(maxHeight * (1 - distancePerc), maxHeight * distancePerc, 1 - distancePerc);
                float angle = Mathf.Atan2(tempPosition.y - lastPosition.y, tempPosition.x - lastPosition.x) * Mathf.Rad2Deg;
                body.position    = tempPosition;
                body.eulerAngles = new Vector3(0, 0, angle - 90);
                lastPosition     = tempPosition;
            }
        }
        else if (timer >= maxTimer)
        {
            GameData.Instance.ReturnSpear(this);
        }
        else
        {
            timer += Time.fixedDeltaTime;
        }
    }
Example #3
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();
        }
    }