added level + editor

This commit is contained in:
MasterGordon 2022-10-12 01:34:58 +02:00
parent dbbe203db8
commit 33ebdd2c8d
16 changed files with 390 additions and 32 deletions

View File

@ -1,18 +1 @@
[ [[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0],[1,2,2,2,2,2,2,0,2,2,3,2,4,0,2,2,2,2,2,4,2,4,2,2,2,2,2,2,2,0,0],[1,2,4,9,4,2,2,2,2,2,2,0,2,2,2,2,2,2,2,2,2,4,3,2,2,4,2,2,2,2,0],[1,2,2,2,2,2,2,2,2,2,2,0,2,2,0,2,2,2,2,2,4,2,4,2,2,4,2,2,2,2,0],[1,4,2,0,0,2,2,2,2,2,2,2,2,2,4,2,2,2,2,2,2,4,2,2,4,2,2,2,2,4,0],[1,4,2,4,4,2,2,2,2,2,2,2,2,2,4,4,2,2,4,2,2,2,2,2,2,2,2,4,2,2,0],[1,2,2,2,4,2,2,4,2,2,2,2,2,2,2,2,4,2,2,2,2,2,4,2,0,4,2,2,2,2,0],[1,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,0],[1,2,0,2,2,2,4,2,2,3,2,0,2,2,4,2,4,2,2,2,2,2,2,2,2,2,2,3,2,4,2],[1,2,2,3,2,2,2,2,2,4,2,2,2,2,2,0,2,2,2,2,2,2,2,2,4,0,0,4,2,2,0],[1,2,2,2,4,2,2,4,2,4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,4,2,4,2,2,0],[1,2,0,2,2,2,2,2,4,2,2,2,2,2,2,2,2,4,4,2,2,2,2,2,2,2,2,4,2,2,0],[1,2,4,2,2,2,2,2,4,2,2,0,0,2,2,2,2,4,2,4,3,2,2,3,2,2,2,2,4,2,0],[1,2,3,4,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,4,4,2,2,4,2,2,2,2,2,2,0],[1,2,2,2,2,2,2,2,2,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,0],[1,0,0,2,2,2,2,2,2,2,2,2,0,2,2,2,3,2,2,2,2,4,2,2,2,2,2,4,2,2,0]]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]

View File

@ -12,7 +12,6 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="ppy.SDL2-CS" Version="1.0.596-alpha" /> <PackageReference Include="ppy.SDL2-CS" Version="1.0.596-alpha" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -18,11 +18,13 @@ class BoulderDash : Game
private void registerScenes() private void registerScenes()
{ {
GameState.sceneManager.AddScene("main", new MainMenu()); GameState.sceneManager.AddScene("main", new MainMenu());
GameState.sceneManager.AddScene("level", new Level());
} }
protected override void update(double dt) protected override void update(double dt)
{ {
SDL_Event e; SDL_Event e;
GameState.saving = false;
while (SDL_PollEvent(out e) != 0) while (SDL_PollEvent(out e) != 0)
{ {
if (e.type == SDL_EventType.SDL_QUIT) if (e.type == SDL_EventType.SDL_QUIT)
@ -31,14 +33,20 @@ class BoulderDash : Game
} }
if (e.type == SDL_EventType.SDL_KEYDOWN) if (e.type == SDL_EventType.SDL_KEYDOWN)
{ {
GameState.sceneManager.GetCurrentScene()?.OnKeyDown(context, e.key.keysym.sym, e.key.keysym.mod);
if (e.key.keysym.sym == SDL_Keycode.SDLK_ESCAPE) if (e.key.keysym.sym == SDL_Keycode.SDLK_ESCAPE)
{ {
System.Environment.Exit(0); System.Environment.Exit(0);
} }
} }
if (e.type == SDL_EventType.SDL_MOUSEBUTTONDOWN)
{
GameState.sceneManager.GetCurrentScene()?.OnMouseDown(context, e.button.button, e.button.x, e.button.y);
}
} }
GameState.keyState = new KeyState(); GameState.keyState = new KeyState();
GameState.sceneManager.GetCurrentScene()?.Update(dt, context); GameState.tick = (GameState.tick + 1) % Game.TPS;
GameState.sceneManager.GetCurrentScene()?.Update(context);
} }
protected override void draw() protected override void draw()
@ -48,12 +56,6 @@ class BoulderDash : Game
#endif #endif
this.context.renderer.Clear(); this.context.renderer.Clear();
GameState.sceneManager.GetCurrentScene()?.Draw(this.context); GameState.sceneManager.GetCurrentScene()?.Draw(this.context);
this.context.renderer.DrawTileSet(this.tileSet, 0, 0, 1, 6);
this.context.renderer.DrawTileSet(this.tileSet, 32, 0, 1, 6);
this.context.renderer.DrawTileSet(this.tileSet, 64, 0, 1, 6);
this.context.renderer.DrawTileSet(this.tileSet, 96, 0, 1, 6);
this.context.renderer.DrawTileSet(this.tileSet, 128, 0, 1, 6);
this.context.renderer.DrawTileSet(this.tileSet, 160, 0, 1, 6);
this.context.renderer.Present(); this.context.renderer.Present();
SDL_Delay(1); SDL_Delay(1);
} }

