Esempio n. 1
0
        public Player()
        {
            gameState    = GameState.Editing;
            selectedItem = PlacableItem.StraightRail;
            FillResources();
            camera = new Camera(Globals.screenCenter, -5000, 5000, -4000, 4000);
            carts  = new List <Cart>();

            turnOrientationEffect = TurnRail.GetEffects(turnOrientation);
        }
Esempio n. 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
        }