first commit

This commit is contained in:
MasterGordon 2022-10-03 23:13:19 +02:00
commit 43433bb405
47 changed files with 926 additions and 0 deletions

6
Program.cs Normal file
View File

@ -0,0 +1,6 @@
// See https://aka.ms/new-console-template for more information
var window = new Window("Hello World!", 800, 600);
var renderer = new Renderer(window);
var scene = new Scene(renderer);
scene.Run();

BIN
bin/Debug/net6.0/SDL2-CS.dll Executable file

Binary file not shown.

BIN
bin/Debug/net6.0/dotnet-console Executable file

Binary file not shown.

View File

@ -0,0 +1,78 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v6.0",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v6.0": {
"dotnet-console/1.0.0": {
"dependencies": {
"ppy.SDL2-CS": "1.0.596-alpha"
},
"runtime": {
"dotnet-console.dll": {}
}
},
"ppy.SDL2-CS/1.0.596-alpha": {
"runtime": {
"lib/netstandard2.0/SDL2-CS.dll": {
"assemblyVersion": "0.0.0.0",
"fileVersion": "0.0.0.0"
}
},
"runtimeTargets": {
"runtimes/linux-x64/native/libSDL2.so": {
"rid": "linux-x64",
"assetType": "native",
"fileVersion": "0.0.0.0"
},
"runtimes/linux-x86/native/libSDL2.so": {
"rid": "linux-x86",
"assetType": "native",
"fileVersion": "0.0.0.0"
},
"runtimes/osx-arm64/native/libSDL2.dylib": {
"rid": "osx-arm64",
"assetType": "native",
"fileVersion": "0.0.0.0"
},
"runtimes/osx-x64/native/libSDL2.dylib": {
"rid": "osx-x64",
"assetType": "native",
"fileVersion": "0.0.0.0"
},
"runtimes/win-arm64/native/SDL2.dll": {
"rid": "win-arm64",
"assetType": "native",
"fileVersion": "0.0.0.0"
},
"runtimes/win-x64/native/SDL2.dll": {
"rid": "win-x64",
"assetType": "native",
"fileVersion": "0.0.0.0"
},
"runtimes/win-x86/native/SDL2.dll": {
"rid": "win-x86",
"assetType": "native",
"fileVersion": "0.0.0.0"
}
}
}
}
},
"libraries": {
"dotnet-console/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
},
"ppy.SDL2-CS/1.0.596-alpha": {
"type": "package",
"serviceable": true,
"sha512": "sha512-yzIdTCjPp9le6J16WyG4py8frdwUTcsnqpGnuAk7Za2pvXGPmvVAiQBrdnFS58l2S6bS8AFNIG3uOSRQrMo1nQ==",
"path": "ppy.sdl2-cs/1.0.596-alpha",
"hashPath": "ppy.sdl2-cs.1.0.596-alpha.nupkg.sha512"
}
}
}

Binary file not shown.

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<dllmap dll="SDL2" os="windows" target="SDL2.dll"/>
<dllmap dll="SDL2" os="osx" target="libSDL2.dylib"/>
<dllmap dll="SDL2" os="linux" target="libSDL2-2.0.so.0"/>
</configuration>

Binary file not shown.

View File

