Cleaned up entity tracking. Scene.Camera. Matrix struct.

This commit is contained in:
Matt Thorson
2020-05-22 23:39:46 -07:00
parent 292213dae5
commit fe3672f30c
5 changed files with 582 additions and 72 deletions

View File

@ -25,7 +25,10 @@ namespace Strawberry
private Scene scene;
private Scene switchToScene;
private bool updating;
private Dictionary<Type, List<Type>> entityAssignableLists;
private Dictionary<Type, List<Type>> componentAssignableLists;
//SDL Vars
public SDL.Renderer* Renderer { get; private set; }
public Color ClearColor = .Black;
@ -78,7 +81,7 @@ namespace Strawberry
SDLMixer.OpenAudio(44100, SDLMixer.MIX_DEFAULT_FORMAT, 2, 4096);
SDLTTF.Init();
TypeTree.[Friend]Build();
BuildTypeLists();
Input.[Friend]Init(gamepadLimit);
}
@ -99,7 +102,7 @@ namespace Strawberry
delete VirtualInputs;
}
TypeTree.[Friend]Dispose();
DisposeTypeLists();
Input.[Friend]Dispose();
Game = null;
}
@ -210,5 +213,57 @@ namespace Strawberry
switchToScene = value;
}
}
// Type assignable caching
private void BuildTypeLists()
{
/*
For each Type that extends Entity, we build a list of all the other Entity Types that it is assignable to.
We cache these lists, and use them later to bucket Entities as they are added to the Scene.
This allows us to retrieve Entities by type very easily.
*/
entityAssignableLists = new Dictionary<Type, List<Type>>();
for (let type in Type.Enumerator())
{
if (type != typeof(Entity) && type.IsSubtypeOf(typeof(Entity)))
{
let list = new List<Type>();
for (let check in Type.Enumerator())
if (check != typeof(Entity) && check.IsSubtypeOf(typeof(Entity)) && type.IsSubtypeOf(check))
list.Add(check);
entityAssignableLists.Add(type, list);
}
}
/*
And then we also do this for components
*/
componentAssignableLists = new Dictionary<Type, List<Type>>();
for (let type in Type.Enumerator())
{
if (type != typeof(Component) && type.IsSubtypeOf(typeof(Component)))
{
let list = new List<Type>();
for (let check in Type.Enumerator())
if (check != typeof(Component) && check.IsSubtypeOf(typeof(Component)) && type.IsSubtypeOf(check))
list.Add(check);
componentAssignableLists.Add(type, list);
}
}
}
private void DisposeTypeLists()
{
for (let list in entityAssignableLists.Values)
delete list;
delete entityAssignableLists;
for (let list in componentAssignableLists.Values)
delete list;
delete componentAssignableLists;
}
}
}

View File

@ -8,6 +8,7 @@ namespace Strawberry
public float TimeStarted { get; private set; }
public Grid SolidGrid;
public Rect Bounds;
public Vector Camera;
private List<Entity> entities;
private HashSet<Entity> toRemove;
@ -22,11 +23,11 @@ namespace Strawberry
toRemove = new HashSet<Entity>();
entityTracker = new Dictionary<Type, List<Entity>>();
for (let type in TypeTree.[Friend]EntityAssignableLists.Keys)
for (let type in Game.[Friend]entityAssignableLists.Keys)
entityTracker.Add(type, new List<Entity>());
componentTracker = new Dictionary<Type, List<Component>>();
for (let type in TypeTree.[Friend]ComponentAssignableLists.Keys)
for (let type in Game.[Friend]componentAssignableLists.Keys)
componentTracker.Add(type, new List<Component>());
}
@ -63,6 +64,8 @@ namespace Strawberry
public virtual void Update()
{
Camera.X += 20 * Time.Delta;
UpdateLists();
for (var e in entities)
if (e.Active)
@ -132,7 +135,7 @@ namespace Strawberry
private void TrackEntity(Entity e)
{
for (let t in TypeTree.[Friend]EntityAssignableLists[e.GetType()])
for (let t in Game.[Friend]entityAssignableLists[e.GetType()])
entityTracker[t].Add(e);
for (let c in e.[Friend]components)
@ -141,7 +144,7 @@ namespace Strawberry
private void UntrackEntity(Entity e)
{
for (let t in TypeTree.[Friend]EntityAssignableLists[e.GetType()])
for (let t in Game.[Friend]entityAssignableLists[e.GetType()])
entityTracker[t].Remove(e);
for (let c in e.[Friend]components)
@ -150,13 +153,13 @@ namespace Strawberry
private void TrackComponent(Component c)
{
for (let t in TypeTree.[Friend]ComponentAssignableLists[c.GetType()])
for (let t in Game.[Friend]componentAssignableLists[c.GetType()])
componentTracker[t].Add(c);
}
private void UntrackComponent(Component c)
{
for (let t in TypeTree.[Friend]ComponentAssignableLists[c.GetType()])
for (let t in Game.[Friend]componentAssignableLists[c.GetType()])
componentTracker[t].Remove(c);
}