Exemplo n.º 1
0
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
#if FULLSCREEN
            graphics.PreferredBackBufferWidth  = 1920;
            graphics.PreferredBackBufferHeight = 1080;
            graphics.IsFullScreen = true;
#else
            graphics.PreferredBackBufferWidth  = 1800;
            graphics.PreferredBackBufferHeight = 900;
#endif
            Content.RootDirectory = "Content";
            IsMouseVisible        = true;

            random = new Random();

            gameState = new GameState();
            gameState.CameraMatrix = Matrix.CreateTranslation(graphics.PreferredBackBufferWidth / 2 - WorldWidth / 2 * World.TileSize, graphics.PreferredBackBufferHeight / 2 - WorldHeight / 2 * World.TileSize, 0);

            input = new InputHelper();

            // create new straight rail
            input.SetKeyboardPressHandler(Keys.NumPad1, () =>
            {
                isDeleting = false;
                rail       = new StraightRail(Direction.North, true);
            });

            // create new curved rail
            input.SetKeyboardPressHandler(Keys.NumPad2, () =>
            {
                isDeleting = false;
                rail       = new CurvedRail(Direction.North, true);
            });

            // create new buffer rail
            //input.SetKeyboardPressHandler(Keys.NumPad3, () =>
            //{
            //    isDeleting = false;
            //    rail = new BufferRail(Direction.North, true, null);
            //});

            // create new cross rail
            input.SetKeyboardPressHandler(Keys.NumPad4, () =>
            {
                isDeleting = false;
                rail       = new CrossRail(Direction.North, true);
            });

            // create escape handler
            input.SetKeyboardPressHandler(Keys.Escape, () =>
            {
                if (rail != null)
                {
                    rail = null;
                }
                else if (isDeleting)
                {
                    isDeleting = false;
                }
                else
                {
                    //Exit();
                }
            });

            // generate level handler
            input.SetKeyboardPressHandler(Keys.N, () =>
            {
                world = GenerateWorld(WorldWidth, WorldHeight, VehicleCount);
            });

            // rotate rail
            input.SetKeyboardPressHandler(Keys.R, () =>
            {
                if (rail != null)
                {
                    rail.Orientation = rail.Orientation.Right();
                }
            });

            // delete rail
            input.SetKeyboardPressHandler(Keys.Q, () =>
            {
                rail       = null;
                isDeleting = true;
            });

            // left mouse button handler
            input.SetLeftButtonPressHandler(() =>
            {
                int mouseTileX = (int)mouseTilePos.X;
                int mouseTileY = (int)mouseTilePos.Y;
                if (rail != null)
                {
                    Rail oldRail = world.GetRail(mouseTileX, mouseTileY);
                    if (oldRail == null)
                    {
                        world.SetRail(mouseTileX, mouseTileY, rail);
                        railPos = mouseTilePos;
                        if (rail.GetType() == typeof(StraightRail))
                        {
                            rail = new StraightRail(rail.Orientation, true);
                        }
                        else if (rail.GetType() == typeof(CurvedRail))
                        {
                            rail = new CurvedRail(rail.Orientation, true);
                        }
                        else
                        {
                            rail = null;
                        }
                        railParticles.SpawnPerFrame = 50;
                    }
                }
                else if (isDeleting)
                {
                    Rail deleteRail = world.GetRail(mouseTileX, mouseTileY);
                    if (deleteRail != null && deleteRail.Deletable)
                    {
                        world.SetRail(mouseTileX, mouseTileY, null);
                    }
                }
            });

            // start level handler
            input.SetKeyboardPressHandler(Keys.Space, () =>
            {
                // remove all vehicles
                for (int x = 0; x < world.Width; x++)
                {
                    for (int y = 0; y < world.Height; y++)
                    {
                        Rail rail = world.GetRail(x, y);
                        if (rail != null)
                        {
                            rail.Vehicles.Clear();
                        }
                    }
                }

                // add vehicles
                foreach (KeyValuePair <Rail, Payload> pair in world.StartingRails)
                {
                    pair.Key.Vehicles.Add(new Wagon(0.8f, 3f, Direction.East, pair.Value));
                }
            });

            // help message handler
            input.SetKeyboardPressHandler(Keys.H, () =>
            {
                showHelp = !showHelp;
            });
        }