コード例 #1
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();
        }