This commit is contained in:
Elias Stepanik 2022-07-19 23:37:54 +02:00
parent 4bbfcec9c9
commit 526c9a3973
21 changed files with 206 additions and 66 deletions

View File

@ -1,7 +0,0 @@
namespace RaylibTest;
public class Collider : Component
{
}

View File

@ -1,6 +0,0 @@
namespace RaylibTest;
public class Component
{
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 106 KiB

View File

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

View File

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

View File

@ -11,8 +11,4 @@
<PackageReference Include="Raylib-cs" Version="4.0.0.1" />
</ItemGroup>
<ItemGroup>
<Folder Include="Sprites" />
</ItemGroup>
</Project>

View File

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

View File

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

View File

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.0 KiB

View File

@ -1,6 +1,6 @@
using System.Numerics;
namespace RaylibTest;
namespace RaylibTest.Support;
public class GameObject
{

View File

@ -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()
{
}
}

View File

@ -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<GameObject> gameObjects = new List<GameObject>();
private List<UiObject> uiObjects = new List<UiObject>();
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();
}

View File

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

View File

@ -1 +1 @@
a2d1cedbc5ed7528cb3f4a0e5feb8a12993c50f2
6eb834945136a68f0e375e88a382ef0898f0909d