diff --git a/assets/font.ttf b/assets/font.ttf new file mode 100644 index 0000000..39adf42 Binary files /dev/null and b/assets/font.ttf differ diff --git a/assets/roboto.ttf b/assets/roboto.ttf new file mode 100644 index 0000000..ddf4bfa Binary files /dev/null and b/assets/roboto.ttf differ diff --git a/src/Controlls.cs b/src/Controls.cs similarity index 67% rename from src/Controlls.cs rename to src/Controls.cs index 1a27b7c..0be6ebc 100644 --- a/src/Controlls.cs +++ b/src/Controls.cs @@ -5,7 +5,8 @@ enum Control THRUST, LEFT, RIGHT, - SHOOT + SHOOT, + RESTART } static class ControlKeyExtension @@ -15,13 +16,15 @@ static class ControlKeyExtension switch (c) { case Control.THRUST: - return SDL_Keycode.SDLK_UP; + return SDL_Keycode.SDLK_w; case Control.LEFT: - return SDL_Keycode.SDLK_LEFT; + return SDL_Keycode.SDLK_a; case Control.RIGHT: - return SDL_Keycode.SDLK_RIGHT; + return SDL_Keycode.SDLK_d; case Control.SHOOT: return SDL_Keycode.SDLK_SPACE; + case Control.RESTART: + return SDL_Keycode.SDLK_r; default: throw new ArgumentException("Invalid control"); } diff --git a/src/Scene.cs b/src/Scene.cs index 0be4c91..af42025 100644 --- a/src/Scene.cs +++ b/src/Scene.cs @@ -5,6 +5,7 @@ class Scene public const int SCREEN_WIDTH = 800; public const int SCREEN_HEIGHT = 600; private Ship ship; + private UI ui; private Renderer renderer; private static Scene? instance; @@ -13,10 +14,12 @@ class Scene public int Score = 0; public int Level = 1; public AudioPlayer AudioPlayer; + public bool running = true; public Scene(Renderer renderer) { this.ship = new Ship(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2); + this.ui = new UI(); this.Shots = new HashSet(); this.Asteroids = new HashSet(); this.renderer = renderer; @@ -26,12 +29,22 @@ class Scene instance = this; } + public void Restart() + { + this.running = true; + this.Score = 0; + this.Level = 1; + this.Asteroids.Clear(); + this.Shots.Clear(); + this.ship = new Ship(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2); + SpawnAsteroids(Level + 3); + } + public int Run() { - + SpawnAsteroids(Level + 3); double dx = 0; DateTime start = DateTime.Now; - SpawnAsteroids(Level + 3); while (true) { start = DateTime.Now; @@ -39,6 +52,7 @@ class Scene var entities = new List(); entities.Add(ship); + entities.Add(ui); entities.AddRange(Shots); entities.AddRange(Asteroids); @@ -47,7 +61,7 @@ class Scene foreach (var entity in entities) { - if (entity is Logic) + if (entity is Logic && running) { ((Logic)entity).Update(keyState, dx); } @@ -62,8 +76,16 @@ class Scene Level++; SpawnAsteroids(Level + 3); } + if (!running) + { + if (keyState.isPressed(Control.RESTART)) + { + this.Restart(); + } + } renderer.Present(); + SDL_Delay(10); DateTime end = DateTime.Now; dx = (end - start).TotalSeconds; } @@ -106,8 +128,7 @@ class Scene public void Loose() { - Console.WriteLine("You loose!"); - Environment.Exit(0); + running = false; } private void pollEvents() diff --git a/src/engine/Renderer.cs b/src/engine/Renderer.cs index 907888f..465b380 100644 --- a/src/engine/Renderer.cs +++ b/src/engine/Renderer.cs @@ -52,4 +52,9 @@ class Renderer { SDL_SetRenderDrawColor(renderer, (byte)r, (byte)g, (byte)b, (byte)a); } + + public IntPtr GetRaw() + { + return renderer; + } } diff --git a/src/entities/Asteroid.cs b/src/entities/Asteroid.cs index 7d7bef1..1284f88 100644 --- a/src/entities/Asteroid.cs +++ b/src/entities/Asteroid.cs @@ -91,7 +91,5 @@ class Asteroid : Renderable, Logic Scene.Instance.AudioPlayer.Play(Sound.EXPLOSION); Scene.Instance.Score += (int)Size; Scene.Instance.Asteroids.Remove(this); - Console.WriteLine("Asteroid destroyed"); - Console.WriteLine("Score: " + Scene.Instance.Score); } } diff --git a/src/entities/Ship.cs b/src/entities/Ship.cs index 39eddda..56be96f 100644 --- a/src/entities/Ship.cs +++ b/src/entities/Ship.cs @@ -1,6 +1,6 @@ class Ship : Logic, Renderable { - const double SPEED = 0.3; + const double SPEED = 3; const double ROTATION_SPEED = 5; double rotation = 0; diff --git a/src/entities/UI.cs b/src/entities/UI.cs new file mode 100644 index 0000000..5197323 --- /dev/null +++ b/src/entities/UI.cs @@ -0,0 +1,56 @@ +using static SDL2.SDL_ttf; +using static SDL2.SDL; +using System.Runtime.InteropServices; + +class UI : Renderable +{ + IntPtr font; + + public UI() + { + TTF_Init(); + this.font = SDL2.SDL_ttf.TTF_OpenFont("/home/gordon/git/dotnet-console/assets/font.ttf", 18); + } + + public void Render(Renderer renderer, double dx) + { + var white = new SDL_Color(); + white.r = 255; + white.g = 255; + white.b = 255; + white.a = 255; + var surfaceMessage = TTF_RenderText_Solid(this.font, "Score: " + Scene.Instance.Score, white); + + var texture = SDL_CreateTextureFromSurface(renderer.GetRaw(), surfaceMessage); + int width; + int height; + + SDL_QueryTexture(texture, out _, out _, out width, out height); + + SDL_Rect rect = new SDL_Rect(); + rect.x = 0; + rect.y = 0; + rect.w = width; + rect.h = height; + + SDL_RenderCopy(renderer.GetRaw(), texture, IntPtr.Zero, ref rect); + + if (!Scene.Instance.running) + { + var surfaceMessage2 = TTF_RenderText_Solid(this.font, "Press r to restart", white); + var texture2 = SDL_CreateTextureFromSurface(renderer.GetRaw(), surfaceMessage2); + int width2; + int height2; + + SDL_QueryTexture(texture2, out _, out _, out width2, out height2); + + SDL_Rect rect2 = new SDL_Rect(); + rect2.x = Scene.SCREEN_WIDTH / 2 - width2 / 2; + rect2.y = Scene.SCREEN_HEIGHT / 2 - height2 / 2; + rect2.w = width2; + rect2.h = height2; + + SDL_RenderCopy(renderer.GetRaw(), texture2, IntPtr.Zero, ref rect2); + } + } +}