private void Tick() { GameController.tickTimer.Restart(); while (!stopTickerThread) { waitForNextFrame(GameController.tickTimer); frameDeltaTime = DeltaTime(GameController.tickTimer.Elapsed.TotalMilliseconds); if (GameController.DebugFrameTimings) { FrameDebugTimer.startFrameTimer(); } frameDisplayed = false; try { InputController.applyInput(); GameController.calculateFrame(frameDeltaTime); } catch (TaskCanceledException) { // TODO Do we need to handle this? } GameController.tickTimer.Restart(); if (GameController.DebugFrameTimings) { FrameDebugTimer.stopFrameTimer(); } } }
/// <summary> /// Moves a entitys polygon, angle and centerpoint to /// a new location based on velocity and rpm /// </summary> /// <param name="moveable">The Moveable to move</param> /// <param name="deltaTime">The timefactor to scale move to</param> public void Move(IMoveable moveable, double deltaTime) { if (GameController.DebugFrameTimings) { FrameDebugTimer.startMoveShapeTimer(); } IShape shape = moveable.Shape; double xCalculation = moveable.Velocity * Math.Sin(shape.Ray.Angle * Math.PI / 180) * deltaTime; double yCalculation = moveable.Velocity * Math.Cos(shape.Ray.Angle * Math.PI / 180) * deltaTime; shape.Ray.Angle += moveable.RPM / 10 * deltaTime; // RPM * 360 / (60 * 60) = Rounds per Frame => RPM * 6 / 60 = RPM / 10 bool checkX = true; bool checkY = true; PointCollection pc = null; UIDispatcher.Invoke(() => { pc = shape.Polygon.Points; for (int i = 0; pc != null && i < pc.Count - 3 && (checkX || checkY); i++) // last 3 points of the polygon is the header { if (!(xCalculation + pc[i].X >= 1 && xCalculation + pc[i].X <= ArenaController.ArenaBoundWidth - 1)) { checkX = false; } if (!(yCalculation + pc[i].Y >= 1 && yCalculation + pc[i].Y <= ArenaController.ArenaBoundHeight - 1)) { checkY = false; } } }); Point cp = shape.Ray.CenterPoint; double offsetX = checkX ? xCalculation : 0; double offsetY = checkY ? yCalculation : 0; cp.Offset(offsetX, offsetY); shape.Ray.CenterPoint = cp; UIDispatcher.Invoke(() => { for (int i = 0; i < pc.Count; i++) { Point p = pc[i]; p.Offset(offsetX, offsetY); pc[i] = p; } shape.Polygon.RenderTransform = new RotateTransform(-1 * shape.Ray.Angle, shape.Ray.CenterPoint.X, shape.Ray.CenterPoint.Y); }); if (GameController.DebugFrameTimings) { FrameDebugTimer.stopMoveShapeTimer(); } CollisionDetection.runCollisionDetection(); }
private void waitForNextFrame(Stopwatch tickTimer) { if (GameController.DebugFrameTimings) { FrameDebugTimer.startFpsLimitTimer(); } while (!frameDisplayed || tickTimer.Elapsed.TotalMilliseconds <= 1000d / 480) { Thread.Sleep(1); } if (GameController.DebugFrameTimings) { FrameDebugTimer.stopFpsLimitTimer(); } }
public static void applyInput() { ButtonDown input = Input.queryInput(); if (!GameController.IsPlayerDead) { IMoveable shape = GameController.Player.PlayerShip; shape.RPM = ((int)(input & ButtonDown.LEFT) >> 2) * shape.MaxRPM - (int)(input & ButtonDown.RIGHT) * shape.MaxRPM; shape.Velocity = ((int)(input & ButtonDown.UP) >> 1) * shape.MaxVelocity - ((int)(input & ButtonDown.DOWN) >> 3) * shape.MaxVelocity; bool shootPressed = (int)(input & ButtonDown.SHOOT) >> 4 > 0; if (shootPressed && !shootFlag && (!shootTimer.IsRunning || shootTimer.Elapsed.TotalSeconds >= 1)) { shootTimer.Restart(); Task.Run(() => NetworkController.GameService.playerShoots(Constants.standardShotDamage)); shootFlag = true; } else if (!shootPressed) { shootFlag = false; } } // Only run timeings output when DebugFrameTimings is true if (GameController.DebugFrameTimings && (int)(input & ButtonDown.DEBUG) >> 5 > 0 && hasRun == false) { FrameDebugTimer.outpuFrameTimerResults(); FrameDebugTimer.outputMoveShapeTimerResults(); FrameDebugTimer.outputCollisionTimerResults(); FrameDebugTimer.outputFpsLimitTimerResults(); hasRun = true; } }