View File

@ -7,6 +7,7 @@ enum Control
LEFT, LEFT,
RIGHT, RIGHT,
STAY, STAY,
CONFIRM,
} }
static class ControlKeyExtension static class ControlKeyExtension
@ -25,6 +26,8 @@ static class ControlKeyExtension
return SDL_Keycode.SDLK_d; return SDL_Keycode.SDLK_d;
case Control.STAY: case Control.STAY:
return SDL_Keycode.SDLK_LCTRL; return SDL_Keycode.SDLK_LCTRL;
case Control.CONFIRM:
return SDL_Keycode.SDLK_SPACE;
default: default:
throw new ArgumentException("Invalid control"); throw new ArgumentException("Invalid control");
} }

View File

@ -1,25 +1,138 @@
using Newtonsoft.Json; // using Newtonsoft.Json;
using System.Text.Json;
class Map : Actor class Map : Actor
{ {
private int[][] map; private int[][] map;
private Dictionary<int, Tile> tiles;
public Map(uint width, uint height) public Map(uint width, uint height, Dictionary<int, Tile> tiles)
{ {
this.map = new int[height][]; this.map = new int[height][];
for (uint i = 0; i < height; i++) for (uint i = 0; i < height; i++)
{ {
this.map[i] = new int[width]; this.map[i] = new int[width];
} }
this.tiles = tiles;
} }
public Map(string json) public Map(string json, Dictionary<int, Tile> tiles)
{ {
var map = JsonConvert.DeserializeObject<int[][]>(json); var map = JsonSerializer.Deserialize<int[][]>(json);
if (map == null) if (map == null)
{ {
throw new Exception("Invalid map"); throw new Exception("Invalid map");
} }
this.map = map; this.map = map;
this.tiles = tiles;
}
public string ToJson()
{
return JsonSerializer.Serialize(this.map);
}
public void Dump()
{
for (uint i = 0; i < this.map.Length; i++)
{
var row = String.Join("", this.map[i]);
Console.WriteLine(row);
}
}
public override void Draw(Context context)
{
for (int y = 0; y < this.map.Length; y++)
{
for (int x = 0; x < this.map[y].Length; x++)
{
if (this.tiles.ContainsKey(this.map[y][x]))
{
var tile = this.tiles[this.map[y][x]];
tile.Draw(context, x * 32, y * 32);
}
}
}
}
public override void Update(Context context)
{
for (int y = 0; y < this.map.Length; y++)
{
for (int x = 0; x < this.map[y].Length; x++)
{
if (this.tiles.ContainsKey(this.map[y][x]))
{
var tile = this.tiles[this.map[y][x]];
tile.Update(context, this, x, y);
}
}
}
}
public bool IsSolid(int x, int y)
{
if (this.tiles.ContainsKey(this.map[y][x]))
{
var tile = this.tiles[this.map[y][x]];
return tile.IsSolid();
}
return false;
}
public bool IsSemiSolid(int x, int y)
{
if (this.tiles.ContainsKey(this.map[y][x]))
{
var tile = this.tiles[this.map[y][x]];
return tile.IsSemiSolid();
}
return false;
}
public void SetTile(int x, int y, int tile)
{
if (this.map.Length <= y)
{
// expand map
var newMap = new int[y + 1][];
for (int i = 0; i < y + 1; i++)
{
newMap[i] = new int[this.map[0].Length];
}
for (int i = 0; i < this.map.Length; i++)
{
for (int j = 0; j < this.map[i].Length; j++)
{
newMap[i][j] = this.map[i][j];
}
}
this.map = newMap;
}
if (this.map[y].Length <= x)
{
// expand map
var newMap = new int[this.map.Length][];
for (int i = 0; i < this.map.Length; i++)
{
newMap[i] = new int[x + 1];
}
for (int i = 0; i < this.map.Length; i++)
{
for (int j = 0; j < this.map[i].Length; j++)
{
newMap[i][j] = this.map[i][j];
}
}
this.map = newMap;
}
this.map[y][x] = tile;
}
public int GetTile(int x, int y)
{
return this.map[y][x];
} }
} }

31
src/actors/Tile.cs Normal file
View File

