Added text and restart

This commit is contained in:
Simon Lübeß
2022-02-13 22:51:55 +01:00
parent d9e34b27a3
commit 7f11b982ee
2 changed files with 45 additions and 19 deletions
+20 -3
View File
@@ -32,6 +32,9 @@ namespace Sandbox
List<Obstacle> _obstacles = new List<Obstacle>() ~ DeleteContainerAndItems!(_); List<Obstacle> _obstacles = new List<Obstacle>() ~ DeleteContainerAndItems!(_);
FontRenderer.PreparedText pressSpaceToStart ~ _.ReleaseRef();
FontRenderer.PreparedText pressSpaceToRestart ~ _.ReleaseRef();
[AllowAppend] [AllowAppend]
public this() : base("Example") public this() : base("Example")
{ {
@@ -59,6 +62,10 @@ namespace Sandbox
}; };
_depthStencilState = new DepthStencilState(dssDesc); _depthStencilState = new DepthStencilState(dssDesc);
pressSpaceToStart = FontRenderer.PrepareText(_font, "Press [SPACE]", 1);
pressSpaceToRestart = FontRenderer.PrepareText(_font, "YOU DIED!\nPress [SPACE] to restart", 0.5f);
InitGame(); InitGame();
} }
@@ -149,14 +156,24 @@ namespace Sandbox
obs.Draw(); obs.Draw();
} }
Renderer.BeginScene(_camera);
_rocket.Draw(); _rocket.Draw();
FontRenderer.DrawText(_font, scope $"{_rocket.Score}", 0, 1.5f, 1, .Black); 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)); 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(); Renderer2D.EndScene();
} }
+24 -15
View File
@@ -21,11 +21,11 @@ namespace Sandbox
private Vector2 _position; 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<Obstacle> Obstacles; public List<Obstacle> Obstacles;
@@ -39,28 +39,34 @@ namespace Sandbox
public void Update(GameTime gameTime) public void Update(GameTime gameTime)
{ {
FlightSpeed = _dead ? 0.0f : 2.0f; FlightSpeed = Dead ? 0.0f : 2.0f;
if (!_started) if (Dead && Input.IsKeyPressing(Key.Space))
{
Dead = false;
Started = false;
_speed = .Zero;
Score = 0;
}
else if (!Started)
{ {
_position = .Zero; _position = .Zero;
if (Input.IsKeyPressed(Key.Space)) if (Input.IsKeyPressing(Key.Space))
_started = true; Started = true;
else else
return; return;
} }
UpdatePosition(gameTime); UpdatePosition(gameTime);
CheckDead(); if (Started)
CheckDead();
} }
public void Draw() public void Draw()
{ {
Renderer2D.DrawQuad(_position, .One, _rotation, _rocketTexture, .White); Renderer2D.DrawQuad(_position, .One, _rotation, _rocketTexture, .White);
CheckObstacles();
} }
Line2D rocketLine; Line2D rocketLine;
@@ -69,7 +75,7 @@ namespace Sandbox
{ {
_acceleration.Y = 0; _acceleration.Y = 0;
if (!_dead && Input.IsKeyPressed(Key.Space)) if (!Dead && Input.IsKeyPressed(Key.Space))
{ {
_acceleration.Y += Power; _acceleration.Y += Power;
} }
@@ -95,10 +101,12 @@ namespace Sandbox
{ {
if (Math.Abs(_position.Y) >= worldBorder) if (Math.Abs(_position.Y) >= worldBorder)
{ {
_dead = true; Dead = true;
_speed.Y *= -0.8f; _speed.Y *= -0.8f;
_position.Y = Math.Clamp(_position.Y, -worldBorder, worldBorder); _position.Y = Math.Clamp(_position.Y, -worldBorder, worldBorder);
} }
CheckObstacles();
} }
private int scoreLine = -1; private int scoreLine = -1;
@@ -107,7 +115,8 @@ namespace Sandbox
{ {
for (Obstacle obs in Obstacles) 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; Vector2 center = obs.Position;
@@ -132,10 +141,10 @@ namespace Sandbox
(topR.Intersects(rocketLine) case .Intersection) || (topR.Intersects(rocketLine) case .Intersection) ||
(topL.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) if (Line2D(bottomTip, topTip).Intersects(rocketLine) case .Intersection)
{ {