/// <summary> /// Grabs the first key that is currently pressed and returns it as a character. /// </summary> /// <returns>ASCII character of the first key current pressed.</returns> public static char GetChar() { while (_grabIndex < 160) { KeyCodes keyCode = GetKey(); if ((int)keyCode != 0) { char chr = NativeMethods.VirtualKeyToChar((uint)_virtualKeys[(int)keyCode]); if (chr != '\0') { if (_lastKey == keyCode && _fastKeyTimer.DurationMillisecond > _fastKeyDelay) { return(chr); } else { if (_lastKey != keyCode) { _fastKeyTimer.Restart(); _lastKey = keyCode; return(chr); } return('\0'); } } } } _fastKeyTimer.Restart(); _lastKey = (KeyCodes)0; return('\0'); }
/// <summary> /// This will setup the drawing buffers so that they are ready for you to render on /// them this must be called before you can render anything/ /// </summary> public static void BeginScene() { // Track render timer. _renderTimer.Restart(); // Notify the driver of the begining scene. _driver.BeginScene(); }
/// <summary> /// Called when the process this process is waiting for finishes. /// </summary> public override void WaitFinished() { _timer.Restart(); }
/// <summary> /// Pauses the execution of the current process for the given amount of time. /// </summary> /// <param name="time">Amount of time to pause for.</param> public virtual void Pause(int time) { _pauseTimer.Restart(); _isPaused = true; _pauseDelay = time; }
/// <summary> /// Called by the base engine class when its safe to update and render the scene. /// </summary> protected override void Update() { // Ignore everything else if we are not running a game. if (_gameName == "") { return; } //System.Console.WriteLine("Collective Time("+EntityNode.CollectiveCalls+" calls): "+EntityNode.CollectiveTime); //EntityNode.CollectiveTime = 0.0f; //EntityNode.CollectiveCalls = 0; //EntityNode.CollectiveTimer.Restart(); // Time to load a map? if (_mapLoadPending == true) { // Pump out a MapFinish event. EventManager.FireEvent(new Event("map_finish", this, null)); // Allow the processes some time to be notified of closing events. //EventManager.ProcessEvents(); //ProcessManager.RunProcesses(1); // Tell the scripts that we are now loading the new map (so they can show a loadings screen). _gameScriptProcess.Process.InvokeFunction("OnLoadingBegin", true, true); // Don't want to render the scene graph as its currently being loaded >.<. bool priorSceneGraphRender = true; if (_isServer == false) { priorSceneGraphRender = _window.RenderSceneGraph; _window.RenderLoadingScreen = true; _window.RenderSceneGraph = false; } // Keep track of time :). HighPreformanceTimer loadTimer = new HighPreformanceTimer(); // Get starting memory. long startingMemory = GC.GetTotalMemory(true); if (GraphicsManager.ThreadSafe == false) { // Load the new map. DebugLogger.WriteLog("Loading map from " + _mapLoadFile + " " + (_mapLoadPassword != "" ? " with password " + _mapLoadPassword : "") + "."); LoadMapThread(); } else { // Load the new map. DebugLogger.WriteLog("Loading map from " + _mapLoadFile + " " + (_mapLoadPassword != "" ? " with password " + _mapLoadPassword : "") + "."); Thread thread = new Thread(LoadMapThread); thread.Priority = ThreadPriority.Highest; // Thread.CurrentThread.Priority = ThreadPriority.Lowest; thread.IsBackground = true; thread.Start(); // Ech, there has to be a better way than this. I hate thread safety >.>. HighPreformanceTimer timer = new HighPreformanceTimer(); while (thread != null && thread.IsAlive == true) { // Track frame stats. TrackFrameStatsBegin(); // Update the process. //timer = new HighPreformanceTimer(); //_gameScriptProcess.Run(_deltaTime); //_processProcessingDuration = (float)timer.DurationMillisecond; // Update the graphical console. if (_isServer == false) { GraphicalConsole.Update(1.0f); } // Tell the window to render if (_isServer == false) { timer.Restart(); GraphicsCanvas.RenderAll(); _window.Render(); _renderingDuration = (float)timer.DurationMillisecond; } // Update network. NetworkManager.Poll(); // Process application level events. timer.Restart(); Application.DoEvents(); _applicationProcessingDuration = (float)timer.DurationMillisecond; // Track frame stats. TrackFrameStatsFinish(); } } // Invoke OnCreate events of scripted entities. foreach (SceneNode node in _map.SceneGraph.EnumerateNodes()) { if (node != null && node is ScriptedEntityNode && node.IsPersistent == false) { ((ScriptedEntityNode)node).ScriptProcess[0].InvokeFunction("OnCreate", true, false); } } // Run the collision manager quickly so that we can sort out any unplesent problems. CollisionManager.ProcessCollisions(); //Thread.CurrentThread.Priority = ThreadPriority.Normal; // We can render again! Oh holy days! if (_isServer == false) { _window.RenderLoadingScreen = false; _window.RenderSceneGraph = priorSceneGraphRender; } // Remove any old resource from the cache that haven't been used for 5 maps :P. ResourceManager.CollectGarbage(3); // Free up some space. GC.Collect(); // And we are done! DebugLogger.WriteLog("Map loaded successfully in " + loadTimer.DurationMillisecond + "ms, " + (((GC.GetTotalMemory(false) - startingMemory) / 1024.0f) / 1024.0f) + "mb allocated during loading."); // Yay, loaded! _mapLoadPending = false; // Reset the delta time! _forcedDeltaTimeThisFrame = 1.0f; } }
/// <summary> /// Updates the following entity associated with this process. /// </summary> /// <param name="deltaTime">Elapsed time since last frame.</param> public override void Run(float deltaTime) { //if (_entity.IsEnabled == false) return; // Work out the central points. Transformation entityTransform = _entity.CalculateTransformation(); // If its a camera then we need to invert the coordinates as it uses // slightly different ones from normal entities. if (_entity is CameraNode) { entityTransform.X = -entityTransform.X; entityTransform.Y = -entityTransform.Y; } // Are we at the correct place? If so why bother with the below code :P. if (entityTransform.X == _x && entityTransform.Y == _y) { Finish(ProcessResult.Success); return; } // If we can't move then finish. if (_gotPreviousTransformation == true) { if (_previousTransformation.X == entityTransform.X && _previousTransformation.Y == entityTransform.Y && _previousTransformation.Z == entityTransform.Z) { if (_previousTransformationTimer.DurationMillisecond > 500) // Half a second and no movement O_o, oh shiz we must have something in out way. { Finish(ProcessResult.Failed); return; } } else { _previousTransformationTimer.Restart(); _previousTransformation = entityTransform; } } else { _previousTransformation = entityTransform; _previousTransformationTimer.Restart(); _gotPreviousTransformation = true; } float entityTransformCenterX = entityTransform.X + ((_entity.BoundingRectangle.Width / 2) * entityTransform.ScaleX); float entityTransformCenterY = entityTransform.Y + ((_entity.BoundingRectangle.Height / 2) * entityTransform.ScaleY); float vectorX = entityTransformCenterX - _x; float vectorY = entityTransformCenterY - _y; float distance = (float)Math.Sqrt(vectorX * vectorX + vectorY * vectorY); vectorX /= distance; vectorY /= distance; float followSpeed = _followSpeed.Start + (((_followSpeed.Finish - _followSpeed.Start) / _originalDistance) * (_originalDistance - distance)); // Work out vector towards entity. float movementX = (vectorX * followSpeed) * deltaTime; float movementY = (vectorY * followSpeed) * deltaTime; _entity.Move(-movementX, -movementY, 0.0f); // Are we done yet? if (Math.Abs(Math.Round(distance)) <= Math.Max(1, _followSpeed.Finish) * 2)// || followSpeed == _followSpeed.Finish) { Finish(ProcessResult.Success); return; } }