This commit is contained in:
MasterGordon 2022-10-05 15:15:59 +02:00
parent fb37b70772
commit 99a3abd0f1
8 changed files with 95 additions and 12 deletions

BIN
assets/font.ttf Normal file

Binary file not shown.

BIN
assets/roboto.ttf Normal file

Binary file not shown.

View File

@ -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");
}

View File

@ -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<Shot>();
this.Asteroids = new HashSet<Asteroid>();
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<Object>();
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()

View File

@ -52,4 +52,9 @@ class Renderer
{
SDL_SetRenderDrawColor(renderer, (byte)r, (byte)g, (byte)b, (byte)a);
}
public IntPtr GetRaw()
{
return renderer;
}
}

View File

@ -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);
}
}

View File

@ -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;

56
src/entities/UI.cs Normal file
View File

@ -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);
}
}
}