예제 #1
0
        public void Update(GameTime gameTime)
        {
            mousePosition = Cell.SnapToGrid(Input.mousePosition);

            if (placeObject == PlaceType.Rail)
            {
                #region StraightRail
                if (selectedRail == Rail.RailType.Straight || selectedRail == Rail.RailType.Boost)
                {
                    if (Input.mouseState.LeftButton == ButtonState.Pressed && Input.prevMouseState.LeftButton == ButtonState.Released && !placingRail && Cell.GetCell(mousePosition).isEmpty) // start placing rail
                    {
                        placingRail = true;

                        railStartPosition = mousePosition;

                        drawRect = Rectangle.Empty;
                    }
                    else if (Input.mouseState.LeftButton == ButtonState.Released && placingRail) // place the rail
                    {
                        placingRail = false;

                        length = (int)Math.Max(Math.Abs(railEndPosition.Y - railStartPosition.Y), Math.Abs(railEndPosition.X - railStartPosition.X)) / Cell.cellWidth + 1;

                        if (Math.Abs(railEndPosition.X - railStartPosition.X) > Math.Abs(railEndPosition.Y - railStartPosition.Y))
                        {
                            orientation = StraightRail.Orientation.Horizontal;
                            rotation    = MathHelper.PiOver2;

                            if (railEndPosition.X < railStartPosition.X) // switch the start and end points so that the start of the rail is always on top or to the left
                            {
                                float temp = railStartPosition.X;
                                railStartPosition.X = railEndPosition.X;
                                railEndPosition.X   = temp;
                            }
                        }
                        else
                        {
                            orientation = StraightRail.Orientation.Vertical;
                            rotation    = 0f;

                            if (railEndPosition.Y < railStartPosition.Y)
                            {
                                float temp = railStartPosition.Y;
                                railStartPosition.Y = railEndPosition.Y;
                                railEndPosition.Y   = temp;
                            }
                        }

                        switch (selectedRail)
                        {
                        case Rail.RailType.Straight:
                            StraightRail.PlaceMultipleRails(railStartPosition, length, orientation, false, true);     // placing the rails
                            break;

                        case Rail.RailType.Boost:
                            StraightRail.PlaceMultipleRails(new BoosterRail(Cell.GetCell(railStartPosition), orientation, false), length, true);     // placing the rails
                            break;
                        }
                    }

                    else if (placingRail)
                    {
                        if (Input.mouseState.RightButton == ButtonState.Pressed)
                        {
                            placingRail = false;
                        }

                        railEndPosition = Cell.SnapToGrid(mousePosition);
                        length          = (int)Math.Max(Math.Abs(railEndPosition.Y - railStartPosition.Y), Math.Abs(railEndPosition.X - railStartPosition.X)) / Cell.cellWidth + 1; // limit the length of the rail when drawn by amount of resources

                        if (Math.Abs(railEndPosition.X - railStartPosition.X) > Math.Abs(railEndPosition.Y - railStartPosition.Y))                                                  // we want to place it horizontally
                        {
                            rotation = MathHelper.PiOver2;

                            if (railEndPosition.X > railStartPosition.X) // placing rails from left to right
                            {
                                drawRect = new Rectangle(0, Rail.straightTexture.Height * -length, Rail.straightTexture.Width, length * Cell.cellHeight);
                                origin   = new Vector2(Rail.straightTexture.Width / 2f, Rail.straightTexture.Height * (length - 0.5f));
                            }
                            else
                            {
                                drawRect = new Rectangle(0, 0, Rail.straightTexture.Width, length * Cell.cellHeight);
                                origin   = new Vector2(Rail.straightTexture.Width / 2f, Rail.straightTexture.Height / 2f);
                            }
                        }
                        else                                                                                                       // we want to place it vertically
                        {
                            rotation = 0f;
                            if (railEndPosition.Y < railStartPosition.Y) // placing rails from bottom to top
                            {
                                drawRect = new Rectangle(0, Rail.straightTexture.Height * -length, Rail.straightTexture.Width, length * Cell.cellHeight);
                                origin   = new Vector2(Rail.straightTexture.Width / 2f, Rail.straightTexture.Height * (length - 0.5f));
                            }
                            else
                            {
                                drawRect = new Rectangle(0, 0, Rail.straightTexture.Width, length * Cell.cellHeight);
                                origin   = new Vector2(Rail.straightTexture.Width / 2f, Rail.straightTexture.Height / 2f);
                            }
                        }
                    }
                }
                #endregion

                #region TurnRails

                if (selectedRail == Rail.RailType.Turn && Input.mouseState.LeftButton == ButtonState.Released && Input.prevMouseState.LeftButton == ButtonState.Pressed) // place a turn-railtrack
                {
                    Rail.PlaceRail(new TurnRail(Cell.GetCell(Input.mouseState.Position.ToVector2()), turnOrientation, false), true);
                }

                #endregion

                #region Rotation
                if (Input.keyState.IsKeyDown(rotateKey) && Input.prevKeyState.IsKeyUp(rotateKey)) // rotate the track
                {
                    switch (selectedRail)
                    {
                    case Rail.RailType.Straight:
                        orientation = (StraightRail.Orientation)Globals.IncrementEnum(typeof(StraightRail.Orientation), (int)orientation, 1);
                        break;

                    case Rail.RailType.Turn:
                        turnOrientation = (TurnRail.TurnDirection)Globals.IncrementEnum(typeof(TurnRail.TurnDirection), (int)turnOrientation, 1);
                        break;

                    case Rail.RailType.Boost:
                        orientation = (StraightRail.Orientation)Globals.IncrementEnum(typeof(StraightRail.Orientation), (int)orientation, 1);
                        break;

                    default:
                        break;
                    }
                }
                #endregion

                #region RailSwitching

                if (Input.mouseState.ScrollWheelValue != Input.prevMouseState.ScrollWheelValue) // detecting mouse-scrolling
                {
                    selectedRail = Input.mouseState.ScrollWheelValue > Input.prevMouseState.ScrollWheelValue ? (Rail.RailType)Globals.IncrementEnum(typeof(Rail.RailType), (int)selectedRail, 1) : selectedRail = (Rail.RailType)Globals.IncrementEnum(typeof(Rail.RailType), (int)selectedRail, -1);
                }

                #endregion
            }
            else if (Input.mouseState.LeftButton == ButtonState.Pressed && Input.prevMouseState.LeftButton == ButtonState.Released)
            {
                Cell cell = Cell.GetCell(mousePosition);
                switch (placeObject)
                {
                case PlaceType.Obstacle:
                    if (!cell.isEmpty)
                    {
                        break;
                    }
                    cell.isEmpty = false;
                    Globals.level.obstacles.Add(new Obstacle(cell));
                    break;

                case PlaceType.Bump:
                    if (!cell.isEmpty)
                    {
                        break;
                    }
                    cell.isEmpty = false;
                    Globals.level.bumps.Add(new Bump(0.2f, cell));
                    break;

                case PlaceType.Portal:
                    if (!cell.isEmpty)
                    {
                        break;
                    }
                    cell.isEmpty = false;
                    Globals.level.portals.Add(new Portal(cell));
                    break;

                default:
                    break;
                }
            }
            #region Removing objects
            if (Input.mouseState.RightButton == ButtonState.Pressed)
            {
                Cell cell = Cell.GetCell(mousePosition);
                if (cell.rail != null)
                {
                    cell.rail.Destroy(true);
                }
                else if (cell.obstacle != null)
                {
                    Globals.level.obstacles.Remove(cell.obstacle);
                    cell.obstacle = null;
                    cell.isEmpty  = true;
                }
                else if (cell.bump != null)
                {
                    Globals.level.bumps.Remove(cell.bump);
                    cell.bump    = null;
                    cell.isEmpty = true;
                }
                else if (cell.portal != null)
                {
                    Globals.level.portals.Remove(cell.portal);
                    cell.portal  = null;
                    cell.isEmpty = true;
                }
                else if (cell.orb != null)
                {
                    Globals.level.orbs.Remove(cell.orb);
                    cell.orb     = null;
                    cell.isEmpty = true;
                }
            }
            #endregion
        }
