示例#1
0
        public override void Load()
        {
            #region Chargement de la musique
            sndMusic = AssetManager.mscVictory;
            MediaPlayer.Play(sndMusic);
            MediaPlayer.IsRepeating = true;
            #endregion

            #region Image de background
            _backgroundColor = Color.Transparent;
            _background      = AssetManager.Victory;
            #endregion

            #region Création des éléments la GUI
            SpriteFont font         = AssetManager.MenuFont;
            int        screenWidth  = MainGame.Screen.Width;
            int        screenHeight = MainGame.Screen.Height;

            _groupMenu = new GroupSelection();

            string text = "MENU";
            _menu = new Textbox(new Vector2(-200, (screenHeight - font.MeasureString(text).Y) / 2 - 40), font, text);
            _groupMenu.AddElement((IIntegrableMenu)_menu);

            text  = "QUITTER";
            _exit = new Textbox(new Vector2(-2200, (screenHeight - font.MeasureString(text).Y) / 2 + 40), font, text);
            _groupMenu.AddElement((IIntegrableMenu)_exit);

            text    = "L'" + MainGame.Winner + " gagne !";
            _winner = new Textbox(new Vector2(-3000, 50), font, text);
            _winner.ApplyColor(Color.Green, Color.Black);

            _groupMenu.RefreshColors();
            _groupMenu.CurrentSelection = 0;

            foreach (Textbox txt in _groupMenu.Elements)
            {
                txt.OnHover += Textbox_OnHover;
                txt.OnClick += Textbox_OnClick;
            }
            #endregion

            #region Création du tweening
            _tweeningMenu   = new Tweening(Tweening.Tween.InSine, (int)_menu.Position.X, (int)(screenWidth - _menu.Size.X) / 2, new TimeSpan(0, 0, 0, TIMER_INTRO));
            _tweeningExit   = new Tweening(Tweening.Tween.InSine, (int)_exit.Position.X, (int)(screenWidth - _exit.Size.X) / 2, new TimeSpan(0, 0, 0, TIMER_INTRO));
            _tweeningWinner = new Tweening(Tweening.Tween.InSine, (int)_winner.Position.X, (int)(screenWidth - _winner.Size.X) / 2, new TimeSpan(0, 0, 0, TIMER_INTRO));
            #endregion

            base.Load();
        }
