📄 gameengine.cs
字号:
(float)r.NextDouble() * 10.0f + 120.0f,
(float)r.NextDouble() * 5.0f + 15.0f
);
// Randomly switch
if (r.Next(100) > 49)
{
currentCameraPosition.X *= -1;
}
// Randomly switch
if (r.Next(100) > 49)
{
currentCameraPosition.Z *= -1;
}
}
/// <summary>
/// Update the camera if it isn't in the correct position yet
/// </summary>
/// <param name="elapsedTime">time elapsed since the last update</param>
private void UpdateCamera(float elapsedTime)
{
if (currentCameraPosition != CameraDefaultLocation)
{
Vector3 diff = CameraDefaultLocation - currentCameraPosition;
// Are we close enough to just move there?
if (diff.Length() > (MaximumSpeed * elapsedTime))
{
// No we're not, move slowly there
diff.Normalize();
diff.Scale(MaximumSpeed * elapsedTime);
currentCameraPosition += diff;
}
else
{
// Indeed we are, just move there
currentCameraPosition = CameraDefaultLocation;
}
// Set the view transform now
sampleFramework.Device.Transform.View = Matrix.LookAtLH(currentCameraPosition,
CameraDefaultLookAtLocation, CameraUp);
}
else
{
isLoadingLevel = false;
}
}
#endregion
#region User Input methods
/// <summary>Hook the mouse events</summary>
private void OnMouseEvent(bool leftDown, bool rightDown, bool middleDown,
bool side1Down, bool side2Down, int wheel, int x, int y)
{
if (!leftDown)
{
if (isMainMenuShowing)
{
mainScreen.OnMouseMove(x, y);
}
else if (isSelectScreenShowing)
{
selectScreen.OnMouseMove(x, y);
}
else if (isQuitMenuShowing)
{
quitScreen.OnMouseMove(x, y);
}
}
else if (leftDown)
{
if (isMainMenuShowing)
{
mainScreen.OnMouseClick(x, y);
}
else if (isSelectScreenShowing)
{
selectScreen.OnMouseClick(x, y);
}
else if (isQuitMenuShowing)
{
quitScreen.OnMouseClick(x, y);
}
}
}
/// <summary>Handle keyboard strokes</summary>
private void OnKeyEvent(System.Windows.Forms.Keys key, bool keyDown, bool altDown)
{
// Only do this when it's down
if (keyDown)
{
if (isMainMenuShowing)
{
mainScreen.OnKeyPress(key);
}
else if (isSelectScreenShowing)
{
selectScreen.OnKeyPress(key);
}
else if (isQuitMenuShowing)
{
quitScreen.OnKeyPress(key);
}
else
{
// Allow you to quit at any time
if (key == System.Windows.Forms.Keys.Escape)
{
ShowQuitMenu();
}
// Ignore any keystrokes while the fly through is displayed
if (!isLoadingLevel)
{
// Otherwise, let the level handle the key strokes
currentLevel.OnKeyPress(key);
}
}
}
}
/// <summary>
/// Only allow closing through the quit menu
/// </summary>
private IntPtr OnWndProc(IntPtr hWnd, NativeMethods.WindowMessage msg, IntPtr wParam, IntPtr lParam, ref bool noFurtherProcessing)
{
if (msg == NativeMethods.WindowMessage.Close)
{
ShowQuitMenu();
if (!canClose)
{
noFurtherProcessing = true;
return Framework.TrueIntPtr;
}
}
return IntPtr.Zero;
}
#endregion
private void ShowQuitMenu()
{
isQuitMenuShowing = !isQuitMenuShowing;
if (isQuitMenuShowing)
{
// The game is now paused, the 'quit' message will be shown
// Pause the timer as well
sampleFramework.Pause(true, false);
}
else
{
// Restart the timer
sampleFramework.Pause(false, false);
}
}
#region UI Screen Event Handlers
/// <summary>
/// Fired when the cancel button is clicked on the quit screen
/// meaning you do not want to quit
/// </summary>
private void OnQuitScreenCancel(object sender, EventArgs e)
{
// The game is paused, they clicked no, unpause
sampleFramework.Pause(false, false);
isQuitMenuShowing = false;
}
/// <summary>
/// Fired when the quit button is clicked
/// </summary>
private void OnQuitScreenQuit(object sender, EventArgs e)
{
// The game is paused, they want to quit, quit to main screen
isMainMenuShowing = true;
isQuitMenuShowing = false;
sampleFramework.Pause(false, false);
// Reset the level back to the first
levelNumber = 1;
}
/// <summary>
/// Fired when your character has been selected
/// </summary>
private void OnLoopySelected(object sender, EventArgs e)
{
SelectLoopyScreen screen = sender as SelectLoopyScreen;
System.Diagnostics.Debug.Assert(screen != null, GameName,
"The selection screen should never be null.");
// Create the graphics items
CreateGraphicObjects(screen);
// And not showing the select screen
isSelectScreenShowing = false;
// Fix the lights
EnableLights();
}
#endregion
#region Main UI Screen Events
private void OnNewGame(object sender, EventArgs e)
{
// Show the select screen
isMainMenuShowing = false;
isSelectScreenShowing = true;
// Fix the lights
EnableLights();
}
private void OnMainQuit(object sender, EventArgs e)
{
// Allow the application to quit now
canClose = true;
sampleFramework.CloseWindow();
}
#endregion
#region Run/Main methods
/// <summary>
/// Entry point to the program. Initializes everything and goes into a message processing
/// loop. Idle time is used to render the scene.
/// </summary>
static int Main()
{
using(Framework sampleFramework = new Framework())
{
GameEngine blockersEngine = new GameEngine(sampleFramework);
// Set the callback functions. These functions allow the sample framework to notify
// the application about device changes, user input, and windows messages. The
// callbacks are optional so you need only set callbacks for events you're interested
// in. However, if you don't handle the device reset/lost callbacks then the sample
// framework won't be able to reset your device since the application must first
// release all device resources before resetting. Likewise, if you don't handle the
// device created/destroyed callbacks then the sample framework won't be able to
// recreate your device resources.
sampleFramework.Disposing += new EventHandler(blockersEngine.OnDestroyDevice);
sampleFramework.DeviceLost += new EventHandler(blockersEngine.OnLostDevice);
sampleFramework.DeviceCreated += new DeviceEventHandler(blockersEngine.OnCreateDevice);
sampleFramework.DeviceReset += new DeviceEventHandler(blockersEngine.OnResetDevice);
sampleFramework.SetKeyboardCallback(new KeyboardCallback(blockersEngine.OnKeyEvent));
// Catch mouse move events
sampleFramework.IsNotifiedOnMouseMove = true;
sampleFramework.SetMouseCallback(new MouseCallback(blockersEngine.OnMouseEvent));
sampleFramework.SetCallbackInterface(blockersEngine);
try
{
#if (!DEBUG)
// In retail mode, force the app to fullscreen mode
sampleFramework.IsOverridingFullScreen = true;
#endif
// Show the cursor and clip it when in full screen
sampleFramework.SetCursorSettings(true, true);
// Initialize the sample framework and create the desired window and Direct3D
// device for the application. Calling each of these functions is optional, but they
// allow you to set several options which control the behavior of the sampleFramework.
sampleFramework.Initialize( false, false, true ); // Parse the command line, handle the default hotkeys, and show msgboxes
sampleFramework.CreateWindow("Blockers - The Game");
sampleFramework.CreateDevice( 0, true, Framework.DefaultSizeWidth, Framework.DefaultSizeHeight,
blockersEngine);
// Pass control to the sample framework for handling the message pump and
// dispatching render calls. The sample framework will call your FrameMove
// and FrameRender callback when there is idle time between handling window messages.
sampleFramework.MainLoop();
}
#if(DEBUG)
catch (Exception e)
{
// In debug mode show this error (maybe - depending on settings)
sampleFramework.DisplayErrorMessage(e);
#else
catch
{
// In release mode fail silently
#endif
// Ignore any exceptions here, they would have been handled by other areas
return (sampleFramework.ExitCode == 0) ? 1 : sampleFramework.ExitCode; // Return an error code here
}
// Perform any application-level cleanup here. Direct3D device resources are released within the
// appropriate callback functions and therefore don't require any cleanup code here.
return sampleFramework.ExitCode;
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -