Esempio n. 1
0
        public static Task MoveAsync(this INonPlayableEntity nonPlayableEntity, IDistanceCalculator distanceCalculator)
        {
            if (!nonPlayableEntity.IsAlive)
            {
                return(Task.CompletedTask);
            }

            if (!nonPlayableEntity.IsMoving || (nonPlayableEntity.Speed <= 0))
            {
                return(Task.CompletedTask);
            }

            var time = (SystemTime.Now() - nonPlayableEntity.LastMove).TotalMilliseconds;

            if (!(time > RandomHelper.Instance.RandomNumber(400, 3200)))
            {
                return(Task.CompletedTask);
            }

            var mapX = nonPlayableEntity.MapX;
            var mapY = nonPlayableEntity.MapY;

            if (!nonPlayableEntity.MapInstance.Map.GetFreePosition(ref mapX, ref mapY,
                                                                   (byte)RandomHelper.Instance.RandomNumber(0, 3),
                                                                   (byte)RandomHelper.Instance.RandomNumber(0, 3)))
            {
                return(Task.CompletedTask);
            }

            var distance = (int)distanceCalculator.GetDistance(new MapCell {
                X = nonPlayableEntity.PositionX, Y = nonPlayableEntity.PositionY
            }, new MapCell {
                X = mapX, Y = mapY
            });
            var value = 1000d * distance / (2 * nonPlayableEntity.Speed);

            Observable.Timer(TimeSpan.FromMilliseconds(value))
            .Subscribe(
                _ =>
            {
                nonPlayableEntity.PositionX = mapX;
                nonPlayableEntity.PositionY = mapY;
            });

            nonPlayableEntity.LastMove = SystemTime.Now().AddMilliseconds(value);
            return(nonPlayableEntity.MapInstance.SendPacketAsync(
                       nonPlayableEntity.GenerateMove(mapX, mapY)));
        }
Esempio n. 2
0
        public static void Move(this INonPlayableEntity nonPlayableEntity)
        {
            if (!nonPlayableEntity.IsAlive)
            {
                return;
            }

            if (nonPlayableEntity.IsMoving && nonPlayableEntity.Speed > 0)
            {
                var time = (DateTime.Now - nonPlayableEntity.LastMove).TotalMilliseconds;

                if (time > RandomFactory.Instance.RandomNumber(400, 3200))
                {
                    short mapX = nonPlayableEntity.MapX;
                    short mapY = nonPlayableEntity.MapY;
                    if (nonPlayableEntity.MapInstance.Map.GetFreePosition(ref mapX, ref mapY,
                                                                          (byte)RandomFactory.Instance.RandomNumber(0, 3),
                                                                          (byte)RandomFactory.Instance.RandomNumber(0, 3)))
                    {
                        var distance = (int)Heuristic.Octile(Math.Abs(nonPlayableEntity.PositionX - mapX),
                                                             Math.Abs(nonPlayableEntity.PositionY - mapY));
                        var value = 1000d * distance / (2 * nonPlayableEntity.Speed);
                        Observable.Timer(TimeSpan.FromMilliseconds(value))
                        .Subscribe(
                            _ =>
                        {
                            nonPlayableEntity.PositionX = mapX;
                            nonPlayableEntity.PositionY = mapY;
                        });

                        nonPlayableEntity.LastMove = DateTime.Now.AddMilliseconds(value);
                        nonPlayableEntity.MapInstance.Sessions.SendPacket(
                            nonPlayableEntity.GenerateMove(mapX, mapY));
                    }
                }
            }
        }