Exemplo n.º 1
0
        private MapLocation validateOutOfBounds(MapLocation headLocation, MapLocation requestedLocation)
        {
            var difference  = headLocation - requestedLocation;
            var newLocation = requestedLocation; // No need to clone because we do not change individual members

            if (_utilities.OutOfBounds(newLocation))
            {
                var incrementor = difference.Normalized().Abs();

                // If the requested location is going in the negative direction then we need our incrementor
                // to also be pointing in the negative direction.
                if (difference > 0)
                {
                    incrementor *= -1;
                }

                // Decrement movement position until it is no longer out of bounds
                do
                {
                    newLocation = newLocation - incrementor;
                }while (_utilities.OutOfBounds(newLocation));

                // If we're back at our head location then we need to set the position to be out of bounds (by one) (collision)
                if (newLocation.SameAs(headLocation))
                {
                    newLocation += incrementor;
                }
            }

            return(newLocation);
        }
Exemplo n.º 2
0
        public void ValidateCollision(Cycle cycle)
        {
            var cycleLocation = _utilities.ToMapLocation(cycle.MovementController);

            // Check if we're out of bounds
            if (_utilities.OutOfBounds(cycleLocation))
            {
                cycle.HandleCollisionWith(null);
                return;
            }

            long occupiedById = _map[cycleLocation];

            if (occupiedById != 0 && occupiedById != -cycle.ID) // Check if we're colliding with something other than our head location
            {
                Cycle occupiedBy = _map.GetCycle(Math.Abs(occupiedById));

                cycle.HandleCollisionWith(occupiedBy);
            }
        }