1
0
Fork 0

Adjust layout and added some more basics

This commit is contained in:
Booklordofthedings 2024-11-19 21:11:58 +01:00
parent c06c70a3ff
commit 6748cae305
15 changed files with 146 additions and 53 deletions

BIN
img.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 927 B

After

Width:  |  Height:  |  Size: 284 B

BIN
pellet.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 131 B

View file

@ -1,13 +1,17 @@
namespace Theater_ECS_Example;
namespace Theater_ECS_Example.Archetypes;
using Theater_ECS_Example.Components;
using Theater_ECS;
struct MovingBall : IArchetypeable
{
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());
ecs.Components.AddToEntity(e, ecs.Components.GetId(typeof(Sprite).GetFullName(.. scope .())), &Sprite());
ecs.Components.AddToEntity(e, ecs.Components.GetId(typeof(Sprite).GetFullName(.. scope .())), &Sprite(1));
}
}

21
src/Archetypes/Player.bf Normal file
View file

@ -0,0 +1,21 @@
namespace Theater_ECS_Example.Archetypes;
using Theater_ECS_Example.Components;
using Theater_ECS;
struct Player : IArchetypeable
{
public void Instantiate(ECS ecs, Entity e)
{
ecs.Components.AddToEntity( e, ecs.Components.GetId(typeof(Position).GetFullName(.. scope .())),
&Position()
{
x = 1280/2,
y = 720/2}
);
ecs.Components.AddToEntity(e, ecs.Components.GetId(typeof(PlayerControlable).GetFullName(.. scope .())), &PlayerControlable());
ecs.Components.AddToEntity(e, ecs.Components.GetId(typeof(Velocity).GetFullName(.. scope .())), &Velocity());
ecs.Components.AddToEntity(e, ecs.Components.GetId(typeof(Sprite).GetFullName(.. scope .())), &Sprite());
}
}

View file

@ -0,0 +1,11 @@
namespace Theater_ECS_Example.Components;
using RaylibBeef;
struct PlayerControlable
{
public KeyboardKey Left = .KEY_A;
public KeyboardKey Right = .KEY_D;
public KeyboardKey Down = .KEY_S;
public KeyboardKey Up = .KEY_W;
}

View file

@ -0,0 +1,15 @@
namespace Theater_ECS_Example.Components;
using System;
struct Position
{
public float x;
public float y;
public void Update(float a, float b) mut
{
x += a;
y += b;
}
}

26
src/Components/Sprite.bf Normal file
View file

@ -0,0 +1,26 @@
namespace Theater_ECS_Example.Components;
using System;
using RaylibBeef;
struct Sprite
{
private static Texture2D? _texture = null;
private static Texture2D? _pellet = null;
public Texture2D sprite;
public this(uint8 s = 0)
{
if(_texture == null)
_texture = Raylib.LoadTexture("img.png");
if(_pellet == null)
_pellet = Raylib.LoadTexture("pellet.png");
if(s == 0)
sprite = (.)_texture;
else
sprite = (.)_pellet;
}
}

View file

@ -0,0 +1,9 @@
namespace Theater_ECS_Example.Components;
using System;
struct Velocity
{
public float x = 0;
public float y = 0;
}

View file

@ -1,15 +0,0 @@
namespace Theater_ECS_Example;
using System;
struct Position
{
public float x = (.)gRand.NextI32() % 1280;
public float y = (.)gRand.NextI32() % 720;
public void Update(float a, float b) mut
{
x += a;
y += b;
}
}

View file

@ -1,4 +1,8 @@
namespace Theater_ECS_Example;
using Theater_ECS_Example.Systems;
using Theater_ECS_Example.Components;
using Theater_ECS_Example.Archetypes;
using System;
@ -23,20 +27,21 @@ class Program
new MovementSystem(),
"MovementSystem");
uint64 entityCounter = 0;
ecs.Systems.RegisterSystem(
new CharacterSystem(),
"CharacterSystem");
for(int32 i < 50)
{
entityCounter++;
ecs.Entities.Create(MovingBall());
}
uint64 entityCounter = 1;
ecs.Entities.Create(Player());
while(!Raylib.WindowShouldClose())
{
ecs.Systems.RunSystem("MovementSystem", ecs)
.IgnoreError();
ecs.Systems.RunSystem("CharacterSystem", ecs)
.IgnoreError();
if(Raylib.IsMouseButtonDown((.)MouseButton.MOUSE_BUTTON_LEFT))
{

View file

@ -1,19 +0,0 @@
namespace Theater_ECS_Example;
using System;
using RaylibBeef;
struct Sprite
{
private static Texture2D? _texture = null;
public Texture2D sprite;
public this()
{
if(_texture == null)
_texture = Raylib.LoadTexture("img.png");
sprite = (.)_texture;
}
}

View file

@ -0,0 +1,34 @@
namespace Theater_ECS_Example.Systems;
using Theater_ECS_Example.Components;
using Theater_ECS;
using RaylibBeef;
class CharacterSystem : System
{
public override void RegisterSystem(ECS ecs)
{
RegisterComponent<PlayerControlable>(ecs);
RegisterComponent<Velocity>(ecs);
Run_2 = => RunFunction;
}
public static void RunFunction(void* p1, void* p2)
{
var vel = (Velocity*)p2;
var controls = (PlayerControlable*)p1;
if(Raylib.IsKeyDown(controls.Right))
vel.x += 100;
else if(Raylib.IsKeyDown(controls.Left))
vel.x += -100;
if(Raylib.IsKeyDown(controls.Down))
vel.y += 100;
else if(Raylib.IsKeyDown(controls.Up))
vel.y += -100;
//What is this normalization thing that you speak of ?
}
}

View file

@ -1,4 +1,7 @@
namespace Theater_ECS_Example;
namespace Theater_ECS_Example.Systems;
using Theater_ECS_Example.Components;
using RaylibBeef;
using Theater_ECS;
@ -20,6 +23,12 @@ 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)
((Velocity*)vel).x *= -1;

View file

@ -1,4 +1,6 @@
namespace Theater_ECS_Example;
namespace Theater_ECS_Example.Systems;
using Theater_ECS_Example.Components;
using Theater_ECS;

View file

@ -1,9 +0,0 @@
namespace Theater_ECS_Example;
using System;
struct Velocity
{
public float x = (.)gRand.NextI32() % 60;
public float y = (.)gRand.NextI32() % 60;
}