Exemple #1
0
        public static bool Execute(UnitOrder order)
        {
            //Debug.Log("Executing to pos - " + order.GetTo().Index);
            var index       = ChunkUtil.GetUpper(order.GetTo().Index);
            var chunkNumber = order.GetUnit().ChunkNumber;
            var chunk       = ChunkManager.GetChunkByNum(chunkNumber);

            if (order.GetUnit().CurrentPos != index &&
                ChunkUtil.IsAnyEntity(chunkNumber, index))
            {
                var obj = chunk.GetGameObjectByIndex(index);
                if (obj == null)
                {
                    return(GameMoveManager.CancelPathVVay(order.GetUnit())); //return false;
                }
                var underEnt = obj.GetComponent <GameEntity>();
                if (underEnt.Owner != order.GetUnit().Owner)
                {
                    return(AttackTo(order));
                }
                //Debug.Log("Cancel attacking");
                return(GameMoveManager.CancelPathVVay(order.GetUnit())); //return false;
            }
            //Debug.Log("Moving to: " + order.GetTo().Index);
            return(MoveExecutor.Execute(order));
        }
Exemple #2
0
        public static void CastAbility(GameUnit unit, Vector3Int posTarget, Ability ability)
        {
            if (unit == null)
            {
                return;
            }
            if (posTarget == Vector3Int.zero)
            {
                return;
            }
            var goodNeighbour =
                AI_Calculation.FindFreePosNearPos(unit.ChunkNumber, ChunkUtil.GetUpper(posTarget), false);

            if (goodNeighbour == Vector3Int.zero)
            {
                Debug.Log("Neighbour is zero");
                return;
            }
            var neighDist = Util.GetDistanceFromTo(goodNeighbour, posTarget);
            var dist      = Util.GetDistanceFromTo(unit.CurrentPos, posTarget);


            if (dist > 1)
            {
                if (neighDist > 1)
                {
                    Debug.Log("Error. Cant reach target");
                    return;
                }
                MoveToIndex(unit, ChunkUtil.GetDovvner(goodNeighbour));
            }

            GameOrderManager.AddCastAbilityOrder(unit, new TilePosition(ChunkUtil.GetUpper(posTarget)), ability);
        }
Exemple #3
0
        public static Vector3Int GetNearestNeighbour(int chunkNumber, Vector3Int myPos, Vector3Int pos)
        {
            var list = GetGoodNeighbors(chunkNumber, pos);

            var minDist = -1;
            var p       = new Vector3Int();

            foreach (var point in list)
            {
                var dist = GetDistTo(myPos, point, true);
                if (minDist != -1 && dist > minDist)
                {
                    continue;
                }
                if ((ChunkUtil.IsAnyEntity(chunkNumber, ChunkUtil.GetUpper(point)) &&
                     myPos != ChunkUtil.GetUpper(point)) ||
                    !ChunkUtil.IsCanStayHere(chunkNumber, ChunkUtil.GetDovvner(point)) ||
                    !PathCalcManager.IsReaching(ChunkUtil.GetDovvner(myPos), ChunkUtil.GetDovvner(point))
                    )
                {
                    continue;
                }

                minDist = dist;
                p       = point;
            }


            return(p);
        }
Exemple #4
0
 public static bool IsInvalidPoint(int chunkNumber, Vector3Int point)
 {
     return((point.z + 1 < ChunkManager.MaxGroundsLvls &&
             ChunkUtil.IsAnyEntity(chunkNumber, ChunkUtil.GetUpper(point))) ||
            ChunkUtil.IsGround(chunkNumber, point) ||
            ChunkUtil.IsEntity(chunkNumber, point));
 }
Exemple #5
0
        public virtual bool IsTimeForReproduction(GameUnit unit)
        {
            var lairPos = ChunkUtil.GetUpper(unit.GroupObj.GetLairPos());

            return(IsSetUpedLairPos(unit) &&
                   !SecondaryGroundLvL.IsEmptyPos(unit.ChunkNumber, lairPos) &&
                   SecondaryGroundLvL.GetGroundEnt(unit.ChunkNumber, lairPos).OriginalName == "building_lair");
        }
Exemple #6
0
        public static bool IsInvalidPath(Vector3Int point)
        {
            var chunkNumber = ChunkManager.CurrentChunk.ChunkNumber;

            return((point.z + 1 < ChunkManager.MaxGroundsLvls &&
                    ChunkUtil.IsAnyEntity(chunkNumber, ChunkUtil.GetUpper(point))) ||
                   ChunkUtil.IsMarkedIndex(chunkNumber, point) ||
                   ChunkUtil.IsEntity(chunkNumber, point));
        }
