Beispiel #1
0
        public void StartTrainBoxApp()
        {
            Level.PrepareEntityTypes();

            bool quit = false;

            TextureMan.LoadTextures();
            TextureMan.CreateShadows();

            bufferSpr = new Sprite();

            buffer = new RenderTexture(320, 120);
            buffer.Clear(Color.Black);

            menu.Active = true;
            menu.State  = Menu.MenuState.Intro;
            menu.CounterReset();
            menu.LoadLevelInfo();

            while (true)
            {
                if (fullScreen)
                {
                    screenSizeX = 1366;
                    screenSizeY = 768;
                }
                else
                {
                    screenSizeX = 960;
                    screenSizeY = 540;
                }

                if (fullScreen)
                {
                    window = new RenderWindow(new VideoMode(screenSizeX, screenSizeY),
                                              "Super Starbox", Styles.Fullscreen);
                }
                else
                {
                    window = new RenderWindow(new VideoMode(screenSizeX, screenSizeY),
                                              "Super Starbox", Styles.Titlebar);
                }

                window.SetMouseCursorVisible(!fullScreen);

                window.SetVisible(true);
                window.Closed += new EventHandler(OnClosed);
                window.SetFramerateLimit(60);

                window.LostFocus   += new EventHandler(OnLostFocus);
                window.GainedFocus += new EventHandler(OnGainedFocus);

                bool editorButtonTrigger = false;

                SoundMan.PlayMusic();

                while (true)
                {
                    buffer.Clear(Color.Black);

                    if (!menu.Active)
                    {
                        level.DrawLevel(buffer);
                    }


                    if (fadeOut != 0)
                    {
                        RectangleShape rect = new RectangleShape();

                        rect.Position  = new Vector2f(0, 0);
                        rect.Size      = new Vector2f(320, 120);
                        rect.FillColor = new Color(0, 0, 0, (byte)fadeOut);

                        buffer.Draw(rect);
                    }

                    if (menu.Active)
                    {
                        menu.Draw(buffer);
                    }
                    if (editor.Active)
                    {
                        editor.Draw(buffer);
                    }

                    buffer.Display();

                    bufferSpr.Texture = buffer.Texture;
                    bufferSpr.Scale   = new Vector2f((screenSizeX / 320f) * screenScale, (screenSizeY / 120f) * screenScale);

                    bufferSpr.Origin   = new Vector2f(160, 60);
                    bufferSpr.Position = new Vector2f(screenSizeX / 2, screenSizeY / 2);

                    if (screenShake > 0)
                    {
                        bufferSpr.Position = new Vector2f(bufferSpr.Position.X + (float)new Random().Next(-200, 200) / 75,
                                                          bufferSpr.Position.Y + (float)new Random().Next(-200, 200) / 75);
                    }

                    if (gameState == GameState.ResetFadeOut)
                    {
                        bufferSpr.Rotation = gameStateC / 2;

                        screenScale = 1f + (float)gameStateC / 50f;
                    }

                    if (gameState == GameState.LevelFinished)
                    {
                        bufferSpr.Rotation = gameStateC / 2;

                        screenScale = 1f + (float)gameStateC / 25f;
                    }

                    if (gameState == GameState.ResetFadeIn)
                    {
                        bufferSpr.Rotation = (50 - gameStateC) / 2;

                        screenScale = 1f + (float)(50 - gameStateC) / 50f;
                    }

                    if (gameState == GameState.GamePlay || menu.Active)
                    {
                        screenScale        = 1;
                        bufferSpr.Rotation = 0;
                    }

                    window.DispatchEvents();
                    window.Clear(Color.Black);
                    window.Draw(bufferSpr);
                    window.Display();


                    if (Keyboard.IsKeyPressed(Keyboard.Key.F4))
                    {
                        fullScreen = !fullScreen;
                        break;
                    }

                    if (Keyboard.IsKeyPressed(Keyboard.Key.F10) && false)
                    {
                        if (!menu.Active)
                        {
                            if (!editorButtonTrigger)
                            {
                                editor.Active = !editor.Active;
                                if (editor.Active)
                                {
                                    editor.LevelName = levelName;
                                    level.ResetLevel();
                                    level.ResetScrollBounds();
                                    CollisionMan.Entities = level.GetEntities();
                                    EntityFinder.Entities = level.GetEntities();
                                    editor.Level          = level;
                                }
                                else
                                {
                                    levelName = editor.LevelName;
                                    level     = editor.Level;
                                    level.FindScrollBounds();
                                    gameState = GameState.StartLevel;
                                }
                            }
                        }
                        editorButtonTrigger = true;
                    }
                    else
                    {
                        editorButtonTrigger = false;
                    }

                    if (!editor.Active && !menu.Active)
                    {
                        GameLoop();
                    }

                    if (editor.Active)
                    {
                        if (windowHasFocus)
                        {
                            editor.Loop(window);
                        }
                    }
                    if (menu.Active)
                    {
                        if (windowHasFocus)
                        {
                            menu.Loop(window);
                        }

                        if (menu.State == Menu.MenuState.Quit)
                        {
                            quit = true;
                            break;
                        }

                        if (menu.State == Menu.MenuState.StartLevel)
                        {
                            levelName = menu.LevelName;

                            menu.Active = false;
                            level.LoadLevel(levelName);

                            gameStateC = 0;
                            gameState  = GameState.ResetFadeIn;
                            fadeOut    = 255;

                            CollisionMan.Entities = level.GetEntities();
                            EntityFinder.Entities = level.GetEntities();
                        }
                    }
                }

                window.Dispose();
                if (quit)
                {
                    break;
                }
            }

            menu.SaveLevelInfo();
        }
