Ejemplo n.º 1
0
    // Pickup the item in the specified direction
    public bool PickUpItem(Directions direction)
    {
        try
        {
            if (!hasElement || carrying == null)
            {
                // Check the direction of operation and set relevant flag
                CheckDirection(direction);

                // Check if the target position has an object to pickup
                if (generator.GetMapLayout(X + dx, Z + dz) == SoftwareLevelGenerator.Layout.ELEMENT)
                {
                    carrying = generator.GetObject(X + dx, Z + dz);
                    generator.SetMapLayout(X + dx, Z + dz, Command.PICKUP, null);
                    hasElement = true;
                    // Initialise required var
                    var sprite = Resources.Load <Sprite>(HAS_ELEMENT + carrying.GetComponent <ArrayElement>().Value);
                    this.GetComponent <SpriteRenderer>().sprite = sprite;
                    return(true);
                }
            }
        }
        catch (NullReferenceException e)
        {
            return(false);
        }
        catch (IndexOutOfRangeException e)
        {
            return(false);
        }

        return(false);
    }
Ejemplo n.º 2
0
        public override void Execute(RobotController target, InstructionExecutor executor)
        {
            instructionExecutor = executor;
            robot       = target;
            executeNext = false;

            var currentX = robot.X;
            var currentZ = robot.Z;

            switch (moveDirection)
            {
            case Directions.Up:
                currentX++;
                break;

            case Directions.Down:
                currentX--;
                break;

            case Directions.Left:
                currentZ++;
                break;

            case Directions.Right:
                currentZ--;
                break;
            }

            // Find object at the given direction
            var obj = softwareLevelGenerator.GetObject(currentX, currentZ);

            if (obj == null)
            {
                throw new InstructionException("Could not find object " + moveDirection.ToString());
                return;
            }

            var arrayElement = obj.GetComponent <IndexElement>();

            if (arrayElement == null)
            {
                throw new InstructionException("Could not find object " + moveDirection.ToString());
                return;
            }

            // Jump to the index of the object adjacent
            targetPos = softwareLevelGenerator.IndexLocation("a" + arrayElement.Value);
            var didMove = robot.MoveTo(Vector3.zero, "a" + arrayElement.Value);

            if (!didMove)
            {
                throw new InstructionException("Could not move to " + arrayElement.Value);
            }

            // Increment index element
            arrayElement.Value++;
        }