From 7f11b982ee18b5b6c1669100866078a1c0f11ba2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Sun, 13 Feb 2022 22:51:55 +0100 Subject: [PATCH] Added text and restart --- Sandbox/src/GameLayer.bf | 23 +++++++++++++++++++--- Sandbox/src/Rocket.bf | 41 ++++++++++++++++++++++++---------------- 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/Sandbox/src/GameLayer.bf b/Sandbox/src/GameLayer.bf index 7a91f3d..9ad4b02 100644 --- a/Sandbox/src/GameLayer.bf +++ b/Sandbox/src/GameLayer.bf @@ -32,6 +32,9 @@ namespace Sandbox List _obstacles = new List() ~ DeleteContainerAndItems!(_); + FontRenderer.PreparedText pressSpaceToStart ~ _.ReleaseRef(); + FontRenderer.PreparedText pressSpaceToRestart ~ _.ReleaseRef(); + [AllowAppend] public this() : base("Example") { @@ -59,6 +62,10 @@ namespace Sandbox }; _depthStencilState = new DepthStencilState(dssDesc); + pressSpaceToStart = FontRenderer.PrepareText(_font, "Press [SPACE]", 1); + + pressSpaceToRestart = FontRenderer.PrepareText(_font, "YOU DIED!\nPress [SPACE] to restart", 0.5f); + InitGame(); } @@ -149,14 +156,24 @@ namespace Sandbox obs.Draw(); } - Renderer.BeginScene(_camera); - _rocket.Draw(); FontRenderer.DrawText(_font, scope $"{_rocket.Score}", 0, 1.5f, 1, .Black); FontRenderer.DrawText(_font, scope $"{_rocket.Score}", 0.1f, 1.6f, 1, .(230, 230, 230)); - Renderer.EndScene(); + if (!_rocket.Started) + { + float f = (float)Math.Cos(gameTime.TotalTime.TotalSeconds) * 0.5f + 0.55f; + + FontRenderer.DrawText(pressSpaceToStart, -pressSpaceToStart.AdvanceX / 2f, 0, .(1, 0, 0, f)); + } + + if (_rocket.Dead) + { + float f = (float)Math.Cos(gameTime.TotalTime.TotalSeconds) * 0.5f + 0.55f; + + FontRenderer.DrawText(pressSpaceToRestart, -pressSpaceToStart.AdvanceX / 2f, 0, .(1, 0, 0, f)); + } Renderer2D.EndScene(); } diff --git a/Sandbox/src/Rocket.bf b/Sandbox/src/Rocket.bf index 36b2655..ff3fbf2 100644 --- a/Sandbox/src/Rocket.bf +++ b/Sandbox/src/Rocket.bf @@ -21,11 +21,11 @@ namespace Sandbox private Vector2 _position; - private float _rotation; + private float _rotation = -MathHelper.PiOverTwo; - private bool _dead; + public bool Dead; - private bool _started = false; + public bool Started = false; public List Obstacles; @@ -39,28 +39,34 @@ namespace Sandbox public void Update(GameTime gameTime) { - FlightSpeed = _dead ? 0.0f : 2.0f; - - if (!_started) + FlightSpeed = Dead ? 0.0f : 2.0f; + + if (Dead && Input.IsKeyPressing(Key.Space)) + { + Dead = false; + Started = false; + _speed = .Zero; + Score = 0; + } + else if (!Started) { _position = .Zero; - if (Input.IsKeyPressed(Key.Space)) - _started = true; + if (Input.IsKeyPressing(Key.Space)) + Started = true; else return; } UpdatePosition(gameTime); - CheckDead(); + if (Started) + CheckDead(); } public void Draw() { Renderer2D.DrawQuad(_position, .One, _rotation, _rocketTexture, .White); - - CheckObstacles(); } Line2D rocketLine; @@ -69,7 +75,7 @@ namespace Sandbox { _acceleration.Y = 0; - if (!_dead && Input.IsKeyPressed(Key.Space)) + if (!Dead && Input.IsKeyPressed(Key.Space)) { _acceleration.Y += Power; } @@ -95,10 +101,12 @@ namespace Sandbox { if (Math.Abs(_position.Y) >= worldBorder) { - _dead = true; + Dead = true; _speed.Y *= -0.8f; _position.Y = Math.Clamp(_position.Y, -worldBorder, worldBorder); } + + CheckObstacles(); } private int scoreLine = -1; @@ -107,7 +115,8 @@ namespace Sandbox { for (Obstacle obs in Obstacles) { - const float f = Vector2.One.Magnitude(); + //const float f = Vector2.One.Magnitude(); + float f = Vector2.One.Magnitude(); Vector2 center = obs.Position; @@ -132,10 +141,10 @@ namespace Sandbox (topR.Intersects(rocketLine) case .Intersection) || (topL.Intersects(rocketLine) case .Intersection)) { - _dead = true; + Dead = true; } - if (!_dead) + if (!Dead) { if (Line2D(bottomTip, topTip).Intersects(rocketLine) case .Intersection) {