Exemple #7
0
        static void OnGroundEntity(GameEntity ent)
        {
            var eUP = ChunkManager.CurrentChunk.GetGameObjectByIndex
                          (ChunkUtil.GetUpper(ent.CurrentPos));

            if (eUP != null && IsIgnored(eUP) && IsVisionBefore(eUP))
            {
                SemiColor(ent, eUP);
            }
            else
            {
                HidingColor(ent, eUP);
            }
        }
Exemple #8
0
        public virtual bool IsTimeForDeliverItem(GameUnit unit)
        {
            var lairPos = ChunkUtil.GetUpper(unit.GroupObj.GetLairPos());

            if (unit.pickUped == null || !unit.GroupObj.IsSetUpedLairPos(unit) ||
                (ChunkUtil.IsAnyEntity(unit.ChunkNumber, lairPos) && unit.CurrentPos != lairPos))
            {
                return(false);
            }
            if (!SecondaryGroundLvL.IsEmptyPos(unit.ChunkNumber, lairPos))
            {
                return(ItemGroup.IsCanBeStacked(unit.pickUped.OriginalName,
                                                SecondaryGroundLvL.GetGroundEnt(unit.ChunkNumber, lairPos).OriginalName));
            }
            return(true);
        }
Exemple #9
0
        static void HidingColor(GameEntity ent, GameEntity eUP)
        {
            var check = false;

            if (eUP == null)
            {
                var friends =
                    AI_Calculation.GetGoodNeighbors
                        (ChunkManager.CurrentChunk.ChunkNumber, ent.CurrentPos);

                foreach (var friend in friends)
                {
                    var fUP = ChunkManager.CurrentChunk.GetGameObjectByIndex
                                  (ChunkUtil.GetUpper(friend));
                    if (fUP != null && !IsHided(fUP))
                    {
                        check = true;
                    }
                }
            }
            else
            {
                check = !IsHided(eUP);
            }
            if (!check)
            {
                var spD = ent.GetComponent <SpriteRenderer>();
                spD.color = static_nonVisionColor;
            }
            else
            {
                Coloring.RecolorObject(ent, true);

                Coloring.RecolorObject(ChunkUtil.GetUpper(ent.CurrentPos), true);

                Coloring.RecolorObject(ChunkUtil.GetDovvner(ent.CurrentPos), true);
            }
        }
Exemple #10
0
        private const float MinDist = 0.05f; //0.05f

        //False if countinue moving
        private static bool DoMove(UnitOrder order)
        {
            var ent    = order.GetUnit();
            var moveTo = order.GetTo();

            if (ent == null)
            {
                return(true);
            }
            var vec = ((moveTo.Pos3D + ent.ExtraPos) - ent.transform.position);

            //var idealVec = (moveTo.Pos3D - Util.Get3DPosByIndex(ent.CurrentPos)).normalized;


            if (Mathf.Abs(vec.x) <= MinDist && Mathf.Abs(vec.y) <= MinDist)
            {
                ent.transform.position = moveTo.Pos3D + ent.ExtraPos;
                ent.MovingTo           = null;

                var upperIndexFrom = ent.CurrentPos;
                var upperIndexTo   = ChunkUtil.GetUpper(moveTo.Index);

                var chunk = ChunkManager.GetChunkByNum(ent.ChunkNumber);

                chunk.MoveFromTo(upperIndexFrom, upperIndexTo);

                ent.CurrentPos = upperIndexTo;



                if (upperIndexFrom != upperIndexTo)
                {
                    PathCalcManager.CalculatePoint(upperIndexFrom);
                }


                GameOrderManager.RemoveMark(ent, moveTo.Index);

                return(true);
            }
            if (ChunkUtil.GetDovvner(ent.CurrentPos) != order.GetTo().Index&&
                PathFind.IsInvalidPath(order.GetTo().Index))
            {
                SimpleOrderManager.CancelOrders(ent);
            }

            ent.State    = EventManager.InProgressEvents.Move;
            ent.MovingTo = moveTo;
            var speedMod  = GameMoveManager.StaticMoveSpeed;
            var moveSpeed = ent.EntStats.MoveSpeed;

            var modVec = MultVector(vec, new Vector3(speedMod, speedMod, 1f));

            var speedVec =
                MultVector(modVec, new Vector3(moveSpeed, moveSpeed, 1f))
                // vec * speedMod
                * Time.deltaTime;

            if (Mathf.Abs(vec.x) <= MinDist * 3 && Mathf.Abs(vec.y) <= MinDist * 3)
            {
                speedVec *= 3;
            }


            ent.transform.Translate(speedVec);
            return(false);
        }
