📄 gameclass.cs
字号:
using (Sprite pointerSprite = new Sprite(device))
{
pointerSprite.Begin(SpriteFlags.AlphaBlend);
pointerSprite.Draw(bgPointerTexture, new Rectangle(0, 0, 128, 128),
new Vector3(0, 0, 0), new Vector3(42, this.Height - 250, 0), Color.White);
pointerSprite.Transform = Matrix.Identity;
pointerSprite.Draw(vectorPanel,new Rectangle(0,0,193,173),
new Vector3(0,0,0), new Vector3(10, this.Height- 282, 0), Color.White);
pointerSprite.End();
}
//Output the scores
drawingFont.DrawText(5, 20, Color.Red, playerShip.HostName.ToString() + ":");
drawingFont.DrawText(100,20,Color.Red,playerShip.Score.ToString());
if (remotePlayerActive)
{
drawingFont.DrawText(5,45,Color.White, peer.RemotePlayer.Name + ":");
drawingFont.DrawText(100,45, Color.White, opponentShip.Score.ToString());
}
//Display any status messages
if (statusMessageTimer < Constants.StatusMessageDisplayTime)
{
drawingFont.DrawText(200, screenCenter.Y, Color.FromArgb(200,220,220,255), statusMessage);
}
if (debugging)
{
drawingFont.DrawText(5,5,Color.Yellow,debugText);
}
device.EndScene();
}
private void RenderBGPointer()
{
Viewport view = new Viewport();
view.Width = 128;
view.Height = 128;
view.MaxZ = 1.0f;
Matrix currentViewMatrix = device.Transform.View;
rts.BeginScene(renderSurface, view);
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
if (playerShip.State == ShipState.Normal &&
opponentShip.State == ShipState.Normal)
{
Matrix pointerViewMatrix = Matrix.Translation(0, 2, -15);
pointerViewMatrix *= playerShip.Position.WorldMatrix;
device.Transform.View = Matrix.Invert(pointerViewMatrix);
bgPointer.Render();
drawingFont.DrawText(2,2,Color.LimeGreen, "Range: " + range.ToString());
}
rts.EndScene(Filter.Linear);
device.Transform.View = currentViewMatrix;
}
/// <summary>
/// Initialize scene objects.
/// </summary>
protected override void InitializeDeviceObjects()
{
drawingFont.InitializeDeviceObjects(device);
spaceSphere = new PositionedMesh(device, "SpaceSphere.x");
playerShip = new Ship(device, this, hullColor);
if (playerShip.HostName == null)
playerShip.HostName = "Player";
playerShip.State = ShipState.Normal;
HullColors opponentHullColor;
if (hullColor == HullColors.Red)
opponentHullColor = HullColors.White;
else
opponentHullColor = HullColors.Red;
opponentShip = new Ship(device, this, opponentHullColor);
if (opponentShip.HostName == null)
opponentShip.HostName = "Opponent";
opponentShip.State = ShipState.Normal;
bgPointer = new BGPointer(device);
vectorPanel = TextureLoader.FromFile(device, MediaUtilities.FindFile("vectorPanel.png"));
rts = new RenderToSurface(device, 128, 128, Format.X8R8G8B8, true, DepthFormat.D16);
}
/// <summary>
/// Called when a device needs to be restored.
/// </summary>
protected override void RestoreDeviceObjects(System.Object sender, System.EventArgs e)
{
device.RenderState.Ambient = Color.FromArgb(150,150,150);
device.RenderState.SpecularEnable = true;
device.Lights[0].Type = LightType.Directional;
device.Lights[0].Direction = new Vector3(0,-1, -1);
device.Lights[0].Diffuse = Color.White;
device.Lights[0].Specular = Color.White;
device.Lights[0].Enabled = true;
device.RenderState.Lighting = true;
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4,
(presentParams.BackBufferWidth/presentParams.BackBufferHeight),
1.5f, 20000.0f );
device.Transform.View = camera.ViewMatrix;
screenCenter = new Vector2(
presentParams.BackBufferWidth / 2,
presentParams.BackBufferHeight / 2);
if (bgPointerTexture != null)
bgPointerTexture.Dispose();
bgPointerTexture = new Texture(device, 128, 128, 1, Usage.RenderTarget,
Format.X8R8G8B8, Pool.Default);
renderSurface = bgPointerTexture.GetSurfaceLevel(0);
gameState = GameStates.Running;
}
public void RemotePlayerJoined(string playerName)
{
remotePlayerActive = true;
statusMessage = playerName + " has joined the game.";
statusMessageTimer = 0;
}
public void RemotePlayerLeft(string playerName)
{
remotePlayerActive = false;
statusMessage = playerName + " has left the game.";
statusMessageTimer = 0;
}
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 Vector3[Constants.NumShots];
shotUpdate.ShotAge = new float[Constants.NumShots];
shotUpdate.ShotAlive = new bool[Constants.NumShots];
for (int i = 0; i < Constants.NumShots; i++)
{
shotUpdate.ShotPosition[i] = (Vector3)rea.Message.ReceiveData.Read(typeof(Vector3));
shotUpdate.ShotAge[i] = (int)rea.Message.ReceiveData.Read(typeof(int));
shotUpdate.ShotAlive[i] = (bool)rea.Message.ReceiveData.Read(typeof(bool));
}
rea.Message.ReceiveData.Dispose();
lock(opponentShip)
{
opponentShip.Position.WorldMatrix = update.WorldMatrix;
opponentShip.Score = update.Score;
opponentShip.Sounds = (Sounds)update.Sounds;
opponentShip.WaitCount = update.WaitCount;
opponentShip.SetState((ShipState)update.State);
Photon[] shotArray = opponentShip.ShotHandler.GetShotArray();
for (int i = 0; i < Constants.NumShots; i++)
{
shotArray[i].Location = shotUpdate.ShotPosition[i];
shotArray[i].Age = shotUpdate.ShotAge[i];
shotArray[i].Alive = shotUpdate.ShotAlive[i];
}
opponentShip.ShotHandler.SetShotArray(shotArray);
}
break;
}
case MessageType.Add1ToScore:
{
rea.Message.ReceiveData.Dispose();
playerShip.Score += 1;
break;
}
case MessageType.GamePaused:
{
rea.Message.ReceiveData.Dispose();
gameState = GameStates.Paused;
break;
}
case MessageType.GameRunning:
{
rea.Message.ReceiveData.Dispose();
if (gameState == GameStates.Paused)
gameState = GameStates.Running;
break;
}
}
}
public void SendPoint()
{
if (!remotePlayerActive)
{
playerShip.Score += 1;
return;
}
peer.SendScorePoint();
}
public void SendMyPlayerUpdate()
{
if (!networkEnabled)
return;
PlayerUpdate update = new PlayerUpdate();
update.WorldMatrix = playerShip.Position.WorldMatrix;
update.State = (int)playerShip.State;
update.WaitCount = playerShip.WaitCount;
update.DeathCount = playerShip.DeathCount;
update.Sounds = (int)playerShip.Sounds;
update.Score = playerShip.Score;
ShotUpdate shotUpdate = new ShotUpdate();
shotUpdate.ShotAge = new float[Constants.NumShots];
shotUpdate.ShotPosition = new Vector3[Constants.NumShots];
shotUpdate.ShotAlive = new bool[Constants.NumShots];
Photon[] shotArray = playerShip.ShotHandler.GetShotArray();
for (int i = 0; i < Constants.NumShots; i++)
{
shotUpdate.ShotPosition[i] = shotArray[i].Location;
shotUpdate.ShotAge[i] = shotArray[i].Age;
shotUpdate.ShotAlive[i] = shotArray[i].Alive;
}
peer.SendPlayerUpdate(update, shotUpdate);
}
/// <summary>
// When the peer closes, the code here is executed.
/// </summary>
public void PeerClose()
{
// The session was terminated, go ahead and shut down
this.Dispose();
}
private void SelectNextCameraMode()
{
int mode = (int) cameraMode;
mode++;
if (mode > 2)
mode = 0;
cameraMode = (CameraMode) mode;
}
private void GameClass_MouseMove(object sender, MouseEventArgs e)
{
mouseLoc.X = e.X;
mouseLoc.Y = e.Y;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -