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,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); }
}
}