Exemplo n.º 1
0
        public void UpdateGame()
        {
            if (Followee != null)
            {
                Target = Followee.Pos;
            }
            if (Target == null)
            {
                return;
            }
            if (_krtransform.Pos == Target)
            {
                Target   = null;
                Followee = null;                 // TEST
                return;
            }

            if (_currentFrame > 0)
            {
                --_currentFrame;
                return;
            }
            else
            {
                _currentFrame = Speed;
            }

            _waypoints = KRFacade.FindPath(_krtransform.Pos, Target);
            __waynodes.AddRange(_waypoints.Where(w => !__waynodes.Contains(w)));

            if (_waypoints == null)
            {
                Target = null;
            }
            else
            {
                QuadTreeNode <KRTransform> node;
                Vec2 nextPos = _krtransform.Pos;

                if (!_waypoints.Any())
                {
                    nextPos = Target;
                }
                else
                {
                    bool canTraceStraightLine = true;
                    do
                    {
                        node    = _waypoints.ElementAtOrDefault(1);
                        nextPos = (node == null) ? Target : node.Pos;
                        Bresenham.Line(_krtransform.Pos.X, _krtransform.Pos.Y, nextPos.X, nextPos.Y, delegate(int x, int y) {
                            if (!KRFacade.IsEmpty(new Vec2(x, y)))
                            {
                                canTraceStraightLine = false;
                                if (nextPos == Target)
                                {
                                    nextPos = _waypoints.First().Pos;
                                }
                                return(false);
                            }
                            return(true);
                        });
                        if (canTraceStraightLine)
                        {
                            if (nextPos != Target)
                            {
                                _waypoints = _waypoints.Skip(1);
                            }
                        }
                        else
                        {
                            node    = _waypoints.FirstOrDefault();
                            nextPos = (node == null) ? Target : node.Pos;
                        }
                    } while(canTraceStraightLine && nextPos != Target);
                }

                var lt = new List <Vec2>();

                Bresenham.Line(_krtransform.Pos.X, _krtransform.Pos.Y, nextPos.X, nextPos.Y, delegate(int x, int y) {
                    if (lt.Count == 0)
                    {
                        nextPos = new Vec2(x, y);
                    }
                    lt.Add(new Vec2(x, y));
                    return(true);
                });
                if (nextPos != _krtransform.Pos && KRFacade.IsEmpty(nextPos))
                {
                    KRFacade.Remove(GetComponent <KRTransform>());
                    _krtransform.Pos = nextPos;
                    KRFacade.Add(GetComponent <KRTransform>());
#if UNITY_EDITOR
                    __way.Add(_krtransform.Pos);
#endif
                }
                else
                {
                    _waypoints = null;
                    Target     = null;
                    Followee   = null;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Find all gameObjects in the rect define by v1 and v2 and v3.
        /// </summary>
        public static IEnumerable <GameObject> Find(Vec2 v1, Vec2 v2, Vec2 v3)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            Vec2 v4 = v2 - (v3 - v1);

#if !UNITY_EDITOR
            __walkedNode.Clear();
            __walkedFind.Clear();
            __walkedFind.Add(v1);
            __walkedFind.Add(v2);
            __walkedFind.Add(v3);
            __walkedFind.Add(v4);
#endif

            // bottomLeft, bottomRight, topLeft, topRight
            Vec2[] vecs = { v1, v2, v3, v4 };
            Array.Sort <Vec2>(vecs);
            Vec2 bottomLeft = vecs[0], bottomRight = vecs[2], topLeft = vecs[1], topRight = vecs[3];

            // boundBottomLeft, boundBottomRight, boundTopLeft, boundTopRight
            IEnumerable <int> xs = vecs.Select(v => v.X);
            IEnumerable <int> ys = vecs.Select(v => v.Y);

            int minXS = xs.Min();
            int maxXS = xs.Max();
            int minYS = ys.Min();
            int maxYS = ys.Max();

            Func <Vec2, Vec2, IList <Vec2> > _getLine = delegate(Vec2 orig, Vec2 dest) {
                IList <Vec2> d = new List <Vec2>();
                d.Add(orig);
                Bresenham.Line(orig.X, orig.Y, dest.X, dest.Y, delegate(int x, int y) {
                    d.Add(new Vec2(x, y));
                    return(true);
                });
                return(d);
            };

            IList <Vec2> bltp = _getLine(bottomLeft, topLeft);
            IList <Vec2> tltr = _getLine(topLeft, topRight);
            IList <Vec2> trbr = _getLine(topRight, bottomRight);
            IList <Vec2> brbl = _getLine(bottomRight, bottomLeft);

            // FIXME
            Func <Vec2, bool> _in = delegate(Vec2 v) {
                int x = v.X, y = v.Y;
                int ax = bottomLeft.X, bx = topLeft.X, dx = bottomRight.X;
                int ay = bottomLeft.Y, by = topLeft.Y, dy = bottomRight.Y;
                int ex = bx - ax, ey = by - ay, fx = dx - ax, fy = dy - ay;

                if ((x - ax) * ex + (y - ay) * ey < 0)
                {
                    return(false);
                }
                if ((x - bx) * ex + (y - by) * ey > 0)
                {
                    return(false);
                }
                if ((x - ax) * fx + (y - ay) * fy < 0)
                {
                    return(false);
                }
                if ((x - dx) * fx + (y - dy) * fy > 0)
                {
                    return(false);
                }

                return(true);
            };
            Func <Vec2, bool> _on = delegate(Vec2 v) {
                return(bltp.Contains(v) || tltr.Contains(v) || trbr.Contains(v) || brbl.Contains(v));
            };

            List <GameObject> gos = new List <GameObject>();

            KRTransform u;
            for (int x = minXS; x < maxXS; ++x)
            {
                for (int y = minYS; y < maxYS; ++y)
                {
                    Vec2 c = new Vec2(x, y);
                    if (_in(c) || _on(c))
                    {
#if !UNITY_EDITOR
                        __walkedNode.Add(c);
#endif
                        u = _Map.Find(c);
                        if (u != null)
                        {
                            gos.Add(u.gameObject);
                        }
                    }
                }
            }

            stopwatch.Stop();
            Debug.Log(String.Format("DragFind :: time elapsed: {0}", stopwatch.Elapsed));

            return(gos);
        }
Exemplo n.º 3
0
 public List <Coordinate> First()
 {
     return(Bresenham.Line(new Coordinate(0, 0, 0), new Coordinate(10, 3, 0)).ToList());
 }