public void ShootLaser(GameTime gameTime, Vector3 shipPosition, Quaternion shipRotation) { double currentTime = gameTime.TotalGameTime.TotalMilliseconds; if (currentTime - _lastBulletTime > DELAY_BETWEEN_LASERS) { LaserStruct newBullet = new LaserStruct(); newBullet.position = shipPosition; newBullet.rotation = shipRotation; _bulletList.Add(newBullet); _lastBulletTime = currentTime; shot.Play(); } }
private void shootLaser() { // create the laser object if (ship.canShoot && ship.lasers.Count < LASER_MAX) { LaserStruct tmp = new LaserStruct(); tmp.x = ship.x + 4 / 3 * ship.r * (float)Math.Cos(ship.a); tmp.y = ship.y - 4 / 3 * ship.r * (float)Math.Sin(ship.a); tmp.xv = LASER_SPD * (float)Math.Cos(ship.a) / FPS; tmp.yv = -LASER_SPD * (float)Math.Sin(ship.a) / FPS; tmp.dist = 0f; tmp.explodeTime = 0; ship.lasers.Add(tmp); } // prevent further shooting ship.canShoot = false; }
public CollisionType UpdateLaserAndCheckCollision(float moveSpeed, Vector3 spaceshipPosition, int spaceshipHealth) { for (int i = 0; i < _bulletList.Count; i++) { LaserStruct currentBullet = _bulletList[i]; MoveForward(ref currentBullet.position, currentBullet.rotation, moveSpeed * LASER_SPEED); _bulletList[i] = currentBullet; BoundingSphere bulletSphere = new BoundingSphere(currentBullet.position, 0.05f); CollisionType colType = CheckCollision(bulletSphere, spaceshipPosition); if (colType != CollisionType.None) { _bulletList.RemoveAt(i); i--; return(CollisionType.Laser); } } return(CollisionType.None); }
private void UpdateScene() { bool blinkOn = ship.blinkNum % 2 == 0; bool exploding = ship.explodeTime > 0; // gfx.DrawImage(Bitmap, new Rectangle(x, y, w, h)); gfx.Clear(Color.Transparent); // Paint Asteroids DrawTheAsteroids(); // thrust the ship if (ship.thrusting && !ship.dead) { ship.thrustX += SHIP_THRUST * (float)Math.Cos(ship.a) / FPS; ship.thrustY -= SHIP_THRUST * (float)Math.Sin(ship.a) / FPS; } else { // apply friction (slow the ship down when not thrusting) ship.thrustX -= FRICTION * ship.thrustX / FPS; if (Math.Abs(ship.thrustX) < 0.01f) { ship.thrustX = 0; } ship.thrustY -= FRICTION * ship.thrustY / FPS; if (Math.Abs(ship.thrustY) < 0.01f) { ship.thrustY = 0; } } // paint the ship if (!exploding) { if (blinkOn && !ship.dead) { //drawShip(ship.x, ship.y, ship.a); //PaintShip(); gfx.DrawImage(RotateImage(shipBMP, ship.a / (float)Math.PI * 180 * -1 + 90), ship.x - ship.r, ship.y - ship.r, ship.r * 2, ship.r * 2); } // handle blinking if (ship.blinkNum > 0) { // reduce the blink time ship.blinkTime--; // reduce the blink num if (ship.blinkTime == 0) { ship.blinkTime = (int)Math.Ceiling(SHIP_BLINK_DUR * FPS); ship.blinkNum--; } } } else { // draw the explosion gfx.DrawImage(res.BOOM, ship.x - ship.r, ship.y - ship.r, ship.r * 2, ship.r * 2); } // draw the lasers for (var i = 0; i < ship.lasers.Count; i++) { if (ship.lasers[i].explodeTime == 0) { gfx.DrawArc(new Pen(Color.White, 1), ship.lasers[i].x, ship.lasers[i].y, SHIP_SIZE / 15f, SHIP_SIZE / 15f, 0, 360); } else { gfx.DrawArc(new Pen(Color.OrangeRed, 7), ship.lasers[i].x, ship.lasers[i].y, ship.r * 0.5f, ship.r * 0.5f, 0, 360); } } // detect laser hits on asteroids float ax, ay, ar, lx, ly; for (int i = roids.Count - 1; i >= 0; i--) { // loop over the lasers for (int j = ship.lasers.Count - 1; j >= 0; j--) { // detect hits if (ship.lasers[j].explodeTime == 0 && distBetweenPoints(roids[i].x, roids[i].y, ship.lasers[j].x, ship.lasers[j].y) < roids[i].r) { // destroy the asteroid and activate the laser explosion destroyAsteroid(i); LaserStruct tmp = ship.lasers[j]; tmp.explodeTime = (int)Math.Ceiling(LASER_EXPLODE_DUR * FPS); ship.lasers[j] = tmp; break; } } } // handle edge of screen HandleEdgeOfScreen(); // move the lasers for (var i = ship.lasers.Count - 1; i >= 0; i--) { LaserStruct tmp = ship.lasers[i]; // check distance travelled if (tmp.dist > LASER_DIST * SIZE_X) { //ship.lasers.splice(i, 1); ship.lasers.RemoveAt(i); continue; } // handle the explosion if (ship.lasers[i].explodeTime > 0) { tmp.explodeTime--; // destroy the laser after the duration is up if (tmp.explodeTime == 0) { //ship.lasers.splice(i, 1); ship.lasers.RemoveAt(i); continue; } } else { // move the laser tmp.x += tmp.xv; tmp.y += tmp.yv; // calculate the distance travelled tmp.dist += (float)Math.Sqrt(Math.Pow(tmp.xv, 2) + Math.Pow(tmp.yv, 2)); } // handle edge of screen if (tmp.x < 0) { tmp.x = SIZE_X; } else if (tmp.x > SIZE_X) { tmp.x = 0; } if (tmp.y < 0) { tmp.y = SIZE_Y; } else if (tmp.y > SIZE_Y) { tmp.y = 0; } ship.lasers[i] = tmp; } // Move asteroids for (var i = 0; i < roids.Count; i++) { AsteroidStruct tmp = roids[i]; tmp.x += roids[i].xv; tmp.y += roids[i].yv; // handle asteroid edge of screen if (roids[i].x < 0 - roids[i].r) { tmp.x = SIZE_X + roids[i].r; } else if (roids[i].x > SIZE_X + roids[i].r) { tmp.x = 0 - roids[i].r; } if (roids[i].y < 0 - roids[i].r) { tmp.y = SIZE_Y + roids[i].r; } else if (roids[i].y > SIZE_Y + roids[i].r) { tmp.y = 0 - roids[i].r; } roids[i] = tmp; } // show ship collision // gfx.DrawArc(new Pen(Color.Red, 1), ship.x - (SHIP_SIZE / 2), ship.y - (SHIP_SIZE / 2), SHIP_SIZE, SHIP_SIZE, 0, 360); PaintString("Кораблі: " + lives, 10, 10); PaintString("Рівень: " + (level + 1), 10, 50); //if (DateTime.Now.Second == 0 && SoundFlag) { PlaySoundFromRes(); SoundFlag = false; } //if (DateTime.Now.Second == 1) SoundFlag = true; if (!exploding) { // only check when not blinking if (ship.blinkNum == 0 && !ship.dead) { for (var i = 0; i < roids.Count; i++) { if (distBetweenPoints(ship.x, ship.y, roids[i].x, roids[i].y) < ship.r + roids[i].r) { explodeShip(); destroyAsteroid(i); break; } } } // paint thrusting if (ship.thrusting && !ship.dead) { PaintShipThrust(); } // rotate the ship ship.a += ship.rot; // move the ship ship.x += ship.thrustX; ship.y += ship.thrustY; } else { ship.explodeTime--; // reset the ship after the explosion has finished if (ship.explodeTime == 0) { lives--; if (lives == 0) { gameOver(); } else { ship = NewShip(); } } } PaintString("Рахунок: " + score, 10, 30); if (ship.dead) { PaintString(Color.Yellow, "F2 нова гра\n" + " F10 вихід"); } pic.Image = finalImage; //if (DateTime.Now.Second % 2 == 0) GC.Collect(); }