@ -0,0 +1,31 @@
class Tile
{
private TileSet tileSet;
private int srcX, srcY;
public Tile(TileSet tileSet, int srcX, int srcY)
{
this.tileSet = tileSet;
this.srcX = srcX;
this.srcY = srcY;
}
public virtual void Draw(Context context, int x, int y)
{
context.renderer.DrawTileSet(tileSet, x, y, srcX, srcY);
}
public virtual void Update(Context context, Map map, int x, int y)
{
}
public virtual bool IsSolid()
{
return true;
}
public virtual bool IsSemiSolid()
{
return false;
}
}

View File

@ -0,0 +1,28 @@
class Diamond : Rock
{
private TileSet tileSet;
private double i;
private int tileX;
public Diamond(TileSet tileSet, int srcX, int srcY) : base(tileSet, srcX, srcY)
{
this.tileSet = tileSet;
}
public override void Update(Context context, Map map, int x, int y)
{
++i;
i = i % 128;
tileX = (int)(i * (8.0 / 128.0));
}
public override void Draw(Context context, int x, int y)
{
context.renderer.DrawTileSet(tileSet, x, y, tileX, 10);
}
public override bool IsSemiSolid()
{
return true;
}
}

11
src/actors/Tiles/Dirt.cs Normal file
View File

@ -0,0 +1,11 @@
class Dirt : Tile
{
public Dirt(TileSet tileSet, int srcX, int srcY) : base(tileSet, srcX, srcY)
{
}
public override bool IsSemiSolid()
{
return true;
}
}

View File

@ -0,0 +1,56 @@
class Player : Tile
{
private int walkCD = 0;
const int TICKS_PAR_TILE = 10;
public Player(TileSet tileSet, int srcX, int srcY) : base(tileSet, srcX, srcY)
{
}
public override void Draw(Context context, int x, int y)
{
base.Draw(context, x, y);
}
public override void Update(Context context, Map map, int x, int y)
{
if (GameState.keyState.isPressed(Control.UP) && walkCD is 0)
{
if (!map.IsSolid(x, y - 1) || map.IsSemiSolid(x, y - 1))
{
map.SetTile(x, y - 1, 9);
map.SetTile(x, y, 0);
}
walkCD = TICKS_PAR_TILE;
}
if (GameState.keyState.isPressed(Control.DOWN) && walkCD is 0)
{
if (!map.IsSolid(x, y + 1) || map.IsSemiSolid(x, y + 1))
{
map.SetTile(x, y + 1, 9);
map.SetTile(x, y, 0);
}
walkCD = TICKS_PAR_TILE;
}
if (GameState.keyState.isPressed(Control.LEFT) && walkCD is 0)
{
if (!map.IsSolid(x - 1, y) || map.IsSemiSolid(x - 1, y))
{
map.SetTile(x - 1, y, 9);
map.SetTile(x, y, 0);
}
walkCD = TICKS_PAR_TILE;
}
if (GameState.keyState.isPressed(Control.RIGHT) && walkCD is 0)
{
if (!map.IsSolid(x + 1, y) || map.IsSemiSolid(x + 1, y))
{
map.SetTile(x + 1, y, 9);
map.SetTile(x, y, 0);
}
walkCD = TICKS_PAR_TILE;
}
walkCD = Math.Max(0, walkCD - 1);
base.Update(context, map, x, y);
}
}

6
src/actors/Tiles/Rock.cs Normal file
View File

@ -0,0 +1,6 @@
class Rock : Tile
{
public Rock(TileSet tileSet, int srcX, int srcY) : base(tileSet, srcX, srcY)
{
}
}

View File

@ -1,6 +1,6 @@
abstract class Actor abstract class Actor
{ {
public virtual void Update(double dt, Context context) public virtual void Update(Context context)
{ {
} }

View File

@ -3,4 +3,6 @@ static class GameState
public static KeyState keyState = new KeyState(); public static KeyState keyState = new KeyState();
public static SceneManager sceneManager = new SceneManager(); public static SceneManager sceneManager = new SceneManager();
public static int fps = 0; public static int fps = 0;
public static int tick = 0;
public static bool saving = false;
} }

View File

@ -11,6 +11,10 @@ class ResourceLoader
public string LoadString(string resourceName) public string LoadString(string resourceName)
{ {
#if (DEBUG)
Console.WriteLine("Loading resource: " + resourceName);
return File.ReadAllText(ToPath(resourceName));
#endif
using var stream = this.GetType().Assembly.GetManifestResourceStream($"{this.assemblyName}.{resourceName}"); using var stream = this.GetType().Assembly.GetManifestResourceStream($"{this.assemblyName}.{resourceName}");
using var reader = new StreamReader(stream!); using var reader = new StreamReader(stream!);
return reader.ReadToEnd(); return reader.ReadToEnd();
@ -31,4 +35,16 @@ class ResourceLoader
var ptr = handle.AddrOfPinnedObject(); var ptr = handle.AddrOfPinnedObject();
return (ptr, bytes.Length); return (ptr, bytes.Length);
} }
public void SaveString(string resourceName, string content)
{
using var stream = new StreamWriter(ToPath(resourceName));
stream.Write(content);
}
private static string ToPath(string resourceName)
{
var s = resourceName.Split('.');
return String.Join('/', s[..^1]) + "." + s[^1];
}
} }

