Fixed Movement, now slide along walls instead of stopping immediately. Also fixed error where chunks don't load their collision

This commit is contained in:
2020-06-25 07:37:58 -06:00
parent 7d4420c54c
commit b6f39d8f12
3 changed files with 37 additions and 31 deletions

View File

@@ -5,13 +5,11 @@ namespace Collector.Character
{ {
public class Collision public class Collision
{ {
private readonly int _tileWidth;
public RectangleF Rectangle { get; private set; } public RectangleF Rectangle { get; private set; }
public Collision(int x, int y,int tileWidth) public Collision(int x, int y)
{ {
_tileWidth = tileWidth; Rectangle = new RectangleF(x,y,IRestrictions.TileSize,IRestrictions.TileSize);
Rectangle = new RectangleF(x,y,tileWidth,tileWidth);
} }
} }
} }

View File

@@ -25,20 +25,27 @@ namespace Collector.Character
} }
public static void Move(OrthographicCamera cam, float x, float y) public static void Move(OrthographicCamera cam, float x, float y)
{
Teleport(cam, x, 0);
if (Chunks.LoadedCollisions.Values.Any(collisionsValue => collisionsValue.Rectangle.Intersects(PlayerBounds)))
{
Teleport(cam, -x, 0);
}
Teleport(cam, 0, y);
if (Chunks.LoadedCollisions.Values.Any(collisionsValue => collisionsValue.Rectangle.Intersects(PlayerBounds)))
{
Teleport(cam, 0, -y);
}
}
private static void Teleport(OrthographicCamera cam, float x, float y)
{ {
X += x; X += x;
Y += y; Y += y;
PlayerBounds.X = X + 0.2f; PlayerBounds.X = X + 0.2f;
PlayerBounds.Y = Y + 1.5f; PlayerBounds.Y = Y + 1.5f;
cam.Move(new Vector2(x, y)); cam.Move(new Vector2(x, y));
if (!Chunks.loadedCollisions.Values.Any(collisionsValue => collisionsValue.Rectangle.Intersects(PlayerBounds))) return;
X -= x;
Y -= y;
cam.Move(new Vector2(-x, -y));
PlayerBounds.X -= X;
PlayerBounds.Y -= Y;
} }
} }
} }

View File

@@ -14,8 +14,8 @@ namespace Collector.Dimension
{ {
public static Dictionary<Tuple<int, int, int>, Blocks> LoadedChunks { get; } = new Dictionary<Tuple<int, int, int>, Blocks>(); public static Dictionary<Tuple<int, int, int>, Blocks> LoadedChunks { get; } = new Dictionary<Tuple<int, int, int>, Blocks>();
private static readonly Dictionary<Tuple<int, int, int>, Blocks> SavedChunks = new Dictionary<Tuple<int, int, int>, Blocks>(); private static readonly Dictionary<Tuple<int, int, int>, Blocks> SavedChunks = new Dictionary<Tuple<int, int, int>, Blocks>();
private static readonly Dictionary<Tuple<int, int>, Collision> savedCollisions = new Dictionary<Tuple<int, int>, Collision>(); public static readonly Dictionary<Tuple<int, int>, Collision> LoadedCollisions = new Dictionary<Tuple<int, int>, Collision>();
public static readonly Dictionary<Tuple<int, int>, Collision> loadedCollisions = new Dictionary<Tuple<int, int>, Collision>(); public static readonly Dictionary<Tuple<int, int>, Collision> SavedCollisions = new Dictionary<Tuple<int, int>, Collision>();
public static readonly List<Blocks> Impassable = new List<Blocks>(); public static readonly List<Blocks> Impassable = new List<Blocks>();
@@ -35,8 +35,8 @@ namespace Collector.Dimension
var tuple = new Tuple<int, int, int>(x, y, z); var tuple = new Tuple<int, int, int>(x, y, z);
var pair = new Tuple<int, int>(x, y); var pair = new Tuple<int, int>(x, y);
loadedCollisions[pair] = new Collision(x,y,IRestrictions.TileSize); LoadedCollisions[pair] = new Collision(x,y);
savedCollisions[pair] = new Collision(x,y,IRestrictions.TileSize); SavedCollisions[pair] = new Collision(x,y);
LoadedChunks[tuple] = name; LoadedChunks[tuple] = name;
SavedChunks[tuple] = name; SavedChunks[tuple] = name;
} }
@@ -58,8 +58,8 @@ namespace Collector.Dimension
if (!LoadedChunks.ContainsKey(tuple)) return; if (!LoadedChunks.ContainsKey(tuple)) return;
if (LoadedChunks[tuple].Equals(Blocks.BlockAir)) return; if (LoadedChunks[tuple].Equals(Blocks.BlockAir)) return;
loadedCollisions.Remove(pair); LoadedCollisions.Remove(pair);
savedCollisions.Remove(pair); SavedCollisions.Remove(pair);
LoadedChunks[tuple] = Blocks.BlockAir; LoadedChunks[tuple] = Blocks.BlockAir;
SavedChunks[tuple] = Blocks.BlockAir; SavedChunks[tuple] = Blocks.BlockAir;
@@ -75,13 +75,8 @@ namespace Collector.Dimension
for (var i = startX; i != endX; i++) { for (var i = startX; i != endX; i++) {
for (var j = startY; j != endY; j++) { for (var j = startY; j != endY; j++) {
LoadedChunks.Remove(new Tuple<int,int,int>(i, j, 0)); LoadedChunks.Remove(new Tuple<int,int,int>(i, j, 0));
} LoadedChunks.Remove(new Tuple<int, int, int>(i, j,1));
} LoadedCollisions.Remove(new Tuple<int, int>(i,j));
//Second Layer
for (var i = startX; i != endX; i++){
for (var j = startY; j != endY; j++) {
var triple = new Tuple<int, int, int>(i, j,1);
LoadedChunks.Remove(triple);
} }
} }
} }
@@ -96,16 +91,22 @@ namespace Collector.Dimension
for (var i = startX; i != endX; i++) { for (var i = startX; i != endX; i++) {
for (var j = startY; j != endY; j++) { for (var j = startY; j != endY; j++) {
var triple = new Tuple<int, int, int>(i, j, 0); var triple = new Tuple<int, int, int>(i, j, 0);
var pair = new Tuple<int, int>(i,j);
if (SavedCollisions.ContainsKey(pair))
{
LoadedCollisions.Add(pair, SavedCollisions[pair]);
}
if(SavedChunks.ContainsKey(triple)){ if(SavedChunks.ContainsKey(triple)){
LoadedChunks.Add(triple,SavedChunks[triple]); LoadedChunks.Add(triple,SavedChunks[triple]);
} }
else { else {
var terrain = GetTerrain(i, j); var terrain = GetTerrain(i, j);
var pair = new Tuple<int, int>(i, j);
if (Impassable.Contains(terrain)) if (Impassable.Contains(terrain))
{ {
loadedCollisions[pair] = new Collision(i,j,IRestrictions.TileSize); var collision = new Collision(i,j);
savedCollisions[pair] = new Collision(i,j,IRestrictions.TileSize); LoadedCollisions[pair] = collision;
SavedCollisions[pair] = collision;
} }
LoadedChunks.Add(triple, terrain); LoadedChunks.Add(triple, terrain);