Ejemplo 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);
        }
Ejemplo n.º 2
0
        public ItemsMenu()
        {
            itemsOpenerPositionClosed = new Vector2(Globals.screenWidth - Textures.ItemsMenuOpen.Width * scale / 2f, Globals.screenHeight / 2f);
            itemsOpenerPositionOpened = new Vector2(Globals.screenWidth - Textures.ItemsMenuOpen.Width * scale / 2f - Textures.ItemsMenu.Width * scale, Globals.screenHeight / 2f);

            itemsOpenerPosition = itemsOpenerPositionClosed;

            menuPosition = itemsOpenerPosition - new Vector2(0, (categoryCount - 1) * (Textures.ItemsMenuOpen.Height * scale / 2f + Textures.ItemsMenu.Height * scale / 2f));

            expandMenuPositions = new Vector2[categoryCount];
            categories          = new ItemCategory[categoryCount];

            PlacableItem[] rails = new PlacableItem[5];

            rails[0] = PlacableItem.StraightRail;
            rails[1] = PlacableItem.BoosterRail;
            rails[2] = PlacableItem.PushRail;
            rails[3] = PlacableItem.TurnRail;
            rails[4] = PlacableItem.DetectorRail;

            PlacableItem[] power = new PlacableItem[]
            {
                PlacableItem.Lever, PlacableItem.InverterModule
            };

            categories[0] = new ItemCategory(Vector2.Zero, "Rails", rails);
            categories[1] = new ItemCategory(Vector2.Zero, "Power", power);
            categories[2] = new ItemCategory(Vector2.Zero, "Other", power);

            for (int i = 0; i < categoryCount; i++)
            {
                float yPos = menuPosition.Y + i * Textures.ItemsMenu.Height * scale + i * Textures.ItemsMenuExpand.Height * scale;
                categories[i].UpdatePosition(Vector2.UnitY * yPos);
                expandMenuPositions[i].Y = yPos + Textures.ItemsMenu.Height / 2f * scale + Textures.ItemsMenuExpand.Height / 2f * scale;
            }

            for (int i = 0; i < categoryCount; i++)
            {
                categories[i].Update(10000);
                expandMenuPositions[i].X = 10000;
            }
            menuPosition = itemsOpenerPosition + new Vector2(Textures.ItemsMenu.Width * scale / 2f + Textures.ItemsMenuOpen.Width * scale / 2f, 0f);
        }
Ejemplo n.º 3
0
        public ItemElement(Vector2 position, PlacableItem item, string description)
        {
            this.item     = item;
            this.position = position;

            switch (item)
            {
            case PlacableItem.StraightRail:
                texture = Textures.RailStraight;
                break;

            case PlacableItem.TurnRail:
                texture = Textures.RailTurn;
                break;

            case PlacableItem.BoosterRail:
                texture = Textures.RailBooster;
                break;

            case PlacableItem.DetectorRail:
                texture = Textures.RailDetector;
                break;

            case PlacableItem.PushRail:
                texture = Textures.RailPush;
                break;

            case PlacableItem.Lever:
                texture = Textures.LeverDeactive;
                break;

            case PlacableItem.InverterModule:
                texture = Textures.ModuleInverter;
                break;

            default:
                break;
            }

            GUI.ItemElements.Add(this);
        }
Ejemplo n.º 4
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
        }