@ -0,0 +1,9 @@
{
"runtimeOptions": {
"tfm": "net6.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "6.0.0"
}
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

15
dotnet-console.csproj Normal file
View File

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>dotnet_console</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ppy.SDL2-CS" Version="1.0.596-alpha" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,4 @@
// <autogenerated />
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v6.0", FrameworkDisplayName = "")]

BIN
obj/Debug/net6.0/apphost Executable file

Binary file not shown.

View File

@ -0,0 +1,22 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("dotnet-console")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyProductAttribute("dotnet-console")]
[assembly: System.Reflection.AssemblyTitleAttribute("dotnet-console")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// Generated by the MSBuild WriteCodeFragment class.

View File

@ -0,0 +1 @@
5a44bd4435b7014cf09d126ef56021bc8e8fc082

View File

@ -0,0 +1,10 @@
is_global = true
build_property.TargetFramework = net6.0
build_property.TargetPlatformMinVersion =
build_property.UsingMicrosoftNETSdkWeb =
build_property.ProjectTypeGuids =
build_property.InvariantGlobalization =
build_property.PlatformNeutralAssembly =
build_property._SupportedPlatformList = Linux,macOS,Windows
build_property.RootNamespace = dotnet_console
build_property.ProjectDir = /home/gordon/git/dotnet-console/

View File

@ -0,0 +1,8 @@
// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;

Binary file not shown.

View File

@ -0,0 +1 @@
1430158ffd0195d00b80893ad9906b7cccc4512f

View File

@ -0,0 +1,25 @@
/home/gordon/git/dotnet-console/bin/Debug/net6.0/dotnet-console
/home/gordon/git/dotnet-console/bin/Debug/net6.0/dotnet-console.deps.json
/home/gordon/git/dotnet-console/bin/Debug/net6.0/dotnet-console.runtimeconfig.json
/home/gordon/git/dotnet-console/bin/Debug/net6.0/dotnet-console.dll
/home/gordon/git/dotnet-console/bin/Debug/net6.0/dotnet-console.pdb
/home/gordon/git/dotnet-console/obj/Debug/net6.0/dotnet-console.csproj.AssemblyReference.cache
/home/gordon/git/dotnet-console/obj/Debug/net6.0/dotnet-console.GeneratedMSBuildEditorConfig.editorconfig
/home/gordon/git/dotnet-console/obj/Debug/net6.0/dotnet-console.AssemblyInfoInputs.cache
/home/gordon/git/dotnet-console/obj/Debug/net6.0/dotnet-console.AssemblyInfo.cs
/home/gordon/git/dotnet-console/obj/Debug/net6.0/dotnet-console.csproj.CoreCompileInputs.cache
/home/gordon/git/dotnet-console/obj/Debug/net6.0/dotnet-console.dll
/home/gordon/git/dotnet-console/obj/Debug/net6.0/refint/dotnet-console.dll
/home/gordon/git/dotnet-console/obj/Debug/net6.0/dotnet-console.pdb
/home/gordon/git/dotnet-console/obj/Debug/net6.0/dotnet-console.genruntimeconfig.cache
/home/gordon/git/dotnet-console/obj/Debug/net6.0/ref/dotnet-console.dll
/home/gordon/git/dotnet-console/bin/Debug/net6.0/dotnet-console.dll.config
/home/gordon/git/dotnet-console/bin/Debug/net6.0/SDL2-CS.dll
/home/gordon/git/dotnet-console/bin/Debug/net6.0/runtimes/linux-x64/native/libSDL2.so
/home/gordon/git/dotnet-console/bin/Debug/net6.0/runtimes/linux-x86/native/libSDL2.so
/home/gordon/git/dotnet-console/bin/Debug/net6.0/runtimes/osx-arm64/native/libSDL2.dylib
/home/gordon/git/dotnet-console/bin/Debug/net6.0/runtimes/osx-x64/native/libSDL2.dylib
/home/gordon/git/dotnet-console/bin/Debug/net6.0/runtimes/win-arm64/native/SDL2.dll
/home/gordon/git/dotnet-console/bin/Debug/net6.0/runtimes/win-x64/native/SDL2.dll
/home/gordon/git/dotnet-console/bin/Debug/net6.0/runtimes/win-x86/native/SDL2.dll
/home/gordon/git/dotnet-console/obj/Debug/net6.0/dotnet-console.csproj.CopyComplete

Binary file not shown.

View File

@ -0,0 +1 @@
6e47d573ce23f499195def0267a82bede4ac78dc

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,73 @@
{
"format": 1,
"restore": {
"/home/gordon/git/dotnet-console/dotnet-console.csproj": {}
},
"projects": {
"/home/gordon/git/dotnet-console/dotnet-console.csproj": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "/home/gordon/git/dotnet-console/dotnet-console.csproj",
"projectName": "dotnet-console",
"projectPath": "/home/gordon/git/dotnet-console/dotnet-console.csproj",
"packagesPath": "/home/gordon/.nuget/packages/",
"outputPath": "/home/gordon/git/dotnet-console/obj/",
"projectStyle": "PackageReference",
"configFilePaths": [
"/home/gordon/.nuget/NuGet/NuGet.Config"
],
"originalTargetFrameworks": [
"net6.0"
],
"sources": {
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net6.0": {
"targetAlias": "net6.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
}
},
"frameworks": {
"net6.0": {
"targetAlias": "net6.0",
"dependencies": {
"ppy.SDL2-CS": {
"target": "Package",
"version": "[1.0.596-alpha, )"
}
},
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"downloadDependencies": [
{
"name": "Microsoft.AspNetCore.App.Ref",
"version": "[6.0.9, 6.0.9]"
}
],
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/6.0.401/RuntimeIdentifierGraph.json"
}
}
}
}
}

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">/home/gordon/.nuget/packages/</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">/home/gordon/.nuget/packages/</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.3.0</NuGetToolVersion>
</PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<SourceRoot Include="/home/gordon/.nuget/packages/" />
</ItemGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<Content Include="$(NuGetPackageRoot)ppy.sdl2-cs/1.0.596-alpha/contentFiles/any/netstandard2.0/app.config" Condition="Exists('$(NuGetPackageRoot)ppy.sdl2-cs/1.0.596-alpha/contentFiles/any/netstandard2.0/app.config')">
<NuGetPackageId>ppy.SDL2-CS</NuGetPackageId>
<NuGetPackageVersion>1.0.596-alpha</NuGetPackageVersion>
<NuGetItemType>Content</NuGetItemType>
<Pack>false</Pack>
<Private>False</Private>
<Link>app.config</Link>
</Content>
</ItemGroup>
</Project>

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />

149
obj/project.assets.json Normal file
View File

@ -0,0 +1,149 @@
{
"version": 3,
"targets": {
"net6.0": {
"ppy.SDL2-CS/1.0.596-alpha": {
"type": "package",
"compile": {
"lib/netstandard2.0/SDL2-CS.dll": {}
},
"runtime": {
"lib/netstandard2.0/SDL2-CS.dll": {}
},
"contentFiles": {
"contentFiles/any/netstandard2.0/app.config": {
"buildAction": "Content",
"codeLanguage": "any",
"copyToOutput": false
}
},
"runtimeTargets": {
"runtimes/linux-x64/native/libSDL2.so": {
"assetType": "native",
"rid": "linux-x64"
},
"runtimes/linux-x86/native/libSDL2.so": {
"assetType": "native",
"rid": "linux-x86"
},
"runtimes/osx-arm64/native/libSDL2.dylib": {
"assetType": "native",
"rid": "osx-arm64"
},
"runtimes/osx-x64/native/libSDL2.dylib": {
"assetType": "native",
"rid": "osx-x64"
},
"runtimes/win-arm64/native/SDL2.dll": {
"assetType": "native",
"rid": "win-arm64"
},
"runtimes/win-x64/native/SDL2.dll": {
"assetType": "native",
"rid": "win-x64"
},
"runtimes/win-x86/native/SDL2.dll": {
"assetType": "native",
"rid": "win-x86"
}
}
}
}
},
"libraries": {
"ppy.SDL2-CS/1.0.596-alpha": {
"sha512": "yzIdTCjPp9le6J16WyG4py8frdwUTcsnqpGnuAk7Za2pvXGPmvVAiQBrdnFS58l2S6bS8AFNIG3uOSRQrMo1nQ==",
"type": "package",
"path": "ppy.sdl2-cs/1.0.596-alpha",
"files": [
".nupkg.metadata",
".signature.p7s",
"content/app.config",
"contentFiles/any/netstandard2.0/app.config",
"lib/netstandard2.0/SDL2-CS.dll",
"ppy.sdl2-cs.1.0.596-alpha.nupkg.sha512",
"ppy.sdl2-cs.nuspec",
"runtimes/linux-x64/native/libSDL2.so",
"runtimes/linux-x86/native/libSDL2.so",
"runtimes/osx-arm64/native/libSDL2.dylib",
"runtimes/osx-x64/native/libSDL2.dylib",
"runtimes/win-arm64/native/SDL2.dll",
"runtimes/win-x64/native/SDL2.dll",
"runtimes/win-x86/native/SDL2.dll"
]
}
},
"projectFileDependencyGroups": {
"net6.0": [
"ppy.SDL2-CS >= 1.0.596-alpha"
]
},
"packageFolders": {
"/home/gordon/.nuget/packages/": {}
},
"project": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "/home/gordon/git/dotnet-console/dotnet-console.csproj",
"projectName": "dotnet-console",
"projectPath": "/home/gordon/git/dotnet-console/dotnet-console.csproj",
"packagesPath": "/home/gordon/.nuget/packages/",
"outputPath": "/home/gordon/git/dotnet-console/obj/",
"projectStyle": "PackageReference",
"configFilePaths": [
"/home/gordon/.nuget/NuGet/NuGet.Config"
],
"originalTargetFrameworks": [
"net6.0"
],
"sources": {
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net6.0": {
"targetAlias": "net6.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
}
},
"frameworks": {
"net6.0": {
"targetAlias": "net6.0",
"dependencies": {
"ppy.SDL2-CS": {
"target": "Package",
"version": "[1.0.596-alpha, )"
}
},
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"downloadDependencies": [
{
"name": "Microsoft.AspNetCore.App.Ref",
"version": "[6.0.9, 6.0.9]"
}
],
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "/usr/share/dotnet/sdk/6.0.401/RuntimeIdentifierGraph.json"
}
}
}
}

