From 6641e29997f4f6361d00bfc820064b482a6f07d6 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Sat, 2 Jul 2022 13:40:41 -0400 Subject: [PATCH] map scrolling isn't sideways Signed-off-by: Christine Dodrill --- src/main.zig | 84 +++++++++++++++++++++++++++++++----------- src/maps/dungeon.zig | 2 +- src/maps/overworld.zig | 2 +- src/sh0rk.zig | 6 ++- src/tools/genmap.js | 2 +- 5 files changed, 71 insertions(+), 25 deletions(-) diff --git a/src/main.zig b/src/main.zig index 1166053..898f413 100644 --- a/src/main.zig +++ b/src/main.zig @@ -29,7 +29,7 @@ var sound_timer: u8 = 0; var textBuf: [160]u8 = undefined; var done: bool = false; -var camera = Point{ .x = 0, .y = 0 }; +var camera = Point{ .x = 32, .y = 0 }; var state: sh0rk.State = .Gameplay; @@ -92,6 +92,21 @@ fn map_colors() void { } } +fn clampCamera() void { + if (camera.x < 0) { + camera.x = 0; + } + if (camera.y < 0) { + camera.y = 0; + } + if (camera.x >= (map.width * 8) - screen_width * 8) { + camera.x = (map.width * 8) - screen_width * 8; + } + if (camera.y >= (map.height * 8) - screen_height * 8) { + camera.y = (map.height * 8) - screen_height * 8; + } +} + fn drawMap() !void { map_colors(); @@ -100,28 +115,23 @@ fn drawMap() !void { var startRow = @divTrunc(camera.y, tile_size); var endRow = startRow + screen_height; - if (!done) { - var buf = fmt.bufPrint(&textBuf, "{},{}: {},{}", .{ startCol, startRow, endCol, endRow }) catch unreachable; - w4.trace(buf); - } - var col: i16 = startCol; - while (col < endCol) { + while (col < endCol + 1) { var row: i16 = startRow; defer col += 1; - while (row < endRow) { + while (row < endRow + 1) { defer row += 1; if (col < map.width and row < map.height) { var tile = map.get_tile(col, row); var world = Point{ .x = col * 8, .y = row * 8 }; var scr = world_to_screen(world); - if(!done) { - var buf = fmt.bufPrint(&textBuf, "{},{}: {},{}", .{ row, col, scr.x, scr.y }) catch unreachable; - w4.trace(buf); - } + // if(!done) { + // var buf = fmt.bufPrint(&textBuf, "{},{}: {},{}", .{ row, col, scr.x, scr.y }) catch unreachable; + // w4.trace(buf); + // } - draw_tile(tile, scr.y, scr.x); + draw_tile(tile, scr.x, scr.y); } } } @@ -182,16 +192,20 @@ fn move_mara(gamepad: w4.GamePad) void { switch (mara_direction) { .Up => { - camera.x -= mara_speed; + camera.y -= mara_speed; + mara_box.base.y -= mara_speed; }, .Down => { - camera.x += mara_speed; + camera.y += mara_speed; + mara_box.base.y += mara_speed; }, .Left => { - camera.y -= mara_speed; + camera.x -= mara_speed; + mara_box.base.x -= mara_speed; }, .Right => { - camera.y += mara_speed; + camera.x += mara_speed; + mara_box.base.x += mara_speed; }, } } @@ -271,6 +285,9 @@ fn storydump() !void { export fn update() void { defer frame_count += 1; + if (frame_count % 60 == 0) { + done = false; + } switch (state) { .Title => title() catch unreachable, @@ -281,6 +298,7 @@ export fn update() void { fn gameplay() !void { defer done = true; + defer clampCamera(); if (sound_timer != 0) { sound_timer -= 1; } @@ -292,7 +310,7 @@ fn gameplay() !void { if (!tframe.enabled) { move_mara(gamepad); } else { - tframe.update(gamepad); + tframe.update(gamepad); } for (map.coll) |box| { @@ -322,12 +340,12 @@ fn gameplay() !void { } } - if (mara_box.base.x <= 0) { + if (mara_box.base.x < 0) { mara_box.base.x = 0; bonk(); } - if (mara_box.base.y <= 0) { + if (mara_box.base.y < 0) { mara_box.base.y = 0; bonk(); } @@ -375,7 +393,31 @@ fn draw_mara() void { }; var step: u32 = if (mara_frame) 1 else 0; - w4.blitSub(&sprites.Mara, mara_box.base.x, mara_box.base.y, 16, 16, 16 * (frame + step), 0, 96, flags); + w4.blitSub( + &sprites.Mara, + mara_box.base.x, + mara_box.base.y, + 16, + 16, + 16 * (frame + step), + 0, + 96, + flags, + ); + + var scr = world_to_screen(mara_box.base); + + w4.blitSub( + &sprites.Mara, + scr.x, + scr.y, + 16, + 16, + 16 * (frame + step), + 0, + 96, + flags, + ); } pub fn panic(msg: []const u8, st: ?*std.builtin.StackTrace) noreturn { diff --git a/src/maps/dungeon.zig b/src/maps/dungeon.zig index 2a7f280..6596bd4 100644 --- a/src/maps/dungeon.zig +++ b/src/maps/dungeon.zig @@ -17,7 +17,7 @@ pub fn get_tile(x: i32, y: i32) u9 { if (x >= width or y >= height) { return 0; } - return data[@intCast(u32, x) * height + @intCast(u32, y)]; + return data[@intCast(u32, y) * width + @intCast(u32, x)]; } pub const tileset: Tileset = .Dungeon; pub const ts_width = 14; diff --git a/src/maps/overworld.zig b/src/maps/overworld.zig index e7b1923..db094ff 100644 --- a/src/maps/overworld.zig +++ b/src/maps/overworld.zig @@ -17,7 +17,7 @@ pub fn get_tile(x: i32, y: i32) u9 { if (x >= width or y >= height) { return 0; } - return data[@intCast(u32, x) * height + @intCast(u32, y)]; + return data[@intCast(u32, y) * width + @intCast(u32, x)]; } pub const tileset: Tileset = .Rpg; pub const ts_width = 34; diff --git a/src/sh0rk.zig b/src/sh0rk.zig index 2ea34d0..f245343 100644 --- a/src/sh0rk.zig +++ b/src/sh0rk.zig @@ -44,7 +44,11 @@ pub const Rect = packed struct { } pub fn collides(this: @This(), other: @This()) bool { - return this.base.x < other.base.x + other.width and this.base.x + this.width > other.base.x and this.base.y < other.base.y + other.height and this.base.y + this.height > other.base.y; + return + this.base.x < other.base.x + other.width and + this.base.x + this.width > other.base.x and + this.base.y < other.base.y + other.height and + this.base.y + this.height > other.base.y; } }; diff --git a/src/tools/genmap.js b/src/tools/genmap.js index a8d5b57..8668c0c 100644 --- a/src/tools/genmap.js +++ b/src/tools/genmap.js @@ -31,7 +31,7 @@ out += `pub fn get_tile(x: i32, y: i32) u9 { if (x >= width or y >= height) { return 0; } - return data[@intCast(u32, x) * height + @intCast(u32, y)]; + return data[@intCast(u32, y) * width + @intCast(u32, x)]; } `;