예제 #1
0
    private void Move()
    {
        var before = new ObjectState(this.gameObject);

        // moving code
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            if (!BlockMove(transform.position, Vector3.right))
            {
                this.transform.position += new Vector3(1, 0, 0);                    //changes the transform

                var after = new ObjectState(this.gameObject);                       // creates an object state instance

                UndoIt.AddAction(new UndoableChangePlayer(before, after));          // add the the undoable change Player instance to a stack
                mov++;
            }
        }
        else if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            if (!BlockMove(transform.position, Vector3.left))
            {
                this.transform.position += new Vector3(-1, 0, 0);

                var after = new ObjectState(this.gameObject);

                UndoIt.AddAction(new UndoableChangePlayer(before, after));
                mov++;
            }
        }
        else if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            if (!BlockMove(transform.position, Vector3.down))
            {
                this.transform.position += new Vector3(0, -1, 0);

                var after = new ObjectState(this.gameObject);

                UndoIt.AddAction(new UndoableChangePlayer(before, after));
                mov++;
            }
        }
        else if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            if (!BlockMove(transform.position, Vector3.up))
            {
                this.transform.position += new Vector3(0, 1, 0);

                var after = new ObjectState(this.gameObject);

                UndoIt.AddAction(new UndoableChangePlayer(before, after));
                mov++;
            }
        }
    }
예제 #2
0
    public bool MoveBox(Vector3 directions)
    {
        var before = allBoxes.Select(obj => new ObjectState(obj)).ToList();

        if (BoxBlocked(directions))                                                                 //check if box is blocked or not to be able to move or not
        {
            return(false);
        }
        else
        {
            this.transform.position += directions;
            var after = allBoxes.Select(obj => new ObjectState(obj)).ToList();
            UndoIt.AddBoxAction(new UndoableChange(before, after));
            return(true);
        }
    }