11
obj/project.nuget.cache Normal file
View File

@ -0,0 +1,11 @@
{
"version": 2,
"dgSpecHash": "Z+HnQHJqEH3pQORn3oK/9WhS17M4sshlWSQ2haefUhu7xmUXe4i6NFbFXNMZCMcHlSu1wQ2wELP7qH7RGQ0Smw==",
"success": true,
"projectFilePath": "/home/gordon/git/dotnet-console/dotnet-console.csproj",
"expectedPackageFiles": [
"/home/gordon/.nuget/packages/ppy.sdl2-cs/1.0.596-alpha/ppy.sdl2-cs.1.0.596-alpha.nupkg.sha512",
"/home/gordon/.nuget/packages/microsoft.aspnetcore.app.ref/6.0.9/microsoft.aspnetcore.app.ref.6.0.9.nupkg.sha512"
],
"logs": []
}

139
src/Scene.cs Normal file
View File

@ -0,0 +1,139 @@
using static SDL2.SDL;
class Scene
{
public const int SCREEN_WIDTH = 800;
public const int SCREEN_HEIGHT = 600;
private Ship ship;
private Renderer renderer;
private static Scene? instance;
public HashSet<Shot> Shots;
public HashSet<Asteroid> Asteroids;
public int Score = 0;
public int Level = 1;
public Scene(Renderer renderer)
{
this.ship = new Ship(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2);
this.Shots = new HashSet<Shot>();
this.Asteroids = new HashSet<Asteroid>();
this.renderer = renderer;
instance = this;
}
public int Run()
{
double dx = 0;
DateTime start = DateTime.Now;
SpawnAsteroids(Level + 3);
while (true)
{
start = DateTime.Now;
pollEvents();
var entities = new List<Object>();
entities.Add(ship);
entities.AddRange(Shots);
entities.AddRange(Asteroids);
var keyState = new KeyState();
renderer.Clear();
foreach (var entity in entities)
{
if (entity is Logic)
{
((Logic)entity).Update(keyState, dx);
}
if (entity is Renderable)
{
((Renderable)entity).Render(renderer, dx);
}
}
if (Asteroids.Count == 0)
{
Level++;
SpawnAsteroids(Level + 3);
}
renderer.Present();
DateTime end = DateTime.Now;
dx = (end - start).TotalSeconds;
}
}
public void SpawnAsteroids(int count)
{
for (int i = 0; i < count; i++)
{
SpawnAsteroid();
}
}
public void SpawnAsteroid()
{
var edge = new Random().Next(0, 4);
var x = 0;
var y = 0;
switch (edge)
{
case 0:
x = 0;
y = new Random().Next(0, SCREEN_HEIGHT);
break;
case 1:
x = SCREEN_WIDTH;
y = new Random().Next(0, SCREEN_HEIGHT);
break;
case 2:
x = new Random().Next(0, SCREEN_WIDTH);
y = 0;
break;
case 3:
x = new Random().Next(0, SCREEN_WIDTH);
y = SCREEN_HEIGHT;
break;
}
Asteroids.Add(new Asteroid(x, y, new Random().Next(0, 200) / 100.0 * Math.PI, Size.Large));
}
public void Loose()
{
Console.WriteLine("You loose!");
Environment.Exit(0);
}
private void pollEvents()
{
SDL_Event e;
while (SDL_PollEvent(out e) != 0)
{
if (e.type == SDL_EventType.SDL_QUIT)
{
System.Environment.Exit(0);
}
if (e.type == SDL_EventType.SDL_KEYDOWN)
{
if (e.key.keysym.sym == SDL_Keycode.SDLK_ESCAPE)
{
System.Environment.Exit(0);
}
}
}
}
public static Scene Instance
{
get
{
if (instance == null)
{
throw new Exception("Scene not initialized");
}
return instance;
}
}
}

