2020-05-05 11:50:38 +08:00
|
|
|
using System.Collections;
|
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace Strawberry
|
|
|
|
{
|
|
|
|
public class Scene
|
|
|
|
{
|
2020-05-05 12:11:12 +08:00
|
|
|
public float TimeStarted { get; private set; }
|
|
|
|
|
2020-05-05 11:50:38 +08:00
|
|
|
private List<Entity> entities;
|
|
|
|
private HashSet<Entity> toRemove;
|
|
|
|
private HashSet<Entity> toAdd;
|
|
|
|
|
|
|
|
public this()
|
|
|
|
{
|
|
|
|
entities = new List<Entity>();
|
|
|
|
toAdd = new HashSet<Entity>();
|
|
|
|
toRemove = new HashSet<Entity>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public ~this()
|
|
|
|
{
|
|
|
|
for (var e in entities)
|
|
|
|
if (e.DeleteOnRemove)
|
|
|
|
delete e;
|
|
|
|
delete entities;
|
|
|
|
|
|
|
|
for (var e in toAdd)
|
|
|
|
if (e.DeleteOnRemove)
|
|
|
|
delete e;
|
|
|
|
delete toAdd;
|
|
|
|
|
|
|
|
delete toRemove;
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void Started()
|
|
|
|
{
|
2020-05-05 12:11:12 +08:00
|
|
|
TimeStarted = Time.Elapsed;
|
2020-05-05 11:50:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void Update()
|
|
|
|
{
|
|
|
|
UpdateLists();
|
|
|
|
for (var e in entities)
|
|
|
|
if (e.Active)
|
|
|
|
e.Update();
|
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void Draw()
|
|
|
|
{
|
|
|
|
for (var e in entities)
|
|
|
|
if (e.Visible)
|
|
|
|
e.Draw();
|
|
|
|
}
|
|
|
|
|
|
|
|
public T Add<T>(T e) where T : Entity
|
|
|
|
{
|
|
|
|
if (e.Scene == null)
|
|
|
|
toAdd.Add(e);
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
public T Remove<T>(T e) where T : Entity
|
|
|
|
{
|
|
|
|
if (e.Scene == this)
|
|
|
|
toRemove.Add(e);
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UpdateLists()
|
|
|
|
{
|
|
|
|
if (toRemove.Count > 0)
|
|
|
|
{
|
|
|
|
for (var e in toRemove)
|
|
|
|
{
|
|
|
|
entities.Remove(e);
|
|
|
|
e.Removed();
|
|
|
|
if (e.DeleteOnRemove)
|
|
|
|
delete e;
|
|
|
|
}
|
|
|
|
|
|
|
|
toRemove.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (toAdd.Count > 0)
|
|
|
|
{
|
|
|
|
for (var e in toAdd)
|
|
|
|
{
|
|
|
|
entities.Add(e);
|
|
|
|
e.Added(this);
|
|
|
|
}
|
2020-05-06 10:53:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (var e in entities)
|
|
|
|
e.UpdateLists();
|
2020-05-05 11:50:38 +08:00
|
|
|
|
2020-05-06 10:53:13 +08:00
|
|
|
if (toAdd.Count > 0)
|
|
|
|
{
|
2020-05-05 11:50:38 +08:00
|
|
|
for (var e in toAdd)
|
|
|
|
e.Started();
|
|
|
|
|
|
|
|
toAdd.Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 12:11:12 +08:00
|
|
|
// Time
|
|
|
|
|
|
|
|
public float TimeElapsed
|
|
|
|
{
|
|
|
|
[Inline]
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return Time.Elapsed - TimeStarted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public float PreviousTimeElapsed
|
|
|
|
{
|
|
|
|
[Inline]
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return Time.PreviousElapsed - TimeStarted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool TimeOnInterval(float interval, float offset = 0)
|
|
|
|
{
|
|
|
|
return (int)((TimeElapsed - offset) / interval) != (int)((PreviousTimeElapsed - offset) / interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool TimeBetweenInterval(float interval, float offset = 0)
|
|
|
|
{
|
|
|
|
return (TimeElapsed - offset) % (interval * 2) >= interval;
|
|
|
|
}
|
|
|
|
|
2020-05-05 11:50:38 +08:00
|
|
|
// Finding Entities
|
|
|
|
|
|
|
|
public T First<T>() where T : Entity
|
|
|
|
{
|
|
|
|
for (var e in entities)
|
|
|
|
if (e is T)
|
|
|
|
return e as T;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public T First<T>(Point point) where T : Entity
|
|
|
|
{
|
|
|
|
for (var e in entities)
|
|
|
|
if (e is T && e.Check(point))
|
|
|
|
return e as T;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public T First<T>(Rect rect) where T : Entity
|
|
|
|
{
|
|
|
|
for (var e in entities)
|
|
|
|
if (e is T && e.Check(rect))
|
|
|
|
return e as T;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<T> All<T>(List<T> into) where T : Entity
|
|
|
|
{
|
|
|
|
for (var e in entities)
|
|
|
|
if (e is T)
|
|
|
|
into.Add(e as T);
|
|
|
|
return into;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<T> All<T>(Point point, List<T> into) where T : Entity
|
|
|
|
{
|
|
|
|
for (var e in entities)
|
|
|
|
if (e is T && e.Check(point))
|
|
|
|
into.Add(e as T);
|
|
|
|
return into;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<T> All<T>(Rect rect, List<T> into) where T : Entity
|
|
|
|
{
|
|
|
|
for (var e in entities)
|
|
|
|
if (e is T && e.Check(rect))
|
|
|
|
into.Add(e as T);
|
|
|
|
return into;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|