Exemplo n.º 1
0
 private void HandleInput(MovementInstruction instruction)
 {
     if (_playerMovementManager.GetState() == this && _playerMovementManager.IsValidMove(instruction))
     {
         _playerMovementManager.SetState(new EntityMovingState(_playerMovementManager, instruction));
     }
 }
Exemplo n.º 2
0
        public void ReadMoveInstruction(string input)
        {
            var parts = input.Split(SPLITTER);
            var direction = Direction.None;

            switch (parts[0])
            {
                case "N":
                    direction = Direction.North;
                    break;
                case "S":
                    direction = Direction.South;
                    break;
                case "W":
                    direction = Direction.West;
                    break;
                case "E":
                    direction = Direction.East;
                    break;
            }

            var steps = Int32.Parse(parts[1]);

            var instruction = new MovementInstruction(direction, steps);
            _instructions.Add(instruction);
        }
Exemplo n.º 3
0
        public void Robot_Movement_With_Zero_Step()
        {
            RobotInstructions robotInstructions = new RobotInstructions();

            robotInstructions.GetInputInstructions("1");
            robotInstructions.GetInputInstructions("10 2");
            robotInstructions.GetInputInstructions("E 1");
            MovementInstruction moveCommand = robotInstructions._movementInstruction[0];

            Assert.AreEqual(1, moveCommand.StepstoMove);
        }
Exemplo n.º 4
0
        public void Robot_Movement_With_Morethan99999_Steps()
        {
            RobotInstructions robotInstructions = new RobotInstructions();

            robotInstructions.GetInputInstructions("1");
            robotInstructions.GetInputInstructions("10 2");
            robotInstructions.GetInputInstructions("E 1500000");
            MovementInstruction moveCommand = robotInstructions._movementInstruction[0];

            Assert.AreEqual(99999, moveCommand.StepstoMove);
        }
Exemplo n.º 5
0
 public EntityMovingState(
     AbstractStateManager stateManager,
     MovementInstruction movementInstruction
     )
 {
     Instruction     = movementInstruction;
     _targetPosition = new Vector3(
         movementInstruction.Direction.x,
         0,
         movementInstruction.Direction.y
         ) + stateManager.transform.position;
 }
Exemplo n.º 6
0
        private AbstractMovementManager GetEntityToPush(MovementInstruction instruction)
        {
            Vector3    instructionDirection = new Vector3(instruction.Direction.x, GetOffset().y, instruction.Direction.y);
            RaycastHit entity;

            Physics.Raycast(transform.position, instructionDirection, out entity, 1f, interactionMask);

            if (entity.collider == null)
            {
                return(null);
            }

            AbstractMovementManager entityMovementManager = entity.collider.GetComponent <AbstractMovementManager>();

            return(entityMovementManager);
        }
Exemplo n.º 7
0
        public virtual bool IsValidMove(MovementInstruction instruction, int?entityLayer = null)
        {
            Vector2Int   target = GetCartesianPosition() + instruction.Direction;
            AbstractTile tile   = grid.GetTile(target);

            int layer = entityLayer ?? gameObject.layer;

            if (tile == null)
            {
                return(false);
            }

            Debug.Log(tile.transform.position);

            return(LayerUtils.IsLayer(layer, tile.entityWhiteList));
        }
Exemplo n.º 8
0
        public override bool IsValidMove(MovementInstruction instruction, int?playerLayer = null)
        {
            if (!base.IsValidMove(instruction))
            {
                return(false);
            }

            AbstractMovementManager entityToPush = GetEntityToPush(instruction);

            if (entityToPush == null)
            {
                return(true);
            }

            // Determine where the pushed entity will land
            MovementInstruction pushedEntityDirection = new MovementInstruction(instruction.Direction * 2);

            // Validate the position the pushed entity will land
            return(base.IsValidMove(pushedEntityDirection, entityToPush.gameObject.layer));
        }
Exemplo n.º 9
0
        private void HandleCollision(GameObject hit)
        {
            PlayerMovementManager playerMovementManager = hit.GetComponent <PlayerMovementManager>();

            if (playerMovementManager == null)
            {
                return;
            }

            IState            state             = playerMovementManager.GetState();
            EntityMovingState entityMovingState = state as EntityMovingState;

            if (entityMovingState == null)
            {
                return;
            }

            MovementInstruction instruction = new MovementInstruction(entityMovingState.Instruction.Direction);

            _movementManager.SetState(new EntityMovingState(_movementManager, instruction));
        }
Exemplo n.º 10
0
        private void Update()
        {
            if (OnRepel == null)
            {
                return;
            }

            AbstractMovementManager other = Detect();

            if (other != null)
            {
                MovementInstruction instruction = new MovementInstruction(GetOtherDirection(other));

                // TODO: This is broken

                if (movementManager.IsValidMove(instruction))
                {
                    OnRepel?.Invoke(instruction.Direction);
                }
            }
        }
