📄 ship.cs
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
namespace SpaceWar2D
{
class Ship : PhysicalObject, IGameStateChangeSink
{
/// <summary>
/// The maximum allowed speed
/// </summary>
private const int maxSpeed = 40000;
/// <summary>
/// The initial speed of the missile, relative to the ship.
/// In other words, the "firing speed" of the missile.
/// </summary>
private const int missileRelativeSpeed = 11000;
/// <summary>
/// The acceleration the thrusters provide.
/// </summary>
private const int thrustAcceleration = 10;
/// <summary>
/// The size of the ship
/// </summary>
private const int shipRadius = Game.MaxPosition / 20;
/// <summary>
/// The maximum number of missiles that can be launched at one time.
/// </summary>
private const int maxMissiles = 5;
/// <summary>
/// How long the ship stays damaged.
/// </summary>
private const int damageLifetime = 2000;
/// <summary>
/// The ship's missiles, both launched and unlaunched.
/// </summary>
private Missile[] missiles;
/// <summary>
/// The other ship.
/// </summary>
private Ship otherShip;
/// <summary>
/// The sink by which the player's ship notifies the enemy ship of changes.
/// Will be null if this is an enemy ship.
/// </summary>
private IGameStateChangeSink sink;
/// <summary>
/// Is the ship damaged?
/// </summary>
private bool damaged = false;
/// <summary>
/// Is the ship firing its thrusters?
/// </summary>
private bool thrusting = false;
/// <summary>
/// The direction the ship is pointing.
/// </summary>
private int rotation;
/// <summary>
/// When the damage will be fixed.
/// </summary>
private int damageTimeout;
/// <summary>
/// The number of times the ship has been damaged.
/// In other words, the score.
/// </summary>
private int damageCount;
/// <summary>
/// Information about how to draw.
/// </summary>
private DrawingInfo draw;
public Ship(Vector vPos) :
base(vPos, Vector.Null, shipRadius)
{
missiles = new Missile[maxMissiles];
for (int i = 0; i < maxMissiles; i++)
{
missiles[i] = new Missile();
}
}
/// <summary>
/// The number of times the ship has been damaged.
/// In other words, the score.
/// </summary>
public int DamageCount
{
get { return damageCount; }
}
/// <summary>
/// Information about how to draw.
/// </summary>
public DrawingInfo Draw
{
get { return draw; }
set { draw = value; }
}
/// <summary>
/// The other ship.
/// </summary>
public Ship OtherShip
{
get { return otherShip; }
set { otherShip = value; }
}
/// <summary>
/// The sink by which the friendly ship notifies the enemy of its state.
/// </summary>
public IGameStateChangeSink Sink
{
get { return sink; }
set
{
sink = value;
if (sink != null)
{
// This means a connection was just established with the
// other ship.
// Tell the sink our rotation because otherwise it won't know
// until the next time the ship rotates.
sink.OnShipRotate(rotation);
// Reset the score.
damageCount = 0;
otherShip.damageCount = 0;
}
}
}
/// <summary>
/// The ship's missiles
/// </summary>
public Missile[] Missiles
{
get { return missiles; }
}
/// <summary>
/// Gets or sets a value indicating whether the ship is firing its thrusters.
/// </summary>
public bool Thrusting
{
get { return thrusting; }
set
{
if (!Damaged)
{
thrusting = value;
if (thrusting)
{
Acceleration = new Vector((int)(thrustAcceleration * Cos(Rotation)),
(int)(thrustAcceleration * Sin(Rotation)));
}
else
{
Acceleration = Vector.Null;
}
if (sink != null)
{
sink.OnShipThrust(thrusting);
}
}
}
}
/// <summary>
/// Gets or sets a value indicating whether the ship is damaged.
/// </summary>
public bool Damaged
{
get { return damaged; }
set
{
if (damaged == value)
{
// No change
return;
}
if (value)
{
// Wasn't damaged before, but is now, so start timer.
damageTimeout = Environment.TickCount + damageLifetime;
// Disable thrusters.
thrusting = false;
// Keep score.
damageCount++;
}
damaged = value;
if (sink != null)
{
sink.OnShipDamage(damaged);
}
}
}
/// <summary>
/// Update the ship's state.
/// </summary>
/// <param name="ticks">The amount of time that has passed since the last update.</param>
public new void Update(int ticks)
{
// Let PhysicalObject update our position.
base.Update(ticks);
// Enforce a speed limit
Vector v = Velocity;
if (v.Magnitude > maxSpeed)
{
v.SetMagnitude(maxSpeed);
Velocity = v;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -