public Laser(Ship S) { myShip = S; }
public void Update(GameTime gameTime) { //Update Laser Laser.Update(gameTime); //Get Target TargetPosition = new Vector2(99999, 99999); Ship oldS = targetedShip; Player oldP = targetedMothership; foreach (Player p in myLevel.Players) { if (p.myIndex == myPlayer.myIndex) continue; if (Vector2.Distance(Position, p.MotherShipVec) < Vector2.Distance(Position, TargetPosition) && p.MotherShipHP > 0) { TargetPosition = p.MotherShipVec; targetedMothership = p; targetedShip = null; } foreach (Ship s in p.Ships) { if (Vector2.Distance(Position, s.Position) < Vector2.Distance(Position, TargetPosition)) { TargetPosition = s.Position; targetedMothership = null; targetedShip = s; } } } if (TargetPosition.X == 99999 && TargetPosition.Y == 99999) { targetedShip = null; targetedMothership = null; TargetPosition = myPlayer.MotherShipVec; } //Laser Position is always updated, even if it's not being drawn if (targetedShip != null || targetedMothership != null) Laser.Target = TargetPosition; //Accelerate in its direction Vector2 AccDir = Vector2.Normalize(TargetPosition - Position) * AccelerationForce; //If in range, fire! if (Vector2.Distance(Position, TargetPosition) < Range) { AccDir *= 0.1f; if (FiringTimer.HasTimeElapsed(gameTime, FiringRate)) { if (targetedMothership != null) targetedMothership.MotherShipHP -= (int)(AttackPower * myPlayer.DamageModifier); if (targetedShip != null) targetedShip.HitPoints -= (int)(AttackPower * myPlayer.DamageModifier); for (int j = 0; j < OneSpace.Random.Next(2, 4); j++) { if (targetedMothership != null) { myLevel.Particles.Add(new Particle(myPlayer.Colour, targetedMothership.MotherShipVec, 1, gameTime)); Audio.PlayCue("laser"); } if (targetedShip != null) { myLevel.Particles.Add(new Particle(myPlayer.Colour, targetedShip.Position, 1, gameTime)); Audio.PlayCue("laser"); } } Laser.Reset(gameTime); FiringTimer.Reset(gameTime); } } //Move ship Velocity += AccDir; Position += Velocity; //Cap Velocity if (Vector2.Distance(Vector2.Zero, Velocity) > MaxVelocity) Velocity = Vector2.Normalize(Velocity) * MaxVelocity; //Keep on screen if (Position.X < -16) Position.X = -16; Position.Y = MathHelper.Clamp(Position.Y, -shipTex.Height / 2, OneSpace.theGame.GraphicsDevice.Viewport.Height + shipTex.Height / 2); if (HealthBar != null) { HealthBar.Position = new Vector2(Position.X, Position.Y + shipTex.Height / (Type == ShipType.Medium ? 3 : 2)); HealthBar.UpdateAngle(0); HealthBar.CurrentValue = HitPoints; } }