From ccf4db2f9fce7a2510d09fa012a368e5a7cf32bf Mon Sep 17 00:00:00 2001 From: Maddy Thorson Date: Sun, 7 Feb 2021 18:01:14 -0800 Subject: [PATCH] Grid out of bounds checks snap into bounds. RemoveSelf->Destroy --- src/Components/Logic/Timer.bf | 2 +- src/Components/Logic/Tween.bf | 8 +-- src/Core/Component.bf | 4 +- src/Core/Entity.bf | 2 +- src/Physics/Grid.bf | 122 ++-------------------------------- 5 files changed, 13 insertions(+), 125 deletions(-) diff --git a/src/Components/Logic/Timer.bf b/src/Components/Logic/Timer.bf index 9c2d547..c290109 100644 --- a/src/Components/Logic/Timer.bf +++ b/src/Components/Logic/Timer.bf @@ -33,7 +33,7 @@ namespace Strawberry value = 0; OnComplete?.Invoke(); if (RemoveOnComplete) - RemoveSelf(); + Entity.Remove(this); } } } diff --git a/src/Components/Logic/Tween.bf b/src/Components/Logic/Tween.bf index 1b4ab03..976a7a1 100644 --- a/src/Components/Logic/Tween.bf +++ b/src/Components/Logic/Tween.bf @@ -6,7 +6,7 @@ namespace Strawberry public Ease.Easer Easer ~ delete _; public delegate void(float) OnUpdate ~ delete _; public delegate void() OnComplete ~ delete _; - public bool RemoveOnComplete; + public bool DestroyOnComplete; public bool Playing { get; private set; } public float T { get; private set; } @@ -17,7 +17,7 @@ namespace Strawberry Easer = easer; OnUpdate = onUpdate; OnComplete = onComplete; - RemoveOnComplete = removeOnComplete; + DestroyOnComplete = removeOnComplete; } [Inline] @@ -45,8 +45,8 @@ namespace Strawberry { OnComplete?.Invoke(); Playing = false; - if (RemoveOnComplete) - RemoveSelf(); + if (DestroyOnComplete) + Destroy(); } } } diff --git a/src/Core/Component.bf b/src/Core/Component.bf index 5d14309..2dccd7f 100644 --- a/src/Core/Component.bf +++ b/src/Core/Component.bf @@ -12,9 +12,9 @@ namespace Strawberry public virtual void End() { } [Inline] - public void RemoveSelf() + public void Destroy() { - Entity?.Remove(this); + Entity.Destroy(); } [Inline] diff --git a/src/Core/Entity.bf b/src/Core/Entity.bf index e5a82ca..d403fa6 100644 --- a/src/Core/Entity.bf +++ b/src/Core/Entity.bf @@ -58,7 +58,7 @@ namespace Strawberry } [Inline] - public void RemoveSelf() + public void Destroy() { Scene?.Remove(this); } diff --git a/src/Physics/Grid.bf b/src/Physics/Grid.bf index 80b2fdf..cf39d73 100644 --- a/src/Physics/Grid.bf +++ b/src/Physics/Grid.bf @@ -46,7 +46,7 @@ namespace Strawberry [Inline] get { - return contents[x, y]; + return contents[Math.Clamp(x, 0, CellsX - 1), Math.Clamp(y, 0, CellsY - 1)]; } [Inline] @@ -61,13 +61,13 @@ namespace Strawberry [Inline] get { - return contents[p.X, p.Y]; + return this[p.X, p.Y]; } [Inline] set { - contents[p.X, p.Y] = value; + this[p.X, p.Y] = value; } } @@ -169,7 +169,7 @@ namespace Strawberry for (int y = from.Y; y < to.Y; y++) { let p = Point(x, y); - if (IsInBounds(p) && this[p] != '0') + if (this[p] != '0') return true; } } @@ -177,122 +177,10 @@ namespace Strawberry 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) { Point check = (point - Offset) / CellSize; - return IsInBounds(check) && this[check] != '0'; + return this[check] != '0'; } public void Draw(Color color)