Exemplo n.º 11
0
    public void Reset()
    {
        nodesWorld           = new List <Vector3>();
        movementInstructions = new List <MovementInstruction>();
        currentNodeIndex     = 0;

        if (Nodes.Count > 0)
        {
            nodesWorld.Add(transform.position);
        }

        foreach (Vector3 node in Nodes)
        {
            nodesWorld.Add(UseWorldSpace ? node : transform.TransformPoint(node));
        }

        if (Nodes.Count > 0)
        {
            foreach (Vector3 node in nodesWorld)
            {
                UnityEvent instructionOnEnd = new UnityEvent();
                instructionOnEnd.AddListener(OnEndPath);

                MovementInstruction currentInstruction = gameObject.AddComponent <MovementInstruction>();
                currentInstruction.Destination   = node;
                currentInstruction.UseWorldSpace = true;
                currentInstruction.Speed         = Speed;
                currentInstruction.OnEnd         = instructionOnEnd;
                currentInstruction.enabled       = false;

                movementInstructions.Add(currentInstruction);
            }

            OnEndPath();
        }
        else
        {
            Debug.Log("There must be at least one Node in the MovementPath");
        }
    }
Exemplo n.º 12
0
        private void ExecuteInstruction(MovementInstruction instruction)
        {
            var newPositionX = CurrentPosition.X;
            var newPositionY = CurrentPosition.Y;

            switch (instruction.Direction)
            {
                case Direction.North:
                    newPositionY = CurrentPosition.Y + 1;
                    break;
                case Direction.South:
                    newPositionY = CurrentPosition.Y - 1;
                    break;
                case Direction.West:
                    newPositionX = CurrentPosition.X - 1;
                    break;
                case Direction.East:
                    newPositionX = CurrentPosition.X + 1;
                    break;
            }

            CleanCell(new Location(newPositionX, newPositionY));
        }
Exemplo n.º 13
0
        private MovementInstruction SplitMovementInstruction(string movementInstruction)
        {
            MovementInstruction _movementInstruction = new MovementInstruction();

            string[] movementTokens = movementInstruction.Split(' ');
            switch (movementTokens[0].ToUpper())
            {
            case "N":
                _movementInstruction.RobotDirection = Direction.North;
                break;

            case "S":
                _movementInstruction.RobotDirection = Direction.South;
                break;

            case "E":
                _movementInstruction.RobotDirection = Direction.East;
                break;

            case "W":
                _movementInstruction.RobotDirection = Direction.West;
                break;
            }
            _movementInstruction.StepstoMove = int.Parse(movementTokens[1]);

            if (_movementInstruction.StepstoMove > MaxSteps)
            {
                _movementInstruction.StepstoMove = MaxSteps;
            }
            if (_movementInstruction.StepstoMove < MinSteps)
            {
                _movementInstruction.StepstoMove = MinSteps;
            }

            return(_movementInstruction);
        }
Exemplo n.º 14
0
        private void HandleRepel(Vector2Int direction)
        {
            MovementInstruction instruction = new MovementInstruction(direction);

            _movementManager.SetState(new EntityMovingState(_movementManager, instruction));
        }
Exemplo n.º 15
0
        public async Task <ActionResult <RoverCurrentPositionResponse> > UpdateCurrentPositionAsync(string id, [FromBody] MovementInstruction movement)
        {
            var positionResponse = await _roverService.UpdateAsync(new AlphaNumericIdString(id), movement);

            if (!positionResponse.Success)
            {
                return(StatusCode(positionResponse.ErrorCode.Value, positionResponse.Message));
            }

            return(positionResponse);
        }
Exemplo n.º 16
0
        public async Task <RoverCurrentPositionResponse> UpdateAsync(AlphaNumericIdString id, MovementInstruction movement)
        {
            var rover = await _roverRepository.GetAsync(id);

            _compassTurnKey.CurrentIndex = _compassTurnKey.FindIndex(i => i.Direction == rover.FacingDirection);

            foreach (var individualInstruction in movement.Instruction.ToCharArray())
            {
                if (individualInstruction == 'M')
                {
                    rover.XPosition += _compassTurnKey.CurrentItem.XAxisMovement;
                    rover.YPosition += _compassTurnKey.CurrentItem.YAxisMovement;
                }

                if (individualInstruction == 'L')
                {
                    _compassTurnKey.PreviousItem();
                }

                if (individualInstruction == 'R')
                {
                    _compassTurnKey.NextItem();
                }
            }

            rover.FacingDirection = _compassTurnKey.CurrentItem.Direction;

            // Use concatenation to invoke the implicit conversion for AlphanumericIdString
            string convertedId = id;

            try
            {
                rover = await _roverRepository.UpdateAsync(rover);
            }
            catch (Exception exception)
            {
                var errorMessage = $"Error occurred saving position for rover with {convertedId}";
                _logger.LogError(exception, errorMessage);
                return(new RoverCurrentPositionResponse(errorMessage, string.Empty, false, StatusCodes.Status500InternalServerError));
            }

            if (rover == null)
            {
                return(new RoverCurrentPositionResponse($"Rover with id {convertedId} not found.", null, false, StatusCodes.Status404NotFound));
            }

            return(new RoverCurrentPositionResponse($"Updated position for rover with id {convertedId}.", FormatRelativePosition(rover.XPosition, rover.YPosition), true, null));
        }