/// <summary>
        /// Constructs movement data.
        /// </summary>
        /// <param name="walk">Must be a valid movement. CANNOT be a noop.</param>
        /// <param name="run">Optional, if set, movement is will be interpreted as running.</param>
        public MoveMessage(DirectionDelta walk, DirectionDelta run)
        {
            Debug.Assert(!walk.IsNoop());

            Dir1 = walk;
            Dir2 = run;

            IsWalking = Dir2.IsNoop();
        }
        /// <summary>
        /// Generates direction deltas to move an entity from the beginning position <see cref="us"/> to the target ending position <see cref="target"/>.
        /// This algorithm does not generate deltas which navigate around obsticles.
        /// This algorithim will cause undefined behaviour if any other <see cref="DirectionDelta"/> were to be applied to entity for which directions are being generated.
        /// If <see cref="us"/> and <see cref="target"/> are equal, this algorithm will return noop directions.
        /// </summary>
        /// <param name="us">The starting position.</param>
        /// <param name="target">The goal.</param>
        /// <returns>A lazy <see cref="IEnumerable{T}"/> containing DirectionDeltas that lead to <see cref="target"/></returns>
        public static IEnumerable <DirectionDelta> WalkTo(IPosition us, IPosition target)
        {
            /*
             * Starting position is only used once to initialize a starting location,
             * to which the algorithm will add to during each evaluation of the IEnumerable
             * in order to keep track of where the entity is.
             */
            var x = us.X;
            var y = us.Y;
            var z = us.Z;

            // "while we still need to move"
            while (x != target.X || y != target.Y)
            {
                if (z != target.Z)
                {
                    yield return(DirectionDelta.Noop);
                }
                else
                {
                    /* if target is higher than us, we have to walk up
                     * and if the target is lower than us, then we have to walk down.
                     */
                    /* We do this comparison for both X and Y axis so we know in which
                     * direction we will have to walk for both of them
                     */
                    var diffX = x < target.X ? (sbyte)1 : (sbyte)-1;
                    var diffY = y < target.Y ? (sbyte)1 : (sbyte)-1;

                    // We return the delta that will get us closer to the target.

                    /* We do have to check if one of the axis coordinates is the same for us and the target,
                     * since if we do share a mutual coordinate, we do not have to move.
                     * Therefore we return 0 if we have mututal coordinates and the diff of that axis if we do not (in order to get closer to it)
                     */
                    var delta = new DirectionDelta(
                        target.X == x ? (sbyte)0 : diffX,
                        target.Y == y ? (sbyte)0 : diffY);

                    // sync local x y's
                    x += delta.X;
                    y += delta.Y;

                    yield return(delta);
                }
            }

            // we've arrived, no further work needs to be done.
            while (true)
            {
                yield return(DirectionDelta.Noop);
            }
        }
        public void TestCtorConsistency()
        {
            foreach (var dir in Enum.GetValues(typeof(Direction)).Cast <Direction>())
            {
                var dirFromDir   = new DirectionDelta(dir);
                var dirFromDelta = new DirectionDelta(dirFromDir.X, dirFromDir.Y);

                Assert.AreEqual(dirFromDir.Direction, dir);
                Assert.AreEqual(dirFromDelta.Direction, dir);

                Assert.AreEqual(dirFromDir.X, dirFromDelta.X);
                Assert.AreEqual(dirFromDir.Y, dirFromDelta.Y);
            }
        }
Example #4
0
 public GeneratedDirections(DirectionDelta walk, DirectionDelta run)
 {
     // if walk is noop but run isin't, swap places
     if (walk.IsNoop() && !run.IsNoop())
     {
         Walk = run;
         Run  = walk;
     }
     else
     {
         Walk = walk;
         Run  = run;
     }
 }
 public bool TryConvertToDelta(out DirectionDelta delta)
 {
     delta = DirectionDelta.Noop;
     return(false);
 }
 public bool TryConvertToDelta(out DirectionDelta delta) => _default.TryConvertToDelta(out delta);
Example #7
0
 public DirecionFacingState(DirectionDelta dir, ITransform transform)
 {
     _dir       = dir;
     _transform = transform;
 }
Example #8
0
 public bool TryConvertToDelta(out DirectionDelta delta)
 {
     delta = _dir;
     return(true);
 }