GetDistance() public method

Returns distance between this and another position.
public GetDistance ( Position otherPos ) : int
otherPos Position
return int
Ejemplo n.º 1
0
		public void GetDistance()
		{
			var pos1 = new Position(0, 0);
			var pos2 = new Position(0, 500);
			var pos3 = new Position(0, -500);
			var pos4 = new Position(500, 500);
			var pos5 = new Position(-500, -500);

			Assert.Equal(500, pos1.GetDistance(pos2));
			Assert.Equal(500, pos1.GetDistance(pos3));
			Assert.Equal(707, pos1.GetDistance(pos4));
			Assert.Equal(707, pos1.GetDistance(pos5));
			Assert.Equal(1414, pos4.GetDistance(pos5));
		}
Ejemplo n.º 2
0
        /// <summary>
        /// Activates AIs in range of the movement path.
        /// </summary>
        /// <param name="creature"></param>
        /// <param name="from"></param>
        /// <param name="to"></param>
        public void ActivateAis(Creature creature, Position from, Position to)
        {
            // Bounding rectangle
            var minX = Math.Min(from.X, to.X) - VisibleRange;
            var minY = Math.Min(from.Y, to.Y) - VisibleRange;
            var maxX = Math.Max(from.X, to.X) + VisibleRange;
            var maxY = Math.Max(from.Y, to.Y) + VisibleRange;

            // Activation
            _creaturesRWLS.EnterReadLock();
            try
            {
                foreach (var npc in _creatures.Values.OfType <NPC>())
                {
                    if (npc.AI == null)
                    {
                        continue;
                    }

                    var pos = npc.GetPosition();
                    if (!(pos.X >= minX && pos.X <= maxX && pos.Y >= minY && pos.Y <= maxY))
                    {
                        continue;
                    }

                    var time = (from.GetDistance(to) / creature.GetSpeed()) * 1000;

                    npc.AI.Activate(time);
                }
            }
            finally
            {
                _creaturesRWLS.ExitReadLock();
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Activates AIs in range of the movement path.
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        public void ActivateAis(Creature creature, Position from, Position to)
        {
            // Bounding rectangle
            var minX = Math.Min(from.X, to.X) - VisibleRange;
            var minY = Math.Min(from.Y, to.Y) - VisibleRange;
            var maxX = Math.Max(from.X, to.X) + VisibleRange;
            var maxY = Math.Max(from.Y, to.Y) + VisibleRange;

            // Activation
            _creaturesRWLS.EnterReadLock();
            try
            {
                foreach (NPC npc in _creatures.Values.Where(a => a.Is(EntityType.NPC)))
                {
                    if (npc.AI == null)
                        continue;

                    var pos = npc.GetPosition();
                    if (!(pos.X >= minX && pos.X <= maxX && pos.Y >= minY && pos.Y <= maxY))
                        continue;

                    var time = (from.GetDistance(to) / creature.GetSpeed()) * 1000;

                    npc.AI.Activate(time);
                }
            }
            finally
            {
                _creaturesRWLS.ExitReadLock();
            }
        }
Ejemplo n.º 4
0
		public void GetRandomInRange()
		{
			var rnd = new Random(Environment.TickCount);
			var pos = new Position(10, 10);

			for (int i = 0; i < 10000; ++i)
			{
				var rndPos1 = pos.GetRandomInRange(10, rnd);
				Assert.InRange(pos.GetDistance(rndPos1), 0, 11);

				var rndPos2 = pos.GetRandomInRange(5, 10, rnd);
				Assert.InRange(pos.GetDistance(rndPos2), 4, 11);
			}
		}