示例#2
0
        public override void Load()
        {
            #region Chargement de la musique
            sndMusic = AssetManager.mscMenu;
            MediaPlayer.Play(sndMusic);
            MediaPlayer.IsRepeating = true;
            #endregion

            #region Image de background
            _backgroundColor = Color.Transparent;
            _background      = AssetManager.Menu;
            #endregion

            #region Création des éléments la GUI
            SpriteFont font         = AssetManager.MenuFont;
            int        screenWidth  = MainGame.Screen.Width;
            int        screenHeight = MainGame.Screen.Height;

            #region Menu
            _groupMenu = new GroupSelection();

            string text = "JOUER";
            _play = new Textbox(new Vector2(-200, (screenHeight - font.MeasureString(text).Y) / 2 - 80), font, text);
            _groupMenu.AddElement((IIntegrableMenu)_play);

            text       = "COMMENT JOUER";
            _howToPlay = new Textbox(new Vector2(-1200, (screenHeight - font.MeasureString(text).Y) / 2), font, text);
            _groupMenu.AddElement((IIntegrableMenu)_howToPlay);

            text  = "QUITTER";
            _exit = new Textbox(new Vector2(-2200, (screenHeight - font.MeasureString(text).Y) / 2 + 80), font, text);
            _groupMenu.AddElement((IIntegrableMenu)_exit);

            text          = "ENTRAINER L'IA";
            _trainingMode = new Textbox(new Vector2(-3200, (screenHeight - font.MeasureString(text).Y) / 2 + 160), font, text);
            _groupMenu.AddElement((IIntegrableMenu)_trainingMode);

            _groupMenu.RefreshColors();
            _groupMenu.CurrentSelection = 0;

            foreach (Textbox txt in _groupMenu.Elements)
            {
                txt.OnHover += MenuTextbox_OnHover;
                txt.OnClick += MenuTextbox_OnClick;
            }
            #endregion

            #region Nombre d'équipes
            _numberOfTeamDescription = new Textbox(new Vector2(-1000, 10), font, "Nombre d'équipes : ");

            _groupNbOfTeam = new GroupSelection();
            _numberOfTeam  = new Textbox[3];
            for (int i = 0; i < _numberOfTeam.Length; i++)
            {
                _numberOfTeam[i] = new Textbox(new Vector2(-1610 + 80 * i, 10), font, (i + 2).ToString());
                _groupNbOfTeam.AddElement((IIntegrableMenu)_numberOfTeam[i]);

                _numberOfTeam[i].OnClick += NbOfTeamTextbox_OnClick;
            }
            _groupNbOfTeam.CurrentSelection = 0;
            SelectNbOfTeam();
            #endregion

            #region Nombre de tanks
            _numberOfTankDescription = new Textbox(new Vector2(-1000, 80), font, "Nombre de tanks : ");

            _groupNbOfTank = new GroupSelection();
            _numberOfTank  = new Textbox[5];
            for (int i = 0; i < _numberOfTank.Length; i++)
            {
                _numberOfTank[i] = new Textbox(new Vector2(-1610 + 80 * i, 80), font, (i + 1).ToString());
                _groupNbOfTank.AddElement((IIntegrableMenu)_numberOfTank[i]);

                _numberOfTank[i].OnClick += NbOfTankTextbox_OnClick;
            }
            _groupNbOfTank.CurrentSelection = 0;
            SelectNbOfTank();
            #endregion

            #endregion

            #region Création du tweening
            _tweeningPlay         = new Tweening(Tweening.Tween.InSine, (int)_play.Position.X, (int)(screenWidth - _play.Size.X) / 2, new TimeSpan(0, 0, 0, TIMER_INTRO));
            _tweeninghowToPlay    = new Tweening(Tweening.Tween.InSine, (int)_howToPlay.Position.X, (int)(screenWidth - _howToPlay.Size.X) / 2, new TimeSpan(0, 0, 0, TIMER_INTRO));
            _tweeningExit         = new Tweening(Tweening.Tween.InSine, (int)_exit.Position.X, (int)(screenWidth - _exit.Size.X) / 2, new TimeSpan(0, 0, 0, TIMER_INTRO));
            _tweeningTrainingMode = new Tweening(Tweening.Tween.InSine, (int)_trainingMode.Position.X, (int)(screenWidth - _trainingMode.Size.X) / 2, new TimeSpan(0, 0, 0, TIMER_INTRO));

            _tweeningNbOfTeamDescription = new Tweening(Tweening.Tween.InSine, (int)_numberOfTeamDescription.Position.X, 10, new TimeSpan(0, 0, 0, TIMER_INTRO));
            _tweeningNbOfTankDescription = new Tweening(Tweening.Tween.InSine, (int)_numberOfTankDescription.Position.X, 10, new TimeSpan(0, 0, 0, TIMER_INTRO));

            _tweeningNbOfTeam = new Tweening[_numberOfTeam.Length];
            for (int i = 0; i < _tweeningNbOfTeam.Length; i++)
            {
                _tweeningNbOfTeam[i] = new Tweening(Tweening.Tween.InSine, (int)_numberOfTeam[i].Position.X, 610 + 80 * i, new TimeSpan(0, 0, 0, TIMER_INTRO));
            }

            _tweeningNbOfTank = new Tweening[_numberOfTank.Length];
            for (int i = 0; i < _tweeningNbOfTank.Length; i++)
            {
                _tweeningNbOfTank[i] = new Tweening(Tweening.Tween.InSine, (int)_numberOfTank[i].Position.X, 610 + 80 * i, new TimeSpan(0, 0, 0, TIMER_INTRO));
            }
            #endregion

            base.Load();
        }