20
src/engine/KeyState.cs Normal file
View File

@ -0,0 +1,20 @@
using System.Runtime.InteropServices;
using static SDL2.SDL;
class KeyState
{
private byte[] keys;
public KeyState()
{
var origArray = SDL_GetKeyboardState(out var arraySize);
this.keys = new byte[arraySize];
Marshal.Copy(origArray, keys, 0, arraySize);
}
public bool isPressed(SDL_Keycode keycode)
{
byte scanCode = (byte)SDL_GetScancodeFromKey(keycode);
return (this.keys[scanCode] == 1);
}
}

55
src/engine/Renderer.cs Normal file
View File

@ -0,0 +1,55 @@
using static SDL2.SDL;
class Renderer
{
IntPtr renderer;
public Renderer(Window window)
{
this.renderer = SDL_CreateRenderer(window.GetWindow(), -1, SDL_RendererFlags.SDL_RENDERER_ACCELERATED);
}
public void Clear()
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
}
public void Present()
{
SDL_RenderPresent(renderer);
}
public void DrawRect(double x, double y, int w, int h)
{
this.DrawRect((int)x, (int)y, w, h);
}
public void DrawRect(int x, int y, int w, int h)
{
SDL_Rect rect = new SDL_Rect();
rect.x = x;
rect.y = y;
rect.w = w;
rect.h = h;
SDL_RenderFillRect(renderer, ref rect);
}
public void DrawLines(double[][] points)
{
SDL_Point[] sdlPoints = new SDL_Point[points.Length];
for (int i = 0; i < points.Length; i++)
{
sdlPoints[i].x = (int)points[i][0];
sdlPoints[i].y = (int)points[i][1];
}
SDL_RenderDrawLines(renderer, sdlPoints, points.Length);
}
public void setColor(int r, int g, int b, int a = 255)
{
SDL_SetRenderDrawColor(renderer, (byte)r, (byte)g, (byte)b, (byte)a);
}
}

26
src/engine/Window.cs Normal file
View File

@ -0,0 +1,26 @@
using static SDL2.SDL;
class Window
{
IntPtr window;
public Window(string title, int w, int h)
{
window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WindowFlags.SDL_WINDOW_VULKAN);
}
public Window(string title, int x, int y, int w, int h, SDL_WindowFlags flags)
{
this.window = SDL_CreateWindow(title, x, y, w, h, flags);
}
public void Dispose()
{
SDL_DestroyWindow(this.window);
}
public IntPtr GetWindow()
{
return this.window;
}
}

View File

@ -0,0 +1,4 @@
interface Logic
{
public void Update(KeyState keyState, double dx);
}

View File

@ -0,0 +1,4 @@
interface Renderable
{
public void Render(Renderer renderer, double dx);
}

View File

@ -0,0 +1,7 @@
class Point
{
public static double Distance(double x1, double y1, double x2, double y2)
{
return Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2));
}
}

