From ce099d0cc8d7f8f3891ad2861c01e8107b7e7eae Mon Sep 17 00:00:00 2001 From: Matt Thorson Date: Sat, 12 Sep 2020 23:52:13 -0700 Subject: [PATCH] Moved example physics stuff into the sample game --- SampleGame/src/Level.bf | 2 ++ SampleGame/src/Player.bf | 2 +- .../src/physics}/Actor.bf | 33 ++++++++++++------- .../src/physics}/Collision.bf | 7 ++-- .../src/physics}/Geometry.bf | 2 +- .../src/physics}/JumpThru.bf | 3 +- .../src/physics}/Solid.bf | 2 +- src/Components/Logic/StateMachine.bf | 25 +++++--------- src/Core/Entity.bf | 16 ++++----- src/Core/Scene.bf | 25 +++++++++++--- src/JSON/JSON.bf | 3 ++ src/Physics/Grid.bf | 12 ++++++- src/PlatformLayer/SDL2/SDL2Batcher.bf | 1 + src/Struct/Cardinals.bf | 15 +++++++++ src/Struct/Point.bf | 5 +++ src/Struct/Rect.bf | 12 +++---- src/Struct/Vector.bf | 19 +++++++++++ 17 files changed, 127 insertions(+), 57 deletions(-) rename {src/Physics => SampleGame/src/physics}/Actor.bf (84%) rename {src/Physics => SampleGame/src/physics}/Collision.bf (62%) rename {src/Physics => SampleGame/src/physics}/Geometry.bf (97%) rename {src/Physics => SampleGame/src/physics}/JumpThru.bf (97%) rename {src/Physics => SampleGame/src/physics}/Solid.bf (98%) diff --git a/SampleGame/src/Level.bf b/SampleGame/src/Level.bf index 69bb0ba..893542a 100644 --- a/SampleGame/src/Level.bf +++ b/SampleGame/src/Level.bf @@ -2,6 +2,8 @@ namespace Strawberry.Sample { public class Level : Scene { + public Grid SolidGrid { get; private set; } + public this() { Add(new Player(.(50, 50))); diff --git a/SampleGame/src/Player.bf b/SampleGame/src/Player.bf index 5f3ad3b..99342ad 100644 --- a/SampleGame/src/Player.bf +++ b/SampleGame/src/Player.bf @@ -103,7 +103,7 @@ namespace Strawberry.Sample base.Draw(); DrawHitbox(.Green); - Game.Batcher.Tex(Assets.Textures["test"], X - 4, Y - 8); + //Game.Batcher.Tex(Assets.Textures["test"], X - 4, Y - 8); } } } diff --git a/src/Physics/Actor.bf b/SampleGame/src/physics/Actor.bf similarity index 84% rename from src/Physics/Actor.bf rename to SampleGame/src/physics/Actor.bf index f06812a..368b7f8 100644 --- a/src/Physics/Actor.bf +++ b/SampleGame/src/physics/Actor.bf @@ -1,6 +1,6 @@ using System; -namespace Strawberry +namespace Strawberry.Sample { [Reflect] public class Actor : Entity @@ -16,9 +16,21 @@ namespace Strawberry } + public Level Level => SceneAs(); + + public bool Check(Level level) + { + return level.SolidGrid != null && Check(level.SolidGrid); + } + + public bool Check(Level level, Point offset) + { + return level.SolidGrid != null && Check(level.SolidGrid, offset); + } + public bool GroundCheck(int distance = 1) { - return Check(.(0, distance)) || Check(Scene, .(0, distance)) || CheckOutside(.(0, distance)); + return Check(.(0, distance)) || Check(Level, .(0, distance)) || CheckOutside(.(0, distance)); } public virtual bool IsRiding(Solid solid) @@ -92,7 +104,7 @@ namespace Strawberry if (hit != null) { let c = Collision( - Point.Right * sign, + Cardinals.FromPoint(Point.Right * sign), Math.Abs(amount), Math.Abs(amount - move), hit, @@ -103,10 +115,10 @@ namespace Strawberry return true; } - if (Check(Scene, .(sign, 0))) + if (Check(Level, .(sign, 0))) { let c = Collision( - Point.Right * sign, + Cardinals.FromPoint(Point.Right * sign), Math.Abs(amount), Math.Abs(amount - move), null, @@ -141,7 +153,7 @@ namespace Strawberry if (hit != null) { let c = Collision( - Point.Right * sign, + Cardinals.FromPoint(Point.Down * sign), Math.Abs(amount), Math.Abs(amount - move), hit, @@ -152,10 +164,10 @@ namespace Strawberry return true; } - if (Check(Scene, .(0, sign))) + if (Check(Level, .(0, sign))) { let c = Collision( - Point.Right * sign, + Cardinals.FromPoint(Point.Down * sign), Math.Abs(amount), Math.Abs(amount - move), null, @@ -195,7 +207,6 @@ namespace Strawberry MovedByGeometry += amount; } - public bool CornerCorrection(Cardinals direction, int maxAmount, int lookAhead = 1, int onlySign = 0) { Point dir = direction; @@ -205,9 +216,9 @@ namespace Strawberry delegate bool(Point) checker; if (dir == Point.Down) - checker = scope:: (p) => !Check(Scene, p) && !Check(p) && !CheckOutside(p); + checker = scope:: (p) => !Check(Level, p) && !Check(p) && !CheckOutside(p); else - checker = scope:: (p) => !Check(Scene, p) && !Check(p); + checker = scope:: (p) => !Check(Level, p) && !Check(p); for (int i = 1; i <= maxAmount; i++) { diff --git a/src/Physics/Collision.bf b/SampleGame/src/physics/Collision.bf similarity index 62% rename from src/Physics/Collision.bf rename to SampleGame/src/physics/Collision.bf index 2df53dd..37fab56 100644 --- a/src/Physics/Collision.bf +++ b/SampleGame/src/physics/Collision.bf @@ -1,14 +1,15 @@ -namespace Strawberry + +namespace Strawberry.Sample { public struct Collision { - public Point Direction; + public Cardinals Direction; public int Magnitude; public int Completed; public Geometry Stopper; public Geometry Pusher; - public this(Point direction, int magnitude, int completed, Geometry stopper, Geometry pusher) + public this(Cardinals direction, int magnitude, int completed, Geometry stopper, Geometry pusher) { Direction = direction; Magnitude = magnitude; diff --git a/src/Physics/Geometry.bf b/SampleGame/src/physics/Geometry.bf similarity index 97% rename from src/Physics/Geometry.bf rename to SampleGame/src/physics/Geometry.bf index e4377b9..cfc9b55 100644 --- a/src/Physics/Geometry.bf +++ b/SampleGame/src/physics/Geometry.bf @@ -1,7 +1,7 @@ using System; using System.Collections; -namespace Strawberry +namespace Strawberry.Sample { public abstract class Geometry : Entity { diff --git a/src/Physics/JumpThru.bf b/SampleGame/src/physics/JumpThru.bf similarity index 97% rename from src/Physics/JumpThru.bf rename to SampleGame/src/physics/JumpThru.bf index 1605108..97f9ec3 100644 --- a/src/Physics/JumpThru.bf +++ b/SampleGame/src/physics/JumpThru.bf @@ -1,6 +1,7 @@ using System.Collections; using System; -namespace Strawberry + +namespace Strawberry.Sample { public class JumpThru : Geometry { diff --git a/src/Physics/Solid.bf b/SampleGame/src/physics/Solid.bf similarity index 98% rename from src/Physics/Solid.bf rename to SampleGame/src/physics/Solid.bf index 5353b7d..65409c8 100644 --- a/src/Physics/Solid.bf +++ b/SampleGame/src/physics/Solid.bf @@ -1,6 +1,6 @@ using System.Collections; -namespace Strawberry +namespace Strawberry.Sample { public class Solid : Geometry { diff --git a/src/Components/Logic/StateMachine.bf b/src/Components/Logic/StateMachine.bf index a9b96c7..2490eaf 100644 --- a/src/Components/Logic/StateMachine.bf +++ b/src/Components/Logic/StateMachine.bf @@ -40,7 +40,7 @@ namespace Strawberry } - public void Add(TIndex state, delegate TIndex() enter = null, delegate TIndex() update = null, delegate TIndex() exit = null) + public void Add(TIndex state, delegate void() enter = null, delegate TIndex() update = null, delegate void() exit = null) { let s = new State(); s.Enter = enter; @@ -63,12 +63,9 @@ namespace Strawberry if (to != state) { NextState = to; - if (CallExit()) - return true; - + CallExit(); PreviousState = state; state = to; - CallEnter(); return true; } @@ -76,18 +73,15 @@ namespace Strawberry return false; } - private bool CallEnter() + private void CallEnter() { let s = states[state]; if (s != null && s.Enter != null) { inStateCall = true; - let set = s.Enter(); + s.Enter(); inStateCall = false; - return Set(set); } - else - return false; } private bool CallUpdate() @@ -104,25 +98,22 @@ namespace Strawberry return false; } - private bool CallExit() + private void CallExit() { let s = states[state]; if (s != null && s.Exit != null) { inStateCall = true; - let set = s.Exit(); + s.Exit(); inStateCall = false; - return Set(set); } - else - return false; } public class State { - public delegate TIndex() Enter; + public delegate void() Enter; public delegate TIndex() Update; - public delegate TIndex() Exit; + public delegate void() Exit; public ~this() { diff --git a/src/Core/Entity.bf b/src/Core/Entity.bf index 3522679..e5a3202 100644 --- a/src/Core/Entity.bf +++ b/src/Core/Entity.bf @@ -303,16 +303,6 @@ namespace Strawberry return SceneHitbox.Intersects(rect); } - public bool Check(Scene scene) - { - return scene.SolidGrid != null && Check(scene.SolidGrid); - } - - public bool Check(Scene scene, Point offset) - { - return scene.SolidGrid != null && Check(scene.SolidGrid, offset); - } - public bool Check(Grid grid) { return grid != null && grid.Check(SceneHitbox); @@ -426,6 +416,12 @@ namespace Strawberry Game.Batcher.Rect(SceneHitbox, color); } + public T SceneAs() where T : Scene + { + Runtime.Assert(Scene is T, "Scene type mismatch!"); + return Scene as T; + } + static public int Compare(Entity a, Entity b) { return a.Priority <=> b.Priority; diff --git a/src/Core/Scene.bf b/src/Core/Scene.bf index f0b0f0a..5236b84 100644 --- a/src/Core/Scene.bf +++ b/src/Core/Scene.bf @@ -6,7 +6,6 @@ namespace Strawberry public class Scene { public float TimeStarted { get; private set; } - public Grid SolidGrid; public Rect Bounds; private List entities; @@ -32,9 +31,6 @@ namespace Strawberry public ~this() { - if (SolidGrid != null) - delete SolidGrid; - for (var e in entities) if (e.DeleteOnRemove) delete e; @@ -183,6 +179,22 @@ namespace Strawberry return entityTracker[typeof(T)].Count; } + public bool Check(Point point) where T : Entity + { + for (let e in entityTracker[typeof(T)]) + if (e.Check(point)) + return true; + return false; + } + + public bool Check(Rect rect) where T : Entity + { + for (let e in entityTracker[typeof(T)]) + if (e.Check(rect)) + return true; + return false; + } + public T First() where T : Entity { for (let e in entityTracker[typeof(T)]) @@ -229,11 +241,14 @@ namespace Strawberry return into; } - // Finding Components + /* + Finding Components + */ public int Count() where T : Component { return componentTracker[typeof(T)].Count; } + } } diff --git a/src/JSON/JSON.bf b/src/JSON/JSON.bf index 2d07dfa..628be1a 100644 --- a/src/JSON/JSON.bf +++ b/src/JSON/JSON.bf @@ -26,6 +26,9 @@ namespace Strawberry return T.Parse(String, true); } + public Point Point => .(this["x"].Int, this["y"].Int); + public Vector Vector => .(this["x"].Number, this["y"].Number); + private List array; private Dictionary children; diff --git a/src/Physics/Grid.bf b/src/Physics/Grid.bf index adb5589..b13941e 100644 --- a/src/Physics/Grid.bf +++ b/src/Physics/Grid.bf @@ -15,7 +15,10 @@ namespace Strawberry CellSize = .(cellWidth, cellHeight); Offset = .(offsetX, offsetY); - contents = new char8[cellsX, cellsY]; + contents = new char8[cellsX, cellsY]; + for (let x < CellsX) + for (let y < CellsY) + contents[x, y] = '0'; } public this(JSON ogmoJson) @@ -68,6 +71,13 @@ namespace Strawberry } } + public void Set(Rect r, char8 val) + { + for (let x < r.Width) + for (let y < r.Height) + contents[r.X + x, r.Y + y] = val; + } + public int CellsX => contents.GetLength(0); public int CellsY => contents.GetLength(1); diff --git a/src/PlatformLayer/SDL2/SDL2Batcher.bf b/src/PlatformLayer/SDL2/SDL2Batcher.bf index f71a993..d2f0447 100644 --- a/src/PlatformLayer/SDL2/SDL2Batcher.bf +++ b/src/PlatformLayer/SDL2/SDL2Batcher.bf @@ -56,6 +56,7 @@ namespace Strawberry.SDL2 int32 num = 0; GL.glUniform1iv(platformLayer.TextureMatrixLocation, 1, &num); } + GL.glDrawElements(GL.GL_TRIANGLES, b.IndicesCount, GL.GL_UNSIGNED_INT, (void*)(b.IndicesStart * sizeof(uint32))); } diff --git a/src/Struct/Cardinals.bf b/src/Struct/Cardinals.bf index 64cb7be..c16af23 100644 --- a/src/Struct/Cardinals.bf +++ b/src/Struct/Cardinals.bf @@ -115,6 +115,21 @@ namespace Strawberry } } + static public implicit operator Vector(Cardinals c) + { + switch (c) + { + case .Right: + return Vector.Right; + case .Left: + return Vector.Left; + case .Up: + return Vector.Up; + case .Down: + return Vector.Down; + } + } + static public Result FromPoint(Point p) { if (p.X > 0 && p.Y == 0) diff --git a/src/Struct/Point.bf b/src/Struct/Point.bf index 0f7696a..57b1929 100644 --- a/src/Struct/Point.bf +++ b/src/Struct/Point.bf @@ -41,6 +41,11 @@ namespace Strawberry public float Length => Math.Sqrt(LengthSquared); public int LengthSquared => X * X + Y * Y; + public Vector Normalized() + { + return ((Vector)this).Normalized(); + } + public override void ToString(String strBuffer) { strBuffer.Set("Point [ "); diff --git a/src/Struct/Rect.bf b/src/Struct/Rect.bf index f7c525c..fdd8c0e 100644 --- a/src/Struct/Rect.bf +++ b/src/Struct/Rect.bf @@ -139,32 +139,32 @@ namespace Strawberry static public Rect operator+(Rect a, Point b) { - return Rect(a.X + b.X, a.Y + b.Y, a.Width, a.Height); + return .(a.X + b.X, a.Y + b.Y, a.Width, a.Height); } static public Rect operator-(Rect a, Point b) { - return Rect(a.X - b.X, a.Y - b.Y, a.Width, a.Height); + return .(a.X - b.X, a.Y - b.Y, a.Width, a.Height); } static public Rect operator/(Rect a, int b) { - return Rect(a.X / b, a.Y / b, a.Width / b, a.Height / b); + return .(a.X / b, a.Y / b, a.Width / b, a.Height / b); } static public Rect operator/(Rect a, Point b) { - return Rect(a.X / b.X, a.Y / b.Y, a.Width / b.X, a.Height / b.Y); + return .(a.X / b.X, a.Y / b.Y, a.Width / b.X, a.Height / b.Y); } static public Rect operator*(Rect a, int b) { - return Rect(a.X * b, a.Y * b, a.Width * b, a.Height * b); + return .(a.X * b, a.Y * b, a.Width * b, a.Height * b); } static public Rect operator*(Rect a, Point b) { - return Rect(a.X * b.X, a.Y * b.Y, a.Width * b.X, a.Height * b.Y); + return .(a.X * b.X, a.Y * b.Y, a.Width * b.X, a.Height * b.Y); } static public implicit operator SDL2.SDL.Rect(Rect r) diff --git a/src/Struct/Vector.bf b/src/Struct/Vector.bf index d41fc0b..2d7ebd2 100644 --- a/src/Struct/Vector.bf +++ b/src/Struct/Vector.bf @@ -33,6 +33,11 @@ namespace Strawberry return .(-Y, X); } + public Vector Normalized() + { + return this / Length; + } + public float Length => Math.Sqrt(LengthSquared); public float LengthSquared => X * X + Y * Y; @@ -90,5 +95,19 @@ namespace Strawberry { return Vector(a.X / b, a.Y / b); } + + static public Vector Approach(Vector value, Vector target, float maxDelta) + { + Vector diff = target - value; + if (diff.Length < maxDelta) + return target; + else + return value + diff.Normalized() * maxDelta; + } + + static public void Approach(Vector* value, Vector target, float maxDelta) + { + *value = Approach(*value, target, maxDelta); + } } }