Entity and Component Ended()

This commit is contained in:
Matt Thorson 2020-09-19 22:46:45 -07:00
parent 8747ae1cab
commit 610ef2308f
10 changed files with 194 additions and 39 deletions

View File

@ -17,11 +17,6 @@ namespace Strawberry
delete Action; delete Action;
} }
public override void Started()
{
}
public override void Update() public override void Update()
{ {
if (Action != null) if (Action != null)
@ -32,10 +27,5 @@ namespace Strawberry
break; break;
} }
} }
public override void Draw()
{
}
} }
} }

View File

@ -35,11 +35,6 @@ namespace Strawberry
CallUpdate(); CallUpdate();
} }
public override void Draw()
{
}
public void Add(TIndex state, delegate void() enter = null, delegate TIndex() update = null, delegate void() exit = null) public void Add(TIndex state, delegate void() enter = null, delegate TIndex() update = null, delegate void() exit = null)
{ {
let s = new State(); let s = new State();

View File

@ -31,11 +31,6 @@ namespace Strawberry
RemoveOnComplete = destroyOnComplete; RemoveOnComplete = destroyOnComplete;
} }
public override void Started()
{
}
public override void Update() public override void Update()
{ {
if (value > 0) if (value > 0)
@ -53,11 +48,6 @@ namespace Strawberry
} }
} }
public override void Draw()
{
}
public float Value public float Value
{ {
[Inline] [Inline]

View File

@ -27,11 +27,6 @@ namespace Strawberry
Active = true; Active = true;
} }
public override void Started()
{
}
public override void Update() public override void Update()
{ {
T = Math.Min(T + Time.Delta, 1); T = Math.Min(T + Time.Delta, 1);
@ -45,10 +40,5 @@ namespace Strawberry
RemoveSelf(); RemoveSelf();
} }
} }
public override void Draw()
{
}
} }
} }

View File

@ -25,9 +25,10 @@ namespace Strawberry
Entity = null; Entity = null;
} }
public abstract void Started(); public virtual void Started() { }
public abstract void Update(); public virtual void Ended() { }
public abstract void Draw(); public virtual void Update() { }
public virtual void Draw() { }
[Inline] [Inline]
public void RemoveSelf() public void RemoveSelf()

View File

@ -36,6 +36,7 @@ namespace Strawberry
private void Removed() private void Removed()
{ {
Ended();
Scene = null; Scene = null;
} }
@ -45,6 +46,12 @@ namespace Strawberry
c.Started(); c.Started();
} }
public virtual void Ended()
{
for (var c in components)
c.Ended();
}
public virtual void Update() public virtual void Update()
{ {
for (var c in components) for (var c in components)
@ -382,6 +389,46 @@ namespace Strawberry
return null; return null;
} }
public T LeftmostOutside<T>(Point offset) where T : Entity
{
T ret = null;
for (var e in Scene.All<T>(scope List<T>()))
if (CheckOutside(e, offset) && (ret == null || e.Left < ret.Left))
ret = e;
return ret;
}
public T RightmostOutside<T>(Point offset) where T : Entity
{
T ret = null;
for (var e in Scene.All<T>(scope List<T>()))
if (CheckOutside(e, offset) && (ret == null || e.Right > ret.Right))
ret = e;
return ret;
}
public T TopmostOutside<T>(Point offset) where T : Entity
{
T ret = null;
for (var e in Scene.All<T>(scope List<T>()))
if (CheckOutside(e, offset) && (ret == null || e.Top < ret.Top))
ret = e;
return ret;
}
public T BottommostOutside<T>(Point offset) where T : Entity
{
T ret = null;
for (var e in Scene.All<T>(scope List<T>()))
if (CheckOutside(e, offset) && (ret == null || e.Bottom > ret.Bottom))
ret = e;
return ret;
}
public List<T> All<T>(List<T> into) where T : Entity public List<T> All<T>(List<T> into) where T : Entity
{ {
for (var e in Scene.All<T>(scope List<T>())) for (var e in Scene.All<T>(scope List<T>()))

View File

@ -250,5 +250,27 @@ namespace Strawberry
return componentTracker[typeof(T)].Count; return componentTracker[typeof(T)].Count;
} }
public List<T> All<T>(List<T> into) where T : Component
{
for (let c in componentTracker[typeof(T)])
into.Add(c as T);
return into;
}
public List<T> All<T>(Point point, List<T> into) where T : Component
{
for (let c in componentTracker[typeof(T)])
if (c.Entity.Check(point))
into.Add(c as T);
return into;
}
public List<T> All<T>(Rect rect, List<T> into) where T : Component
{
for (let c in componentTracker[typeof(T)])
if (c.Entity.Check(rect))
into.Add(c as T);
return into;
}
} }
} }

