Esempio n. 1
0
        public static void launch()
        {
            Sound music = new Sound();

            if (Program.SoundEnabled && Program.Musics.Length > 0)
            {
                music = new Sound(Program.Musics[Generator.Next(Program.Musics.Length)])
                {
                    Loop = true, Volume = Program.DefaultVolume
                }
            }
            ;
            var tiles = new GameTile[40, 30];

            for (int x = 0; x < 40; x++)
            {
                for (int y = 0; y < 30; y++)
                {
                    tiles[x, y] = new GameTile
                    {
                        type         = Tile.BASE,
                        lavaVersion  = Generator.Next(3),
                        breakState   = 0,
                        erosionLevel = 0
                    };
                }
            }
            //lava borders
            for (int x = 0; x < 40; x++)
            {
                for (int y = 0; y < 2; y++)
                {
                    tiles[x, y].type = Tile.LAVA;
                }
            }
            for (int x = 0; x < 2; x++)
            {
                for (int y = 0; y < 30; y++)
                {
                    tiles[x, y].type = Tile.LAVA;
                }
            }
            for (int x = 38; x < 40; x++)
            {
                for (int y = 0; y < 30; y++)
                {
                    tiles[x, y].type = Tile.LAVA;
                }
            }
            for (int x = 0; x < 40; x++)
            {
                for (int y = 28; y < 30; y++)
                {
                    tiles[x, y].type = Tile.LAVA;
                }
            }
            {
                int nbHoles = Generator.Next(20);
                for (int i = 0; i < nbHoles; i++)
                {
                    tiles[Generator.Next(40), Generator.Next(30)].type = Tile.LAVA;
                }
            }
            void updateSingleTile(int x, int y, bool randomize)
            {
                const int TOP   = 1;
                const int LEFT  = 1 << 1;
                const int BOT   = 1 << 2;
                const int RIGHT = 1 << 3;

                if (tiles[x, y].type != Tile.LAVA)
                {
                    int borders = 0;
                    if (x > 0 && tiles[x - 1, y].type == Tile.LAVA)
                    {
                        borders |= LEFT;
                    }
                    if (x < 49 && tiles[x + 1, y].type == Tile.LAVA)
                    {
                        borders |= RIGHT;
                    }
                    if (y > 0 && tiles[x, y - 1].type == Tile.LAVA)
                    {
                        borders |= TOP;
                    }
                    if (y < 49 && tiles[x, y + 1].type == Tile.LAVA)
                    {
                        borders |= BOT;
                    }
                    if (borders == LEFT)
                    {
                        tiles[x, y].type = Tile.BORDER_LEFT;
                    }
                    if (borders == RIGHT)
                    {
                        tiles[x, y].type = Tile.BORDER_RIGHT;
                    }
                    if (borders == TOP)
                    {
                        tiles[x, y].type = Tile.BORDER_TOP;
                    }
                    if (borders == BOT)
                    {
                        tiles[x, y].type = Tile.BORDER_BOT;
                    }
                    if (borders == (RIGHT | LEFT))
                    {
                        tiles[x, y].type = Tile.STRAIGHT_VER;
                    }
                    if (borders == (TOP | BOT))
                    {
                        tiles[x, y].type = Tile.STRAIGHT_HOR;
                    }
                    if (borders == (TOP | LEFT))
                    {
                        tiles[x, y].type = Tile.CORNER_TOPLEFT;
                    }
                    if (borders == (BOT | LEFT))
                    {
                        tiles[x, y].type = Tile.CORNER_BOTLEFT;
                    }
                    if (borders == (TOP | RIGHT))
                    {
                        tiles[x, y].type = Tile.CORNER_TOPRIGHT;
                    }
                    if (borders == (BOT | RIGHT))
                    {
                        tiles[x, y].type = Tile.CORNER_BOTRIGHT;
                    }
                    if (borders == (BOT | RIGHT | TOP))
                    {
                        tiles[x, y].type = Tile.TRIBORDER_RIGHT;
                    }
                    if (borders == (BOT | LEFT | TOP))
                    {
                        tiles[x, y].type = Tile.TRIBORDER_LEFT;
                    }
                    if (borders == (RIGHT | LEFT | TOP))
                    {
                        tiles[x, y].type = Tile.TRIBORDER_TOP;
                    }
                    if (borders == (RIGHT | LEFT | BOT))
                    {
                        tiles[x, y].type = Tile.TRIBORDER_BOT;
                    }
                    if (borders == 0b1111)
                    {
                        tiles[x, y].type = Tile.ISLE;
                    }
                }
                if (randomize)
                {
                    tiles[x, y].version = Generator.Next(Tile.TilesTextures[tiles[x, y].type].Count);
                }
            }

            void updateTiles(bool randomize)
            {
                for (int x = 0; x < 40; x++)
                {
                    for (int y = 0; y < 30; y++)
                    {
                        updateSingleTile(x, y, randomize);
                    }
                }
            }

            updateTiles(true);
            var multStates = RenderStates.Default;

            multStates.BlendMode = BlendMode.Multiply;
            var addStates = RenderStates.Default;

            addStates.BlendMode = BlendMode.Add;
            var dummyShape    = new VertexArray(PrimitiveType.TriangleFan, 6);
            var lightOpacity  = 1f;
            var delta         = Time.Zero;
            var lightTimer    = Time.Zero;
            var gameView      = new View(App.GetView());
            var viewObjective = new Vector2f();

            player = new Miner(false);
            var stage       = 0;
            var keepPlaying = true;

            do
            {
                player.Position = new Vector2f((Generator.Next(38)) << 6, (Generator.Next(28)) << 6);
            }while (tiles[(int)player.Position.X >> 6, (int)player.Position.Y >> 6].type == Tile.LAVA);

            var selectedBloc = player.Position;
            var endMessage   = new CustomText {
                Height = 30
            };
            var endExit = new CustomText("Quitter")
            {
                Color = Utilities.Red, Height = 30
            };
            var endCursor = new RightCursor(48)
            {
                Color = Utilities.Red
            };

            endMessage.Position = (Vector2f)App.Size / 2;
            endExit.Position    = (Vector2f)App.Size / 2 + new Vector2f(0, 100);
            endExit.Origin      = new Vector2f(endExit.GlobalBounds.Width, endExit.GlobalBounds.Height) / 2;
            endCursor.Position  = new Vector2f(endExit.GlobalBounds.Left - 10, endExit.Position.Y);
            var pauseContinue = new CustomText("Continuer")
            {
                Height = 40
            };

            pauseContinue.Position = (Vector2f)App.Size / 2 - new Vector2f(0, 50);
            pauseContinue.Origin   = new Vector2f(pauseContinue.GlobalBounds.Width, pauseContinue.GlobalBounds.Height) / 2;
            var pauseQuit = new CustomText("Quitter")
            {
                Height = 40
            };

            pauseQuit.Position = (Vector2f)App.Size / 2 + new Vector2f(0, 50);
            pauseQuit.Origin   = new Vector2f(pauseQuit.GlobalBounds.Width, pauseQuit.GlobalBounds.Height) / 2;
            CustomText selectedPauseOption = null;
            var        pauseCursor         = new RightCursor(64);

            void mouseClick(object sender, MouseButtonEventArgs e)
            {
                if (e.Button == Mouse.Button.Right)
                {
                    App.SetMouseCursorGrabbed(true);
                    App.SetMouseCursorVisible(false);
                    Mouse.SetPosition((Vector2i)App.Size / 2, App);
                }
                if (e.Button == Mouse.Button.Left)
                {
                    if (stage == 0)
                    {
                        var mousePos = App.MapPixelToCoords(e);
                        int x        = (int)mousePos.X >> 6;
                        int y        = (int)mousePos.Y >> 6;
                        selectedBloc = new Vector2f(x << 6, y << 6);
                    }
                    if (stage == 3)
                    {
                        if (endExit.GlobalBounds.Contains(App.MapPixelToCoords(e)))
                        {
                            keepPlaying = false;
                        }
                    }
                    if (stage == 4)
                    {
                        if (pauseQuit.GlobalBounds.Contains(App.MapPixelToCoords(e)))
                        {
                            keepPlaying = false;
                        }
                        if (pauseContinue.GlobalBounds.Contains(App.MapPixelToCoords(e)))
                        {
                            stage = 0;
                        }
                    }
                }
            }

            void mouseRelease(object sender, MouseButtonEventArgs e)
            {
                if (e.Button == Mouse.Button.Right)
                {
                    App.SetMouseCursorGrabbed(false);
                    App.SetMouseCursorVisible(true);
                }
            }

            var movementAvailable = 0;
            var stage2Clock       = new Clock();
            var stage3Clock       = new Clock();
            Dictionary <Miner, (Vector2f, Vector2f, int)> EnnemiesDepl = null;
            Vector2f initialStage2PlayerPos = default;

            void keyDown(object sender, KeyEventArgs e)
            {
                if (stage == 0)
                {
                    if (Utilities.IsUpKey(e.Code))
                    {
                        selectedBloc -= new Vector2f(0, 64);
                    }
                    if (Utilities.IsDownKey(e.Code))
                    {
                        selectedBloc += new Vector2f(0, 64);
                    }
                    if (Utilities.IsLeftKey(e.Code))
                    {
                        selectedBloc -= new Vector2f(64, 0);
                    }
                    if (Utilities.IsRightKey(e.Code))
                    {
                        selectedBloc += new Vector2f(64, 0);
                    }
                    if (Utilities.IsCancelKey(e.Code) && e.Code != Keyboard.Key.Escape)
                    {
                        selectedBloc = player.Position;
                    }
                    if (e.Code == Keyboard.Key.Escape)
                    {
                        selectedPauseOption = pauseContinue;
                        stage = 4;
                    }
                    selectedBloc.X = Math.Min(39 * 64, selectedBloc.X);
                    selectedBloc.X = Math.Max(0, selectedBloc.X);
                    selectedBloc.Y = Math.Min(29 * 64, selectedBloc.Y);
                    selectedBloc.Y = Math.Max(0, selectedBloc.Y);
                    if (Utilities.IsConfirmKey(e.Code) && movementAvailable < 2)
                    {
                        stage2Clock.Restart();
                        stage = 1;
                        initialStage2PlayerPos = player.Position;
                        updateBreakState();
                        var currTile = tiles[(int)initialStage2PlayerPos.X >> 6, (int)initialStage2PlayerPos.Y >> 6];
                        if (currTile.breakState == 0 && currTile.type != Tile.LAVA)
                        {
                            currTile.breakState++;
                        }
                    }
                }
                else if (stage == 3)
                {
                    if (Utilities.IsConfirmKey(e.Code))
                    {
                        keepPlaying = false;
                    }
                }
                else if (stage == 4)
                {
                    if (Utilities.IsUpKey(e.Code) || Utilities.IsDownKey(e.Code))
                    {
                        selectedPauseOption = selectedPauseOption == pauseContinue ? pauseQuit : pauseContinue;
                    }
                    if (Utilities.IsCancelKey(e.Code))
                    {
                        stage = 0;
                    }
                    if (Utilities.IsConfirmKey(e.Code))
                    {
                        if (selectedPauseOption == pauseContinue)
                        {
                            stage = 0;
                        }
                        else
                        {
                            keepPlaying = false;
                        }
                    }
                }
            }

            App.MouseButtonPressed  += mouseClick;
            App.MouseButtonReleased += mouseRelease;
            App.KeyPressed          += keyDown;
            gameView.Center          = player.Position;
            void updateBlocGraphics(int x, int y)
            {
                var current = tiles[x, y];

                if (current.breakState >= 4)
                {
                    current.type       = Tile.LAVA;
                    current.version    = 0;
                    current.breakState = 0;
                }
            }

            void updateSingleBreakState(int x, int y)
            {
                var current = tiles[x, y];

                if (current.type != Tile.LAVA)
                {
                    if (current.breakState > 0)
                    {
                        current.breakState++;
                    }
                    updateBlocGraphics(x, y);
                    if (current.breakState == 0 && current.type != Tile.LAVA)
                    {
                        if (tiles[x - 1, y].type == Tile.LAVA)
                        {
                            current.erosionLevel++;
                        }
                        if (tiles[x + 1, y].type == Tile.LAVA)
                        {
                            current.erosionLevel++;
                        }
                        if (tiles[x, y - 1].type == Tile.LAVA)
                        {
                            current.erosionLevel++;
                        }
                        if (tiles[x, y + 1].type == Tile.LAVA)
                        {
                            current.erosionLevel++;
                        }
                        if (current.erosionLevel >= 4)
                        {
                            current.breakState = 1;
                            current.version    = Generator.Next(Tile.TilesTextures[current.type].Count);
                        }
                    }
                }
            }

            void updateBreakState()
            {
                for (int x = 2; x < 38; x++)
                {
                    for (int y = 2; y < 28; y++)
                    {
                        updateSingleBreakState(x, y);
                    }
                }
                updateTiles(false);
            }

            var ennemies = new List <Miner>();
            var selector = new BlocSelector();

            for (int i = 0; i < 9; i++)
            {
                var  ennemy = new Miner(true);
                bool good;
                do
                {
                    good            = true;
                    ennemy.Position = new Vector2f((Generator.Next(38)) << 6, (Generator.Next(28)) << 6);
                    if (tiles[(int)ennemy.Position.X >> 6, (int)ennemy.Position.Y >> 6].type == Tile.LAVA)
                    {
                        good = false;
                    }
                    if (player.Position == ennemy.Position)
                    {
                        good = false;
                    }
                    foreach (var other in ennemies)
                    {
                        if (other.Position == ennemy.Position)
                        {
                            good = false;
                        }
                    }
                }while (!good);
                ennemies.Add(ennemy);
            }
            var ennemiesPlaying = new CustomText {
                Height = 20
            };

            music.Play();
            var clock = new Clock();

            while (App.IsOpen && keepPlaying)
            {
                delta       = clock.Restart();
                lightTimer += delta;
                App.DispatchEvents();
                player.Update(delta);
                {
                    var perc   = stage3Clock.ElapsedTime.AsSeconds() / Time.FromMilliseconds(250).AsSeconds();
                    var height = (float)Math.Sqrt(1 - 2 * Math.Abs(.5f - perc));
                    foreach (var ennemy in ennemies)
                    {
                        ennemy.Update(delta);
                        if (stage == 2 && !ennemy.Dead)
                        {
                            var initialPos = EnnemiesDepl[ennemy].Item1;
                            var endPos     = EnnemiesDepl[ennemy].Item2;
                            var movType    = EnnemiesDepl[ennemy].Item3;
                            if (movType == 0)
                            {
                                ennemy.Position = initialPos + perc * (endPos - initialPos) + new Vector2f(0, -height * 16);
                            }
                            else
                            {
                                ennemy.Position = initialPos + perc * (endPos - initialPos) + new Vector2f(0, -height * 48);
                            }
                        }
                    }
                }
                if (stage == 4)
                {
                    if (pauseQuit.GlobalBounds.Contains(App.MapPixelToCoords(Mouse.GetPosition(App))))
                    {
                        selectedPauseOption = pauseQuit;
                    }
                    if (pauseContinue.GlobalBounds.Contains(App.MapPixelToCoords(Mouse.GetPosition(App))))
                    {
                        selectedPauseOption = pauseContinue;
                    }
                    if (selectedPauseOption == pauseContinue)
                    {
                        pauseContinue.Color = Utilities.Green;
                        pauseQuit.Color     = Color.White;
                    }
                    else
                    {
                        pauseQuit.Color     = Utilities.Red;
                        pauseContinue.Color = Color.White;
                    }
                    pauseCursor.Color    = selectedPauseOption.Color;
                    pauseCursor.Position = new Vector2f(selectedPauseOption.GlobalBounds.Left - 5, selectedPauseOption.Position.Y);
                }
                ennemiesPlaying.Text = "Ennemis restant : " + ennemies.Where(m => !m.Dead).Count();
                if (stage == 2 && stage3Clock.ElapsedTime > Time.FromMilliseconds(250))
                {
                    foreach (var ennemy in ennemies)
                    {
                        var endPos  = EnnemiesDepl[ennemy].Item2;
                        var movType = EnnemiesDepl[ennemy].Item3;
                        ennemy.Position = endPos;
                        if (tiles[(int)endPos.X >> 6, (int)endPos.Y >> 6].type == Tile.LAVA)
                        {
                            ennemy.Die();
                        }
                        if (movType != 0)
                        {
                            tiles[(int)endPos.X >> 6, (int)endPos.Y >> 6].erosionLevel = 4;
                            updateSingleBreakState((int)endPos.X >> 6, (int)endPos.Y >> 6);
                            if (tiles[(int)endPos.X >> 6, (int)endPos.Y >> 6].type == Tile.LAVA)
                            {
                                updateSingleTile(((int)endPos.X >> 6) - 1, (int)endPos.Y >> 6, false);
                                updateSingleTile(((int)endPos.X >> 6) + 1, (int)endPos.Y >> 6, false);
                                updateSingleTile((int)endPos.X >> 6, ((int)endPos.Y >> 6) + 1, false);
                                updateSingleTile((int)endPos.X >> 6, ((int)endPos.Y >> 6) - 1, false);
                            }
                        }
                    }
                    stage = 0;
                }
                selector.Update(delta);
                selector.Position = selectedBloc;
                if (stage == 0)
                {
                    if (tiles[(int)player.Position.X >> 6, (int)player.Position.Y >> 6].type == Tile.LAVA)
                    {
                        stage = 3;
                        player.Die();
                        int pos = 1;
                        foreach (var en in ennemies)
                        {
                            if (!en.Dead)
                            {
                                pos++;
                            }
                        }
                        endMessage.Text   = "Vous avez perdu ! Vous etes " + pos.ToString() + "eme.";
                        endMessage.Origin = new Vector2f(endMessage.GlobalBounds.Width, endMessage.GlobalBounds.Height) / 2;
                    }
                    {
                        bool allDead = true;
                        foreach (var en in ennemies)
                        {
                            if (!en.Dead)
                            {
                                allDead = false;
                            }
                        }
                        if (allDead)
                        {
                            stage             = 3;
                            endMessage.Text   = "Vous avez gagne ! Vous etes le dernier survivant !";
                            endMessage.Color  = Utilities.Green;
                            endMessage.Origin = new Vector2f(endMessage.GlobalBounds.Width, endMessage.GlobalBounds.Height) / 2;
                        }
                    }
                    viewObjective = selectedBloc + new Vector2f(32, 32);
                    bool onEnnemy = false;
                    foreach (var ennemy in ennemies)
                    {
                        if (selectedBloc == ennemy.Position)
                        {
                            if (!ennemy.Dead)
                            {
                                onEnnemy = true;
                            }
                            break;
                        }
                    }
                    if (onEnnemy)
                    {
                        movementAvailable = 2;
                    }
                    else if (selector.Position == player.Position + new Vector2f(64, 0) ||
                             selector.Position == player.Position + new Vector2f(-64, 0) ||
                             selector.Position == player.Position + new Vector2f(0, 64) ||
                             selector.Position == player.Position + new Vector2f(0, -64) ||
                             selector.Position == player.Position)
                    {
                        movementAvailable = 0;
                    }
                    else if (selector.Position == player.Position + new Vector2f(128, 0) ||
                             selector.Position == player.Position + new Vector2f(-128, 0) ||
                             selector.Position == player.Position + new Vector2f(0, 128) ||
                             selector.Position == player.Position + new Vector2f(0, -128))
                    {
                        movementAvailable = 1;
                    }
                    else
                    {
                        movementAvailable = 2;
                    }
                    switch (movementAvailable)
                    {
                    case 0:
                        selector.Color = new Color(50, 250, 150);
                        break;

                    case 1:
                        selector.Color = new Color(250, 150, 50);
                        break;

                    case 2:
                        selector.Color = new Color(250, 50, 150);
                        break;
                    }
                }
                else if (stage == 1)
                {
                    void generateEnnemyMoves()
                    {
                        EnnemiesDepl = new Dictionary <Miner, (Vector2f, Vector2f, int)>();
                        foreach (var ennemy in ennemies)
                        {
                            if (tiles[(int)ennemy.Position.X >> 6, (int)ennemy.Position.Y >> 6].type == Tile.LAVA)
                            {
                                ennemy.Die();
                            }
                            if (!ennemy.Dead)
                            {
                                var movAvail = new List <(Vector2f, int)>();
                                {
                                    int x = (int)ennemy.Position.X >> 6;
                                    int y = (int)ennemy.Position.Y >> 6;
                                    if (tiles[x, y].type != Tile.LAVA && tiles[x, y].breakState < 3)
                                    {
                                        movAvail.Add((new Vector2f(x << 6, y << 6), 0));
                                    }
                                    if (tiles[x + 1, y].type != Tile.LAVA && tiles[x + 1, y].breakState < 3)
                                    {
                                        movAvail.Add((new Vector2f((x + 1) << 6, y << 6), 0));
                                    }
                                    if (tiles[x + 2, y].type != Tile.LAVA && tiles[x + 2, y].breakState < 2)
                                    {
                                        movAvail.Add((new Vector2f((x + 2) << 6, y << 6), 1));
                                    }
                                    if (tiles[x - 1, y].type != Tile.LAVA && tiles[x - 1, y].breakState < 3)
                                    {
                                        movAvail.Add((new Vector2f((x - 1) << 6, y << 6), 0));
                                    }
                                    if (tiles[x - 2, y].type != Tile.LAVA && tiles[x - 2, y].breakState < 2)
                                    {
                                        movAvail.Add((new Vector2f((x - 2) << 6, y << 6), 1));
                                    }
                                    if (tiles[x, y + 1].type != Tile.LAVA && tiles[x, y + 1].breakState < 3)
                                    {
                                        movAvail.Add((new Vector2f(x << 6, (y + 1) << 6), 0));
                                    }
                                    if (tiles[x, y + 2].type != Tile.LAVA && tiles[x, y + 2].breakState < 2)
                                    {
                                        movAvail.Add((new Vector2f(x << 6, (y + 2) << 6), 1));
                                    }
                                    if (tiles[x, y - 1].type != Tile.LAVA && tiles[x, y - 1].breakState < 3)
                                    {
                                        movAvail.Add((new Vector2f(x << 6, (y - 1) << 6), 0));
                                    }
                                    if (tiles[x, y - 2].type != Tile.LAVA && tiles[x, y - 2].breakState < 2)
                                    {
                                        movAvail.Add((new Vector2f(x << 6, (y - 2) << 6), 1));
                                    }
                                    for (int i = movAvail.Count - 1; i >= 0; i--)
                                    {
                                        var  move      = movAvail[i];
                                        bool deletThis = false;
                                        if (move.Item1 == player.Position)
                                        {
                                            deletThis = true;
                                        }
                                        foreach (var en in EnnemiesDepl)
                                        {
                                            if (!en.Key.Dead)
                                            {
                                                if (move.Item1 == en.Value.Item1)
                                                {
                                                    deletThis = true;
                                                }
                                                if (move.Item1 == en.Value.Item2)
                                                {
                                                    deletThis = true;
                                                }
                                            }
                                        }
                                        if (deletThis)
                                        {
                                            movAvail.Remove(move);
                                        }
                                    }
                                    if (movAvail.Count > 0)
                                    {
                                        var rand = Generator.Next(movAvail.Count);
                                        EnnemiesDepl.Add(ennemy, (ennemy.Position, movAvail[rand].Item1, movAvail[rand].Item2));
                                    }
                                    else
                                    {
                                        EnnemiesDepl.Add(ennemy, (ennemy.Position, ennemy.Position, 0));
                                    }
                                }
                                var currTile = tiles[(int)ennemy.Position.X >> 6, (int)ennemy.Position.Y >> 6];
                                if (currTile.breakState == 0 && currTile.type != Tile.LAVA)
                                {
                                    currTile.breakState++;
                                }
                            }
                            else
                            {
                                EnnemiesDepl.Add(ennemy, (ennemy.Position, ennemy.Position, 0));
                            }
                        }
                    }

                    if (movementAvailable == 0)
                    {
                        if (stage2Clock.ElapsedTime < Time.FromMilliseconds(250))
                        {
                            var perc   = stage2Clock.ElapsedTime.AsSeconds() / Time.FromMilliseconds(250).AsSeconds();
                            var height = (float)Math.Sqrt(1 - 2 * Math.Abs(.5f - perc));
                            player.Position = initialStage2PlayerPos + perc * (selectedBloc - initialStage2PlayerPos) + new Vector2f(0, -height * 16);
                        }
                        else
                        {
                            player.Position = selectedBloc;
                            stage           = 2;
                            stage3Clock.Restart();
                            generateEnnemyMoves();
                        }
                    }
                    else
                    {
                        if (stage2Clock.ElapsedTime < Time.FromMilliseconds(250))
                        {
                            var perc   = stage2Clock.ElapsedTime.AsSeconds() / Time.FromMilliseconds(250).AsSeconds();
                            var height = (float)Math.Sqrt(1 - 2 * Math.Abs(.5f - perc));
                            player.Position = initialStage2PlayerPos + perc * (selectedBloc - initialStage2PlayerPos) + new Vector2f(0, -height * 48);
                        }
                        else
                        {
                            player.Position = selectedBloc;
                            stage           = 2;
                            stage3Clock.Restart();
                            generateEnnemyMoves();
                            tiles[(int)selectedBloc.X >> 6, (int)selectedBloc.Y >> 6].erosionLevel = 4;
                            updateSingleBreakState((int)selectedBloc.X >> 6, (int)selectedBloc.Y >> 6);
                            if (tiles[(int)selectedBloc.X >> 6, (int)selectedBloc.Y >> 6].type == Tile.LAVA)
                            {
                                updateSingleTile(((int)selectedBloc.X >> 6) - 1, (int)selectedBloc.Y >> 6, false);
                                updateSingleTile(((int)selectedBloc.X >> 6) + 1, (int)selectedBloc.Y >> 6, false);
                                updateSingleTile((int)selectedBloc.X >> 6, ((int)selectedBloc.Y >> 6) + 1, false);
                                updateSingleTile((int)selectedBloc.X >> 6, ((int)selectedBloc.Y >> 6) - 1, false);
                            }
                        }
                    }
                }
                if (lightTimer > Time.FromMilliseconds(120))
                {
                    lightTimer   = Time.Zero;
                    lightOpacity = .8f + (float)Generator.NextDouble() * .2f;
                }
                if (Mouse.IsButtonPressed(Mouse.Button.Right))
                {
                    if (App.MapPixelToCoords(new Vector2i(900, 600)).X - App.MapPixelToCoords(new Vector2i()).X < 900 * 1.5f)
                    {
                        gameView.Zoom(1 + 5 * delta.AsSeconds());
                    }
                    gameView.Move((App.MapPixelToCoords(Mouse.GetPosition(App)) - App.MapPixelToCoords((Vector2i)App.Size / 2)) * -2);
                    Mouse.SetPosition((Vector2i)App.Size / 2, App);
                }
                else
                {
                    if (App.MapPixelToCoords(new Vector2i(900, 600)).X - App.MapPixelToCoords(new Vector2i()).X > 900)
                    {
                        gameView.Zoom(1 - 2f * delta.AsSeconds());
                    }
                    var toObjective = viewObjective - gameView.Center;
                    var dist        = (float)Math.Sqrt(toObjective.X * toObjective.X + toObjective.Y * toObjective.Y);
                    if (dist > 10)
                    {
                        gameView.Move(toObjective / dist * Math.Max(5, dist * 10 * delta.AsSeconds()));
                    }
                }
                Tile.UpdateLava(delta);

                App.Clear(new Color(255, 255, 50));
                LightMap.Clear();
                var visibleBlocks = new FloatRect(App.MapPixelToCoords(new Vector2i()), App.MapPixelToCoords(new Vector2i(900, 600)));
                visibleBlocks.Left   = Math.Max(0, visibleBlocks.Left / 64 - 2);
                visibleBlocks.Top    = Math.Max(0, visibleBlocks.Top / 64 - 2);
                visibleBlocks.Width  = Math.Min(40, visibleBlocks.Width / 64 + 3);
                visibleBlocks.Height = Math.Min(30, visibleBlocks.Height / 64 + 3);

                App.SetView(gameView);

                {
                    var topleft = App.MapPixelToCoords(new Vector2i());
                    if (topleft.X < 0)
                    {
                        gameView.Move(new Vector2f(-topleft.X, 0));
                    }
                    if (topleft.Y < 0)
                    {
                        gameView.Move(new Vector2f(0, -topleft.Y));
                    }
                }
                {
                    var botright = App.MapPixelToCoords(new Vector2i(900, 600));
                    if (botright.X > 64 * 40)
                    {
                        gameView.Move(new Vector2f(64 * 40 - botright.X, 0));
                    }
                    if (botright.Y > 64 * 30)
                    {
                        gameView.Move(new Vector2f(0, 64 * 30 - botright.Y));
                    }
                }
                LightMap.SetView(gameView);
                App.SetView(gameView);

                for (int x = (int)visibleBlocks.Left; x < (int)visibleBlocks.Width; x++)
                {
                    for (int y = (int)visibleBlocks.Top; y < (int)visibleBlocks.Height; y++)
                    {
                        if (tiles[x, y].type == Tile.LAVA && x > 0 && y > 0 && x < 39 && y < 29)
                        {
                            if (tiles[x + 1, y].type != Tile.LAVA ||
                                tiles[x - 1, y].type != Tile.LAVA ||
                                tiles[x, y + 1].type != Tile.LAVA ||
                                tiles[x, y - 1].type != Tile.LAVA)
                            {
                                dummyShape[0] = new Vertex(new Vector2f(x * 64 + 32, y * 64 + 32), new Color(255, 150, 60));
                                for (int i = 1; i <= 5; i++)
                                {
                                    dummyShape[(uint)i] = new Vertex(new Vector2f(x * 64 + 32, y * 64 + 32) + new Vector2f((float)Math.Cos(i * Math.PI * 2 / 4), (float)Math.Sin(i * Math.PI * 2 / 4)) * 256 * lightOpacity, Color.Black);
                                }
                                LightMap.Draw(dummyShape, addStates);
                            }
                        }
                        if (((tiles[x, y].type - 1) % 4) == 3)
                        {
                            dummyShape[0] = new Vertex(new Vector2f(x * 64 + 32, y * 64 + 32), new Color(150, 80, 30));
                            for (int i = 1; i <= 5; i++)
                            {
                                dummyShape[(uint)i] = new Vertex(new Vector2f(x * 64 + 32, y * 64 + 32) + new Vector2f((float)Math.Cos(i * Math.PI * 2 / 4), (float)Math.Sin(i * Math.PI * 2 / 4)) * 94 * lightOpacity, Color.Black);
                            }
                            LightMap.Draw(dummyShape, addStates);
                        }
                        Tile.Draw(App, tiles[x, y].type + tiles[x, y].breakState, new Vector2f(x * Tile.TILE_SIZE, y * Tile.TILE_SIZE), tiles[x, y].version, tiles[x, y].lavaVersion);
                        Tile.DrawLights(LightMap, tiles[x, y].type + tiles[x, y].breakState, new Vector2f(x * Tile.TILE_SIZE, y * Tile.TILE_SIZE), tiles[x, y].version);
                    }
                }

                LightMap.Display();
                foreach (var ennemy in ennemies)
                {
                    App.Draw(ennemy);
                }
                App.Draw(player);
                App.SetView(App.DefaultView);
                App.Draw(new Sprite(LightMap.Texture), multStates);
                App.Draw(ennemiesPlaying);
                if (stage == 4)
                {
                    App.Draw(pauseContinue);
                    App.Draw(pauseQuit);
                    App.Draw(pauseCursor);
                }
                App.SetView(gameView);
                if (stage == 0)
                {
                    App.Draw(selector);
                }
                if (stage == 3)
                {
                    App.SetView(App.DefaultView);
                    App.Draw(endMessage);
                    App.Draw(endExit);
                    App.Draw(endCursor);
                    App.SetView(gameView);
                }
                App.Display();
            }
            App.MouseButtonPressed  -= mouseClick;
            App.MouseButtonReleased -= mouseRelease;
            App.KeyPressed          -= keyDown;
            music.Stop();
            App.SetView(App.DefaultView);
        }
    }
