Browse Source

Expanded more features for making simple game objects

master
Ludovic 'Archivist' Lagouardette 8 months ago
parent
commit
5e4f812f19
8 changed files with 91 additions and 25 deletions
  1. +6
    -13
      Expansion1/CrosshairGizmoComponent.cs
  2. +5
    -1
      Expansion2/ExampleShootingInputComponent.cs
  3. +59
    -0
      Expansion2/InertiaComponent.cs
  4. +12
    -0
      Expansion2/RaylibAssetManagerModule.cs
  5. +2
    -8
      Expansion2/SpriteComponent.cs
  6. +1
    -1
      Program.cs
  7. +1
    -1
      Smoll/Entity.cs
  8. +5
    -1
      Smoll/Layer.cs

+ 6
- 13
Expansion1/CrosshairGizmoComponent.cs View File

@ -34,29 +34,22 @@ namespace Smoll.Ex1 {
Color.Red);
Raylib.DrawText(
tr.angle.ToString(),
(int)tr.position.Real+10,
(int)tr.position.Imaginary+10,
14,
Color.Magenta
);
Raylib.DrawText(
tr.angle.ToString(),
(int)tr.position.Real+10,
(int)tr.position.Imaginary+10,
(int)tr.position.Real+7,
(int)tr.position.Imaginary+7,
14,
Color.Magenta
);
Raylib.DrawText(
tr.position.Real.ToString(),
(int)tr.position.Real+10,
(int)tr.position.Imaginary+26,
(int)tr.position.Real+7,
(int)tr.position.Imaginary+23,
14,
Color.Red
);
Raylib.DrawText(
tr.position.Imaginary.ToString(),
(int)tr.position.Real+10,
(int)tr.position.Imaginary+42,
(int)tr.position.Real+7,
(int)tr.position.Imaginary+39,
14,
Color.Lime
);

+ 5
- 1
Expansion2/ExampleShootingInputComponent.cs View File

@ -1,6 +1,7 @@
using Smoll;
using Raylib_cs;
using System.Numerics;
using Smoll.Ex1;
namespace Smoll.Ex2
{
@ -26,10 +27,13 @@ namespace Smoll.Ex2
if(arrow == null) throw new Exception("Example Shooter used on arrowless object");
if(Raylib.IsKeyPressed(KeyboardKey.Space)) {
var shoot = new Entity(owner.layers.First());
shoot.zOrder = -5;
var details = arrow.OriginAndVector();
shoot.Attach(new Transform2DComponent((float)details.Item1.Real,(float)details.Item1.Imaginary,transform.transform.angle, transform.transform.scale));
var selfTransform = transform.AbsoluteTransform();
shoot.Attach(new Transform2DComponent((float)details.Item1.Real, (float)details.Item1.Imaginary, selfTransform.angle, selfTransform.scale));
shoot.Attach(new TimeToLiveComponent(4f));
shoot.Attach(new SpriteComponent("Beam.png"));
shoot.Attach(new InertiaComponent(1.0f, 0.001f*(float)details.Item2.Real, 0.001f*(float)details.Item2.Imaginary));
}
}
}

+ 59
- 0
Expansion2/InertiaComponent.cs View File

@ -0,0 +1,59 @@
using System.Numerics;
namespace Smoll.Ex2 {
sealed class InertiaComponent : Component {
public Transform2D transform;
public float dampening;
public float minimalDampening = 2.0f;
private Transform2DComponent? ownerTransform;
public InertiaComponent(float dampening = 1)
: this(dampening,0,0,0)
{}
public InertiaComponent(float dampening, float x, float y)
: this(dampening,x,y,0)
{}
public InertiaComponent(float dampening, float x, float y, float angle) {
transform = new Transform2D();
transform.position = y*Complex.ImaginaryOne + x;
transform.angle = angle;
transform.scale = 1;
this.dampening = dampening;
}
public InertiaComponent(float dampening, Transform2D tr) {
transform = tr;
this.dampening = dampening;
}
public override void OnAttached() {
ownerTransform = owner.GetComponent<Transform2DComponent>();
}
public override void Update(float deltaTimeSeconds)
{
ownerTransform ??= owner.GetComponent<Transform2DComponent>();
ownerTransform ??= new Transform2DComponent();
ownerTransform.transform.position += transform.position;
ownerTransform.transform.angle += transform.angle;
if(dampening != 1) {
if(transform.position.Magnitude > minimalDampening) {
transform.position *= dampening;
} else {
transform.position *= 0;
}
if(transform.angle > minimalDampening) {
transform.angle *= dampening;
} else {
transform.angle *= 0;
}
}
}
}
}