View File

@ -172,6 +172,118 @@ namespace Strawberry
return false; return false;
} }
public bool CheckTop(Rect rect, out int top)
{
Point from = .(
(int)Math.Floor((rect.X - Offset.X) / (float)CellSize.X),
(int)Math.Floor((rect.Y - Offset.Y) / (float)CellSize.Y)
);
Point to = .(
(int)Math.Ceiling((rect.Right - Offset.X) / (float)CellSize.X),
(int)Math.Ceiling((rect.Bottom - Offset.Y) / (float)CellSize.Y)
);
for (int y = from.Y; y < to.Y; y++)
{
for (int x = from.X; x < to.X; x++)
{
let p = Point(x, y);
if (IsInBounds(p) && this[p] != '0')
{
top = Offset.Y + y * CellSize.Y;
return true;
}
}
}
top = 0;
return false;
}
public bool CheckBottom(Rect rect, out int bottom)
{
Point from = .(
(int)Math.Floor((rect.X - Offset.X) / (float)CellSize.X),
(int)Math.Floor((rect.Y - Offset.Y) / (float)CellSize.Y)
);
Point to = .(
(int)Math.Ceiling((rect.Right - Offset.X) / (float)CellSize.X),
(int)Math.Ceiling((rect.Bottom - Offset.Y) / (float)CellSize.Y)
);
for (int y = to.Y - 1; y >= from.Y; y--)
{
for (int x = from.X; x < to.X; x++)
{
let p = Point(x, y);
if (IsInBounds(p) && this[p] != '0')
{
bottom = Offset.Y + (y + 1) * CellSize.Y;
return true;
}
}
}
bottom = 0;
return false;
}
public bool CheckLeft(Rect rect, out int left)
{
Point from = .(
(int)Math.Floor((rect.X - Offset.X) / (float)CellSize.X),
(int)Math.Floor((rect.Y - Offset.Y) / (float)CellSize.Y)
);
Point to = .(
(int)Math.Ceiling((rect.Right - Offset.X) / (float)CellSize.X),
(int)Math.Ceiling((rect.Bottom - Offset.Y) / (float)CellSize.Y)
);
for (int x = from.X; x < to.X; x++)
{
for (int y = from.Y; y < to.Y; y++)
{
let p = Point(x, y);
if (IsInBounds(p) && this[p] != '0')
{
left = Offset.X + x * CellSize.X;
return true;
}
}
}
left = 0;
return false;
}
public bool CheckRight(Rect rect, out int right)
{
Point from = .(
(int)Math.Floor((rect.X - Offset.X) / (float)CellSize.X),
(int)Math.Floor((rect.Y - Offset.Y) / (float)CellSize.Y)
);
Point to = .(
(int)Math.Ceiling((rect.Right - Offset.X) / (float)CellSize.X),
(int)Math.Ceiling((rect.Bottom - Offset.Y) / (float)CellSize.Y)
);
for (int x = to.X - 1; x >= from.X; x--)
{
for (int y = from.Y; y < to.Y; y++)
{
let p = Point(x, y);
if (IsInBounds(p) && this[p] != '0')
{
right = Offset.X + (x + 1) * CellSize.X;
return true;
}
}
}
right = 0;
return false;
}
public bool Check(Point point) public bool Check(Point point)
{ {
Point check = (point - Offset) / CellSize; Point check = (point - Offset) / CellSize;

View File

@ -92,6 +92,11 @@ namespace Strawberry
} }
} }
static public Cardinals operator -(Cardinals c)
{
return c.Opposite();
}
static public implicit operator Cardinals(Facings f) static public implicit operator Cardinals(Facings f)
{ {
if (f == Facings.Right) if (f == Facings.Right)

View File

@ -35,6 +35,9 @@ namespace Strawberry
public Vector Normalized() public Vector Normalized()
{ {
if (X == 0 && Y == 0)
return this;
else
return this / Length; return this / Length;
} }