📄 gameclass.cs
字号:
using System;
using System.Collections;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.DirectDraw;
using Microsoft.DirectX.DirectInput;
using Microsoft.DirectX.DirectPlay;
namespace SpaceWar {
/// <summary>
/// The main game functions
/// </summary>
public class GameClass {
#region Fields and Properties
private Control owner = null;
private MainClass mainClass = null;
private GameSettings gameSettings = null;
private int gravity;
private int gameSpeed;
private bool bounceBack;
private bool inverseGravity;
private bool blackHole;
public int Gravity { get { return gravity;} set { gravity = value;} }
public int GameSpeed { get { return gameSpeed; } set { gameSpeed = value; } }
public bool BounceBack { get { return bounceBack; } set { bounceBack = value; } }
public bool InverseGravity { get { return inverseGravity; } set { inverseGravity = value; } }
public bool BlackHole { get { return blackHole; } set { blackHole = value; } }
private Microsoft.DirectX.DirectDraw.Device localDevice = null;
private float lastFrameTime = 0.0f;
private Clipper localClipper = null;
private Surface surfacePrimary = null;
private Surface surfaceSecondary = null;
private Stars stars;
private Sun sun;
private Point sunLocation = new Point(0,0);
public Point SunLocation { get { return sunLocation; } }
private SoundHandler soundHandler;
private InputClass input;
private PlayClass netPeer;
private SplashScreen splash;
private Rectangle windowBounds;
private Hashtable otherPlayers = new Hashtable();
private Hashtable wordCache = new Hashtable();
public Color[] shipColors = new Color[] {Color.Purple, Color.Yellow, Color.Cyan,
Color.Red, Color.Green, Color.Blue};
private SpaceWar.Ship ship;
private LocalPlayer localPlayer;
private GameStates gameState;
public GameStates GameState { get { return gameState; } set { gameState = value; } }
#endregion //Fields and Properties
#region Constructor and Initialization
// The GameClass constructor. Here we create, but not show, the GameSettings form.
// The
public GameClass(MainClass mainClass, Control owner) {
gameState = GameStates.Loading;
this.owner = owner;
this.mainClass = mainClass;
splash = new SplashScreen(this);
splash.ShowDialog();
gameSettings = new SpaceWar.GameSettings(this);
gameSettings.Location = new Point(owner.Bounds.Right,owner.Bounds.Top);
gravity = gameSettings.Gravity;
gameSpeed = gameSettings.GameSpeed;
bounceBack = gameSettings.Bounce;
inverseGravity = gameSettings.InverseGravity;
blackHole = gameSettings.BlackHole;
localDevice = new Microsoft.DirectX.DirectDraw.Device();
localDevice.SetCooperativeLevel(owner, Microsoft.DirectX.DirectDraw.CooperativeLevelFlags.Normal);
DXUtil.Timer(DirectXTimer.Start);
SpaceWar.RotatableShape.CreateShapes();
input = new InputClass(this.owner);
soundHandler = new SoundHandler(this.owner);
try {
netPeer = new PlayClass(this);
}
catch(DirectXException e) {
MessageBox.Show(owner,e.ToString());
}
}
public void Initialize(Rectangle bounds) {
owner.Bounds = bounds;
this.GameState = GameStates.Config;
this.windowBounds = bounds;
CreateSurfaces();
sunLocation.X = windowBounds.Left + windowBounds.Width / 2;
sunLocation.Y = windowBounds.Top + windowBounds.Height / 2;
ship = new Ship(this);
Random random = new Random((int) DateTime.Now.Ticks);
ship.ScreenBounds = bounds;
ship.SetRandomPosition(true, sunLocation);
if ((null != localPlayer.Name) && (localPlayer.Name.Length > 0))
ship.HostName = localPlayer.Name.ToUpper();
else
ship.HostName = System.Environment.MachineName.ToUpper();
stars = new Stars(bounds, Constants.NumStars);
sun = new Sun(sunLocation, Constants.SunSize);
gameSettings.ControlsEnabled = true;
if (netPeer.InSession) {
if (netPeer.IsHost) {
gameSettings.ControlsEnabled = true;
netPeer.SendGameState(GameStates.Running);
}
else {
gameSettings.ControlsEnabled = false;
}
}
gameState = GameStates.Running;
}
private void CreateSurfaces() {
SurfaceDescription desc = new SurfaceDescription();
SurfaceCaps caps = new SurfaceCaps();
localClipper = new Clipper(localDevice);
localClipper.Window = owner;
desc.SurfaceCaps.PrimarySurface = true;
if (null != surfacePrimary)
surfacePrimary.Dispose();
surfacePrimary = new Surface(desc, localDevice);
surfacePrimary.Clipper = localClipper;
desc.Clear();
desc.SurfaceCaps.OffScreenPlain = true;
desc.Width = surfacePrimary.SurfaceDescription.Width;
desc.Height = surfacePrimary.SurfaceDescription.Height;
if (null != surfaceSecondary)
surfaceSecondary.Dispose();
surfaceSecondary = new Surface(desc, localDevice);
surfaceSecondary.FillStyle = 0;
}
public Ship CreateNewShip(string hostName) {
Ship newShip;
newShip = new Ship(this);
newShip.ScreenBounds = windowBounds;
newShip.HostName = hostName;
return newShip;
}
#endregion //Constructor and Initialization
#region Network play message routines
public void Pause() {
if (gameState == GameStates.Running) {
gameState = GameStates.Paused;
if (netPeer.IsHost)
netPeer.SendGameState(GameStates.Paused);
}
}
public void UnPause() {
if (gameState == GameStates.Paused) {
gameState = GameStates.Running;
if (netPeer.IsHost)
netPeer.SendGameState(GameStates.Running);
}
}
public void PlayerCreated(object sender, PlayerCreatedEventArgs pcea) {
Peer peer = (Peer) sender;
int playerID = pcea.Message.PlayerID;
PlayerInformation playerInfo = peer.GetPeerInformation(playerID);
// See if the player that was just created is us
if (playerInfo.Local) {
localPlayer.ID = playerID;
localPlayer.Name = playerInfo.Name;
}
// If not, create a remote player
else {
Ship newShip = new Ship(this);
newShip.HostName = playerInfo.Name.ToUpper();
newShip.State = (int)ShipState.Normal;
newShip.ScreenBounds = this.windowBounds;
RemotePlayer newPlayer = new RemotePlayer(playerID, playerInfo.Name, newShip);
lock (otherPlayers) {
otherPlayers.Add(playerID, newPlayer);
}
}
}
public void PlayerDestroyed(object sender, PlayerDestroyedEventArgs pdea) {
// Remove this player from our list
// We lock the data here since it is shared across multiple threads.
int playerID = pdea.Message.PlayerID;
if (playerID != localPlayer.ID) {
lock (otherPlayers) {
otherPlayers.Remove(playerID);
}
}
}
public void DataReceived(object sender, ReceiveEventArgs rea) {
int senderID = rea.Message.SenderID;
//Ignore messages received before we are initialized
if ((gameState == GameStates.Loading) ||
(gameState == GameStates.Config)) {
rea.Message.ReceiveData.Dispose();
return;
}
byte mType = (byte)rea.Message.ReceiveData.Read(typeof(byte));
MessageType messageType = (MessageType)mType;
switch (messageType) {
case MessageType.PlayerUpdateID: {
PlayerUpdate update = (PlayerUpdate)rea.
Message.ReceiveData.Read(typeof(PlayerUpdate));
ShotUpdate shotUpdate = new ShotUpdate();
shotUpdate.ShotPosition = new Vector2[Constants.NumShots];
shotUpdate.ShotAge = new int[Constants.NumShots];
for (int i = 0; i < Constants.NumShots; i++) {
shotUpdate.ShotPosition[i] =
(Vector2)rea.Message.ReceiveData.Read(typeof(Vector2));
shotUpdate.ShotAge[i] =
(int)rea.Message.ReceiveData.Read(typeof(int));
}
rea.Message.ReceiveData.Dispose();
lock (otherPlayers) {
object playerObject = otherPlayers[senderID];
if (null == playerObject)
return;
RemotePlayer player = (RemotePlayer) playerObject;
Shot[] shotArray = new Shot[Constants.NumShots];
for (int i = 0; i < Constants.NumShots; i++) {
shotArray[i] = new Shot();
shotArray[i].Position = shotUpdate.ShotPosition[i];
shotArray[i].Age = shotUpdate.ShotAge[i];
}
player.Ship.ShotHandler.SetShotArray(shotArray);
player.Ship.Position = update.ShipPosition;
player.Ship.Outline = update.Outline;
player.Ship.Velocity = update.ShipVelocity;
player.Ship.State = update.State;
player.Ship.WaitCount = update.WaitCount;
player.Ship.DeathCount = update.DeathCount;
player.Ship.FlameIndex = update.FlameIndex;
player.Ship.Sounds = (Sounds)update.Sounds;
player.Ship.Score = update.Score;
player.UpdateTime = DateTime.Now;
player.Active = true;
otherPlayers[senderID] = player;
}
break;
}
case MessageType.GameParamUpdateID: {
GameParamUpdate update = (GameParamUpdate)rea.Message.
ReceiveData.Read(typeof(GameParamUpdate));
rea.Message.ReceiveData.Dispose();
gravity = update.Gravity;
gameSpeed = update.GameSpeed;
if (update.BounceBack != 0)
bounceBack = true;
else
bounceBack = false;
if (update.InverseGravity != 0)
inverseGravity = true;
else
inverseGravity = false;
if (update.BlackHole != 0)
blackHole = true;
else
blackHole = false;
Size newWindowSize = update.WindowSize;
Rectangle newBounds = new
Rectangle(this.windowBounds.Location, newWindowSize);
//Initialize(newBounds);
break;
}
case MessageType.Add1ToScore: {
rea.Message.ReceiveData.Dispose();
ship.Score += 1;
break;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -