More fixes
This commit is contained in:
parent
6748cae305
commit
8b09902f84
5 changed files with 56 additions and 20 deletions
|
@ -1,17 +1,40 @@
|
|||
namespace Theater_ECS_Example.Archetypes;
|
||||
using Theater_ECS_Example.Components;
|
||||
|
||||
using RaylibBeef;
|
||||
|
||||
using Theater_ECS;
|
||||
|
||||
using System;
|
||||
|
||||
struct MovingBall : IArchetypeable
|
||||
{
|
||||
public float X = 0;
|
||||
public float Y = 0;
|
||||
public float offsetX = 0;
|
||||
public float offsetY = 0;
|
||||
|
||||
public this(float x, float y, float oX, float oY)
|
||||
{
|
||||
X = x;
|
||||
Y = y;
|
||||
offsetX = oX;
|
||||
offsetY = oY;
|
||||
}
|
||||
|
||||
public void Instantiate(ECS ecs, Entity e)
|
||||
{
|
||||
ecs.Components.AddToEntity(e, ecs.Components.GetId(typeof(Position).GetFullName(.. scope .())), &Position());
|
||||
ecs.Components.AddToEntity(e, ecs.Components.GetId(typeof(Velocity).GetFullName(.. scope .())), &Velocity());
|
||||
float vel_x = (Raylib.GetMouseX() - X) + offsetX;
|
||||
float vel_y = (Raylib.GetMouseY() - Y) + offsetY;
|
||||
float lenght = Math.Sqrt(vel_x*vel_x + vel_y*vel_y);
|
||||
vel_x = vel_x / lenght;
|
||||
vel_y = vel_y / lenght;
|
||||
|
||||
ecs.Components.AddToEntity(e, ecs.Components.GetId(typeof(Position).GetFullName(.. scope .())), &Position() {x = X, y = Y});
|
||||
ecs.Components.AddToEntity(e, ecs.Components.GetId(typeof(Velocity).GetFullName(.. scope .())), &Velocity() {
|
||||
x = vel_x * 60,
|
||||
y = vel_y * 60
|
||||
});
|
||||
ecs.Components.AddToEntity(e, ecs.Components.GetId(typeof(Sprite).GetFullName(.. scope .())), &Sprite(1));
|
||||
}
|
||||
}
|
|
@ -4,8 +4,8 @@ using System;
|
|||
|
||||
struct Position
|
||||
{
|
||||
public float x;
|
||||
public float y;
|
||||
public float x = 100;
|
||||
public float y = 100;
|
||||
|
||||
public void Update(float a, float b) mut
|
||||
{
|
||||
|
|
|
@ -12,13 +12,14 @@ using Theater_ECS;
|
|||
|
||||
class Program
|
||||
{
|
||||
public static ECS ecs = new .() ~ DeleteECSAndItems!(_);
|
||||
public static uint64 entityCounter = 1;
|
||||
|
||||
|
||||
public static void Main()
|
||||
{
|
||||
Raylib.InitWindow(1280, 720, "Theater-ECS-Example");
|
||||
|
||||
ECS ecs = new .();
|
||||
defer { DeleteECSAndItems!(ecs); }
|
||||
|
||||
ecs.Systems.RegisterSystem(
|
||||
new RendererSystem(),
|
||||
"RendererSystem");
|
||||
|
@ -31,7 +32,6 @@ class Program
|
|||
new CharacterSystem(),
|
||||
"CharacterSystem");
|
||||
|
||||
uint64 entityCounter = 1;
|
||||
|
||||
ecs.Entities.Create(Player());
|
||||
|
||||
|
@ -43,11 +43,6 @@ class Program
|
|||
ecs.Systems.RunSystem("CharacterSystem", ecs)
|
||||
.IgnoreError();
|
||||
|
||||
if(Raylib.IsMouseButtonDown((.)MouseButton.MOUSE_BUTTON_LEFT))
|
||||
{
|
||||
entityCounter++;
|
||||
ecs.Entities.Create(MovingBall());
|
||||
}
|
||||
Raylib.BeginDrawing();
|
||||
Raylib.ClearBackground(Raylib.RAYWHITE);
|
||||
|
||||
|
|
|
@ -1,24 +1,36 @@
|
|||
namespace Theater_ECS_Example.Systems;
|
||||
using Theater_ECS_Example.Components;
|
||||
using Theater_ECS_Example.Archetypes;
|
||||
|
||||
using Theater_ECS;
|
||||
|
||||
using RaylibBeef;
|
||||
|
||||
using System;
|
||||
|
||||
class CharacterSystem : System
|
||||
{
|
||||
public override void RegisterSystem(ECS ecs)
|
||||
{
|
||||
RegisterComponent<PlayerControlable>(ecs);
|
||||
RegisterComponent<Velocity>(ecs);
|
||||
RegisterComponent<Position>(ecs);
|
||||
|
||||
Run_2 = => RunFunction;
|
||||
Run_3 = => RunFunction;
|
||||
}
|
||||
|
||||
public static void RunFunction(void* p1, void* p2)
|
||||
public static void RunFunction(void* p1, void* p2, void* p3)
|
||||
{
|
||||
var vel = (Velocity*)p2;
|
||||
var controls = (PlayerControlable*)p1;
|
||||
Position pos = .() {x = ((Position*)p3).x, y = ((Position*)p3).y};
|
||||
|
||||
|
||||
vel.x = System.Math.Clamp(vel.x, -70, 70);
|
||||
vel.y = System.Math.Clamp(vel.y, -70, 70);
|
||||
|
||||
vel.y *= 0.9f;
|
||||
vel.x *= 0.9f;
|
||||
|
||||
if(Raylib.IsKeyDown(controls.Right))
|
||||
vel.x += 100;
|
||||
|
@ -30,5 +42,14 @@ class CharacterSystem : System
|
|||
else if(Raylib.IsKeyDown(controls.Up))
|
||||
vel.y += -100;
|
||||
//What is this normalization thing that you speak of ?
|
||||
|
||||
if(Raylib.IsMouseButtonDown(0))
|
||||
{
|
||||
for(int i = -3; i < 4; i++)
|
||||
{
|
||||
Program.entityCounter++;
|
||||
Program.ecs.Entities.Create(MovingBall(pos.x, pos.y, i*20, i*20));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
namespace Theater_ECS_Example.Systems;
|
||||
using Theater_ECS_Example.Components;
|
||||
|
||||
|
||||
using RaylibBeef;
|
||||
|
||||
using Theater_ECS;
|
||||
|
@ -23,11 +24,7 @@ class MovementSystem : System
|
|||
((Velocity*)vel).x * RaylibBeef.Raylib.GetFrameTime(),
|
||||
((Velocity*)vel).y * RaylibBeef.Raylib.GetFrameTime()
|
||||
);
|
||||
((Velocity*)vel).x = System.Math.Clamp(((Velocity*)vel).x, -50, 50);
|
||||
((Velocity*)vel).y = System.Math.Clamp(((Velocity*)vel).y, -50, 50);
|
||||
|
||||
((Velocity*)vel).y *= 0.9f;
|
||||
((Velocity*)vel).x *= 0.9f;
|
||||
|
||||
|
||||
|
||||
if(mPos.x > 1280 || mPos.x < 0)
|
||||
|
|
Loading…
Add table
Reference in a new issue