diff --git a/RaylibTest/Collider.cs b/RaylibTest/Collider.cs deleted file mode 100644 index 267ed33..0000000 --- a/RaylibTest/Collider.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace RaylibTest; - -public class Collider : Component -{ - - -} \ No newline at end of file diff --git a/RaylibTest/Component.cs b/RaylibTest/Component.cs deleted file mode 100644 index 540ec7c..0000000 --- a/RaylibTest/Component.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace RaylibTest; - -public class Component -{ - -} \ No newline at end of file diff --git a/RaylibTest/Download50608.png b/RaylibTest/Download50608.png deleted file mode 100644 index a65d922..0000000 Binary files a/RaylibTest/Download50608.png and /dev/null differ diff --git a/RaylibTest/Game.cs b/RaylibTest/Game.cs index 03ad966..3f6aaed 100644 --- a/RaylibTest/Game.cs +++ b/RaylibTest/Game.cs @@ -1,36 +1,57 @@ using System.Numerics; using Raylib_cs; +using RaylibTest.Shapes; +using RaylibTest.Support; +using RaylibTest.UiComponents; +using Rectangle = RaylibTest.Shapes.Rectangle; namespace RaylibTest; public class Game : Window { - public Game(int width, int height, string title, int targetFps) : base(width, height, title, targetFps) + public Game(int width, int height, string title, int targetFps, Camera2D camera) : base(width, height, title, targetFps, camera) { } Vector2 ballPosition = new Vector2(100, 100); + UiText _text = new UiText(new Vector2(0,0), "Hello World!", 20, Color.RED); - Sphere sphere = new Sphere(new Vector2(0,0), 20, Color.RED); + Circle circle = new Circle(new Vector2(0,0), 20, Color.RED); + Circle circleD = new Circle(new Vector2(0,0), 2, Color.RED); + Rectangle _cube = new Rectangle(new Vector2(100,100), new Vector2(10,20), Color.BLUE); public override void Start() { ballPosition = new Vector2((float) Width / 2, (float) Height / 2); - RegisterGameObject(sphere); + RegisterGameObject(circle); + RegisterUiObject(_text); + RegisterGameObject(_cube); + RegisterGameObject(circleD); base.Start(); } public override void Update() { base.Update(); - if (Raylib.IsKeyDown(KeyboardKey.KEY_RIGHT)) ballPosition.X += 2.0f; - if (Raylib.IsKeyDown(KeyboardKey.KEY_LEFT)) ballPosition.X -= 2.0f; - if (Raylib.IsKeyDown(KeyboardKey.KEY_UP)) ballPosition.Y -= 2.0f; - if (Raylib.IsKeyDown(KeyboardKey.KEY_DOWN)) ballPosition.Y += 2.0f; + if (!circle.Intersects(_cube)) + { + + } - Raylib.DrawText("move the ball with arrow keys", 10, 10, 20, Color.DARKGRAY); - sphere.Position = ballPosition; + if (Raylib.IsKeyDown(KeyboardKey.KEY_D)) ballPosition.X += 2.0f; + if (Raylib.IsKeyDown(KeyboardKey.KEY_A)) ballPosition.X -= 2.0f; + if (Raylib.IsKeyDown(KeyboardKey.KEY_W)) ballPosition.Y -= 2.0f; + if (Raylib.IsKeyDown(KeyboardKey.KEY_S)) ballPosition.Y += 2.0f; + + double dir = _cube.DirectionTo(circle); + Vector2 dirVec = new Vector2((float) Math.Cos(dir), (float) Math.Sin(dir)); + + circleD.Position = circle.Position + dirVec * circle.Radius; + + circle.Position = ballPosition; + Camera.target = new Vector2(0, 0); Console.Clear(); - Console.WriteLine(@"BallPisiton: {0}",sphere.Position); + Console.WriteLine(@"BallPisiton: {0}",circle.Position); + Console.WriteLine(@"BallDirection: {0}",circleD.Position); } } \ No newline at end of file diff --git a/RaylibTest/Program.cs b/RaylibTest/Program.cs index 3b6ddc5..226cd37 100644 --- a/RaylibTest/Program.cs +++ b/RaylibTest/Program.cs @@ -1,13 +1,18 @@ -using Raylib_cs; +using System.Numerics; +using Raylib_cs; using RaylibTest; -Game game = new Game(1024, 768, "Raylib Game Template", 60); +Game game = new Game(1024, 768, "Raylib Game Template", 60, new Camera2D(new Vector2(0f,0f), new Vector2(0,0), 0f, 1f)); game.Start(); while (!Raylib.WindowShouldClose()) { Raylib.BeginDrawing(); Raylib.ClearBackground(Color.WHITE); - game.Update(); + Raylib.BeginMode2D(game.Camera); + game.Update(); + Raylib.EndMode2D(); + + game.UpdateUi(); Raylib.EndDrawing(); } \ No newline at end of file diff --git a/RaylibTest/RaylibTest.csproj b/RaylibTest/RaylibTest.csproj index 08bc25e..7200af5 100644 --- a/RaylibTest/RaylibTest.csproj +++ b/RaylibTest/RaylibTest.csproj @@ -11,8 +11,4 @@ - - - - diff --git a/RaylibTest/Shapes/Circle.cs b/RaylibTest/Shapes/Circle.cs new file mode 100644 index 0000000..a69e3d1 --- /dev/null +++ b/RaylibTest/Shapes/Circle.cs @@ -0,0 +1,33 @@ +using System.Numerics; +using Raylib_cs; +using RaylibTest.Support; + +namespace RaylibTest.Shapes; + +public class Circle : GameObject +{ + public float Radius { get; set; } + public Color Color { get; set; } + + public Circle(Vector2 position, float radius, Color color) : base(position) + { + Radius = radius; + Color = color; + } + + public override void Draw() + { + base.Draw(); + Raylib.DrawCircleV(Position,Radius, Color); + } + + public bool Intersects(Circle other) + { + return Raylib.CheckCollisionCircles(Position, Radius, other.Position, other.Radius); + } + + public bool Intersects(Rectangle other) + { + return Raylib.CheckCollisionCircleRec(Position, Radius, new Raylib_cs.Rectangle(other.Position.X,other.Position.Y,other.Size.X,other.Size.Y)); + } +} \ No newline at end of file diff --git a/RaylibTest/Shapes/Rectangle.cs b/RaylibTest/Shapes/Rectangle.cs new file mode 100644 index 0000000..0d1b37e --- /dev/null +++ b/RaylibTest/Shapes/Rectangle.cs @@ -0,0 +1,63 @@ +using System.Numerics; +using Raylib_cs; +using RaylibTest.Support; + +namespace RaylibTest.Shapes; + +public class Rectangle : GameObject +{ + public Vector2 Size; + public Color Color; + + public Rectangle(Vector2 position, Vector2 size, Color color) : base(position) + { + Size = size; + Color = color; + } + + public override void Draw() + { + base.Draw(); + Raylib.DrawRectangleV(Position, Size, Color); + } + + public bool Intersects(Rectangle other) + { + return Raylib.CheckCollisionRecs( + new Raylib_cs.Rectangle(Position.X,Position.Y,Size.X,Size.Y), + new Raylib_cs.Rectangle(other.Position.X,other.Position.Y,other.Size.X,other.Size.Y)); + } + + public bool Intersects(Circle other) + { + return Raylib.CheckCollisionCircleRec( + other.Position, other.Radius, + new Raylib_cs.Rectangle(Position.X,Position.Y,Size.X,Size.Y)); + } + + public double DirectionTo(Rectangle other) + { + Vector2 otherPosition = other.Position; + Vector2 position = Position; + + //Calculate the direction from position to otherPosition + return Math.Tan((position.Y / position.X)); + + } + + public double DirectionTo(Circle other) + { + Vector2 otherPosition = other.Position; + Vector2 position = Position; + + + //α = arccos[(xa * xb + ya * yb) / (√(xa2 + ya2) * √(xb2 + yb2))] + + + return Math.Atan((position.X * otherPosition.X + position.Y * otherPosition.Y) / + (Math.Sqrt(Math.Pow(position.X, 2) + Math.Pow(position.Y, 2)) * + Math.Sqrt(Math.Pow(other.Position.X, 2) + Math.Pow(other.Position.Y, 2)))); + + } + +} \ No newline at end of file diff --git a/RaylibTest/Sphere.cs b/RaylibTest/Sphere.cs deleted file mode 100644 index 5673d11..0000000 --- a/RaylibTest/Sphere.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.Numerics; -using Raylib_cs; - -namespace RaylibTest; - -public class Sphere : GameObject -{ - public float Radius { get; set; } - public Color Color { get; set; } - - public Sphere(Vector2 position, float radius, Color color) : base(position) - { - Radius = radius; - Color = color; - } - - public override void Draw() - { - base.Draw(); - Raylib.DrawCircleV(Position,Radius, Color); - } -} \ No newline at end of file diff --git a/RaylibTest/Sprites/Download50608.png b/RaylibTest/Sprites/Download50608.png deleted file mode 100644 index 8304443..0000000 Binary files a/RaylibTest/Sprites/Download50608.png and /dev/null differ diff --git a/RaylibTest/GameObject.cs b/RaylibTest/Support/GameObject.cs similarity index 87% rename from RaylibTest/GameObject.cs rename to RaylibTest/Support/GameObject.cs index c4bb94e..6c1199f 100644 --- a/RaylibTest/GameObject.cs +++ b/RaylibTest/Support/GameObject.cs @@ -1,6 +1,6 @@ using System.Numerics; -namespace RaylibTest; +namespace RaylibTest.Support; public class GameObject { diff --git a/RaylibTest/Support/UiObject.cs b/RaylibTest/Support/UiObject.cs new file mode 100644 index 0000000..8bc200f --- /dev/null +++ b/RaylibTest/Support/UiObject.cs @@ -0,0 +1,16 @@ +using System.Numerics; + +namespace RaylibTest.Support; + +public class UiObject +{ + public Vector2 Position; + public UiObject(Vector2 position) + { + Position = position; + } + public virtual void Draw() + { + + } +} \ No newline at end of file diff --git a/RaylibTest/Window.cs b/RaylibTest/Support/Window.cs similarity index 58% rename from RaylibTest/Window.cs rename to RaylibTest/Support/Window.cs index 83d666e..aeacfd4 100644 --- a/RaylibTest/Window.cs +++ b/RaylibTest/Support/Window.cs @@ -1,7 +1,8 @@ using System.Numerics; using Raylib_cs; +using RaylibTest.Support; -namespace RaylibTest; +namespace RaylibTest.Support; public class Window { @@ -9,15 +10,19 @@ public class Window protected int Height { get; set; } protected string Title { get; set; } protected int TargetFps { get; set; } + + public Camera2D Camera; private List gameObjects = new List(); + private List uiObjects = new List(); - public Window(int width, int height, string title, int targetFps) + public Window(int width, int height, string title, int targetFps, Camera2D camera) { Width = width; Height = height; Title = title; TargetFps = targetFps; + Camera = camera; } public virtual void Start() @@ -25,29 +30,40 @@ public class Window Raylib.InitWindow(Width, Height, Title); Raylib.SetTargetFPS(TargetFps); Raylib.SetWindowMinSize(Width, Height); - while (!Raylib.WindowShouldClose()) - { - Raylib.BeginDrawing(); - Raylib.ClearBackground(Color.WHITE); - Update(); - Raylib.EndDrawing(); - } } public virtual void Update() { - foreach (var gameObject in gameObjects) + Parallel.ForEach(gameObjects, gameObject => { gameObject.Draw(); - } + }); + } + + public virtual void UpdateUi() + { + Parallel.ForEach(uiObjects, uiObject => + { + uiObject.Draw(); + }); } public virtual void RegisterGameObject(GameObject gameObject) { gameObjects.Add(gameObject); } + + public virtual void UnregisterGameObject(GameObject gameObject) + { + gameObjects.Remove(gameObject); + } + + public virtual void RegisterUiObject(UiObject uiObject) + { + uiObjects.Add(uiObject); + } - public virtual void Stop() + public virtual void Close() { Raylib.CloseWindow(); } diff --git a/RaylibTest/UiComponents/UiText.cs b/RaylibTest/UiComponents/UiText.cs new file mode 100644 index 0000000..f4d8a8f --- /dev/null +++ b/RaylibTest/UiComponents/UiText.cs @@ -0,0 +1,25 @@ +using System.Numerics; +using Raylib_cs; +using RaylibTest.Support; + +namespace RaylibTest.UiComponents; + +public class UiText : UiObject +{ + public string Text; + public int FontSize; + public Color Color; + + public UiText(Vector2 position, string text, int fontSize, Color color) : base(position) + { + Text = text; + FontSize = fontSize; + Color = color; + } + + public override void Draw() + { + base.Draw(); + Raylib.DrawText(Text, (int)Position.X, (int)Position.Y, FontSize, Color); + } +} \ No newline at end of file diff --git a/RaylibTest/bin/Debug/net6.0/RaylibTest.dll b/RaylibTest/bin/Debug/net6.0/RaylibTest.dll index a3b7635..1ba1464 100644 Binary files a/RaylibTest/bin/Debug/net6.0/RaylibTest.dll and b/RaylibTest/bin/Debug/net6.0/RaylibTest.dll differ diff --git a/RaylibTest/bin/Debug/net6.0/RaylibTest.pdb b/RaylibTest/bin/Debug/net6.0/RaylibTest.pdb index 238573a..c5cf15a 100644 Binary files a/RaylibTest/bin/Debug/net6.0/RaylibTest.pdb and b/RaylibTest/bin/Debug/net6.0/RaylibTest.pdb differ diff --git a/RaylibTest/obj/Debug/net6.0/RaylibTest.csproj.CoreCompileInputs.cache b/RaylibTest/obj/Debug/net6.0/RaylibTest.csproj.CoreCompileInputs.cache index 7fa5732..32c6cbb 100644 --- a/RaylibTest/obj/Debug/net6.0/RaylibTest.csproj.CoreCompileInputs.cache +++ b/RaylibTest/obj/Debug/net6.0/RaylibTest.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -a2d1cedbc5ed7528cb3f4a0e5feb8a12993c50f2 +6eb834945136a68f0e375e88a382ef0898f0909d diff --git a/RaylibTest/obj/Debug/net6.0/RaylibTest.dll b/RaylibTest/obj/Debug/net6.0/RaylibTest.dll index a3b7635..1ba1464 100644 Binary files a/RaylibTest/obj/Debug/net6.0/RaylibTest.dll and b/RaylibTest/obj/Debug/net6.0/RaylibTest.dll differ diff --git a/RaylibTest/obj/Debug/net6.0/RaylibTest.pdb b/RaylibTest/obj/Debug/net6.0/RaylibTest.pdb index 238573a..c5cf15a 100644 Binary files a/RaylibTest/obj/Debug/net6.0/RaylibTest.pdb and b/RaylibTest/obj/Debug/net6.0/RaylibTest.pdb differ diff --git a/RaylibTest/obj/Debug/net6.0/ref/RaylibTest.dll b/RaylibTest/obj/Debug/net6.0/ref/RaylibTest.dll index 40911ce..db5dc23 100644 Binary files a/RaylibTest/obj/Debug/net6.0/ref/RaylibTest.dll and b/RaylibTest/obj/Debug/net6.0/ref/RaylibTest.dll differ diff --git a/RaylibTest/obj/Debug/net6.0/refint/RaylibTest.dll b/RaylibTest/obj/Debug/net6.0/refint/RaylibTest.dll index 40911ce..db5dc23 100644 Binary files a/RaylibTest/obj/Debug/net6.0/refint/RaylibTest.dll and b/RaylibTest/obj/Debug/net6.0/refint/RaylibTest.dll differ