示例#3
0
        public override void Load()
        {
            #region Initialisation de la population (en cas de mode entrainement IA)
            IATrainingMode = MainGame.IATrainingMode;
            if (IATrainingMode)
            {
                if (File.Exists("Population.json"))
                {
                    Population = Population.OpenFromFile("Population");
                }
                else
                {
                    Population = new Population();
                    Population.MutationRate = 0.05f;
                }
                Population.OnGenomesDeletion += Population_OnGenomesDeletion;
                Population.OnGenomesChanged  += Population_OnGenomesChanged;
            }
            #endregion

            #region Démarrage des musiques
            sndMusic = AssetManager.mscGameplay;
            MediaPlayer.Play(sndMusic);
            MediaPlayer.Volume      = 0.5f;
            MediaPlayer.IsRepeating = true;
            #endregion

            #region Ajout des bruitages
            _sndexplosion = AssetManager.sndExplosion;
            #endregion

            #region Création du ciel
            _skyTexture = new Texture2D(MainGame.spriteBatch.GraphicsDevice, (int)MapSize.X, _skyHeight);
            Color[] skyData = new Color[_skyTexture.Width * _skyTexture.Height];
            int     skyH    = _skyTexture.Height;
            int     skyW    = _skyTexture.Width;
            for (int y = 0; y < skyH; y++)
            {
                Color skyColor = Color.Lerp(Color.Black, new Color(119, 181, 254), (float)y / skyH);
                for (int x = 0; x < skyW; x++)
                {
                    skyData[x + y * skyW] = skyColor;
                }
            }
            _skyTexture.SetData(skyData);
            #endregion

            #region Création de la map et relevé des points d'eau pour le réseau de neurone
            WaterPosition = new List <Rectangle>();
            bool beginWater         = false;
            int  beginWaterPosition = 0;
            _perlinNoise = PerlinNoise.Generate1DMap((int)MapSize.X, 550f);
            _mapTexture  = new Texture2D(MainGame.spriteBatch.GraphicsDevice, (int)MapSize.X, (int)MapSize.Y);
            MapData      = new byte[(int)(MapSize.X * MapSize.Y)];
            MapColors    = new Color[MapData.Length];

            Color[] colors = new Color[5];
            colors[0] = Color.White;
            colors[1] = Color.Green;
            colors[2] = Color.DarkGreen;
            colors[4] = new Color(132, 73, 44);
            colors[3] = new Color(62, 28, 0);

            for (ushort i = 0; i < _perlinNoise.Length; i++)
            {
                float  noiseVal = _perlinNoise[i];
                ushort x        = (ushort)(i % MapSize.X);
                ushort h        = (ushort)Math.Floor((noiseVal + 1) * MapSize.Y * 0.5f);
                h = (ushort)MathHelper.Clamp(h, 0, WaterLevel + 1);
                int min = (int)Math.Floor((_perlinNoise.Min() + 1) * MapSize.Y * 0.5f);
                int dif = (int)MapSize.Y - min;
                for (ushort y = h; y < MapSize.Y; y++)
                {
                    uint index = (uint)(x + y * MapSize.X);
                    if (y <= WaterLevel)
                    {
                        float pourcent = (float)(y - min) / dif;
                        float p1       = 0.25f;
                        float p2       = 0.5f;
                        float p3       = 0.75f;
                        float p4       = 1f;
                        if (pourcent < p1)
                        {
                            MapColors[index] = Color.Lerp(colors[0], colors[1], pourcent / p1);
                        }
                        else if (pourcent < p2)
                        {
                            MapColors[index] = Color.Lerp(colors[1], colors[2], (pourcent - p1) / (p2 - p1));
                        }
                        else if (pourcent < p3)
                        {
                            MapColors[index] = Color.Lerp(colors[2], colors[3], (pourcent - p2) / (p3 - p2));
                        }
                        else if (pourcent < p4)
                        {
                            MapColors[index] = Color.Lerp(colors[3], colors[4], (pourcent - p3) / (p4 - p3));
                        }
                        MapData[index] = 1;
                    }
                    else
                    {
                        MapColors[index] = Color.Brown;
                    }
                }
                if (!IsSolid(new Vector2(x, WaterLevel - 1)))
                {
                    if (!beginWater)
                    {
                        beginWater         = true;
                        beginWaterPosition = x;
                    }
                }
                else
                {
                    if (beginWater)
                    {
                        beginWater = false;
                        WaterPosition.Add(new Rectangle(beginWaterPosition, WaterLevel, x - beginWaterPosition, (int)MapSize.Y - WaterLevel));
                    }
                }
            }
            _mapTexture.SetData(MapColors);
            #endregion

            #region Création de l'eau
            Vector2 p = new Vector2(0, WaterLevel);
            WaterHeight = (int)(MapSize - p).Y;
            Vector2 s = new Vector2(MapSize.X, WaterHeight);
            _water = new Water(this, p, s);
            #endregion

            #region Paramétrage de la Caméra
            Camera c = MainGame.Camera;
            c.CameraSize   = new Vector3(MapSize.X, MapSize.Y, 0);
            c.CameraOffset = new Vector3(0, MapSize.Y - MainGame.Screen.Height, 0);
            c.Enable       = true;
            c.Speed        = 10;
            #endregion

            #region Création d'un timer de tours
            _timerSecond           = new Timer(1000);
            _timerSecond.Elapsed  += OnTimerElapsed;
            _timerSecond.AutoReset = true;
            _timerSecond.Enabled   = true;
            _counter = TIME_BETWEEN_TOUR;
            #endregion

            #region Création des éléments de GUI
            GUIGroup = new Group();
            Texture2D texture = AssetManager.GameBottomBar;
            _gameBarImage        = new Image(texture, new Vector2(MainGame.Screen.Width / 2, MainGame.Screen.Height - texture.Height / 2));
            _gameBarImage.Layer -= 0.1f;
            _gameBarImage.SetOriginToCenter();
            GUIGroup.AddElement(_gameBarImage);

            texture      = AssetManager.Cursor;
            _cursorImage = new Image(texture, new Vector2(_gameBarImage.Position.X - _gameBarImage.Origin.X + (_gameBarImage.Position.X / MapSize.X), _gameBarImage.Position.Y - texture.Height * 0.75f));
            _cursorImage.SetOriginToCenter();
            GUIGroup.AddElement(_cursorImage);

            Viewport   screen = MainGame.Screen;
            SpriteFont font   = AssetManager.MenuFont;
            _bigTimerTextBox = new Textbox(new Vector2((screen.Width - font.MeasureString("3").X) / 2, (screen.Height - font.MeasureString("3").Y) / 2), font, "3");
            _bigTimerTextBox.ApplyColor(Color.White, Color.Black);
            _bigTimerTextBox.Visible = false;
            _bigTimerTextBox.Layer  += 0.2f;
            GUIGroup.AddElement(_bigTimerTextBox);

            font          = AssetManager.MainFont;
            _timerTextBox = new Textbox(new Vector2(382, 725), font, TIME_BETWEEN_TOUR.ToString() + "sec");
            _timerTextBox.ApplyColor(Color.Red, Color.Black);
            GUIGroup.AddElement(_timerTextBox);

            _currentTeamTextBox = new Textbox(new Vector2(25, 725), font, "Equipe des rouges");
            GUIGroup.AddElement(_currentTeamTextBox);

            _currentTankTextBox = new Textbox(new Vector2(196, 725), font, ".");
            _currentTankTextBox.ApplyColor(Color.Yellow, Color.Black);
            GUIGroup.AddElement(_currentTankTextBox);

            _infoBulle = new Textbox(new Vector2(925, 725), font, "InfoBulle :");
            _infoBulle.ApplyColor(Color.Yellow, Color.Black);
            GUIGroup.AddElement(_infoBulle);

            if (IATrainingMode)
            {
                _fittingScoreTextBox = new Textbox(Vector2.One, font, "Fitness Score : 0 Generation : " + Population.Generation);
                _fittingScoreTextBox.ApplyColor(Color.Yellow, Color.Black);
                GUIGroup.AddElement(_fittingScoreTextBox);
            }

            GUIGroupButtons = new GroupSelection();
            for (int i = 0; i < Enum.GetValues(typeof(Action.eActions)).Length; i++)
            {
                ButtonAction btn;
                if (i == 0)
                {
                    btn = new ButtonAction(this, (Action.eActions)i, Vector2.Zero, Vector2.Zero, AssetManager.MainFont, string.Empty);
                }
                else
                {
                    btn = new ButtonAction(this, (Action.eActions)i, new Vector2(442 + 37 * (i - 1), 725), Vector2.Zero, AssetManager.MainFont, string.Empty);
                }
                GUIGroupButtons.AddElement((IIntegrableMenu)btn);
                btn.OnHover += OnButtonHover;
                btn.OnClick += OnButtonClicked;
            }
            c.OnPositionChange += OnCameraPositionChange;
            #endregion

            #region Remplissage du sac à loot
            _lootBag = new List <Action.eActions>();
            FillLootBag();
            #endregion

            #region Création des équipes
            Teams = new List <Team>();
            Texture2D img = AssetManager.TanksSpriteSheet;
            Team      t;
            int       numberOfTeam        = MainGame.NumberOfTeam;
            int       numberOfTankPerTeam = MainGame.NumberOfTank;
            Random    rnd = new Random();

            for (byte i = 0; i < numberOfTeam; i++)
            {
                eControlType controlType;
                if (IATrainingMode)
                {
                    controlType = eControlType.NeuralNetwork;
                }
                else
                {
                    //if (i < 1)
                    controlType = eControlType.Player;     // MainGame.ControlTypes[i];
                    //else
                    //controlType = eControlType.NeuralNetwork;
                }
                t = new Team(this, img, numberOfTankPerTeam, i, rnd, controlType);
                Teams.Add(t);
                if (IATrainingMode)
                {
                    NeuralNetworkControl nn = (NeuralNetworkControl)t.Control;
                    if (Population.Genomes.Count > i)
                    {
                        nn.Genome = Population.Genomes[i % Population.Genomes.Count];
                        nn.Genome.OnFitnessScoreChange += Genome_OnFitnessScoreChange;
                    }
                    else
                    {
                        Population.Genomes.Add(nn.Genome);
                        nn.Genome.OnFitnessScoreChange += Genome_OnFitnessScoreChange;
                    }
                }
                t.OnTankSelectionChange += OnTankSelectionChange;
            }
            t = Teams[IndexTeam];
            t.RefreshCameraOnSelection();
            _currentTeamTextBox.ApplyColor(t.TeamColor, Color.Black);
            _currentTankTextBox.Text = t.Tanks[t.IndexTank].Name;
            RefreshActionButtonInventory();
            #endregion

            base.Load();
        }