示例#1
0
 public static void SetMousePos(vector vector)
 {
     Mouse.SetPosition((int)vector.x, (int)vector.y);
 }
示例#2
0
        internal static void Update()
        {
            if (!engine.HasFocus()) return;

            _wasDown.Clear();
            _keysDown.ForEach((item) =>
            {
                _wasDown.Add(item);
            });
            _keysDown.Clear();

            _mouseState = Mouse.GetState();
            _keyboardState = Keyboard.GetState();

            inputstring = "";
            foreach (Key key in Enum.GetValues(typeof(Key)))
            {
                if (_keyboardState.IsKeyDown(key) && engine.HasFocus())
                {
                    _keysDown.Add(key);

                    if (_wasDown.Contains(key)) continue;

                    if (key == Key.Space)           inputstring += " ";
                    else if (key == Key.Quote)      inputstring += '"';
                    else if (key == Key.LBracket)   inputstring += '(';
                    else if (key == Key.RBracket)   inputstring += ')';
                    else if (key == Key.Minus)      inputstring += '_';
                    else if (key == Key.Plus)       inputstring += '+';
                    else if (key == Key.Period)     inputstring += '.';

                    else if (key == Key.Number0)    inputstring += "0";
                    else if (key == Key.Number1)    inputstring += "1";
                    else if (key == Key.Number2)    inputstring += "2";
                    else if (key == Key.Number3)    inputstring += "3";
                    else if (key == Key.Number4)    inputstring += "4";
                    else if (key == Key.Number5)    inputstring += "5";
                    else if (key == Key.Number6)    inputstring += "6";
                    else if (key == Key.Number7)    inputstring += "7";
                    else if (key == Key.Number8)    inputstring += "8";
                    else if (key == Key.Number9)    inputstring += "9";

                    else
                    {
                        string c = key.ToString().ToLower();
                        if (c.Length == 1) inputstring += c;
                    }
                }
            }

            var mp = GetMousePos();
            mousepos = new vector((int) engine.windowWidth / 2 - mp.x, (int) engine.windowHeight / 2 - mp.y);

            if (cvarMouselock.Valueb())
            {
                if (mouselocked && !mouselock) mouselocked = false;
                if (!mouselocked && mouselock && engine.HasFocus()) mouselocked = mouselock;

                if (mouselocked) SetMousePos(new vector((int) engine.windowWidth / 2, (int) engine.windowHeight / 2));
                SetMouseVisible(!mouselocked);
            }
            else
            {
                SetMouseVisible(true);
            }
        }
示例#3
0
 public static void GeneratePathTo(vector start, vector goal, vector offset, ref List <vector> path)
 {
     path = Task.Run(() => GeneratePath(start, goal, offset)).Result;
 }
示例#4
0
        private static List <vector> GeneratePath(vector start, vector goal, vector offset)
        {
            var path      = new List <vector>();
            var open      = new List <vector>();
            var closed    = new List <vector>();
            var cameFrom  = new Dictionary <vector, vector>();
            var costSoFar = new Dictionary <vector, double>();

            cameFrom.Clear();
            costSoFar.Clear();
            var frontier = new pqueue <vector>();

            if (world.IsSolid(new vector(goal.x, goal.y)))
            {
                return(path);
            }

            var clock = new Stopwatch();

            //logger.WriteLine(string.Format("calculating path from {0} to {1}", start, goal));

            frontier.Enqueue(start, 0);
            cameFrom[start]  = start;
            costSoFar[start] = 0;

            while (frontier.Count > 0)
            {
                var current = frontier.Dequeue();
                if (current.Equals(goal))
                {
                    break;
                }

                if (clock.Elapsed.TotalSeconds > 1)
                {
                    break;
                }

                // 8 dir movement
                for (var i = 0; i < 8; i++)
                {
                    var next = GetNeighbours(current)[i];

                    // check all axis of movement
                    if (world.IsSolid(new vector(next.x, next.y)) || world.IsSolid(new vector(current.x, next.y)) ||
                        world.IsSolid(new vector(next.x, current.y)))
                    {
                        continue;
                    }

                    if (cameFrom.ContainsValue(next))
                    {
                        continue;
                    }

                    // set cost by heuristic
                    var newCost = costSoFar[current] + (next.x == current.x || next.y == current.y ? 0.5f : 0.46f) +
                                  engine.random.Next(0, 30) / 100;
                    if (!costSoFar.ContainsKey(next) || newCost < costSoFar[next])
                    {
                        costSoFar[next] = newCost;
                        var priority = newCost + Heuristic(next, goal);
                        frontier.Enqueue(next, priority);
                        cameFrom[next] = current;

                        closed.Add(next);
                    }
                }
            }

            path.Clear();
            var c = goal;

            while (c != start)
            {
                open.Add(c);
                path.Add(c + offset);
                if (!cameFrom.ContainsKey(c))
                {
                    return(path);
                }
                c = cameFrom[c];
            }

            path.Add(start + offset); // optional: for complete path
            path.Reverse();

            //logger.WriteLine(string.Format("path calculations finished. (length {0}, took {1}ms)", came_from.Count, clock.ElapsedTime.AsMilliseconds()));
            //clock.Dispose();

            return(path);
        }
示例#5
0
        //TODO: talk on sources

        /*
         *
         *  A* PATHFINDING ALOGORITHM
         *
         *  www.redblobgames.com/pathfinding/a-star/implementation.html
         *
         */

        public static double Heuristic(vector a, vector b)
        {
            return(Math.Abs(a.x - b.x) + Math.Abs(a.y - b.y));
        }