Example #1
0
        // Move the player to next position.
        public void Update()
        {
            if (Math.Abs(transform.localPosition.x - nextPosition.x) <= 0.05 && Math.Abs(transform.localPosition.z - nextPosition.z) <= 0.05)
            {
                gameObject.transform.localPosition = nextPosition;
                positionChangeNeeded = false;
            }

            // If the player's location needs to change and the player hasn't hit next square yet
            if (positionChangeNeeded && (transform.localPosition.x != nextPosition.x || transform.localPosition.z != nextPosition.z))
            {
                // Activate animation
                anim.SetInteger("AnimParam", 1);

                if (nextState.orientationType == Orientation.South)
                {
                    velocity = new Vector3(0, 0, -speed);
                    transform.eulerAngles = OrientationMethods.VectorForOrientation(Orientation.South);
                }
                if (nextState.orientationType == Orientation.North)
                {
                    velocity = new Vector3(0, 0, speed);
                    transform.eulerAngles = OrientationMethods.VectorForOrientation(Orientation.North);
                }
                if (nextState.orientationType == Orientation.West)
                {
                    velocity = new Vector3(-speed, 0, 0);
                    transform.eulerAngles = OrientationMethods.VectorForOrientation(Orientation.West);
                }
                if (nextState.orientationType == Orientation.East)
                {
                    velocity = new Vector3(speed, 0, 0);
                    transform.eulerAngles = OrientationMethods.VectorForOrientation(Orientation.East);
                }
            }
            else
            {
                // Deactivate animation
                velocity = new Vector3(0, 0, 0);
                anim.SetInteger("AnimParam", 0);
            }

            gameObject.transform.localPosition += velocity * Time.deltaTime;
        }
Example #2
0
    /**
     * Read a level from an XML document.
     */
    private PipeElement[,] instanciateLevelFromXml(TextAsset document)
    {
        XmlDocument xml = new XmlDocument();

        PipeElement[,] grid = new PipeElement[GRID_SIZE, GRID_SIZE];

        // load the document and get the level(s)
        xml.LoadXml(document.text);
        XmlNodeList levelNodes = xml.GetElementsByTagName("level");

        XmlAttribute levelEstimatedTime = levelNodes[0].Attributes["estimatedTime"];

        if (levelEstimatedTime != null)
        {
            // use it as the base time for countdown timer
            timebar.maxTime = int.Parse(levelEstimatedTime.Value);
        }
        // start and destination pipes
        // TODO init

        XmlNodeList pipeNodes = levelNodes[0].ChildNodes;

        foreach (XmlNode curNode in pipeNodes)
        {
            if (curNode.Name == "pipe")
            {
                int x = 0;
                int y = 0;
                PipeElement.Orientation orientation = PipeElement.Orientation.NORTH;
                PipeElement.Type        type        = PipeElement.Type.PIPE_I;

                // add the pipe if possible
                x = int.Parse(curNode.Attributes["x"].Value);
                y = int.Parse(curNode.Attributes["y"].Value);

                switch (curNode.Attributes["type"].Value)
                {
                case "L":
                    type = PipeElement.Type.PIPE_L;
                    break;

                case "I":
                    type = PipeElement.Type.PIPE_I;
                    break;

                case "in":
                    type = PipeElement.Type.PIPE_IN;
                    break;

                case "out":
                    type = PipeElement.Type.PIPE_OUT;
                    break;
                }

                XmlAttribute dirAttribute = curNode.Attributes["dir"];
                if (dirAttribute == null)
                {
                    orientation = OrientationMethods.randomOrientation();
                }
                else
                {
                    orientation = OrientationMethods.fromString(dirAttribute.Value);
                }


                if (x >= 0 && x < GRID_SIZE && y >= 0 && y < GRID_SIZE)
                {
                    // add the new pipe to the grid, and set its neighbors
                    PipeElement toAdd = new PipeElement(type, orientation);
                    grid[x, y] = toAdd;

                    if (toAdd.type == PipeElement.Type.PIPE_IN)
                    {
                        inputX           = x;
                        inputY           = y;
                        inputOrientation = orientation;
                    }
                    else if (toAdd.type == PipeElement.Type.PIPE_OUT)
                    {
                        outputX           = x;
                        outputY           = y;
                        outputOrientation = orientation;
                    }

                    // set neighbors

                    // TODO neighbors
                }
            }
            else
            {
            }
        }

        return(grid);
    }