+ 12
- 0
Expansion2/RaylibAssetManagerModule.cs View File

@ -6,11 +6,13 @@ namespace Smoll.Ex2
string path;
Dictionary<string, Image> images;
Dictionary<string, Texture2D> textures;
Dictionary<string, Sound> sounds;
public RaylibAssetManagerModule(string path = "./assets") {
this.path = path;
images = new Dictionary<string, Image>();
textures = new Dictionary<string, Texture2D>();
sounds = new Dictionary<string, Sound>();
}
@ -23,6 +25,16 @@ namespace Smoll.Ex2
return image;
}
public Texture2D GetTexture(string name) {
Texture2D texture;
if(! textures.TryGetValue(name, out texture)) {
Image image = GetImage(name);
texture = Raylib.LoadTextureFromImage(image);
textures.Add(name, texture);
}
return texture;
}
public Sound GetSound(string name) {
Sound sound;
if(! sounds.TryGetValue(name, out sound)) {

+ 2
- 8
Expansion2/SpriteComponent.cs View File

@ -2,7 +2,7 @@ using System.Numerics;
using Raylib_cs;
namespace Smoll.Ex2 {
class SpriteComponent : Smoll.Component , IDisposable{
class SpriteComponent : Smoll.Component {
Complex offset;
string asset_name;
Transform2DComponent? ownerTransform;
@ -16,8 +16,7 @@ namespace Smoll.Ex2 {
public override void OnAttached() {
ownerTransform = owner.GetComponent<Transform2DComponent>();
Image image = owner.layers.First().attachedEngine.GetModule<RaylibAssetManagerModule>().GetImage(asset_name);
texture = Raylib.LoadTextureFromImage(image);
texture = owner.layers.First().attachedEngine.GetModule<RaylibAssetManagerModule>().GetTexture(asset_name);
}
public override void Draw(Smoll.Layer.DrawMode drawMode) {
@ -28,10 +27,5 @@ namespace Smoll.Ex2 {
Raylib.DrawTextureEx(texture, new Vector2((float)tr.position.Real, (float)tr.position.Imaginary), tr.angle*180f/MathF.PI, tr.scale, Color.White);
}
}
public void Dispose()
{
Raylib.UnloadTexture(texture);
}
}
}

+ 1
- 1
Program.cs View File

@ -30,7 +30,7 @@ entity.Attach(ship);
var entity2 = new Entity(entity);
entity2.Attach(new Transform2DComponent(5, -10, 0));
entity2.Attach(new SpriteComponent("Laser.png"));
var arrow = new ArrowComponent(4, 4);
var arrow = new ArrowComponent(2, 0);
arrow.rawValue = new System.Numerics.Complex(0, -12);
entity2.Attach(arrow);
entity2.Attach(new ExampleShootingInputComponent());

+ 1
- 1
Smoll/Entity.cs View File

@ -12,7 +12,7 @@ namespace Smoll {
} else {
foreach (var item in layers)
{
item.actionables.Sort();
item.mustReorder = true;
}
}
}

+ 5
- 1
Smoll/Layer.cs View File

@ -9,6 +9,8 @@ namespace Smoll {
watch = new Stopwatch();
}
public bool mustReorder = false;
internal Engine attachedEngine;
internal List<IActionable> actionables;
@ -43,7 +45,9 @@ namespace Smoll {
{
actionables[i].Update(deltaTime);
}
actionables.Sort();
if(mustReorder) {
actionables.Sort();
}
return deltaTime;
}
public void Draw() {

Loading…
Cancel
Save