// Wątek wykonuje zadanie public void ThreadProc() { while (true) { Application.Current.Dispatcher.Invoke((Action)(() => { enemyShip.MoveTo(x, y); })); if (x < 10)//sprawdza czy statek doleciał do końca lini { endLine = true; } if (x > 700)//sprawdza czy statek doleciał do końca lini { endLine = true; } if (endLine == true && isMovingRigth == true)//jeśłi statek doleciał do końca lini sprawdza kierunek w którym leciał i zmienia go { isMovingRigth = false; endLine = false; } if (endLine == true && isMovingRigth == false) { isMovingRigth = true; endLine = false; } if (isMovingRigth == true)//ustalenie następnej pozycji statku { x += enemyShip.GetSpeed(); } if (isMovingRigth == false)//ustalenie następnej pozycji statku { x -= enemyShip.GetSpeed(); } Application.Current.Dispatcher.Invoke((Action)(() => { for (int i = 0; i < Globals.playerMissiles.Count(); i++) { //Sprawdzenie czy przeciwnik zostal trafiony rakietą przez gracza if (Globals.playerMissiles[i].y > y - 50 && Globals.playerMissiles[i].y <y + 50 && Globals.playerMissiles[i].x> x - 50 && Globals.playerMissiles[i].x < x + 50) // Wystąpienie kolizji { //Sprawdzenie ile zycia posiada przeciwnik if (enemyShip.GetLifes() > 0) { int gundmg = player.gundmg; enemyShip.RemoveLife(gundmg); // Usuniecie ilości życia równej obrażeniom gracza } else // Jezeli przeciwnik straci cale życie jest niszczony { enemyShip.GetImage().Source = null; Globals.points++; MainWindow.main.Dispatcher.Invoke(new Action(delegate() { MainWindow.main.ScorePoints = Globals.points.ToString(); })); // MainWindow.main.LifePoints = Globals.points.ToString(); dead = true; Globals.shipCounter--; } Globals.playerMissiles[i].dynamicImage.Source = null; Globals.playerMissiles.RemoveAt(i); if (dead == true) { break; } } } })); if (dead == true) { break; } Thread.Sleep(100); missileCounter++; if (missileCounter % 50 == 0) // Pociski enemyShip { Missile miss = new Missile(map, enemyShip.GetGunDmg()); FlyingMissile missile = new FlyingMissile(miss, this.x, this.y, player); Thread t = new Thread(new ThreadStart(missile.ThreadProc)); t.Start(); } } }
// The thread performs the task public void ThreadProc() { while (true) { Application.Current.Dispatcher.Invoke((Action)(() => { enemyShip.MoveTo(x, y); })); if (x < 10) { endLine = true; } if (x > 700) { endLine = true; } if (endLine == true && isMovingRigth == true) { isMovingRigth = false; endLine = false; } if (endLine == true && isMovingRigth == false) { isMovingRigth = true; endLine = false; } if (isMovingRigth == true) { x += enemyShip.GetSpeed(); } if (isMovingRigth == false) { x -= enemyShip.GetSpeed(); } Application.Current.Dispatcher.Invoke((Action)(() => { for (int i = 0; i < Globals.playerMissiles.Count(); i++) { //Check if enemyShip was hitted by player bullet if (Globals.playerMissiles[i].y > y - 50 && Globals.playerMissiles[i].y <y + 50 && Globals.playerMissiles[i].x> x - 50 && Globals.playerMissiles[i].x < x + 50) // Collision appears { //Check how many lifes enemyShip gotS if (enemyShip.GetLifes() > 0) // If has more than 0 lifes { int gundmg = player.gundmg; enemyShip.RemoveLife(gundmg); // Remove 1 life //Debug.WriteLine("EnemyShip lost lifes, now:" + enemyShip.GetLifes()); } else // If enemyShip has no life destroy him { //Debug.WriteLine("EnemyShip destroyed, " + enemyShip.GetLifes()); enemyShip.GetImage().Source = null; Globals.points++; MainWindow.main.Dispatcher.Invoke(new Action(delegate() { MainWindow.main.ScorePoints = Globals.points.ToString(); })); // MainWindow.main.LifePoints = Globals.points.ToString(); dead = true; Globals.shipCounter--; } Globals.playerMissiles[i].dynamicImage.Source = null; Globals.playerMissiles.RemoveAt(i); if (dead == true) { break; } } } })); if (dead == true) { break; } Thread.Sleep(100); missileCounter++; if (missileCounter % 50 == 0) // Pociski enemyShip { Missile miss = new Missile(map, enemyShip.GetGunDmg()); FlyingMissile missile = new FlyingMissile(miss, this.x, this.y, player); Thread t = new Thread(new ThreadStart(missile.ThreadProc)); t.Start(); } } }