Moved example physics stuff into the sample game

This commit is contained in:
Matt Thorson 2020-09-12 23:52:13 -07:00
parent ae21809566
commit ce099d0cc8
17 changed files with 127 additions and 57 deletions

View File

@ -2,6 +2,8 @@ namespace Strawberry.Sample
{ {
public class Level : Scene public class Level : Scene
{ {
public Grid SolidGrid { get; private set; }
public this() public this()
{ {
Add(new Player(.(50, 50))); Add(new Player(.(50, 50)));

View File

@ -103,7 +103,7 @@ namespace Strawberry.Sample
base.Draw(); base.Draw();
DrawHitbox(.Green); DrawHitbox(.Green);
Game.Batcher.Tex(Assets.Textures["test"], X - 4, Y - 8); //Game.Batcher.Tex(Assets.Textures["test"], X - 4, Y - 8);
} }
} }
} }

View File

@ -1,6 +1,6 @@
using System; using System;
namespace Strawberry namespace Strawberry.Sample
{ {
[Reflect] [Reflect]
public class Actor : Entity public class Actor : Entity
@ -16,9 +16,21 @@ namespace Strawberry
} }
public Level Level => SceneAs<Level>();
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) public bool GroundCheck(int distance = 1)
{ {
return Check<Solid>(.(0, distance)) || Check(Scene, .(0, distance)) || CheckOutside<JumpThru>(.(0, distance)); return Check<Solid>(.(0, distance)) || Check(Level, .(0, distance)) || CheckOutside<JumpThru>(.(0, distance));
} }
public virtual bool IsRiding(Solid solid) public virtual bool IsRiding(Solid solid)
@ -92,7 +104,7 @@ namespace Strawberry
if (hit != null) if (hit != null)
{ {
let c = Collision( let c = Collision(
Point.Right * sign, Cardinals.FromPoint(Point.Right * sign),
Math.Abs(amount), Math.Abs(amount),
Math.Abs(amount - move), Math.Abs(amount - move),
hit, hit,
@ -103,10 +115,10 @@ namespace Strawberry
return true; return true;
} }
if (Check(Scene, .(sign, 0))) if (Check(Level, .(sign, 0)))
{ {
let c = Collision( let c = Collision(
Point.Right * sign, Cardinals.FromPoint(Point.Right * sign),
Math.Abs(amount), Math.Abs(amount),
Math.Abs(amount - move), Math.Abs(amount - move),
null, null,
@ -141,7 +153,7 @@ namespace Strawberry
if (hit != null) if (hit != null)
{ {
let c = Collision( let c = Collision(
Point.Right * sign, Cardinals.FromPoint(Point.Down * sign),
Math.Abs(amount), Math.Abs(amount),
Math.Abs(amount - move), Math.Abs(amount - move),
hit, hit,
@ -152,10 +164,10 @@ namespace Strawberry
return true; return true;
} }
if (Check(Scene, .(0, sign))) if (Check(Level, .(0, sign)))
{ {
let c = Collision( let c = Collision(
Point.Right * sign, Cardinals.FromPoint(Point.Down * sign),
Math.Abs(amount), Math.Abs(amount),
Math.Abs(amount - move), Math.Abs(amount - move),
null, null,
@ -195,7 +207,6 @@ namespace Strawberry
MovedByGeometry += amount; MovedByGeometry += amount;
} }
public bool CornerCorrection(Cardinals direction, int maxAmount, int lookAhead = 1, int onlySign = 0) public bool CornerCorrection(Cardinals direction, int maxAmount, int lookAhead = 1, int onlySign = 0)
{ {
Point dir = direction; Point dir = direction;
@ -205,9 +216,9 @@ namespace Strawberry
delegate bool(Point) checker; delegate bool(Point) checker;
if (dir == Point.Down) if (dir == Point.Down)
checker = scope:: (p) => !Check(Scene, p) && !Check<Solid>(p) && !CheckOutside<JumpThru>(p); checker = scope:: (p) => !Check(Level, p) && !Check<Solid>(p) && !CheckOutside<JumpThru>(p);
else else
checker = scope:: (p) => !Check(Scene, p) && !Check<Solid>(p); checker = scope:: (p) => !Check(Level, p) && !Check<Solid>(p);
for (int i = 1; i <= maxAmount; i++) for (int i = 1; i <= maxAmount; i++)
{ {

View File

@ -1,14 +1,15 @@
namespace Strawberry
namespace Strawberry.Sample
{ {
public struct Collision public struct Collision
{ {
public Point Direction; public Cardinals Direction;
public int Magnitude; public int Magnitude;
public int Completed; public int Completed;
public Geometry Stopper; public Geometry Stopper;
public Geometry Pusher; 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; Direction = direction;
Magnitude = magnitude; Magnitude = magnitude;

View File

@ -1,7 +1,7 @@
using System; using System;
using System.Collections; using System.Collections;
namespace Strawberry namespace Strawberry.Sample
{ {
public abstract class Geometry : Entity public abstract class Geometry : Entity
{ {

View File

@ -1,6 +1,7 @@
using System.Collections; using System.Collections;
using System; using System;
namespace Strawberry
namespace Strawberry.Sample
{ {
public class JumpThru : Geometry public class JumpThru : Geometry
{ {

View File

@ -1,6 +1,6 @@
using System.Collections; using System.Collections;
namespace Strawberry namespace Strawberry.Sample
{ {
public class Solid : Geometry public class Solid : Geometry
{ {

View File

@ -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(); let s = new State();
s.Enter = enter; s.Enter = enter;
@ -63,12 +63,9 @@ namespace Strawberry
if (to != state) if (to != state)
{ {
NextState = to; NextState = to;
if (CallExit()) CallExit();
return true;
PreviousState = state; PreviousState = state;
state = to; state = to;
CallEnter(); CallEnter();
return true; return true;
} }
@ -76,18 +73,15 @@ namespace Strawberry
return false; return false;
} }
private bool CallEnter() private void CallEnter()
{ {
let s = states[state]; let s = states[state];
if (s != null && s.Enter != null) if (s != null && s.Enter != null)
{ {
inStateCall = true; inStateCall = true;
let set = s.Enter(); s.Enter();
inStateCall = false; inStateCall = false;
return Set(set);
} }
else
return false;
} }
private bool CallUpdate() private bool CallUpdate()
@ -104,25 +98,22 @@ namespace Strawberry
return false; return false;
} }
private bool CallExit() private void CallExit()
{ {
let s = states[state]; let s = states[state];
if (s != null && s.Exit != null) if (s != null && s.Exit != null)
{ {
inStateCall = true; inStateCall = true;
let set = s.Exit(); s.Exit();
inStateCall = false; inStateCall = false;
return Set(set);
} }
else
return false;
} }
public class State public class State
{ {
public delegate TIndex() Enter; public delegate void() Enter;
public delegate TIndex() Update; public delegate TIndex() Update;
public delegate TIndex() Exit; public delegate void() Exit;
public ~this() public ~this()
{ {

View File

@ -303,16 +303,6 @@ namespace Strawberry
return SceneHitbox.Intersects(rect); 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) public bool Check(Grid grid)
{ {
return grid != null && grid.Check(SceneHitbox); return grid != null && grid.Check(SceneHitbox);
@ -426,6 +416,12 @@ namespace Strawberry
Game.Batcher.Rect(SceneHitbox, color); Game.Batcher.Rect(SceneHitbox, color);
} }
public T SceneAs<T>() where T : Scene
{
Runtime.Assert(Scene is T, "Scene type mismatch!");
return Scene as T;
}
static public int Compare(Entity a, Entity b) static public int Compare(Entity a, Entity b)
{ {
return a.Priority <=> b.Priority; return a.Priority <=> b.Priority;

View File

@ -6,7 +6,6 @@ namespace Strawberry
public class Scene public class Scene
{ {
public float TimeStarted { get; private set; } public float TimeStarted { get; private set; }
public Grid SolidGrid;
public Rect Bounds; public Rect Bounds;
private List<Entity> entities; private List<Entity> entities;
@ -32,9 +31,6 @@ namespace Strawberry
public ~this() public ~this()
{ {
if (SolidGrid != null)
delete SolidGrid;
for (var e in entities) for (var e in entities)
if (e.DeleteOnRemove) if (e.DeleteOnRemove)
delete e; delete e;
@ -183,6 +179,22 @@ namespace Strawberry
return entityTracker[typeof(T)].Count; return entityTracker[typeof(T)].Count;
} }
public bool Check<T>(Point point) where T : Entity
{
for (let e in entityTracker[typeof(T)])
if (e.Check(point))
return true;
return false;
}
public bool Check<T>(Rect rect) where T : Entity
{
for (let e in entityTracker[typeof(T)])
if (e.Check(rect))
return true;
return false;
}
public T First<T>() where T : Entity public T First<T>() where T : Entity
{ {
for (let e in entityTracker[typeof(T)]) for (let e in entityTracker[typeof(T)])
@ -229,11 +241,14 @@ namespace Strawberry
return into; return into;
} }
// Finding Components /*
Finding Components
*/
public int Count<T>() where T : Component public int Count<T>() where T : Component
{ {
return componentTracker[typeof(T)].Count; return componentTracker[typeof(T)].Count;
} }
} }
} }

View File

@ -26,6 +26,9 @@ namespace Strawberry
return T.Parse<T>(String, true); return T.Parse<T>(String, true);
} }
public Point Point => .(this["x"].Int, this["y"].Int);
public Vector Vector => .(this["x"].Number, this["y"].Number);
private List<JSON> array; private List<JSON> array;
private Dictionary<String, JSON> children; private Dictionary<String, JSON> children;

View File

@ -16,6 +16,9 @@ namespace Strawberry
Offset = .(offsetX, offsetY); 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) 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 CellsX => contents.GetLength(0);
public int CellsY => contents.GetLength(1); public int CellsY => contents.GetLength(1);

View File

@ -56,6 +56,7 @@ namespace Strawberry.SDL2
int32 num = 0; int32 num = 0;
GL.glUniform1iv(platformLayer.TextureMatrixLocation, 1, &num); GL.glUniform1iv(platformLayer.TextureMatrixLocation, 1, &num);
} }
GL.glDrawElements(GL.GL_TRIANGLES, b.IndicesCount, GL.GL_UNSIGNED_INT, (void*)(b.IndicesStart * sizeof(uint32))); GL.glDrawElements(GL.GL_TRIANGLES, b.IndicesCount, GL.GL_UNSIGNED_INT, (void*)(b.IndicesStart * sizeof(uint32)));
} }

View File

@ -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<Cardinals> FromPoint(Point p) static public Result<Cardinals> FromPoint(Point p)
{ {
if (p.X > 0 && p.Y == 0) if (p.X > 0 && p.Y == 0)

View File

@ -41,6 +41,11 @@ namespace Strawberry
public float Length => Math.Sqrt(LengthSquared); public float Length => Math.Sqrt(LengthSquared);
public int LengthSquared => X * X + Y * Y; public int LengthSquared => X * X + Y * Y;
public Vector Normalized()
{
return ((Vector)this).Normalized();
}
public override void ToString(String strBuffer) public override void ToString(String strBuffer)
{ {
strBuffer.Set("Point [ "); strBuffer.Set("Point [ ");

View File

@ -139,32 +139,32 @@ namespace Strawberry
static public Rect operator+(Rect a, Point b) 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) 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) 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) 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) 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) 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) static public implicit operator SDL2.SDL.Rect(Rect r)

View File

@ -33,6 +33,11 @@ namespace Strawberry
return .(-Y, X); return .(-Y, X);
} }
public Vector Normalized()
{
return this / Length;
}
public float Length => Math.Sqrt(LengthSquared); public float Length => Math.Sqrt(LengthSquared);
public float LengthSquared => X * X + Y * Y; public float LengthSquared => X * X + Y * Y;
@ -90,5 +95,19 @@ namespace Strawberry
{ {
return Vector(a.X / b, a.Y / b); 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);
}
} }
} }