Beispiel #1
0
        public void ActivateMobs(MabiCreature creature, MabiVertex from, MabiVertex to)
        {
            IEnumerable<MabiCreature> mobsInRange = _creatures.Where(c =>
                c.Region == creature.Region
                && c is MabiNPC
                && ((MabiNPC)c).AIScript != null);

            long leftX, rightX, topY, bottomY; //Bounding rectangle coordinates

            if (from.X < to.X) //Moving right
            {
                leftX = from.X - 2600;
                rightX = to.X + 2600;
            }
            else
            {
                leftX = to.X - 2600;
                rightX = from.X + 2600;
            }

            if (from.Y < to.Y) //Moving up
            {
                bottomY = from.Y - 2600;
                topY = to.Y + 2600;
            }
            else
            {
                bottomY = to.Y - 2600;
                topY = from.Y + 2600;
            }

            //Linear movement equation
            double slope;
            if (to.Y == from.Y)
            {
                slope = .001; //double.MinValue produces infinity in B
            }
            else
            {
                slope = ((double)to.Y - from.Y) / ((double)to.X - from.X);
            }
            double b = from.Y - slope * from.X;

            mobsInRange = mobsInRange.Where((c) =>
            {
                var pos = c.GetPosition();
                return (leftX < pos.X && pos.X < rightX && bottomY < pos.Y && pos.Y < topY && (Math.Abs(pos.Y - (long)(slope * pos.X + b)) < 2600));
            });

            double dist = Math.Sqrt(((to.X - from.X) * (to.X - from.X)) + ((to.Y - from.Y) * (to.Y - from.Y)));

            uint time = (uint)Math.Ceiling(dist / creature.GetSpeed());

            foreach (var mob in mobsInRange)
            {
                ((MabiNPC)mob).AIScript.Activate(time);
            }
        }