Beispiel #2
0
        public void Loop(RenderWindow window)
        {
            counter++;

            CursorPosF = new Vector2f(CursorPos.X * 16, CursorPos.Y * 16 + 8);

            Level.ScrollTo(CursorPosF, false);
            Level.GetEntities().RemoveAll(a => a.Destroyed);

            Type entType = Level.EntityTypes[currentEnt];

            cursorEnt = (Entity)Activator.CreateInstance(entType, CursorPos);

            if (buttonDelay == 0)
            {
                if (Keyboard.IsKeyPressed(Keyboard.Key.F3) || Keyboard.IsKeyPressed(Keyboard.Key.F5) ||
                    Keyboard.IsKeyPressed(Keyboard.Key.F1)
                    )
                {
                    showTextWindow = !showTextWindow;

                    if (Keyboard.IsKeyPressed(Keyboard.Key.F3))
                    {
                        textInputOperation = TextInputOperation.Save;
                    }
                    if (Keyboard.IsKeyPressed(Keyboard.Key.F5))
                    {
                        textInputOperation = TextInputOperation.Load;
                    }
                    if (Keyboard.IsKeyPressed(Keyboard.Key.F1))
                    {
                        textInputOperation = TextInputOperation.InfoBox;
                    }

                    if (textInputOperation == TextInputOperation.Save || textInputOperation == TextInputOperation.Load)
                    {
                        textInputString = LevelName;
                    }

                    bool addEvent = true;

                    if (textInputOperation == TextInputOperation.InfoBox)
                    {
                        Entity ent = EntityFinder.GetEnt(new Vector2f(CursorPosF.X + 8, CursorPosF.Y - 8));

                        if (ent != null && ent is InfoBox)
                        {
                            InfoBox infoBox = (InfoBox)ent;
                            textInputString = infoBox.InfoString;
                        }
                        else
                        {
                            showTextWindow = false;
                            addEvent       = false;
                        }
                    }

                    if (addEvent)
                    {
                        if (showTextWindow)
                        {
                            window.TextEntered += TextEntered;
                        }
                        else
                        {
                            window.TextEntered -= TextEntered;
                        }
                    }

                    buttonDelay = 20;
                }

                if (showTextWindow)
                {
                    if (Keyboard.IsKeyPressed(Keyboard.Key.Return))
                    {
                        if (textInputOperation == TextInputOperation.Save)
                        {
                            LevelName = textInputString;
                            Level.SaveLevel(LevelName);
                        }
                        if (textInputOperation == TextInputOperation.Load)
                        {
                            LevelName = textInputString;

                            Level.LoadLevel(LevelName, true);

                            CollisionMan.Entities = Level.GetEntities();
                            EntityFinder.Entities = Level.GetEntities();
                        }
                        if (textInputOperation == TextInputOperation.InfoBox)
                        {
                            Entity ent = EntityFinder.GetEnt(new Vector2f(CursorPosF.X + 8, CursorPosF.Y - 8));

                            if (ent != null && ent is InfoBox)
                            {
                                InfoBox infoBox = (InfoBox)ent;
                                infoBox.InfoString = textInputString;
                            }
                        }

                        showTextWindow      = false;
                        window.TextEntered -= TextEntered;
                    }

                    if (Keyboard.IsKeyPressed(Keyboard.Key.Escape))
                    {
                        showTextWindow      = false;
                        window.TextEntered -= TextEntered;
                    }
                }
            }

            if (buttonDelay == 0 && !showTextWindow)
            {
                if (Keyboard.IsKeyPressed(Keyboard.Key.Z))
                {
                    if (currentEnt > 0)
                    {
                        currentEnt--;
                    }
                    buttonDelay = 10;
                }

                if (Keyboard.IsKeyPressed(Keyboard.Key.X))
                {
                    if (currentEnt < Level.EntityTypes.Count - 1)
                    {
                        currentEnt++;
                    }
                    buttonDelay = 10;
                }

                if (Keyboard.IsKeyPressed(Keyboard.Key.F12))
                {
                    Level.GetEntities().Clear();

                    buttonDelay = 10;
                }

                if (Keyboard.IsKeyPressed(Keyboard.Key.Delete))
                {
                    Entity ent = EntityFinder.GetEnt(new Vector2f(CursorPosF.X + 8, CursorPosF.Y - 8));

                    if (ent != null)
                    {
                        ent.Destroyed = true;
                    }

                    buttonDelay = 10;
                }

                if (Keyboard.IsKeyPressed(Keyboard.Key.F))
                {
                    Entity ent = EntityFinder.GetEnt(new Vector2f(CursorPosF.X + 8, CursorPosF.Y - 8));

                    if (ent != null)
                    {
                        ent.Flipped = !ent.Flipped;
                    }

                    buttonDelay = 10;
                }

                if (Keyboard.IsKeyPressed(Keyboard.Key.Space))
                {
                    Entity ent = EntityFinder.GetEnt(new Vector2f(CursorPosF.X + 8, CursorPosF.Y - 8));

                    if (ent != null)
                    {
                        ent.Destroyed = true;
                    }

                    Entity newEnt = (Entity)Activator.CreateInstance(entType, CursorPos);
                    Level.GetEntities().Add(newEnt);


                    buttonDelay = 10;
                }

                if (Keyboard.IsKeyPressed(Keyboard.Key.Right))
                {
                    CursorPos   = new Vector2i(CursorPos.X + 1, CursorPos.Y);
                    buttonDelay = 10;
                }
                if (Keyboard.IsKeyPressed(Keyboard.Key.Left))
                {
                    CursorPos   = new Vector2i(CursorPos.X - 1, CursorPos.Y);
                    buttonDelay = 10;
                }
                if (Keyboard.IsKeyPressed(Keyboard.Key.Up))
                {
                    CursorPos   = new Vector2i(CursorPos.X, CursorPos.Y - 1);
                    buttonDelay = 10;
                }
                if (Keyboard.IsKeyPressed(Keyboard.Key.Down))
                {
                    CursorPos   = new Vector2i(CursorPos.X, CursorPos.Y + 1);
                    buttonDelay = 10;
                }
            }
            if (buttonDelay > 0)
            {
                buttonDelay--;
            }
        }