96
src/entities/Asteroid.cs Normal file
View File

@ -0,0 +1,96 @@
enum Size : int
{
Large = 60,
Medium = 30,
Small = 20
}
class Asteroid : Renderable, Logic
{
const double SPEED = 100;
private double dx, dy;
public double X, Y;
public Size Size;
private double[][] shape;
public Asteroid(double x, double y, double rotation, Size size)
{
this.X = x;
this.Y = y;
this.Size = size;
dx = Math.Cos(rotation) * SPEED;
dy = Math.Sin(rotation) * SPEED;
var random = new Random();
shape = new double[16][];
for (int i = 0; i < shape.Length; i++)
{
var rad = i * Math.PI * 2 / shape.Length;
shape[i] = new double[2];
shape[i][0] = Math.Cos(rad) * (0.5 + random.NextDouble() / 2) * (int)size / 2;
shape[i][1] = Math.Sin(rad) * (0.5 + random.NextDouble() / 2) * (int)size / 2;
}
}
public void Render(Renderer renderer, double dx)
{
renderer.setColor(255, 255, 255);
// renderer.DrawRect(X - (int)Size / 2, Y - (int)Size / 2, (int)Size, (int)Size);
double[][] drawLines = new double[shape.Length + 1][];
for (int i = 0; i < shape.Length; i++)
{
drawLines[i] = new double[2];
drawLines[i][0] = shape[i][0] + X - (int)Size / 2;
drawLines[i][1] = shape[i][1] + Y - (int)Size / 2;
}
drawLines[drawLines.Length - 1] = new double[2];
drawLines[drawLines.Length - 1][0] = shape[0][0] + X - (int)Size / 2;
drawLines[drawLines.Length - 1][1] = shape[0][1] + Y - (int)Size / 2;
renderer.DrawLines(drawLines);
}
public void Update(KeyState keyState, double dx)
{
X += dx * this.dx;
Y += dx * this.dy;
this.X = this.X % Scene.SCREEN_WIDTH;
this.Y = this.Y % Scene.SCREEN_HEIGHT;
if (this.X < 0)
{
this.X += Scene.SCREEN_WIDTH;
}
if (this.Y < 0)
{
this.Y += Scene.SCREEN_HEIGHT;
}
foreach (var shot in Scene.Instance.Shots)
{
if (Point.Distance(shot.X, shot.Y, X, Y) < (int)Size / 2)
{
shot.Destroy();
this.Destroy();
var rotation = Math.Atan2(Y - shot.Y, X - shot.X);
if (Size == Size.Large)
{
Scene.Instance.Asteroids.Add(new Asteroid(X, Y, rotation + Math.PI * 2, Size.Medium));
Scene.Instance.Asteroids.Add(new Asteroid(X, Y, rotation + Math.PI * 2 * 0.3, Size.Medium));
Scene.Instance.Asteroids.Add(new Asteroid(X, Y, rotation + Math.PI * 2 * 0.6, Size.Medium));
}
if (Size == Size.Medium)
{
Scene.Instance.Asteroids.Add(new Asteroid(X, Y, rotation + Math.PI * 2 * 0.3, Size.Small));
Scene.Instance.Asteroids.Add(new Asteroid(X, Y, rotation + Math.PI * 2 * 0.6, Size.Small));
}
break;
}
}
}
public void Destroy()
{
Scene.Instance.Score += (int)Size;
Scene.Instance.Asteroids.Remove(this);
Console.WriteLine("Asteroid destroyed");
Console.WriteLine("Score: " + Scene.Instance.Score);
}
}

94
src/entities/Ship.cs Normal file
View File

