Initial commit

This commit is contained in:
2026-01-19 13:56:50 -05:00
commit cc4fee7a34
55 changed files with 1824 additions and 0 deletions

View File

@@ -0,0 +1,127 @@
using System;
using System.Collections.Generic;
namespace Gravity.Kernel;
public sealed partial class Body
{
public Generic AddComponent<Generic>(object[] args) where Generic : Component {
if (SingletonExists<Generic>())
throw new Exception("Cannot add component when singleton exists!");
Generic component = (Generic) Activator.CreateInstance(typeof(Generic), args);
if(component == null)
throw new Exception("Failed to create component!");
components.Add(component);
component.Initiate(this);
ComponentCreatedEvent?.Invoke(this, new ComponentCreateEvent(component, this, scene));
return component;
}
public Generic AddComponent<Generic>() where Generic : Component {
if (SingletonExists<Generic>())
throw new Exception("Cannot add component when singleton exists!");
Generic component = (Generic) Activator.CreateInstance(typeof(Generic));
if(component == null)
throw new Exception("Failed to create component!");
components.Add(component);
component.Initiate(this);
ComponentCreatedEvent?.Invoke(this, new ComponentCreateEvent(component, this, scene));
return component;
}
public Generic[] GetComponents<Generic>() where Generic : Component {
List<Component> foundComponents = components.FindAll(x => x.GetType() == typeof(Generic));
if(foundComponents.Count == 0)
throw new Exception("Scene has no components of that type!");
return foundComponents.ToArray() as Generic[];
}
public Generic GetComponent<Generic>() where Generic : Component {
Component foundComponent = components.Find(x => x.GetType() == typeof(Generic));
if(foundComponent == null)
throw new Exception("Scene has no components of that type!");
return foundComponent as Generic;
}
public void RemoveComponent(Component __component) {
__component.End();
components.Remove(__component);
}
public void RemoveComponents<Generic>() where Generic : Component {
Component[] foundComponents = GetComponents<Generic>();
if(foundComponents.Length == 0)
throw new Exception("Scene has no components of that type!");
foreach (Component component in foundComponents) {
component.End();
components.Remove(component);
ComponentDestroyedEvent?.Invoke(this, new ComponentDestroyEvent(component, this, scene));
}
}
public void RemoveComponent<Generic>() where Generic : Component {
Component foundComponent = GetComponent<Generic>();
if(foundComponent == null)
throw new Exception("Scene has no components of that type!");
foundComponent.End();
components.Remove(foundComponent);
ComponentDestroyedEvent?.Invoke(this ,new ComponentDestroyEvent(foundComponent, this, scene));
}
public Generic FindSingleton<Generic>() where Generic : Component
{
foreach (Component component in components)
if (component.GetType() == typeof(Generic))
if(component.EnforceSingleton)
return (Generic) component;
else
throw new Exception("Component is not a singleton");
throw new Exception("Component not found");
return null;
}
public bool SingletonExists<Generic>() where Generic : Component
{
foreach (Component __component in components)
if (__component.GetType() == typeof(Generic))
if (__component.EnforceSingleton)
return true;
else
return false;
return false;
}
public void RecompileComponentOrder() {
components.Sort((a, b) => a.Priority.CompareTo(b.Priority));
components.Reverse();
}
}

View File

@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
namespace Gravity.Kernel;
public sealed partial class Body
{
public readonly Scene scene;
public readonly Transform transform = new Transform();
public readonly List<string> tags = [];
public readonly List<Component> components = [];
public Body(Scene __scene) {
scene = __scene;
}
public Body(Scene __scene, Transform __transform) {
scene = __scene;
transform = __transform;
}
//todo: make internal
}

View File

@@ -0,0 +1,12 @@
using System;
namespace Gravity.Kernel;
public sealed partial class Body
{
//todo: add component events to scene in v5
public event EventHandler<ComponentCreateEvent> ComponentCreatedEvent;
public event EventHandler<ComponentDestroyEvent> ComponentDestroyedEvent;
}

View File

@@ -0,0 +1,19 @@
using Microsoft.Xna.Framework;
namespace Gravity.Kernel;
public sealed partial class Body
{
public void Initialize() { foreach (Component component in components) component.Initialize(); }
public void Terminate() { foreach (Component component in components) component.Terminate(); }
public void Load() { foreach (Component component in components) { component.Load(); } }
public void Update(GameTime __gameTime) { foreach (Component component in components) { component.Update(__gameTime); } }
public void Draw(GameTime __gameTime) { foreach (Component component in components) { component.Draw(__gameTime); } }
public void Destroy() { foreach(Component component in components) component.Destroy(); }
public void Create() { foreach (Component component in components) component.Create(); }
}

View File

