예제 #1
0
        //Actualize information label
        private void ActualizeInformation()
        {
            _possibleAttacks = new List <MapCoordinates>();
            var planedmoves = _c.PlannedMove;
            var movesstring = "";
            var positionToCountPossibleAttacksFrom = _c.PlannedMove != null && _c.PlannedMove.Count != 0 ? _c.PlannedMove.Last()  : _c.ActualPosition;

            for (var i = -1 * _c.AttackRange; i <= _c.AttackRange; i++)
            {
                for (var j = -1 * _c.AttackRange; j <= _c.AttackRange; j++)
                {
                    try
                    {
                        var possible = new MapCoordinates(positionToCountPossibleAttacksFrom.X + i,
                                                          positionToCountPossibleAttacksFrom.Y + j);
                        if (possible.Distance(positionToCountPossibleAttacksFrom) <= _c.AttackRange && possible.Distance(positionToCountPossibleAttacksFrom) > 0)
                        {
                            _possibleAttacks.Add(possible);
                        }
                    }
                    catch (IndexOutOfRangeException)
                    {
                    }
                }
            }

            if (planedmoves != null)
            {
                foreach (var move in planedmoves)
                {
                    movesstring += move.ToString() + Environment.NewLine;
                }
            }

            var attackString = "";

            foreach (var att in _possibleAttacks)
            {
                attackString += att.ToString() + Environment.NewLine;
            }


            informationLabel.Text =
                "Character HP: " + _c.Hp + Environment.NewLine +
                "Character order: " + _c.Order.ToString() + Environment.NewLine +
                "Character Planned Attack: " + _c.PlannedAttack?.Mc.ToString() + Environment.NewLine +
                "Character Planned Move: " + movesstring + Environment.NewLine +
                "Possible Attacks: " + attackString + Environment.NewLine;
        }
예제 #2
0
    //===================== map base function ================================

    //Astar
    public List <MapCoordinates> FindPath(MapCoordinates start, MapCoordinates end)
    {
        List <AStarNodeInfo> openList = new List <AStarNodeInfo>();

        bool[,] closeArray = new bool[mapGrid.width, mapGrid.height];


        var keyNode = new AStarNodeInfo();

        keyNode.pos     = start;
        keyNode.gWeight = 0;
        keyNode.hWeight = MapCoordinates.Distance(start, end);
        openList.Add(keyNode);

        Vector2Int[] neighbourOffsets = new Vector2Int[4]
        {
            new Vector2Int(-1, 0),
            new Vector2Int(1, 0),
            new Vector2Int(0, 1),
            new Vector2Int(0, -1)
        };
        while (openList.Count > 0)
        {
            AStarNodeInfo curNode = openList[0];
            openList.RemoveAt(0);
            //check if curNode is end
            if (curNode.pos.Equal(end))
            {
                keyNode = curNode;
                break;
            }

            //insert neighbours
            for (int i = 0; i < neighbourOffsets.Length; ++i)
            {
                MapCoordinates neighbourPos = new MapCoordinates(curNode.pos.X + neighbourOffsets[i].x, curNode.pos.Z + neighbourOffsets[i].y);
                if (!IsMapCoordCanMove(neighbourPos) || closeArray[neighbourPos.X, neighbourPos.Z])
                {
                    continue;
                }

                //check if openList has contained same node
                AStarNodeInfo sameNode = openList.Find((AStarNodeInfo node) => { return(node.pos.Equal(neighbourPos)); });
                if (sameNode != null)
                {
                    if (sameNode.gWeight > curNode.gWeight + 1)
                    {
                        sameNode.parent  = curNode;
                        sameNode.gWeight = curNode.gWeight + 1;
                    }
                    continue;
                }

                AStarNodeInfo newNode = new AStarNodeInfo();
                newNode.pos     = neighbourPos;
                newNode.gWeight = curNode.gWeight + 1;
                newNode.hWeight = MapCoordinates.Distance(neighbourPos, end);
                newNode.parent  = curNode;
                openList.Add(newNode);
            }
            closeArray[curNode.pos.X, curNode.pos.Z] = true;

            openList.Sort((AStarNodeInfo a, AStarNodeInfo b) =>
            {
                return((a.gWeight + a.hWeight) - (b.gWeight + b.hWeight));
            });
        }

        //return astar result-path
        List <MapCoordinates> path = new List <MapCoordinates>();

        while (keyNode != null)
        {
            path.Insert(0, keyNode.pos);
            keyNode = keyNode.parent;
        }

        return(path);
    }