private static void SetMissileLocation(Point point, MissileForm mf) { //thread switch to message loop if required if (mf.InvokeRequired) { mf.Invoke(new SinglePointDelegate(InvokedSetMissileLocation), point, mf); } else { InvokedSetMissileLocation(point, mf); } }
private static void FireMissile(object objPoint) { //TEMP SO I CAN DEBUG //_mouseHook.Stop(); Point point = (Point)objPoint; //get random position across bottom of screen int x = _random.Next(0, _screenWidth); Point currentLocation = new Point(x, _screenBottom); MissileForm mf = (MissileForm)_dumbForm.Invoke(new CreateMissileFormDelegate(CreateMissileForm), currentLocation); //now we have to slowly move the Missile to the point clicked //start: currentLocation //end: point int distance = (int)Math.Sqrt(Math.Pow(point.X - currentLocation.X, 2) + Math.Pow(point.Y - currentLocation.Y, 2)); int frames = distance / 8; //pixels per frame float deltaX = (point.X - currentLocation.X) / (float)frames; float deltaY = (point.Y - currentLocation.Y) / (float)frames; //don't allow 0 delta. Instead do 1 or -1 depending on position if (deltaX == 0) { deltaX = point.X < currentLocation.X ? -1 : 1; } if (deltaY == 0) { deltaY = point.Y < currentLocation.Y ? -1 : 1; } //precision float _curX = currentLocation.X; float _curY = currentLocation.Y; //more the required number of frames for the specified delta over each direction while (frames-- >= 0) { //adjust for image center SetMissileLocation(new Point((int)_curX - 50, (int)_curY - 50), mf); //move _curX += deltaX; _curY += deltaY; //check step over bounds if (deltaX < 0) { //heading left if (_curX < point.X) { _curX = point.X; } } else { //heading right if (_curX > point.X) { _curX = point.X; } } if (deltaY < 0) { //heading up if (_curY < point.Y) { _curY = point.Y; } } else { //heading down if (_curY > point.Y) { _curY = point.Y; } } Thread.Sleep(20); } //ok, we're there. Now we need to explode mf.Invoke(new ExplodeDelegate(mf.Explode), 10); Thread.Sleep(200); mf.Invoke(new ExplodeDelegate(mf.Explode), 30); Thread.Sleep(200); mf.Invoke(new ExplodeDelegate(mf.Explode), 50); Thread.Sleep(200); mf.Invoke(new ExplodeDelegate(mf.Explode), 30); Thread.Sleep(200); mf.Invoke(new ExplodeDelegate(mf.Explode), 10); Thread.Sleep(300); //DIE FORM, DIE! _dumbForm.Invoke(new MethodInvoker(mf.Dispose)); }