@@ -0,0 +1,134 @@
using System;
using System.Collections.Generic;
namespace Gravity.Kernel;
public sealed partial class Scene
{
public List<Behavior> behaviors { get; private set; } = [];
//todo: use extern keyword to make transform ambiguous to support potential 3D games
public Generic AddBehavior<Generic>(object[] args) where Generic : Behavior {
if (SingletonExists<Generic>())
throw new Exception("Cannot add behavior when singleton exists!");
Generic behavior = (Generic) Activator.CreateInstance(typeof(Generic), args);
if(behavior == null)
throw new Exception("Failed to create behavior!");
behaviors.Add(behavior);
behavior.Initiate(this);
BehaviorCreatedEvent?.Invoke(this, new BehaviorCreateEvent(behavior, this));
return behavior;
}
public Generic AddBehavior<Generic>() where Generic : Behavior {
if (SingletonExists<Generic>())
throw new Exception("Cannot add behavior when singleton exists!");
Generic behavior = (Generic) Activator.CreateInstance(typeof(Generic));
if(behavior == null)
throw new Exception("Failed to create behavior!");
behaviors.Add(behavior);
behavior.Initiate(this);
BehaviorCreatedEvent?.Invoke(this, new BehaviorCreateEvent(behavior, this));
return behavior;
}
public Generic[] GetBehaviors<Generic>() where Generic : Behavior {
List<Behavior> foundBehaviors = behaviors.FindAll(x => x.GetType() == typeof(Generic));
if(foundBehaviors.Count == 0)
throw new Exception("Scene has no behaviors of that type!");
return foundBehaviors.ToArray() as Generic[];
}
public Generic GetBehavior<Generic>() where Generic : Behavior {
Behavior foundBehavior = behaviors.Find(x => x.GetType() == typeof(Generic));
if(foundBehavior == null)
throw new Exception("Scene has no behaviors of that type!");
return foundBehavior as Generic;
}
public void RemoveBehaviors<Generic>() where Generic : Behavior {
Behavior[] foundBehaviors = GetBehaviors<Generic>();
if(foundBehaviors.Length == 0)
throw new Exception("Scene has no behaviors of that type!");
foreach (Behavior behavior in foundBehaviors) {
behavior.End();
behaviors.Remove(behavior);
BehaviorDestroyedEvent?.Invoke(this, new BehaviorDestroyEvent(behavior, this));
}
}
public void RemoveBehavior<Generic>() where Generic : Behavior {
Behavior foundBehavior = GetBehavior<Generic>();
if(foundBehavior == null)
throw new Exception("Scene has no behaviors of that type!");
foundBehavior.End();
behaviors.Remove(foundBehavior);
BehaviorDestroyedEvent?.Invoke(this ,new BehaviorDestroyEvent(foundBehavior, this));
}
public void RemoveBehavior(Behavior __behavior) {
__behavior.End();
behaviors.Remove(__behavior);
BehaviorDestroyedEvent?.Invoke(this, new BehaviorDestroyEvent(__behavior, this));
}
public Generic FindSingleton<Generic>() where Generic : Behavior
{
foreach (Behavior __behavior in behaviors)
if (__behavior.GetType() == typeof(Generic))
if(__behavior.EnforceSingleton)
return (Generic) __behavior;
else
throw new Exception("Behavior is not a singleton");
throw new Exception("Behavior not found");
return null;
}
public bool SingletonExists<Generic>() where Generic : Behavior
{
foreach (Behavior __behavior in behaviors)
if (__behavior.GetType() == typeof(Generic))
if (__behavior.EnforceSingleton)
return true;
else
return false;
return false;
}
public void RecompileBehaviorOrder() {
behaviors.Sort((a, b) => a.Priority.CompareTo(b.Priority));
behaviors.Reverse();
}
}

View File

@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
namespace Gravity.Kernel;
public sealed partial class Scene
{
public List<Body> bodies { get; private set; } = [];
public Body AddBody(Transform __transform) {
Body body = new Body(this, __transform);
bodies.Add(body);
body.Create();
BodyCreatedEvent?.Invoke(this, new BodyCreateEvent(body, this));
return body;
}
public Body AddBody() {
Body body = new Body(this, new Transform());
bodies.Add(body);
body.Create();
BodyCreatedEvent?.Invoke(this, new BodyCreateEvent(body, this));
return body;
}
public Body[] GetBodies(string tag) {
List<Body> _bodies = new List<Body>();
foreach (Body body in bodies)
if (body.tags.Contains(tag))
_bodies.Add(body);
if(_bodies.Count == 0)
throw new Exception("No Bodies found with the tag " + tag);
return _bodies.ToArray();
}
public Body GetBody(string tag) {
foreach (Body body in bodies)
if (body.tags.Contains(tag))
return body;
throw new Exception("No Body found with the tag " + tag);
}
public void DestroyBody(Body __body) {
__body.Destroy();
BodyDestroyedEvent?.Invoke(this, new BodyDestroyEvent(__body, this));
if (!bodies.Remove(__body))
throw new Exception("Removal Failed! Does the Body Exist?");
}
//todo: add destroying and creating bodies with tags
//TAG SYSTEM IN V4
}

View File

@@ -0,0 +1,10 @@
namespace Gravity.Kernel;
public sealed partial class Scene
{
//todo: add scene.destroy in v5
}

View File

@@ -0,0 +1,15 @@
using System;
namespace Gravity.Kernel;
public sealed partial class Scene
{
public event EventHandler<BehaviorCreateEvent> BehaviorCreatedEvent;
public event EventHandler<BehaviorDestroyEvent> BehaviorDestroyedEvent;
public event EventHandler<BodyCreateEvent> BodyCreatedEvent;
public event EventHandler<BodyDestroyEvent> BodyDestroyedEvent;
}

View File

@@ -0,0 +1,32 @@
using Microsoft.Xna.Framework;
namespace Gravity.Kernel;
public sealed partial class Scene
{
public void Initialize() {
foreach (Behavior behavior in behaviors) behavior.Initialize();
foreach (Body body in bodies) body.Initialize();
}
public void Terminate() {
foreach (Behavior behavior in behaviors) behavior.Terminate();
foreach (Body body in bodies) body.Terminate();
}
public void Load() {
foreach (Behavior behavior in behaviors) { behavior.Load(); }
foreach (Body body in bodies) { body.Load(); }
}
public void Update(GameTime __gameTime) {
foreach (Behavior behavior in behaviors) { behavior.Update(__gameTime); }
foreach (Body body in bodies) { body.Update(__gameTime); }
}
public void Draw(GameTime __gameTime) {
foreach (Behavior behavior in behaviors) { behavior.Draw(__gameTime); }
foreach (Body body in bodies) { body.Draw(__gameTime); }
}
}