0

As a personnal work, i have to do a forest fire simulation in WPF.

I created all the map with a grid, and using an array. The Fire is added through a random number ( in the array when ex : case = " Forest "). The Fire is burning all the " forest case " and it depends on the wind ( N S E W, etc. ). Everything is working fine !

But i encounter a minor problem through the "fire loop". I mean, when i click on my button ' fire progress " ( which does the fire expansion ) all the forest case burns in a simultaneous way. ( the direction is correct, only the case with the right direction burns, the problem is not here ).

I wanted to cooldown this loop as each case after case will be burned, such as :

Case forest burns. Wait. Case forest 2 burns. And so on. And not all the cases in the right directions ( of the wind ).

I tried to use a dispatcher timer (TimeSpan ). But with no success. The only thing's working is the results of the loop is slowdown ( in seconds, milliseconds, days, whatever ). BUT all the cases changes in a same time. And not one by one.

Apologies if my english is not grammatically correct !

Here is my code :

private void DeclencherLeFeu()
        {
            TempsDuJeu = new DispatcherTimer();
            TempsDuJeu.Interval = TimeSpan.FromSeconds(1);
            TempsDuJeu.Tick += new EventHandler(OnTimerGameEvent);
            TempsDuJeu.Start();

            TempsQueMetLeFeuPourBrulerLaForet = new DispatcherTimer();
            TempsQueMetLeFeuPourBrulerLaForet.Interval = TimeSpan.FromSeconds(8);
            TempsQueMetLeFeuPourBrulerLaForet.Tick += new EventHandler(LesArbresSontCalcines);
            TempsQueMetLeFeuPourBrulerLaForet.Start();
        }

Note : LesArbresSontCalcines means in english that the trees are burned. It calls a function just as below :

private void LesArbresSontCalcines(object sender, EventArgs e)
        {
            for (int i = 0; i < 7; i++)
            {
                for (int j = 1; j < 10; j++)
                {
                    if (MonTableau[i, j].Name == "Feu")
                    {
                        MonTableau[i, j].Source = new BitmapImage(new Uri(@"E:\tp999\wpfapplication13\wpfapplication13\ArbreMort1.gif", UriKind.RelativeOrAbsolute));
                        ImageBehavior.SetAnimatedSource(MonTableau[i, j], new BitmapImage(new Uri(@"E:\tp999\wpfapplication13\wpfapplication13\ArbreMort1.gif", UriKind.RelativeOrAbsolute)));

                        MonTableau[i, j].Name = "Noir";
                    }
                }
            }
        }

Note : All the burning cases ( i use a fire.gif on the forest case ) are replacing with the ArbreMort1.gif (represents a blacked tree ).

In this part of my code, all the burning cases just change, in a same time, as a dead tree. And Not one by one.

I tried to do that the first burning case will be the first be darked ( with the ArbreMort1.gif) and so on till the ending burning case ( that will be the last to be transformed ).

How should i use this dispatcher timer ?

Thank you for your replies ! :)

Karro
  • 111
  • 1
  • 9
  • I think for each timer tick of your `LesArbresSontCalcines`, you only want to change one or two "Feu". Once that is done you should exit. Then after another 8 seconds one or two more. Maybe via Math.Random you select how many. Currently your for loops just execute all at once. – user326608 Mar 12 '17 at 14:46

2 Answers2

0

You need to re-architect the handler. Create class members int[7] j and int i = 0;

Then inside handler index j array with i value to determine what j index is. Then increment j index then increment i mod 7. So now it will take 560 seconds to complete the sequence.

AQuirky
  • 4,691
  • 2
  • 32
  • 51
0

Thank you for your answers ! I will try to fix it ! :)

Have a nice day !

Karro
  • 111
  • 1
  • 9