Finished docking

This commit is contained in:
2026-02-08 20:58:08 -05:00
parent 67fca0c271
commit a6555e3a48
41 changed files with 194 additions and 407 deletions

View File

@@ -1,134 +0,0 @@
using System;
using System.Collections.Generic;
namespace Awperative;
public sealed partial class Scene : DockerEntity
{
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

@@ -7,8 +7,6 @@ namespace Awperative;
public sealed partial class Scene : DockerEntity
{
public List<Body> bodies { get; private set; } = [];
public Body AddBody(Transform __transform) {
Body body = new Body(this, __transform);
bodies.Add(body);

View File

@@ -1,10 +1,35 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
namespace Awperative;
public sealed partial class Scene : DockerEntity
{
public List<Body> bodies { get; private set; } = [];
public void Unload() {
foreach (Component component in _components) component.Unload();
foreach (Body body in bodies.ToList()) body.Unload();
}
public void Load() {
foreach (Component component in _components) component.Load();
foreach (Body body in bodies.ToList()) { body.Load(); }
}
public void Update(GameTime __gameTime) {
foreach (Component component in _components) component.Update(__gameTime);
foreach (Body body in bodies.ToList()) { body.Update(__gameTime); }
}
public void Draw(GameTime __gameTime) {
foreach (Component component in _components) component.Draw(__gameTime);
foreach (Body body in bodies.ToList()) { body.Draw(__gameTime); }
}
//todo: add scene.destroy in v5
}

View File

@@ -6,9 +6,6 @@ namespace Awperative;
public sealed partial class Scene : DockerEntity
{
public event EventHandler<BehaviorCreateEvent> BehaviorCreatedEvent;
public event EventHandler<BehaviorDestroyEvent> BehaviorDestroyedEvent;
public event EventHandler<BodyCreateEvent> BodyCreatedEvent;
public event EventHandler<BodyDestroyEvent> BodyDestroyedEvent;

View File

@@ -1,29 +0,0 @@
using System.Linq;
using Microsoft.Xna.Framework;
namespace Awperative;
public sealed partial class Scene : DockerEntity
{
public void Unload() {
foreach (Behavior behavior in behaviors.ToList()) behavior.Unload();
foreach (Body body in bodies.ToList()) body.Unload();
}
public void Load() {
foreach (Behavior behavior in behaviors.ToList()) { behavior.Load(); }
foreach (Body body in bodies.ToList()) { body.Load(); }
}
public void Update(GameTime __gameTime) {
foreach (Behavior behavior in behaviors.ToList()) { behavior.Update(__gameTime); }
foreach (Body body in bodies.ToList()) { body.Update(__gameTime); }
}
public void Draw(GameTime __gameTime) {
foreach (Behavior behavior in behaviors.ToList()) { behavior.Draw(__gameTime); }
foreach (Body body in bodies.ToList()) { body.Draw(__gameTime); }
}
}