From dde0a09d3de5f14710576b4237c8a6a13f518a28 Mon Sep 17 00:00:00 2001 From: MasterGordon Date: Wed, 12 Oct 2022 19:21:37 +0200 Subject: [PATCH] added scale --- assets/level1.json | 91 ++++++++++++++++++++++++++++++++++++- src/BoulderDash.cs | 8 ++++ src/actors/Tiles/Diamond.cs | 3 +- src/actors/Tiles/Player.cs | 32 +++++++++++++ src/actors/Tiles/Rock.cs | 3 +- src/engine/GameState.cs | 3 ++ src/engine/Scene.cs | 11 +++++ src/engine/TileSet.cs | 8 ++-- src/scenes/Level.cs | 50 +++++++++++++++++--- 9 files changed, 193 insertions(+), 16 deletions(-) diff --git a/assets/level1.json b/assets/level1.json index dcc1a53..a1d7a45 100644 --- a/assets/level1.json +++ b/assets/level1.json @@ -1 +1,90 @@ -[[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,1,1,1,1,1,1,1,1,1],[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,2,2,2,2,4,2,2,2,2,1],[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,2,2,2,2,2,0,2,2,1],[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,2,2,2,2,4,2,2,2,2,1],[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,2,2,2,4,2,2,2,2,2,1],[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,2,2,2,2,4,2,4,0,2,1],[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,2,2,2,2,4,2,4,4,2,1],[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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 5, 5, 5, 5, 5, 5, 1 + ], + [ + 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, 2, 4, 2, 2, 2, 2, 2, 2, 2, 1 + ], + [ + 1, 0, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 2, 2, 4, 2, 2, 0, 0, 0, 2, 2, 2, + 4, 2, 2, 2, 2, 2, 2, 4, 2, 4, 0, 2, 2, 1, 1 + ], + [ + 1, 2, 4, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 4, 2, 0, 0, 2, + 2, 2, 2, 3, 2, 2, 2, 4, 2, 4, 4, 2, 2, 2, 1 + ], + [ + 1, 2, 2, 2, 2, 4, 3, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, + 4, 2, 4, 3, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 1 + ], + [ + 1, 2, 2, 2, 0, 2, 2, 0, 2, 4, 2, 2, 4, 2, 4, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 4, 2, 4, 3, 2, 2, 2, 2, 2, 2, 4, 2, 2, 4, 1 + ], + [ + 1, 2, 3, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, + 4, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 + ] +] diff --git a/src/BoulderDash.cs b/src/BoulderDash.cs index 167c560..b5ea048 100644 --- a/src/BoulderDash.cs +++ b/src/BoulderDash.cs @@ -43,6 +43,14 @@ class BoulderDash : Game { GameState.sceneManager.GetCurrentScene()?.OnMouseDown(context, e.button.button, e.button.x, e.button.y); } + if (e.type == SDL_EventType.SDL_MOUSEBUTTONUP) + { + GameState.sceneManager.GetCurrentScene()?.OnMouseUp(context, e.button.button, e.button.x, e.button.y); + } + if (e.type == SDL_EventType.SDL_MOUSEMOTION) + { + GameState.sceneManager.GetCurrentScene()?.OnMouseMotion(context, e.button.x, e.button.y); + } } GameState.keyState = new KeyState(); GameState.tick = (GameState.tick + 1); diff --git a/src/actors/Tiles/Diamond.cs b/src/actors/Tiles/Diamond.cs index 0a05477..03d37bd 100644 --- a/src/actors/Tiles/Diamond.cs +++ b/src/actors/Tiles/Diamond.cs @@ -12,8 +12,7 @@ class Diamond : Tile { if (map.GetTile(x, y + 1) is (int)TileType.AIR) { - map.SetTile(x, y, 0); - map.SetTile(x, y + 1, (int)TileType.DIAMOND_FALLING); + map.SetTile(x, y, (int)TileType.DIAMOND_FALLING); } if (map.GetTile(x, y + 1) is (int)TileType.ROCK && map.GetTile(x + 1, y) is (int)TileType.AIR && map.GetTile(x + 1, y + 1) is (int)TileType.AIR) { diff --git a/src/actors/Tiles/Player.cs b/src/actors/Tiles/Player.cs index c1dc570..7227e0f 100644 --- a/src/actors/Tiles/Player.cs +++ b/src/actors/Tiles/Player.cs @@ -19,6 +19,7 @@ class Player : Tile private bool direction = true; private int idleCD = 512; private TileSet tileSet; + private int age = 0; public Player(TileSet tileSet, int srcX, int srcY) : base(tileSet, srcX, srcY) { @@ -27,6 +28,23 @@ class Player : Tile public override void Draw(Context context, int x, int y) { + if (age < 12 * 16) + { + if (age % 32 < 16) + { + context.renderer.DrawTileSet(tileSet, x, y, 1, 6); + } + else + { + context.renderer.DrawTileSet(tileSet, x, y, 2, 6); + } + return; + } + else if (age < 15 * 16) + { + context.renderer.DrawTileSet(tileSet, x, y, 1 + (age - 12 * 16) / 16, 0); + return; + } var animation = Animation.NORMAL; var tileX = 0; if (isMoving) @@ -61,6 +79,8 @@ class Player : Tile ++j; j = j % 32; tileXWalk = (int)(j * (8.0 / 32.0)); + age++; + if (age < 15 * 16) return; if (GameState.keyState.isPressed(Control.UP) && walkCD is 0) { if (!map.IsSolid(x, y - 1) || map.IsSemiSolid(x, y - 1)) @@ -86,6 +106,12 @@ class Player : Tile map.SetTile(x - 1, y, 9); map.SetTile(x, y, 0); } + if (map.GetTile(x - 1, y) is (int)TileType.ROCK && map.GetTile(x - 2, y) is (int)TileType.AIR) + { + map.SetTile(x - 1, y, 9); + map.SetTile(x - 2, y, (int)TileType.ROCK); + map.SetTile(x, y, 0); + } walkCD = TICKS_PER_TILE; } if (GameState.keyState.isPressed(Control.RIGHT) && walkCD is 0) @@ -95,6 +121,12 @@ class Player : Tile map.SetTile(x + 1, y, 9); map.SetTile(x, y, 0); } + if (map.GetTile(x + 1, y) is (int)TileType.ROCK && map.GetTile(x + 2, y) is (int)TileType.AIR) + { + map.SetTile(x + 1, y, 9); + map.SetTile(x + 2, y, (int)TileType.ROCK); + map.SetTile(x, y, 0); + } walkCD = TICKS_PER_TILE; } if (GameState.keyState.isPressed(Control.LEFT) || GameState.keyState.isPressed(Control.RIGHT)) diff --git a/src/actors/Tiles/Rock.cs b/src/actors/Tiles/Rock.cs index 549c856..09a73e8 100644 --- a/src/actors/Tiles/Rock.cs +++ b/src/actors/Tiles/Rock.cs @@ -8,8 +8,7 @@ class Rock : Tile { if (map.GetTile(x, y + 1) is (int)TileType.AIR) { - map.SetTile(x, y, 0); - map.SetTile(x, y + 1, (int)TileType.ROCK_FALLING); + map.SetTile(x, y, (int)TileType.ROCK_FALLING); } if (map.GetTile(x, y + 1) is (int)TileType.ROCK && map.GetTile(x + 1, y) is (int)TileType.AIR && map.GetTile(x + 1, y + 1) is (int)TileType.AIR) { diff --git a/src/engine/GameState.cs b/src/engine/GameState.cs index 7445e56..d8aae73 100644 --- a/src/engine/GameState.cs +++ b/src/engine/GameState.cs @@ -5,4 +5,7 @@ static class GameState public static int fps = 0; public static int tick = 0; public static bool saving = false; + public static double scale = 1.5; + public static double camX = 0; + public static double camY = 0; } diff --git a/src/engine/Scene.cs b/src/engine/Scene.cs index 9e34798..7586aa6 100644 --- a/src/engine/Scene.cs +++ b/src/engine/Scene.cs @@ -15,8 +15,19 @@ abstract class Scene : Actor } + public virtual void OnMouseUp(Context context, byte button, int x, int y) + { + + } + + public virtual void OnMouseMotion(Context context, int x, int y) + { + + } + public virtual void OnKeyDown(Context context, SDL2.SDL.SDL_Keycode key, SDL2.SDL.SDL_Keymod mod) { } + } diff --git a/src/engine/TileSet.cs b/src/engine/TileSet.cs index de7290b..4f4781e 100644 --- a/src/engine/TileSet.cs +++ b/src/engine/TileSet.cs @@ -36,10 +36,10 @@ static class TileSetRendererExtension }; var dst = new SDL_Rect { - x = x, - y = y, - w = tileSet.Resolution, - h = tileSet.Resolution + x = (int)((x - GameState.camX) * GameState.scale), + y = (int)((y - GameState.camY) * GameState.scale), + w = (int)(tileSet.Resolution * GameState.scale), + h = (int)(tileSet.Resolution * GameState.scale) }; SDL_RenderCopy(renderer.GetRaw(), tileSet.Texture, ref src, ref dst); } diff --git a/src/scenes/Level.cs b/src/scenes/Level.cs index c3d263b..38fb34a 100644 --- a/src/scenes/Level.cs +++ b/src/scenes/Level.cs @@ -16,6 +16,7 @@ class Level : Scene { private Map? map; private int debugSelectedTile = 1; + private bool drawing = false; private TileSet? tileSet; private Dictionary? tiles; @@ -42,7 +43,7 @@ class Level : Scene this.map!.Draw(context); #if (DEBUG) context.renderer.SetColor(0, 0, 0); - context.renderer.DrawRect(0, 0, 34, 34); + context.renderer.DrawRect(0, 0, (int)(34 * GameState.scale), (int)(34 * GameState.scale)); if (tiles?.ContainsKey(debugSelectedTile) == true) { tiles[debugSelectedTile]!.Draw(context, 0, 0); @@ -50,7 +51,7 @@ class Level : Scene else { context.renderer.SetColor(255, 0, 255); - context.renderer.DrawRect(0, 0, 32, 32); + context.renderer.DrawRect(0, 0, (int)(32 * GameState.scale), (int)(32 * GameState.scale)); } #endif } @@ -60,12 +61,39 @@ class Level : Scene this.map!.Update(context); } - public override void OnMouseDown(Context context, byte button, int x, int y) + public override void OnMouseUp(Context context, byte button, int x, int y) { - var tileX = x / 32; - var tileY = y / 32; if (button == SDL2.SDL.SDL_BUTTON_LEFT) { + this.drawing = false; + } + } + + public override void OnMouseMotion(Context context, int x, int y) + { + if (x < 0 || y < 0) + { + return; + } + if (this.drawing) + { + var tileX = (int)(x / GameState.scale) / 32; + var tileY = (int)(y / GameState.scale) / 32; + this.map!.SetTile(tileX, tileY, debugSelectedTile); + } + } + + public override void OnMouseDown(Context context, byte button, int x, int y) + { + var tileX = (int)(x / GameState.scale) / 32; + var tileY = (int)(y / GameState.scale) / 32; + if (button == SDL2.SDL.SDL_BUTTON_LEFT) + { + this.drawing = true; + if (x < 0 || y < 0) + { + return; + } this.map!.SetTile(tileX, tileY, this.debugSelectedTile); } if (button == SDL2.SDL.SDL_BUTTON_MIDDLE) @@ -85,16 +113,24 @@ class Level : Scene { this.debugSelectedTile = number; } - if (key == SDL2.SDL.SDL_Keycode.SDLK_e && mod == SDL2.SDL.SDL_Keymod.KMOD_CTRL) + 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_CTRL) + if (key == SDL2.SDL.SDL_Keycode.SDLK_r && mod == SDL2.SDL.SDL_Keymod.KMOD_LCTRL) { Console.WriteLine("Reloading..."); this.Create(context); } + if (key == SDL2.SDL.SDL_Keycode.SDLK_p) + { + GameState.scale += 0.25; + } + if (key == SDL2.SDL.SDL_Keycode.SDLK_o) + { + GameState.scale -= 0.25; + } } }