Exemplo n.º 1
0
        public void MoveTowardsPlayer(FloatPosition playerPosition)
        {
            var diff = playerPosition - RealPosition;

            diff.Normalize();
            var nextPosition = RealPosition + diff * (0.25f * Global.DistancePerBlock);

            if (!MoveRequested(nextPosition))
            {
                currentState = State.BlockedToPlayer;
                return;
            }

            targetTilePosition = new Position(Util.Round(playerPosition.X / Global.DistancePerBlock), Util.Round(playerPosition.Y / Global.DistancePerBlock));
            direction          = lastTilePosition.GetDirectionTo(targetTilePosition);
            currentState       = State.MovingTowardsPlayer;
            NextMoveTimeSlot   = uint.MaxValue;
            lastMoveTicks      = game.CurrentTicks;

            var distance = Math.Min(0.25f, RealPosition.GetMaxDistance(playerPosition) / Global.DistancePerBlock);

            if (distance < 0.01f * Global.DistancePerBlock)
            {
                currentState = RealPosition == targetTilePosition * Global.DistancePerBlock
                    ? State.IdleOnTile : State.Idle;
                return;
            }

            ticksPerMovement = (uint)Util.Round(distance * TicksPerMovement);
        }
 public static string ToCssClass(this FloatPosition position, Breakpoint breakpoint)
 {
     return(new CssClassBuilder("float")
            .Append(() => breakpoint.GetCssClassPart(), () => breakpoint != Breakpoint.None)
            .Append(() => position.GetCssClassPart())
            .Build());
 }
Exemplo n.º 3
0
        public void Update(uint ticks, FloatPosition playerPosition, bool moveRandom, bool canSeePlayer,
                           bool onlyMoveWhenSeePlayer, bool monster)
        {
            if (Paused)
            {
                return;
            }

            switch (currentState)
            {
            case State.IdleOnTile:
                if (canSeePlayer)
                {
                    if (monster)
                    {
                        movedTicks = 0;
                        MoveTowardsPlayer(playerPosition);
                    }
                    else if (moveRandom && game.GameTime.TimeSlot >= NextMoveTimeSlot)
                    {
                        ResetMovementTimer();
                        RandomMovementRequested?.Invoke();
                    }
                }
                else if (!onlyMoveWhenSeePlayer && moveRandom && game.GameTime.TimeSlot >= NextMoveTimeSlot)
                {
                    ResetMovementTimer();
                    RandomMovementRequested?.Invoke();
                }
                break;

            case State.Idle:
                if (canSeePlayer)
                {
                    if (monster)
                    {
                        movedTicks = 0;
                        MoveTowardsPlayer(playerPosition);
                    }
                    else if (moveRandom && game.GameTime.TimeSlot >= NextMoveTimeSlot)
                    {
                        ResetMovementTimer();
                        MoveToTile((uint)targetTilePosition.X, (uint)targetTilePosition.Y);
                    }
                }
                else if (!onlyMoveWhenSeePlayer && moveRandom && game.GameTime.TimeSlot >= NextMoveTimeSlot)
                {
                    ResetMovementTimer();
                    MoveToTile((uint)targetTilePosition.X, (uint)targetTilePosition.Y);
                }
                break;

            case State.MovingToTile:
            {
                if (monster && canSeePlayer)
                {
                    movedTicks       = 0;
                    lastTilePosition = Position;
                    MoveTowardsPlayer(playerPosition);
                }
                else
                {
                    if (ticks > lastMoveTicks)
                    {
                        uint moveTicks = Math.Min(ticks - lastMoveTicks, ticksPerMovement - movedTicks);
                        lastMoveTicks = ticks;
                        var diff = targetTilePosition - lastTilePosition;
                        diff.Normalize();
                        float stepSize = moveTicks * Global.DistancePerBlock / TicksPerMovement;

                        RealPosition.X += diff.X * stepSize;
                        RealPosition.Y += diff.Y * stepSize;
                        movedTicks     += moveTicks;

                        if (movedTicks == ticksPerMovement)     // finished movement
                        {
                            currentState     = State.IdleOnTile;
                            lastTilePosition = Position;
                        }
                    }
                }

                break;
            }

            case State.MovingTowardsPlayer:
            {
                if (!canSeePlayer)
                {
                    LostPlayer();
                    return;
                }

                uint moveTicks = Math.Min(ticks - lastMoveTicks, ticksPerMovement - movedTicks);
                lastMoveTicks = ticks;
                var diff = playerPosition - RealPosition;
                diff.Normalize();
                float stepSize = moveTicks * Global.DistancePerBlock / TicksPerMovement;

                RealPosition.X += diff.X * stepSize;
                RealPosition.Y += diff.Y * stepSize;
                movedTicks     += moveTicks;

                if (movedTicks == ticksPerMovement)     // finished movement
                {
                    currentState     = State.IdleOnTile;
                    lastTilePosition = Position;
                    movedTicks       = 0;
                    MoveTowardsPlayer(playerPosition);
                }

                break;
            }

            case State.BlockedToPlayer:
            {
                if (!canSeePlayer)
                {
                    LostPlayer();
                    return;
                }

                MoveTowardsPlayer(playerPosition);

                break;
            }
            }
        }
 public void ToCssClassSucceeds(FloatPosition position, Breakpoint breakpoint, string expected)
 {
     position.ToCssClass(breakpoint).Should().Be(expected);
 }
Exemplo n.º 5
0
 public static float GetMaxDistance(this FloatPosition position, FloatPosition target)
 {
     return(Math.Max(Math.Abs(target.X - position.X), Math.Abs(target.Y - position.Y)));
 }