improved physics

This commit is contained in:
MasterGordon 2022-10-12 16:27:10 +02:00
parent d82c3b9959
commit ae4d9fadba
2 changed files with 22 additions and 18 deletions

View File

@ -4,6 +4,7 @@ using System.Text.Json;
class Map : Actor class Map : Actor
{ {
private int[][] map; private int[][] map;
private int[][] mapSnapshot;
private Dictionary<int, Tile> tiles; private Dictionary<int, Tile> tiles;
public Map(uint width, uint height, Dictionary<int, Tile> tiles) public Map(uint width, uint height, Dictionary<int, Tile> tiles)
@ -13,6 +14,7 @@ class Map : Actor
{ {
this.map[i] = new int[width]; this.map[i] = new int[width];
} }
this.mapSnapshot = this.map.Select(a => a.ToArray()).ToArray();
this.tiles = tiles; this.tiles = tiles;
} }
@ -25,6 +27,7 @@ class Map : Actor
throw new Exception("Invalid map"); throw new Exception("Invalid map");
} }
this.map = map; this.map = map;
this.mapSnapshot = this.map.Select(a => a.ToArray()).ToArray();
this.tiles = tiles; this.tiles = tiles;
} }
@ -70,6 +73,7 @@ class Map : Actor
} }
} }
} }
this.map = this.mapSnapshot.Select(a => a.ToArray()).ToArray();
} }
public bool IsSolid(int x, int y) public bool IsSolid(int x, int y)
@ -94,41 +98,41 @@ class Map : Actor
public void SetTile(int x, int y, int tile) public void SetTile(int x, int y, int tile)
{ {
if (this.map.Length <= y) if (this.mapSnapshot.Length <= y)
{ {
// expand map // expand map2
var newMap = new int[y + 1][]; var newMap = new int[y + 1][];
for (int i = 0; i < y + 1; i++) for (int i = 0; i < y + 1; i++)
{ {
newMap[i] = new int[this.map[0].Length]; newMap[i] = new int[this.mapSnapshot[0].Length];
} }
for (int i = 0; i < this.map.Length; i++) for (int i = 0; i < this.mapSnapshot.Length; i++)
{ {
for (int j = 0; j < this.map[i].Length; j++) for (int j = 0; j < this.mapSnapshot[i].Length; j++)
{ {
newMap[i][j] = this.map[i][j]; newMap[i][j] = this.mapSnapshot[i][j];
} }
} }
this.map = newMap; this.mapSnapshot = newMap;
} }
if (this.map[y].Length <= x) if (this.mapSnapshot[y].Length <= x)
{ {
// expand map // expand map2
var newMap = new int[this.map.Length][]; var newMap = new int[this.mapSnapshot.Length][];
for (int i = 0; i < this.map.Length; i++) for (int i = 0; i < this.mapSnapshot.Length; i++)
{ {
newMap[i] = new int[x + 1]; newMap[i] = new int[x + 1];
} }
for (int i = 0; i < this.map.Length; i++) for (int i = 0; i < this.mapSnapshot.Length; i++)
{ {
for (int j = 0; j < this.map[i].Length; j++) for (int j = 0; j < this.mapSnapshot[i].Length; j++)
{ {
newMap[i][j] = this.map[i][j]; newMap[i][j] = this.mapSnapshot[i][j];
} }
} }
this.map = newMap; this.mapSnapshot = newMap;
} }
this.map[y][x] = tile; this.mapSnapshot[y][x] = tile;
} }
public int GetTile(int x, int y) public int GetTile(int x, int y)

View File

@ -85,13 +85,13 @@ class Level : Scene
{ {
this.debugSelectedTile = number; this.debugSelectedTile = number;
} }
if (key == SDL2.SDL.SDL_Keycode.SDLK_e && mod == SDL2.SDL.SDL_Keymod.KMOD_LCTRL) if (key == SDL2.SDL.SDL_Keycode.SDLK_e && mod == SDL2.SDL.SDL_Keymod.KMOD_CTRL)
{ {
Console.WriteLine("Saving..."); Console.WriteLine("Saving...");
var json = this.map!.ToJson(); var json = this.map!.ToJson();
context.resourceLoader.SaveString("assets.level1.json", json); context.resourceLoader.SaveString("assets.level1.json", json);
} }
if (key == SDL2.SDL.SDL_Keycode.SDLK_r && mod == SDL2.SDL.SDL_Keymod.KMOD_LCTRL) if (key == SDL2.SDL.SDL_Keycode.SDLK_r && mod == SDL2.SDL.SDL_Keymod.KMOD_CTRL)
{ {
Console.WriteLine("Reloading..."); Console.WriteLine("Reloading...");
this.Create(context); this.Create(context);