Initial commit
This commit is contained in:
127
Awperative/Kernel/Entities/Bodies/Components.cs
Normal file
127
Awperative/Kernel/Entities/Bodies/Components.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
29
Awperative/Kernel/Entities/Bodies/Core.cs
Normal file
29
Awperative/Kernel/Entities/Bodies/Core.cs
Normal 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
|
||||
}
|
||||
12
Awperative/Kernel/Entities/Bodies/Events.cs
Normal file
12
Awperative/Kernel/Entities/Bodies/Events.cs
Normal 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;
|
||||
}
|
||||
19
Awperative/Kernel/Entities/Bodies/Time.cs
Normal file
19
Awperative/Kernel/Entities/Bodies/Time.cs
Normal 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(); }
|
||||
}
|
||||
134
Awperative/Kernel/Entities/Scenes/Behaviors.cs
Normal file
134
Awperative/Kernel/Entities/Scenes/Behaviors.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
66
Awperative/Kernel/Entities/Scenes/Bodies.cs
Normal file
66
Awperative/Kernel/Entities/Scenes/Bodies.cs
Normal 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
|
||||
|
||||
}
|
||||
10
Awperative/Kernel/Entities/Scenes/Core.cs
Normal file
10
Awperative/Kernel/Entities/Scenes/Core.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
|
||||
namespace Gravity.Kernel;
|
||||
|
||||
public sealed partial class Scene
|
||||
{
|
||||
|
||||
|
||||
//todo: add scene.destroy in v5
|
||||
}
|
||||
15
Awperative/Kernel/Entities/Scenes/Events.cs
Normal file
15
Awperative/Kernel/Entities/Scenes/Events.cs
Normal 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;
|
||||
}
|
||||
32
Awperative/Kernel/Entities/Scenes/Time.cs
Normal file
32
Awperative/Kernel/Entities/Scenes/Time.cs
Normal 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); }
|
||||
}
|
||||
}
|
||||
15
Awperative/Kernel/Events/Behaviors/BehaviorCreateEvent.cs
Normal file
15
Awperative/Kernel/Events/Behaviors/BehaviorCreateEvent.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace Gravity.Kernel;
|
||||
|
||||
public sealed record BehaviorCreateEvent
|
||||
{
|
||||
public readonly Behavior behavior;
|
||||
public readonly Scene scene;
|
||||
|
||||
internal BehaviorCreateEvent() {}
|
||||
|
||||
internal BehaviorCreateEvent(Behavior __behavior, Scene __scene)
|
||||
{
|
||||
behavior = __behavior;
|
||||
scene = __scene;
|
||||
}
|
||||
}
|
||||
15
Awperative/Kernel/Events/Behaviors/BehaviorDestroyEvent.cs
Normal file
15
Awperative/Kernel/Events/Behaviors/BehaviorDestroyEvent.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace Gravity.Kernel;
|
||||
|
||||
public sealed record BehaviorDestroyEvent
|
||||
{
|
||||
public readonly Behavior behavior;
|
||||
public readonly Scene scene;
|
||||
|
||||
internal BehaviorDestroyEvent() {}
|
||||
|
||||
internal BehaviorDestroyEvent(Behavior __behavior, Scene __scene)
|
||||
{
|
||||
behavior = __behavior;
|
||||
scene = __scene;
|
||||
}
|
||||
}
|
||||
15
Awperative/Kernel/Events/Bodies/BodyCreateEvent.cs
Normal file
15
Awperative/Kernel/Events/Bodies/BodyCreateEvent.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace Gravity.Kernel;
|
||||
|
||||
public sealed record BodyCreateEvent
|
||||
{
|
||||
public readonly Body body;
|
||||
public readonly Scene scene;
|
||||
|
||||
internal BodyCreateEvent() {}
|
||||
|
||||
internal BodyCreateEvent(Body __body, Scene __scene)
|
||||
{
|
||||
body = __body;
|
||||
scene = __scene;
|
||||
}
|
||||
}
|
||||
15
Awperative/Kernel/Events/Bodies/BodyDestroyEvent.cs
Normal file
15
Awperative/Kernel/Events/Bodies/BodyDestroyEvent.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace Gravity.Kernel;
|
||||
|
||||
public sealed record BodyDestroyEvent
|
||||
{
|
||||
public readonly Body body;
|
||||
public readonly Scene scene;
|
||||
|
||||
internal BodyDestroyEvent() {}
|
||||
|
||||
internal BodyDestroyEvent(Body __body, Scene __scene)
|
||||
{
|
||||
body = __body;
|
||||
scene = __scene;
|
||||
}
|
||||
}
|
||||
17
Awperative/Kernel/Events/Components/ComponentCreateEvent.cs
Normal file
17
Awperative/Kernel/Events/Components/ComponentCreateEvent.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace Gravity.Kernel;
|
||||
|
||||
public sealed record ComponentCreateEvent
|
||||
{
|
||||
public readonly Component component;
|
||||
public readonly Body body;
|
||||
public readonly Scene scene;
|
||||
|
||||
internal ComponentCreateEvent() {}
|
||||
|
||||
internal ComponentCreateEvent(Component __component, Body __body, Scene __scene)
|
||||
{
|
||||
component = __component;
|
||||
body = __body;
|
||||
scene = __scene;
|
||||
}
|
||||
}
|
||||
17
Awperative/Kernel/Events/Components/ComponentDestroyEvent.cs
Normal file
17
Awperative/Kernel/Events/Components/ComponentDestroyEvent.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace Gravity.Kernel;
|
||||
|
||||
public sealed record ComponentDestroyEvent
|
||||
{
|
||||
public readonly Component component;
|
||||
public readonly Body body;
|
||||
public readonly Scene scene;
|
||||
|
||||
internal ComponentDestroyEvent() {}
|
||||
|
||||
internal ComponentDestroyEvent(Component __component, Body __body, Scene __scene)
|
||||
{
|
||||
component = __component;
|
||||
body = __body;
|
||||
scene = __scene;
|
||||
}
|
||||
}
|
||||
14
Awperative/Kernel/Events/Scenes/SceneCreateEvent.cs
Normal file
14
Awperative/Kernel/Events/Scenes/SceneCreateEvent.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace Gravity.Kernel;
|
||||
|
||||
|
||||
public sealed record SceneCreateEvent
|
||||
{
|
||||
public Scene scene;
|
||||
|
||||
internal SceneCreateEvent() {}
|
||||
|
||||
internal SceneCreateEvent(Scene __scene)
|
||||
{
|
||||
scene = __scene;
|
||||
}
|
||||
}
|
||||
14
Awperative/Kernel/Events/Scenes/SceneDestroyEvent.cs
Normal file
14
Awperative/Kernel/Events/Scenes/SceneDestroyEvent.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace Gravity.Kernel;
|
||||
|
||||
|
||||
public sealed record SceneDestroyEvent
|
||||
{
|
||||
public Scene scene;
|
||||
|
||||
internal SceneDestroyEvent() {}
|
||||
|
||||
internal SceneDestroyEvent(Scene __scene)
|
||||
{
|
||||
scene = __scene;
|
||||
}
|
||||
}
|
||||
60
Awperative/Kernel/Events/Transform/TransformModifyEvent.cs
Normal file
60
Awperative/Kernel/Events/Transform/TransformModifyEvent.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
|
||||
namespace Gravity.Kernel;
|
||||
|
||||
public sealed record TransformModifyEvent
|
||||
{
|
||||
public readonly Transform before;
|
||||
public readonly Transform after;
|
||||
|
||||
internal TransformModifyEvent() {}
|
||||
|
||||
internal TransformModifyEvent(Transform __before, Transform __after)
|
||||
{
|
||||
before = __before;
|
||||
after = __after;
|
||||
}
|
||||
|
||||
internal static TransformModifyEvent FromTransforms(Transform __previous, Transform __after)
|
||||
{
|
||||
Transform before = __previous;
|
||||
Transform after = new Transform(__after.Origin, __after.Position, __after.Depth, __after.Rotation, __after.Scale);
|
||||
return new TransformModifyEvent(before, after);
|
||||
}
|
||||
|
||||
internal static TransformModifyEvent FromOrigin(Transform __previous, Vector2 __origin)
|
||||
{
|
||||
Transform before = __previous;
|
||||
Transform after = new Transform(__origin, __previous.Position, __previous.Depth, __previous.Rotation, __previous.Scale);
|
||||
return new TransformModifyEvent(before, after);
|
||||
}
|
||||
|
||||
internal static TransformModifyEvent FromPosition(Transform __previous, Vector2 __position)
|
||||
{
|
||||
Transform before = __previous;
|
||||
Transform after = new Transform(__previous.Origin, __position, __previous.Depth, __previous.Rotation, __previous.Scale);
|
||||
return new TransformModifyEvent(before, after);
|
||||
}
|
||||
|
||||
internal static TransformModifyEvent FromDepth(Transform __previous, float __depth)
|
||||
{
|
||||
Transform before = __previous;
|
||||
Transform after = new Transform(__previous.Origin, __previous.Position, __depth, __previous.Rotation, __previous.Scale);
|
||||
return new TransformModifyEvent(before, after);
|
||||
}
|
||||
|
||||
internal static TransformModifyEvent FromRotation(Transform __previous, float __rotation)
|
||||
{
|
||||
Transform before = __previous;
|
||||
Transform after = new Transform(__previous.Origin, __previous.Position, __previous.Depth, __rotation, __previous.Scale);
|
||||
return new TransformModifyEvent(before, after);
|
||||
}
|
||||
|
||||
internal static TransformModifyEvent FromScale(Transform __previous, Vector2 __scale)
|
||||
{
|
||||
Transform before = __previous;
|
||||
Transform after = new Transform(__previous.Origin, __previous.Position, __previous.Depth, __previous.Rotation, __scale);
|
||||
return new TransformModifyEvent(before, after);
|
||||
}
|
||||
}
|
||||
3
Awperative/Kernel/Kernel.txt
Normal file
3
Awperative/Kernel/Kernel.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
ESSENTIAL TO THE FUNCTION OF THE GAME,
|
||||
|
||||
Component entity system!
|
||||
10
Awperative/Kernel/Overhead/AwperativeClass.cs
Normal file
10
Awperative/Kernel/Overhead/AwperativeClass.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace Gravity.Kernel;
|
||||
|
||||
public interface AwperativeHook
|
||||
{
|
||||
//DONT LOAD ASSETS HERE
|
||||
public void Initialize() {}
|
||||
public void Terminate() {}
|
||||
|
||||
public void Load() {}
|
||||
}
|
||||
107
Awperative/Kernel/Overhead/Base.cs
Normal file
107
Awperative/Kernel/Overhead/Base.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Content;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
|
||||
|
||||
namespace Gravity.Kernel;
|
||||
|
||||
//todo: make static
|
||||
public class Base : Game
|
||||
{
|
||||
public static GraphicsDeviceManager GraphicsDeviceManager;
|
||||
|
||||
public static ContentManager ContentManager { get; private set; }
|
||||
public static SpriteBatch SpritesBatch;
|
||||
|
||||
public static List<Scene> LoadedScenes { get; private set; } = [];
|
||||
|
||||
public static Scene MainScene { get; private set; }
|
||||
|
||||
public Base()
|
||||
{
|
||||
|
||||
//todo: move this asshole to camera
|
||||
GraphicsDeviceManager = new GraphicsDeviceManager(this);
|
||||
GraphicsDeviceManager.PreferredBackBufferWidth = 1920;
|
||||
GraphicsDeviceManager.PreferredBackBufferHeight = 1080;
|
||||
GraphicsDeviceManager.IsFullScreen = true;
|
||||
Content.RootDirectory = "Content";
|
||||
IsMouseVisible = true;
|
||||
}
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
|
||||
|
||||
ContentManager = Content;
|
||||
SpritesBatch = new SpriteBatch(GraphicsDevice);
|
||||
|
||||
MainScene = new Scene();
|
||||
LoadedScenes.Add(MainScene);
|
||||
|
||||
//todo: generalize initialization, load a json file containing scripts to run and try running them
|
||||
//intptr.size
|
||||
//Marshal.Sizeof<Class>
|
||||
|
||||
foreach (AwperativeHook hook in Core.ScriptingHooks)
|
||||
hook.Initialize();
|
||||
|
||||
// TODO: Add your initialization logic here
|
||||
foreach(Scene scene in LoadedScenes)
|
||||
scene.Initialize();
|
||||
|
||||
base.Initialize();
|
||||
}
|
||||
|
||||
protected override void LoadContent()
|
||||
{
|
||||
foreach (AwperativeHook hook in Core.ScriptingHooks)
|
||||
hook.Load();
|
||||
|
||||
foreach(Scene scene in LoadedScenes)
|
||||
scene.Load();
|
||||
}
|
||||
|
||||
protected override void Update(GameTime gameTime)
|
||||
{
|
||||
|
||||
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
|
||||
Keyboard.GetState().IsKeyDown(Keys.Escape))
|
||||
Exit();
|
||||
|
||||
// TODO: Add your update logic here
|
||||
//TODO: add specific error codes so i know when json went wrong
|
||||
foreach(Scene scene in LoadedScenes)
|
||||
scene.Update(gameTime);
|
||||
base.Update(gameTime);
|
||||
}
|
||||
|
||||
protected override void Draw(GameTime gameTime)
|
||||
{
|
||||
GraphicsDevice.Clear(Color.Black);
|
||||
|
||||
// TODO: Add your drawing code here
|
||||
//collider.Center += Vector2.One;
|
||||
|
||||
|
||||
|
||||
|
||||
//ADD MOVING COLLIDERS
|
||||
|
||||
foreach(Scene scene in LoadedScenes)
|
||||
scene.Draw(gameTime);
|
||||
base.Draw(gameTime);
|
||||
}
|
||||
|
||||
protected override void EndRun()
|
||||
{
|
||||
foreach (AwperativeHook hook in Core.ScriptingHooks)
|
||||
hook.Terminate();
|
||||
|
||||
foreach (Scene scene in LoadedScenes)
|
||||
scene.Terminate();
|
||||
}
|
||||
}
|
||||
21
Awperative/Kernel/Overhead/Core.cs
Normal file
21
Awperative/Kernel/Overhead/Core.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
||||
namespace Gravity.Kernel;
|
||||
|
||||
|
||||
public static class Core
|
||||
{
|
||||
public static Base Base;
|
||||
public static List<Scene> LoadedScenes => Base.LoadedScenes;
|
||||
|
||||
public static List<AwperativeHook> ScriptingHooks;
|
||||
|
||||
//hooks are called in order
|
||||
public static void Start(List<AwperativeHook> __hooks) {
|
||||
ScriptingHooks = __hooks;
|
||||
|
||||
Base = new Base();
|
||||
Base.Run();
|
||||
}
|
||||
}
|
||||
65
Awperative/Kernel/Scripting/Behaviors/Behavior.cs
Normal file
65
Awperative/Kernel/Scripting/Behaviors/Behavior.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
|
||||
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
|
||||
namespace Gravity.Kernel;
|
||||
|
||||
public abstract class Behavior
|
||||
{
|
||||
public Scene scene;
|
||||
|
||||
public bool Enabled = false;
|
||||
public bool EnforceSingleton = false;
|
||||
|
||||
public int Priority = 0;
|
||||
|
||||
|
||||
//scene relay
|
||||
protected Body AddBody() => scene.AddBody();
|
||||
protected Body AddBody(Transform __transform) => scene.AddBody(__transform);
|
||||
|
||||
protected Body GetBody(string __tag) => scene.GetBody(__tag);
|
||||
protected Body[] GetBodies(string __tag) => scene.GetBodies(__tag);
|
||||
|
||||
protected void DestroyBody(Body __body) => scene.DestroyBody(__body);
|
||||
|
||||
|
||||
|
||||
protected Generic AddBehavior<Generic>() where Generic : Behavior => scene.AddBehavior<Generic>();
|
||||
protected Generic AddBehavior<Generic>(object[] __args) where Generic : Behavior => scene.AddBehavior<Generic>(__args);
|
||||
|
||||
protected Generic GetBehavior<Generic>() where Generic : Behavior => scene.GetBehavior<Generic>();
|
||||
protected Generic[] GetBehaviors<Generic>() where Generic : Behavior => scene.GetBehaviors<Generic>();
|
||||
|
||||
protected void RemoveBehavior<Generic>() where Generic : Behavior => scene.RemoveBehavior<Generic>();
|
||||
|
||||
|
||||
|
||||
//GAME HAS JUST BEGUN/ended
|
||||
public virtual void Initialize() {}
|
||||
public virtual void Terminate() {}
|
||||
|
||||
//WE ARE LOADING STUFF
|
||||
public virtual void Load() {}
|
||||
|
||||
//You know what these do
|
||||
public virtual void Update(GameTime __gameTime) {}
|
||||
public virtual void Draw(GameTime __gameTime) {}
|
||||
|
||||
//component/body/scene is being created or destroyed
|
||||
public virtual void Create() {}
|
||||
public virtual void Destroy() {}
|
||||
|
||||
//New behavior functionality
|
||||
internal void Initiate(Scene __scene) {
|
||||
scene = __scene;
|
||||
Create();
|
||||
}
|
||||
|
||||
//destroy behavior functionality
|
||||
internal void End()
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
}
|
||||
82
Awperative/Kernel/Scripting/Components/Component.cs
Normal file
82
Awperative/Kernel/Scripting/Components/Component.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
|
||||
namespace Gravity.Kernel;
|
||||
|
||||
public abstract class Component
|
||||
{
|
||||
public Scene scene;
|
||||
public Body body;
|
||||
|
||||
public bool Enabled = false;
|
||||
public bool EnforceSingleton = false;
|
||||
|
||||
//0 = default highest priority called first, lowest priority called last
|
||||
//todo: add optional parameter for priority at creation
|
||||
public int Priority {
|
||||
get => _priority;
|
||||
set { _priority = value; body.RecompileComponentOrder(); }
|
||||
} private int _priority = 0;
|
||||
|
||||
|
||||
protected Transform transform => body.transform;
|
||||
|
||||
protected Body AddBody() => scene.AddBody();
|
||||
protected Body AddBody(Transform __transform) => scene.AddBody(__transform);
|
||||
|
||||
protected Body GetBody(string __tag) => scene.GetBody(__tag);
|
||||
protected Body[] GetBodies(string __tag) => scene.GetBodies(__tag);
|
||||
|
||||
protected void DestroyBody(Body __body) => scene.DestroyBody(__body);
|
||||
|
||||
|
||||
|
||||
public Generic AddBehavior<Generic>() where Generic : Behavior => scene.AddBehavior<Generic>();
|
||||
public Generic AddBehavior<Generic>(object[] __args) where Generic : Behavior => scene.AddBehavior<Generic>(__args);
|
||||
|
||||
public Generic GetBehavior<Generic>() where Generic : Behavior => scene.GetBehavior<Generic>();
|
||||
public Generic[] GetBehaviors<Generic>() where Generic : Behavior => scene.GetBehaviors<Generic>();
|
||||
|
||||
|
||||
public void RemoveBehavior<Generic>() where Generic : Behavior => scene.RemoveBehavior<Generic>();
|
||||
|
||||
|
||||
|
||||
public Generic AddComponent<Generic>() where Generic : Component => body.AddComponent<Generic>();
|
||||
public Generic AddComponent<Generic>(object[] __args) where Generic : Component => body.AddComponent<Generic>(__args);
|
||||
|
||||
public Generic GetComponent<Generic>() where Generic : Component => body.GetComponent<Generic>();
|
||||
public Generic[] GetComponents<Generic>() where Generic : Component => body.GetComponents<Generic>();
|
||||
|
||||
public void RemoveComponent<Generic>() where Generic : Component => body.RemoveComponent<Generic>();
|
||||
|
||||
|
||||
|
||||
//GAME HAS JUST BEGUN/ended
|
||||
public virtual void Initialize() {}
|
||||
public virtual void Terminate() {}
|
||||
|
||||
//WE ARE LOADING STUFF
|
||||
public virtual void Load() {}
|
||||
|
||||
//You know what these do
|
||||
public virtual void Update(GameTime __gameTime) {}
|
||||
public virtual void Draw(GameTime __gameTime) {}
|
||||
|
||||
//component/body/scene is being created or destroyed
|
||||
public virtual void Create() {}
|
||||
public virtual void Destroy() {}
|
||||
|
||||
//creation logic
|
||||
internal void Initiate(Body __body)
|
||||
{
|
||||
body = __body;
|
||||
scene = __body.scene;
|
||||
Create();
|
||||
}
|
||||
|
||||
internal void End()
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
}
|
||||
92
Awperative/Kernel/Types/Transform/Transform.cs
Normal file
92
Awperative/Kernel/Types/Transform/Transform.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
|
||||
namespace Gravity.Kernel;
|
||||
|
||||
public sealed class Transform
|
||||
{
|
||||
|
||||
public event EventHandler<TransformModifyEvent> OnTransformChangedEvent;
|
||||
|
||||
|
||||
public Vector2 Origin {
|
||||
get => _origin; set {
|
||||
if(!value.Equals(_origin))
|
||||
OnTransformChangedEvent?.Invoke(this, TransformModifyEvent.FromPosition(this, value)); _origin = value;
|
||||
}
|
||||
}
|
||||
private Vector2 _origin = Vector2.Zero;
|
||||
|
||||
public Vector2 Position {
|
||||
get => _position; set {
|
||||
if(!value.Equals(_position))
|
||||
OnTransformChangedEvent?.Invoke(this, TransformModifyEvent.FromPosition(this, value)); _position = value;
|
||||
}
|
||||
}
|
||||
private Vector2 _position = Vector2.Zero;
|
||||
|
||||
public float Depth {
|
||||
get => _depth; set {
|
||||
if(!value.Equals(_depth))
|
||||
OnTransformChangedEvent?.Invoke(this, TransformModifyEvent.FromDepth(this, value)); _depth = value;
|
||||
}
|
||||
}
|
||||
private float _depth = 0f;
|
||||
|
||||
public float Rotation {
|
||||
get => _rotation; set {
|
||||
if(!value.Equals(_rotation))
|
||||
OnTransformChangedEvent?.Invoke(this, TransformModifyEvent.FromRotation(this, value)); _rotation = value;
|
||||
}
|
||||
}
|
||||
private float _rotation = 0f;
|
||||
|
||||
public Vector2 Scale {
|
||||
get => _scale; set {
|
||||
if(!value.Equals(_scale))
|
||||
OnTransformChangedEvent?.Invoke(this, TransformModifyEvent.FromScale(this, value)); _scale = value;
|
||||
}
|
||||
}
|
||||
private Vector2 _scale = Vector2.One;
|
||||
|
||||
public void Set(Vector2 __origin, Vector2 __position, float __depth, float __rotation, Vector2 __scale)
|
||||
{
|
||||
//todo: rename to previous and check names`
|
||||
var previous = Clone();
|
||||
bool changed = false;
|
||||
|
||||
if (!_origin.Equals(_origin)) { _origin = __origin; changed = true; }
|
||||
if (!_position.Equals(_position)) { _position = __position; changed = true; }
|
||||
if (!_depth.Equals(_depth)) { _depth = __depth; changed = true; }
|
||||
if (!_rotation.Equals(_rotation)) { _rotation = __rotation; changed = true; }
|
||||
if (!_scale.Equals(_scale)) { _scale = __scale; changed = true; }
|
||||
|
||||
if (changed)
|
||||
OnTransformChangedEvent?.Invoke(this, TransformModifyEvent.FromTransforms(this, previous));
|
||||
}
|
||||
|
||||
public Transform() {}
|
||||
|
||||
public Transform(Vector2 __origin, Vector2 __position, float __depth, float __rotation, Vector2 __scale) {
|
||||
Origin = __origin; Position = __position; Depth = __depth; Rotation = __rotation; Scale = __scale;
|
||||
}
|
||||
|
||||
|
||||
//todo: operators?
|
||||
|
||||
public Transform Clone()
|
||||
{
|
||||
return new Transform(Origin, Position, Depth, Rotation, Scale);
|
||||
}
|
||||
|
||||
public Matrix ToMatrix()
|
||||
{
|
||||
return
|
||||
Matrix.CreateTranslation(new Vector3(-Position, 0f)) *
|
||||
Matrix.CreateTranslation(new Vector3(-Origin, 0f)) *
|
||||
Matrix.CreateRotationZ(Rotation) *
|
||||
Matrix.CreateScale(new Vector3(Scale, 1f)) *
|
||||
Matrix.CreateTranslation(new Vector3(Origin, 0f));
|
||||
}
|
||||
}
|
||||
24
Awperative/Kernel/todo.txt
Normal file
24
Awperative/Kernel/todo.txt
Normal file
@@ -0,0 +1,24 @@
|
||||
events system
|
||||
|
||||
json parser
|
||||
|
||||
cool lossless compressor to make my files look more complex
|
||||
|
||||
name save files something like ansf
|
||||
|
||||
//todo: spinny loady wheel, error graphic and make it so multiple scenes can be loaded and modularized, make it so behaviors can be enabled and disabled and merging scenes loading
|
||||
|
||||
body tags, behavior and component tags search methods add a way to enforce one component between all scenes, behaviors and components upgrade base script
|
||||
|
||||
show colliders option
|
||||
|
||||
streamline asset registries
|
||||
|
||||
make the grass go in blocks kinda
|
||||
and add a fading border to the edge of grass
|
||||
|
||||
add multiple languages
|
||||
|
||||
add collision layers and triggers
|
||||
|
||||
add aabb change events
|
||||
Reference in New Issue
Block a user