Exemple #11
0
        private static Collection <PathNode> GetNeighbours(PathNode pathNode,
                                                           Vector3Int goal)
        {
            var result = new Collection <PathNode>();

            // Соседними точками являются соседние по стороне клетки.
            Vector3Int[] neighbourPoints = new Vector3Int[4];
            neighbourPoints[0] = new Vector3Int(pathNode.Position.x + 1, pathNode.Position.y, pathNode.Position.z);
            neighbourPoints[1] = new Vector3Int(pathNode.Position.x - 1, pathNode.Position.y, pathNode.Position.z);
            neighbourPoints[2] = new Vector3Int(pathNode.Position.x, pathNode.Position.y + 1, pathNode.Position.z);
            neighbourPoints[3] = new Vector3Int(pathNode.Position.x, pathNode.Position.y - 1, pathNode.Position.z);

            var chunk = ChunkManager.CurrentChunk;

            foreach (var point in neighbourPoints)
            {
                // Проверяем, что не вышли за границы карты.
                if (point.x < 0 || point.x >= chunk.MapSize)
                {
                    continue;
                }
                if (point.y < 0 || point.y >= chunk.MapSize)
                {
                    continue;
                }

                if (ChunkUtil.IsStairsIndex(chunk.ChunkNumber, ChunkUtil.GetUpper(point)))
                {
                    var p = ChunkUtil.GetUpper(point);
                    point.Set(p.x, p.y, p.z);
                }
                else if (!ChunkUtil.IsAnyEntity(chunk.ChunkNumber, point) &&
                         ChunkUtil.IsStairsIndex(chunk.ChunkNumber, pathNode.Position))
                {
                    var p = ChunkUtil.GetDovvner(point);
                    point.Set(p.x, p.y, p.z);
                }


                if (point.z < 0 || point.z >= ChunkManager.MaxGroundsLvls)
                {
                    continue;
                }


                // Проверяем, что по клетке можно ходить.
                if (IsInvalidPath(point)
                    )
                {
                    continue;
                }
                // Заполняем данные для точки маршрута.
                var neighbourNode = new PathNode()
                {
                    Position            = point,
                    CameFrom            = pathNode,
                    PathLengthFromStart = pathNode.PathLengthFromStart +
                                          GetDistanceBetweenNeighbours(),
                    HeuristicEstimatePathLength = GetHeuristicPathLength(point, goal)
                };
                result.Add(neighbourNode);
            }
            return(result);
        }
Exemple #12
0
        public static void RecolorObject(GameEntity ent, bool trueCall)
        {
            if (ent == null || ((Recolor.IsHided(ent) && !trueCall) && ChunkManager.staticFogEnabled))
            {
                return;
            }


            var mod   = 3f;
            var upper = ChunkUtil.GetUpper(ent.CurrentPos);

            if ((ChunkUtil.IsAnyEntity(ent.ChunkNumber, upper) ||
                 !SecondaryGroundLvL.IsEmptyPos(ent.ChunkNumber, upper)) &&
                ent.CurrentPos.z + 1 != ChunkManager.MaxGroundsLvls)
            {
                mod += 2.5f;
            }


            var i     = 1f / ((ent.CurrentPos.z + mod) * 0.3f);
            var color = new Color(i, i, i);

            if (ChunkUtil.IsEntity(ent.ChunkNumber, upper) &&
                ent.CurrentPos.z + 1 != ChunkManager.MaxGroundsLvls)
            {
                var chunk    = ChunkManager.GetChunkByNum(ent.ChunkNumber);
                var upperEnt = chunk.GetGameObjectByIndex(upper);
                if (upperEnt != null)
                {
                    if (TreeGroup.IsTreeGroup(upperEnt.Group))
                    {
                        color.r = 0.5f;
                        color.b = 0.7f;
                        color.g = 0.3f;
                    }
                    else if (upperEnt.Group == "rock")
                    {
                        color.r = 0.3f;
                        color.b = 0.7f;
                        color.g = 0.2f;
                    }
                    else if (upperEnt is GameUnit)
                    {
                        var unit = upperEnt as GameUnit;
                        if (unit.IsEnemy(PlayersManager.GetMyPlayer()))
                        {
                            color.r = 1;
                            color.b = 0.3f;
                            color.g = 0.3f;
                        }
                        else
                        {
                            color.r = 0.1f;
                            color.b = 0.1f;
                            color.g = 0.6f;
                        }
                    }
                    else
                    {
                        color.r = 0.1f;
                        color.b = 0.1f;
                        color.g = 0.6f;
                    }
                }
            }

            if (!GroupUtil.isCreatureGroup(ent.Group) &&
                !GroupUtil.isBuilding(ent.Group))
            {
                var spriteRenderer = ent.GetComponent <SpriteRenderer>();
                spriteRenderer.color = color;
            }
        }
