@ -0,0 +1,49 @@ | |||||
using System.Numerics; | |||||
using Raylib_cs; | |||||
namespace Smoll.Ex2 { | |||||
class ArrowComponent : Smoll.Component { | |||||
Complex offset; | |||||
public Complex rawValue; | |||||
Transform2DComponent? ownerTransform; | |||||
public ArrowComponent(int offset_x = 0, int offset_y = 0) | |||||
{ | |||||
this.offset = offset_x + Complex.ImaginaryOne*offset_y; | |||||
} | |||||
public override void OnAttached() { | |||||
ownerTransform = owner.GetComponent<Transform2DComponent>(); | |||||
} | |||||
public Complex Origin() { | |||||
ownerTransform ??= owner.GetComponent<Transform2DComponent>(); | |||||
ownerTransform ??= new Transform2DComponent(); | |||||
var tr = ownerTransform.AbsoluteTransform(); | |||||
return tr.position + offset*Complex.Exp(Complex.ImaginaryOne * tr.angle) * tr.scale; | |||||
} | |||||
public Complex Vector() { | |||||
ownerTransform ??= owner.GetComponent<Transform2DComponent>(); | |||||
ownerTransform ??= new Transform2DComponent(); | |||||
var tr = ownerTransform.AbsoluteTransform(); | |||||
return rawValue*Complex.Exp(Complex.ImaginaryOne * tr.angle) * tr.scale; | |||||
} | |||||
public Tuple<Complex, Complex> OriginAndVector() { | |||||
ownerTransform ??= owner.GetComponent<Transform2DComponent>(); | |||||
ownerTransform ??= new Transform2DComponent(); | |||||
var tr = ownerTransform.AbsoluteTransform(); | |||||
return Tuple.Create(tr.position + offset*Complex.Exp(Complex.ImaginaryOne * tr.angle) * tr.scale, rawValue*Complex.Exp(Complex.ImaginaryOne * tr.angle) * tr.scale); | |||||
} | |||||
public override void Draw(Smoll.Layer.DrawMode drawMode) { | |||||
if(drawMode == Layer.DrawMode.Debug) { | |||||
var data = OriginAndVector(); | |||||
Complex start = data.Item1; | |||||
Complex end = data.Item1 + data.Item2; | |||||
Raylib.DrawLineEx(new Vector2((float)start.Real, (float)start.Imaginary),new Vector2((float)end.Real, (float)end.Imaginary),2f, Color.Red); | |||||
} | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,36 @@ | |||||
using Smoll; | |||||
using Raylib_cs; | |||||
using System.Numerics; | |||||
namespace Smoll.Ex2 | |||||
{ | |||||
class ExampleShootingInputComponent : Component { | |||||
Transform2DComponent? transform; | |||||
ArrowComponent? arrow; | |||||
public ExampleShootingInputComponent() { | |||||
} | |||||
public override void OnAttached() | |||||
{ | |||||
base.OnAttached(); | |||||
transform = owner.GetComponent<Transform2DComponent>(); | |||||
arrow = owner.GetComponent<ArrowComponent>(); | |||||
} | |||||
public override void Update(float deltaTimeSeconds) | |||||
{ | |||||
transform ??= owner.GetComponent<Transform2DComponent>(); | |||||
if(transform == null) throw new Exception("Example Input used on immovable object"); | |||||
arrow ??= owner.GetComponent<ArrowComponent>(); | |||||
if(arrow == null) throw new Exception("Example Shooter used on arrowless object"); | |||||
if(Raylib.IsKeyPressed(KeyboardKey.Space)) { | |||||
var shoot = new Entity(owner.layers.First()); | |||||
var details = arrow.OriginAndVector(); | |||||
shoot.Attach(new Transform2DComponent((float)details.Item1.Real,(float)details.Item1.Imaginary,transform.transform.angle, transform.transform.scale)); | |||||
shoot.Attach(new TimeToLiveComponent(4f)); | |||||
shoot.Attach(new SpriteComponent("Beam.png")); | |||||
} | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,35 @@ | |||||
using Raylib_cs; | |||||
namespace Smoll.Ex2 | |||||
{ | |||||
class RaylibAssetManagerModule : IEngineModule { | |||||
string path; | |||||
Dictionary<string, Image> images; | |||||
Dictionary<string, Sound> sounds; | |||||
public RaylibAssetManagerModule(string path = "./assets") { | |||||
this.path = path; | |||||
images = new Dictionary<string, Image>(); | |||||
sounds = new Dictionary<string, Sound>(); | |||||
} | |||||
public Image GetImage(string name) { | |||||
Image image; | |||||
if(! images.TryGetValue(name, out image)) { | |||||
image = Raylib.LoadImage(path+"/"+name); | |||||
images.Add(name, image); | |||||
} | |||||
return image; | |||||
} | |||||
public Sound GetSound(string name) { | |||||
Sound sound; | |||||
if(! sounds.TryGetValue(name, out sound)) { | |||||
sound = Raylib.LoadSound(path+"/"+name); | |||||
sounds.Add(name, sound); | |||||
} | |||||
return sound; | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,37 @@ | |||||
using System.Numerics; | |||||
using Raylib_cs; | |||||
namespace Smoll.Ex2 { | |||||
class SpriteComponent : Smoll.Component , IDisposable{ | |||||
Complex offset; | |||||
string asset_name; | |||||
Transform2DComponent? ownerTransform; | |||||
Texture2D texture; | |||||
public SpriteComponent(string asset_name, int offset_x = 0, int offset_y = 0) | |||||
{ | |||||
this.offset = offset_x + Complex.ImaginaryOne*offset_y; | |||||
this.asset_name = asset_name; | |||||
} | |||||
public override void OnAttached() { | |||||
ownerTransform = owner.GetComponent<Transform2DComponent>(); | |||||
Image image = owner.layers.First().attachedEngine.GetModule<RaylibAssetManagerModule>().GetImage(asset_name); | |||||
texture = Raylib.LoadTextureFromImage(image); | |||||
} | |||||
public override void Draw(Smoll.Layer.DrawMode drawMode) { | |||||
if(drawMode == Layer.DrawMode.Normal) { | |||||
ownerTransform ??= owner.GetComponent<Transform2DComponent>(); | |||||
ownerTransform ??= new Transform2DComponent(); | |||||
var tr = ownerTransform.AbsoluteTransform(); | |||||
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); | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,25 @@ | |||||
using System.Numerics; | |||||
using Raylib_cs; | |||||
namespace Smoll.Ex2 { | |||||
class TimeToLiveComponent : Smoll.Component { | |||||
float secondsToLive; | |||||
float lived = 0f; | |||||
public TimeToLiveComponent(float secondsToLive) | |||||
{ | |||||
this.secondsToLive = secondsToLive; | |||||
} | |||||
public override void Update(float deltaTimeSeconds) | |||||
{ | |||||
lived += deltaTimeSeconds; | |||||
if(lived > secondsToLive) owner.mustDelete = true; | |||||
} | |||||
public override void Draw(Smoll.Layer.DrawMode drawMode) { | |||||
if(drawMode == Layer.DrawMode.Debug) { | |||||
} | |||||
} | |||||
} | |||||
} |
@ -0,0 +1,6 @@ | |||||
namespace Smoll | |||||
{ | |||||
public interface IEngineModule { | |||||
} | |||||
} |