예제 #2
0
        public void Update(GameTime gameTime)
        {
            camera.MoveWASD(gameTime);

            // check for interaction before placing things

            if (Input.mouseCell.item is Lever lever && Input.mouseClicked)
            {
                lever.ToggleLever();
            }

            switch (gameState)
            {
            case GameState.Editing:
                break;

            case GameState.Simulating:
                if (carts.Count > 0)
                {
                    for (int i = 0; i < carts.Count; i++)
                    {
                        carts[i].Update(gameTime);
                    }
                    if (carts.Count == 0)
                    {
                        gameState = GameState.Editing;
                    }
                }
                return;

            default:
                break;
            }

            mousePosition = Input.mouseCell.ToVector2();

            #region Item Placement

            #region StraightRail
            if ((selectedItem == PlacableItem.StraightRail || selectedItem == PlacableItem.BoosterRail) && canPlaceRail)
            {
                if (Input.mouseClicked && !placingRail && Input.mouseCell.isEmpty) // start placing rail
                {
                    placingRail = true;

                    railStartPosition = mousePosition;

                    drawRect = Rectangle.Empty;
                }
                else if (Input.mouseState.LeftButton == ButtonState.Released && placingRail) // place the rail if this is true
                {
                    placingRail = false;

                    length = (int)Math.Max(Math.Abs(railEndPosition.Y - railStartPosition.Y), Math.Abs(railEndPosition.X - railStartPosition.X)) / Cell.cellWidth + 1;

                    float xDif = Math.Abs(railEndPosition.X - railStartPosition.X);
                    float yDif = Math.Abs(railEndPosition.Y - railStartPosition.Y);

                    StraightRail.Orientation prevOrientation = orientation;

                    if (xDif > yDif)
                    {
                        orientation          = StraightRail.Orientation.Horizontal;
                        straightRailRotation = MathHelper.PiOver2;

                        if (railEndPosition.X < railStartPosition.X) // switch the start and end points so that the start of the rail is always on top or to the left
                        {
                            float temp = railStartPosition.X;
                            railStartPosition.X = railEndPosition.X;
                            railEndPosition.X   = temp;
                        }
                    }
                    else
                    {
                        if (xDif != yDif)
                        {
                            orientation = StraightRail.Orientation.Vertical;
                        }
                        straightRailRotation = 0f;

                        if (railEndPosition.Y < railStartPosition.Y)
                        {
                            float temp = railStartPosition.Y;
                            railStartPosition.Y = railEndPosition.Y;
                            railEndPosition.Y   = temp;
                        }
                    }

                    switch (selectedItem)
                    {
                    case PlacableItem.StraightRail:
                        length = straightRailCount < length ? straightRailCount : length;

                        straightRailCount = MathHelper.Clamp(straightRailCount - StraightRail.PlaceMultipleRails(railStartPosition, length, orientation), 0, Globals.level.straightRailCount);     // placing the rails

                        GUI.straightRailElement.ChangeText(straightRailCount.ToString());
                        break;

                    case PlacableItem.BoosterRail:
                        length = boostRailCount < length ? boostRailCount : length;

                        boostRailCount = MathHelper.Clamp(boostRailCount - StraightRail.PlaceMultipleRails(new BoosterRail(Cell.GetCell(railStartPosition), orientation), length), 0, Globals.level.boostRailCount);     // placing the rails

                        GUI.boostRailElement.ChangeText(boostRailCount.ToString());
                        break;
                    }

                    orientation = prevOrientation;
                }

                else if (placingRail) // Display the rails being placed
                {
                    if (Input.mouseState.RightButton == ButtonState.Pressed)
                    {
                        placingRail = false;
                    }

                    railEndPosition = mousePosition;
                    length          = (int)Math.Max(Math.Abs(railEndPosition.Y - railStartPosition.Y), Math.Abs(railEndPosition.X - railStartPosition.X)) / Cell.cellWidth + 1; // limit the length of the rail when drawn by amount of resources

                    float xDif = Math.Abs(railEndPosition.X - railStartPosition.X);
                    float yDif = Math.Abs(railEndPosition.Y - railStartPosition.Y);

                    if (xDif > yDif) // we want to place it horizontally
                    {
                        straightRailRotation = MathHelper.PiOver2;

                        if (railEndPosition.X > railStartPosition.X) // placing rails from left to right
                        {
                            drawRect = new Rectangle(0, Textures.RailStraight.Height * -length, Textures.RailStraight.Width, length * Cell.cellHeight);
                            origin   = new Vector2(Textures.RailStraight.Width / 2f, Textures.RailStraight.Height * (length - 0.5f));
                        }
                        else
                        {
                            drawRect = new Rectangle(0, 0, Textures.RailStraight.Width, length * Cell.cellHeight);
                            origin   = new Vector2(Textures.RailStraight.Width / 2f, Textures.RailStraight.Height / 2f);
                        }
                    }
                    else                                                                                                       // we want to place it vertically
                    {
                        if (xDif != yDif)
                        {
                            straightRailRotation = 0f;
                        }
                        else
                        {
                            straightRailRotation = straightRotation;
                        }

                        if (railEndPosition.Y < railStartPosition.Y) // placing rails from bottom to top
                        {
                            drawRect = new Rectangle(0, Textures.RailStraight.Height * -length, Textures.RailStraight.Width, length * Cell.cellHeight);
                            origin   = new Vector2(Textures.RailStraight.Width / 2f, Textures.RailStraight.Height * (length - 0.5f));
                        }
                        else
                        {
                            drawRect = new Rectangle(0, 0, Textures.RailStraight.Width, length * Cell.cellHeight);
                            origin   = new Vector2(Textures.RailStraight.Width / 2f, Textures.RailStraight.Height / 2f);
                        }
                    }
                }
            }
            #endregion

            else if (Input.mouseClicked && Input.mouseCell.isEmpty && canPlaceRail)
            {
                if (selectedItem == PlacableItem.Lever) // Lever placement
                {
                    Item.PlaceItem(new Lever(Input.mouseCell));
                }

                else if (selectedItem == PlacableItem.DetectorRail)
                {
                    Item.PlaceItem(new DetectorRail(Input.mouseCell, orientation));
                }

                else if (selectedItem == PlacableItem.PushRail)
                {
                    Item.PlaceItem(new PushRail(Input.mouseCell, orientation));
                }
                else if (selectedItem == PlacableItem.InverterModule)
                {
                    Item.PlaceItem(new InverterModule(Input.mouseCell));
                }

                #region TurnRails

                else if (selectedItem == PlacableItem.TurnRail) // place a turn-railtrack
                {
                    if (turnRailCount > 0)
                    {
                        turnRailCount = MathHelper.Clamp(Item.PlaceItem(new TurnRail(Cell.GetCell(mousePosition), turnOrientation)) ? turnRailCount - 1 : turnRailCount, 0, Globals.level.turnRailCount);
                        GUI.turnRailElement.ChangeText(turnRailCount.ToString());
                    }
                }

                #endregion
            }

            #endregion

            #region Rotation
            if (Input.keyState.IsKeyDown(rotateKey) && Input.prevKeyState.IsKeyUp(rotateKey)) // rotate the track
            {
                if (selectedItem == PlacableItem.TurnRail)
                {
                    turnOrientation       = (TurnRail.TurnDirection)Globals.IncrementEnum(typeof(TurnRail.TurnDirection), (int)turnOrientation, 1);
                    turnOrientationEffect = TurnRail.GetEffects(turnOrientation);
                }
                else if (selectedItem == PlacableItem.StraightRail || selectedItem == PlacableItem.BoosterRail || selectedItem == PlacableItem.DetectorRail || selectedItem == PlacableItem.PushRail)
                {
                    orientation      = (StraightRail.Orientation)Globals.IncrementEnum(typeof(StraightRail.Orientation), (int)orientation, 1);
                    straightRotation = StraightRail.OrientationToRotation(orientation);
                }
            }
            #endregion

            #region ItemSwitching

            if (scrollBetweenRails && Input.mouseState.ScrollWheelValue != Input.prevMouseState.ScrollWheelValue) // detecting mouse-scrolling
            {
                selectedItem = Input.mouseState.ScrollWheelValue > Input.prevMouseState.ScrollWheelValue ? (PlacableItem)Globals.IncrementEnum(typeof(PlacableItem), (int)selectedItem, 1) : selectedItem = (PlacableItem)Globals.IncrementEnum(typeof(PlacableItem), (int)selectedItem, -1);
            }

            #endregion

            #region Removing items
            if (Input.mouseState.RightButton == ButtonState.Pressed)
            {
                Cell cell = Cell.GetCell(mousePosition);
                if (cell.item is Item item && item.removable)
                {
                    if (item is TurnRail)
                    {
                        turnRailCount++;
                        GUI.turnRailElement.ChangeText(turnRailCount.ToString());
                    }
                    else if (item is BoosterRail)
                    {
                        boostRailCount++;
                        GUI.boostRailElement.ChangeText(boostRailCount.ToString());
                    }
                    else if (item is StraightRail)
                    {
                        straightRailCount++;
                        GUI.straightRailElement.ChangeText(straightRailCount.ToString());
                    }

                    item.Destroy();
                }
            }
            #endregion
        }