Esempio n. 2
0
        private static void Main(string[] args)
        {
            Generator = new Random();
            App       = new RenderWindow(new VideoMode(900, 600), "SPLEEF!", Styles.Close);
            App.SetVerticalSyncEnabled(true);
            Game.LightMap = new RenderTexture(900, 600);
            App.Closed   += (sender, e) => App.Close();
            App.SetIcon(256, 256, Utilities.Icon.Pixels);
            var title = new RectangleShape(new Vector2f(43, 26) * 400 / 43)
            {
                Texture = new Texture("assets/images/spleef.png")
            };

            title.Origin   = title.Size / 2;
            title.Position = (Vector2f)App.Size / 2;
            var startMessage = new CustomText("Appuyer sur une touche")
            {
                Height = 20
            };

            startMessage.Origin   = new Vector2f(startMessage.LocalBounds.Width / 2, 10);
            startMessage.Position = new Vector2f(App.Size.X / 2, 560);
            bool bigger  = true;
            var  clock   = new Clock();
            var  darker  = true;
            var  bgGreen = .6f;
            var  spark   = new RectangleShape(new Vector2f(startMessage.LocalBounds.Width, startMessage.LocalBounds.Width))
            {
                Texture = Utilities.SparkTexture
            };

            spark.Origin = spark.Size / 2;
            var shrinker = Transform.Identity;

            shrinker.Translate(startMessage.Position);
            shrinker.Scale(1.5f, 20f / startMessage.LocalBounds.Width * 2.5f);
            var cursor = new RightCursor(64);
            {
                void stopAll(object sender, EventArgs e) => Environment.Exit(0);

                App.Closed += stopAll;
                var text = new CustomText("Chargement des musiques...")
                {
                    Height = 40
                };
                text.Origin   = new Vector2f(text.LocalBounds.Width / 2, text.LocalBounds.Height / 2);
                text.Position = (Vector2f)App.Size / 2;
                var icon = new RectangleShape(new Vector2f(64, 64))
                {
                    Texture = new Texture("assets/images/icon.png")
                };
                icon.Origin   = icon.Size / 2;
                icon.Position = (Vector2f)App.Size - new Vector2f(50, 50);
                var subClock = new Clock();
                var t        = Task.Run(() =>
                {
                    Utilities.InitMusics();
                    text.Text     = "Chargement des textures...";
                    text.Origin   = new Vector2f(text.LocalBounds.Width / 2, text.LocalBounds.Height / 2);
                    text.Position = (Vector2f)App.Size / 2;
                    Tile.InitTiles();
                });
                while (!t.IsCompleted)
                {
                    var delta = subClock.Restart();
                    App.DispatchEvents();
                    icon.Rotation += delta.AsSeconds() * 90;
                    App.Clear(new Color(255, 150, 0));
                    App.Draw(text);
                    App.Draw(icon);
                    App.Display();
                }
                App.Closed -= stopAll;
            }

            var mainMusic = new Sound(MainTheme)
            {
                Loop = true, Volume = DefaultVolume
            };

            var rotation = 0f;

            bool titleTime = true;

            var play = new CustomText("Jouer")
            {
                Height   = 40,
                Position = new Vector2f(450, 250)
            };

            play.Origin = new Vector2f(play.LocalBounds.Width / 2, 20);
            var howTo = new CustomText("Comment jouer")
            {
                Height   = 40,
                Position = new Vector2f(450, 350)
            };

            howTo.Origin = new Vector2f(howTo.LocalBounds.Width / 2, 20);
            var quit = new CustomText("Quitter")
            {
                Height   = 40,
                Position = new Vector2f(450, 450)
            };

            quit.Origin = new Vector2f(quit.LocalBounds.Width / 2, 20);
            var sound = new CustomText("$")
            {
                Height = 40
            };

            sound.Position = new Vector2f(900 - sound.LocalBounds.Width, 550);
            sound.Origin   = new Vector2f(sound.LocalBounds.Width / 2, 20);
            CustomText selection = play;

            void keyPressed(object sender, KeyEventArgs e)
            {
                if (titleTime)
                {
                    titleTime = false;
                    selection = play;
                }
                else
                {
                    if (Utilities.IsUpKey(e.Code))
                    {
                        if (selection == play)
                        {
                            selection = quit;
                        }
                        else if (selection == quit)
                        {
                            selection = howTo;
                        }
                        else if (selection == howTo)
                        {
                            selection = play;
                        }
                        else if (selection == sound)
                        {
                            selection = quit;
                        }
                    }
                    if (Utilities.IsDownKey(e.Code))
                    {
                        if (selection == play)
                        {
                            selection = howTo;
                        }
                        else if (selection == howTo)
                        {
                            selection = quit;
                        }
                        else if (selection == quit)
                        {
                            selection = play;
                        }
                        else if (selection == sound)
                        {
                            selection = play;
                        }
                    }
                    if (Utilities.IsRightKey(e.Code) || Utilities.IsLeftKey(e.Code))
                    {
                        if (selection == sound)
                        {
                            selection = play;
                        }
                        else
                        {
                            selection = sound;
                        }
                    }
                    if (Utilities.IsCancelKey(e.Code))
                    {
                        titleTime = true;
                    }
                    if (Utilities.IsConfirmKey(e.Code))
                    {
                        if (selection == play)
                        {
                            App.KeyPressed         -= keyPressed;
                            App.MouseButtonPressed -= mouseClick;
                            if (SoundEnabled)
                            {
                                mainMusic.Pause();
                            }
                            Game.launch();
                            if (SoundEnabled)
                            {
                                mainMusic.Play();
                            }
                            App.KeyPressed         += keyPressed;
                            App.MouseButtonPressed += mouseClick;
                            clock.Restart();
                        }
                        if (selection == quit)
                        {
                            App.Close();
                        }
                        if (selection == sound)
                        {
                            SoundEnabled = !SoundEnabled;
                            sound.Text   = SoundEnabled ? "$" : "£";
                            if (SoundEnabled)
                            {
                                mainMusic.Play();
                            }
                            else
                            {
                                mainMusic.Pause();
                            }
                        }
                        if (selection == howTo)
                        {
                            App.KeyPressed         -= keyPressed;
                            App.MouseButtonPressed -= mouseClick;
                            HelpPannel();
                            App.KeyPressed         += keyPressed;
                            App.MouseButtonPressed += mouseClick;
                            clock.Restart();
                        }
                    }
                }
            }

            void mouseClick(object sender, MouseButtonEventArgs e)
            {
                if (titleTime)
                {
                    titleTime = false;
                    selection = play;
                }
                else
                {
                    if (play.GlobalBounds.Contains(App.MapPixelToCoords(e)))
                    {
                        App.KeyPressed         -= keyPressed;
                        App.MouseButtonPressed -= mouseClick;
                        if (SoundEnabled)
                        {
                            mainMusic.Pause();
                        }
                        Game.launch();
                        if (SoundEnabled)
                        {
                            mainMusic.Play();
                        }
                        App.KeyPressed         += keyPressed;
                        App.MouseButtonPressed += mouseClick;
                        clock.Restart();
                    }
                    else if (howTo.GlobalBounds.Contains(App.MapPixelToCoords(e)))
                    {
                        App.KeyPressed         -= keyPressed;
                        App.MouseButtonPressed -= mouseClick;
                        HelpPannel();
                        App.KeyPressed         += keyPressed;
                        App.MouseButtonPressed += mouseClick;
                        clock.Restart();
                    }
                    else if (quit.GlobalBounds.Contains(App.MapPixelToCoords(e)))
                    {
                        App.Close();
                    }
                    else if (sound.GlobalBounds.Contains(App.MapPixelToCoords(e)))
                    {
                        SoundEnabled = !SoundEnabled;
                        sound.Text   = SoundEnabled ? "$" : "£";
                        if (SoundEnabled)
                        {
                            mainMusic.Play();
                        }
                        else
                        {
                            mainMusic.Pause();
                        }
                    }
                }
            }

            App.KeyPressed         += keyPressed;
            App.MouseButtonPressed += mouseClick;

            mainMusic.Play();
            clock.Restart();
            while (App.IsOpen)
            {
                App.DispatchEvents();

                var delta = clock.Restart();

                if (titleTime)
                {
                    if (title.Position.Y < App.Size.Y / 2)
                    {
                        title.Position += new Vector2f(0, delta.AsSeconds() * 450);
                    }
                    if (bigger)
                    {
                        startMessage.Scale *= 1 + .2f * delta.AsSeconds();
                        if (startMessage.Scale.X > 1.2f)
                        {
                            bigger = false;
                        }
                    }
                    else
                    {
                        startMessage.Scale /= 1 + .2f * delta.AsSeconds();
                        if (startMessage.Scale.X < 1)
                        {
                            bigger = true;
                        }
                    }
                    title.Scale = startMessage.Scale * .9f;
                }
                else
                {
                    if (title.Position.Y > 100)
                    {
                        title.Position -= new Vector2f(0, delta.AsSeconds() * 450);
                    }
                    if (bigger)
                    {
                        title.Scale *= 1 + .1f * delta.AsSeconds();
                        if (title.Scale.X > .7f)
                        {
                            bigger = false;
                        }
                    }
                    else
                    {
                        if (title.Scale.X > .7f)
                        {
                            title.Scale /= 1 + .8f * delta.AsSeconds();
                        }
                        title.Scale /= 1 + .1f * delta.AsSeconds();
                        if (title.Scale.X < .6f)
                        {
                            bigger = true;
                        }
                    }
                }

                if (darker)
                {
                    bgGreen /= 1 + .2f * delta.AsSeconds();
                    if (bgGreen < .3f)
                    {
                        darker = false;
                    }
                }
                else
                {
                    bgGreen *= 1 + .2f * delta.AsSeconds();
                    if (bgGreen > .65f)
                    {
                        darker = true;
                    }
                }
                rotation      += delta.AsSeconds() * 60;
                spark.Rotation = rotation;
                spark.Scale    = selection.Scale;

                if (play.GlobalBounds.Contains(App.MapPixelToCoords(Mouse.GetPosition(App))))
                {
                    selection = play;
                }
                if (howTo.GlobalBounds.Contains(App.MapPixelToCoords(Mouse.GetPosition(App))))
                {
                    selection = howTo;
                }
                if (quit.GlobalBounds.Contains(App.MapPixelToCoords(Mouse.GetPosition(App))))
                {
                    selection = quit;
                }
                if (sound.GlobalBounds.Contains(App.MapPixelToCoords(Mouse.GetPosition(App))))
                {
                    selection = sound;
                }
                {
                    play.Scale       = new Vector2f(1, 1);
                    howTo.Scale      = new Vector2f(1, 1);
                    quit.Scale       = new Vector2f(1, 1);
                    sound.Scale      = new Vector2f(1, 1);
                    play.Color       = Color.White;
                    howTo.Color      = Color.White;
                    quit.Color       = Color.White;
                    selection.Scale *= 1.3f;
                    cursor.Position  = new Vector2f(selection.GlobalBounds.Left - 10, selection.Position.Y);
                    if (selection == play)
                    {
                        cursor.Color = Utilities.Green;
                    }
                    else if (selection == howTo)
                    {
                        cursor.Color = Utilities.Blue;
                    }
                    else if (selection == quit)
                    {
                        cursor.Color = Utilities.Red;
                    }
                    else if (selection == sound)
                    {
                        cursor.Color = SoundEnabled ? Utilities.Blue : Utilities.Red;
                    }
                    selection.Color = cursor.Color;
                    sound.Color     = Color.White;
                }

                App.Clear(new Color(255, (byte)(bgGreen * 255), 0));

                if (titleTime)
                {
                    App.Draw(spark, new RenderStates(shrinker));
                    App.Draw(startMessage);
                }
                else
                {
                    App.Draw(play);
                    App.Draw(howTo);
                    App.Draw(quit);
                    App.Draw(cursor);
                    App.Draw(sound);
                }
                App.Draw(title);

                App.Display();
            }
        }