View File

@ -9,4 +9,14 @@ abstract class Scene : Actor
{ {
} }
public virtual void OnMouseDown(Context context, byte button, int x, int y)
{
}
public virtual void OnKeyDown(Context context, SDL2.SDL.SDL_Keycode key, SDL2.SDL.SDL_Keymod mod)
{
}
} }

94
src/scenes/Level.cs Normal file
View File

@ -0,0 +1,94 @@
enum TileType : int
{
AIR = 0,
BORDER = 1,
DIRT = 2,
DIAMOND = 3,
ROCK = 4,
WALL = 5,
PLAYER = 9
}
class Level : Scene
{
private Map? map;
private int debugSelectedTile = 0;
private TileSet? tileSet;
private Dictionary<int, Tile>? tiles;
public override void Create(Context context)
{
var levelJson = context.resourceLoader.LoadString("assets.level1.json");
tiles = new Dictionary<int, Tile>();
tileSet = new TileSet(context, "assets.sprites.png", 32);
tiles.Add((int)TileType.DIRT, new Dirt(tileSet, 1, 7));
tiles.Add((int)TileType.BORDER, new Tile(tileSet, 1, 6));
tiles.Add((int)TileType.DIAMOND, new Diamond(tileSet, 0, 10));
tiles.Add((int)TileType.ROCK, new Rock(tileSet, 0, 7));
tiles.Add((int)TileType.WALL, new Tile(tileSet, 3, 6));
tiles.Add((int)TileType.PLAYER, new Player(tileSet, 0, 0));
this.map = new Map(levelJson, tiles);
this.map.Dump();
}
public override void Draw(Context context)
{
this.map!.Draw(context);
#if (DEBUG)
context.renderer.SetColor(0, 0, 0);
context.renderer.DrawRect(0, 0, 34, 34);
if (tiles?.ContainsKey(debugSelectedTile) == true)
{
tiles[debugSelectedTile]!.Draw(context, 0, 0);
}
else
{
context.renderer.SetColor(255, 0, 255);
context.renderer.DrawRect(0, 0, 32, 32);
}
#endif
}
public override void Update(Context context)
{
this.map!.Update(context);
}
public override void OnMouseDown(Context context, byte button, int x, int y)
{
var tileX = x / 32;
var tileY = y / 32;
if (button == SDL2.SDL.SDL_BUTTON_LEFT)
{
this.map!.SetTile(tileX, tileY, this.debugSelectedTile);
}
if (button == SDL2.SDL.SDL_BUTTON_MIDDLE)
{
this.debugSelectedTile = this.map!.GetTile(tileX, tileY);
}
if (button == SDL2.SDL.SDL_BUTTON_RIGHT)
{
this.map!.SetTile(tileX, tileY, 0);
}
}
public override void OnKeyDown(Context context, SDL2.SDL.SDL_Keycode key, SDL2.SDL.SDL_Keymod mod)
{
var number = (int)key - (int)SDL2.SDL.SDL_Keycode.SDLK_0;
if (number >= 0 && number <= 9)
{
this.debugSelectedTile = number;
}
if (key == SDL2.SDL.SDL_Keycode.SDLK_e && mod == SDL2.SDL.SDL_Keymod.KMOD_LCTRL)
{
Console.WriteLine("Saving...");
var json = this.map!.ToJson();
context.resourceLoader.SaveString("assets.level1.json", json);
}
if (key == SDL2.SDL.SDL_Keycode.SDLK_r && mod == SDL2.SDL.SDL_Keymod.KMOD_LCTRL)
{
Console.WriteLine("Reloading...");
this.Create(context);
}
}
}

View File

@ -7,7 +7,7 @@ class MainMenu : Scene
context.fontManager.RegisterFont("MainMenu", "assets.font.ttf", 24); context.fontManager.RegisterFont("MainMenu", "assets.font.ttf", 24);
} }
public override void Update(double dt, Context context) public override void Update(Context context)
{ {
x += dir ? 10 : -10; x += dir ? 10 : -10;
var windowSize = context.window.GetSize(); var windowSize = context.window.GetSize();
@ -19,6 +19,10 @@ class MainMenu : Scene
{ {
dir = true; dir = true;
} }
if (GameState.keyState.isPressed(Control.CONFIRM))
{
GameState.sceneManager.ChangeScene("level", context);
}
} }
public override void Draw(Context context) public override void Draw(Context context)