Exemple #13
0
        void RghtClickedOnGameObject(GameObject obj)
        {
            if (_choosed)
            {
                var from = _choosedObj.GetComponent <GameUnit>();
                if (from == null || !from.IsMy())
                {
                    return;
                }
                var to = obj.GetComponent <GameEntity>();

                if (to == null)
                {
                    return;
                }

                if (!GroupUtil.isCreatureGroup(from.Group))
                {
                    return;
                }


                if (!FieldOfView.IsVisible(to) && !GroupUtil.IsGround(to.Group))
                {
                    ClickedOnSpace();
                    return;
                }

                Vector3Int f;
                if (from.MovingTo != null)
                {
                    f = from.MovingTo.Index;
                }
                else
                {
                    f = from.CurrentPos;
                    f.z--;
                }

                //Cant move ?
                if (from.UpgradedStats.MoveSpeed <= 0f)
                {
                    SimpleOrderManager.CancelOrders(from);
                    ErrorBar_HTML.SetupError("This unit can`t move!");
                    return;
                }

                //Attack
                var t             = to.CurrentPos;
                var buildingCheck = false;
                if (ChunkUtil.IsAnyEntity(from.ChunkNumber, ChunkUtil.GetUpper(t)))
                {
                    var underGround = ChunkManager.CurrentChunk.GetGameObjectByIndex(ChunkUtil.GetUpper(t));
                    var underEnt    = underGround.GetComponent <GameEntity>();
                    if (SecondaryGroundLvL.isSecondaryGroup(underEnt.Group))
                    {
                        buildingCheck = true;
                    }

                    if (!GroupUtil.IsGround(underEnt.Group) && underEnt.Owner != PlayersManager.GetMyPlayer())
                    {
                        var underP = underEnt.CurrentPos;
                        underP.z++;

                        if (!ChunkManager.CurrentChunk.IsMapPos(underP) ||
                            !ChunkUtil.IsAnyEntity(from.ChunkNumber, ChunkUtil.GetUpper(underEnt.CurrentPos)))
                        {
                            if (from.UpgradedStats.Dmg > 0)
                            {
                                SimpleOrderManager.AttackToIndex(from, t);
                            }
                            else if (from.UpgradedStats.Dmg == 0)
                            {
                                ErrorBar_HTML.SetupError("This unit can`t attack!");
                            }
                            return;
                        }
                    }
                }


                //Cancel or Move
                if ((ChunkUtil.IsAnyEntity(from.ChunkNumber, ChunkUtil.GetUpper(t)) ||
                     !PathCalcManager.IsReaching(f, t)) && !buildingCheck)
                {
                    ErrorBar_HTML.SetupError("Can`t reach that place!");
                    SimpleOrderManager.CancelOrders(from);
                }
                else if (ChunkUtil.IsCanStayHere(from.ChunkNumber, t) || buildingCheck)
                {
                    SimpleOrderManager.MoveToIndex(from, t);
                }
            }
            else
            {
                var clicked = obj.GetComponent <GameEntity>();
                if (clicked == null)
                {
                    return;
                }

                var pos = clicked.CurrentPos;

                if (FlagManager.IsFlagAtPos(pos))
                {
                    FlagManager.RemoveFlag(pos);
                }
                else
                {
                    FlagManager.SetupFlag(pos);
                }
            }
        }
Exemple #14
0
        public static bool AttackTo(UnitOrder order)
        {
            var ent    = order.GetUnit();
            var moveTo = order.GetTo();

            if (ent == null)
            {
                return(true);
            }
            var vec = (moveTo.Pos3D + ent.ExtraPos) - ent.transform.position;

            var chunk       = ChunkManager.GetChunkByNum(ent.ChunkNumber);
            var attackedObj = chunk.GetGameObjectByIndex(ChunkUtil.GetUpper(moveTo.Index));

            if (attackedObj == null)
            {
                return(MoveTo(ent));
            }

            var attackedEnt = attackedObj.GetComponent <GameUnit>();

            if (attackedEnt == null)
            {
                return(true);
            }
            if (attackedEnt.Owner == ent.Owner)
            {
                return(MoveTo(ent));
            }

            UnitBar_HTML.SetupUnitBar(attackedEnt);
            ent.State = EventManager.InProgressEvents.Attack;

            if (!ent.AlreadyAttacked && Mathf.Abs(vec.x) <= MinDist && Mathf.Abs(vec.y) <= MinDist)
            {
                // ent.transform.position = ent.Current3Dpos;
                ent.AlreadyAttacked = true;


                attackedEnt.DealDamage(ent.UpgradedStats.Dmg, ent);

                return(false);
            }
            if (ent.AlreadyAttacked)
            {
                var checkMove = MoveTo(ent);
                if (checkMove)
                {
                    ent.AlreadyAttacked = false;
                }

                return(false);
            }


            ent.MovingTo = moveTo;
            var moveSpeed = GameMoveManager.StaticMoveSpeed;

            var speedVec = vec * moveSpeed / 2f * Time.deltaTime;


            ent.transform.Translate(speedVec);
            return(false);
        }