@ -0,0 +1,94 @@
using static SDL2.SDL;
class Ship : Logic, Renderable
{
const double SPEED = 0.3;
const double ROTATION_SPEED = 5;
double rotation = 0;
bool thrust = false;
private double dx, dy = 0;
double x;
double y;
DateTime lastShot = DateTime.Now;
public Ship(double x, double y)
{
this.x = x;
this.y = y;
}
public void Update(KeyState keyState, double dt)
{
thrust = false;
if (keyState.isPressed(SDL_Keycode.SDLK_LEFT))
{
rotation -= ROTATION_SPEED * dt;
}
if (keyState.isPressed(SDL_Keycode.SDLK_RIGHT))
{
rotation += ROTATION_SPEED * dt;
}
if (keyState.isPressed(SDL_Keycode.SDLK_UP))
{
thrust = true;
this.dx += Math.Cos(rotation) * SPEED * dt;
this.dy += Math.Sin(rotation) * SPEED * dt;
}
if (keyState.isPressed(SDL_Keycode.SDLK_SPACE) && (DateTime.Now - lastShot).TotalMilliseconds > 200)
{
lastShot = DateTime.Now;
var shot = new Shot(x, y, rotation);
Scene.Instance.Shots.Add(shot);
}
rotation = rotation % (2 * Math.PI);
if (rotation < 0)
{
rotation += 2 * Math.PI;
}
this.x += this.dx;
this.y += this.dy;
this.dx *= 0.9998;
this.dy *= 0.9998;
this.x = this.x % Scene.SCREEN_WIDTH;
this.y = this.y % Scene.SCREEN_HEIGHT;
if (this.x < 0)
{
this.x += Scene.SCREEN_WIDTH;
}
if (this.y < 0)
{
this.y += Scene.SCREEN_HEIGHT;
}
foreach (var asteroid in Scene.Instance.Asteroids)
{
if (Point.Distance(x, y, asteroid.X, asteroid.Y) < (int)asteroid.Size / 2)
{
Scene.Instance.Loose();
}
}
}
public void Render(Renderer renderer, double dx)
{
renderer.setColor(255, 255, 255);
renderer.DrawLines(new double[][] {
new double[]{x + Math.Cos(rotation) * 10, y + Math.Sin(rotation) * 10},
new double[]{x + Math.Cos(rotation + Math.PI * 2 / 3) * 10, y + Math.Sin(rotation + Math.PI * 2 / 3) * 10},
new double[]{x + Math.Cos(rotation + Math.PI * 4 / 3) * 10, y + Math.Sin(rotation + Math.PI * 4 / 3) * 10},
new double[]{x + Math.Cos(rotation) * 10, y + Math.Sin(rotation) * 10}
});
if (thrust)
{
renderer.setColor(255, 0, 0);
// Draw flame behind ship
renderer.DrawLines(new double[][] {
new double[]{x + Math.Cos(rotation + Math.PI * 2 / 3) * 10, y + Math.Sin(rotation + Math.PI * 2 / 3) * 10},
new double[]{x + Math.Cos(rotation + Math.PI * 2 / 3) * 10 + Math.Cos(rotation + Math.PI) * 10, y + Math.Sin(rotation + Math.PI * 2 / 3) * 10 + Math.Sin(rotation + Math.PI) * 10},
new double[]{x + Math.Cos(rotation + Math.PI * 4 / 3) * 10, y + Math.Sin(rotation + Math.PI * 4 / 3) * 10},
new double[]{x + Math.Cos(rotation + Math.PI * 2 / 3) * 10, y + Math.Sin(rotation + Math.PI * 2 / 3) * 10}
});
}
}
}

35
src/entities/Shot.cs Normal file
View File

@ -0,0 +1,35 @@
class Shot : Renderable, Logic
{
const double SPEED = 300;
private double dx, dy;
public double X, Y;
public Shot(double x, double y, double rotation)
{
this.X = x;
this.Y = y;
dx = Math.Cos(rotation) * SPEED;
dy = Math.Sin(rotation) * SPEED;
}
public void Render(Renderer renderer, double dx)
{
renderer.setColor(255, 255, 255);
renderer.DrawRect(X, Y, 2, 2);
}
public void Update(KeyState keyState, double dx)
{
X += dx * this.dx;
Y += dx * this.dy;
if (X < 0 || X > Scene.SCREEN_WIDTH || Y < 0 || Y > Scene.SCREEN_HEIGHT)
{
Destroy();
}
}
public void Destroy()
{
Scene.Instance.Shots.Remove(this);
}
}