map scrolling isn't sideways

Signed-off-by: Christine Dodrill <me@christine.website>
This commit is contained in:
Cadey Ratio 2022-07-02 13:40:41 -04:00
parent 3a86c4674d
commit 6641e29997
5 changed files with 71 additions and 25 deletions

View File

@ -29,7 +29,7 @@ var sound_timer: u8 = 0;
var textBuf: [160]u8 = undefined; var textBuf: [160]u8 = undefined;
var done: bool = false; var done: bool = false;
var camera = Point{ .x = 0, .y = 0 }; var camera = Point{ .x = 32, .y = 0 };
var state: sh0rk.State = .Gameplay; 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 { fn drawMap() !void {
map_colors(); map_colors();
@ -100,28 +115,23 @@ fn drawMap() !void {
var startRow = @divTrunc(camera.y, tile_size); var startRow = @divTrunc(camera.y, tile_size);
var endRow = startRow + screen_height; 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; var col: i16 = startCol;
while (col < endCol) { while (col < endCol + 1) {
var row: i16 = startRow; var row: i16 = startRow;
defer col += 1; defer col += 1;
while (row < endRow) { while (row < endRow + 1) {
defer row += 1; defer row += 1;
if (col < map.width and row < map.height) { if (col < map.width and row < map.height) {
var tile = map.get_tile(col, row); var tile = map.get_tile(col, row);
var world = Point{ .x = col * 8, .y = row * 8 }; var world = Point{ .x = col * 8, .y = row * 8 };
var scr = world_to_screen(world); var scr = world_to_screen(world);
if(!done) { // if(!done) {
var buf = fmt.bufPrint(&textBuf, "{},{}: {},{}", .{ row, col, scr.x, scr.y }) catch unreachable; // var buf = fmt.bufPrint(&textBuf, "{},{}: {},{}", .{ row, col, scr.x, scr.y }) catch unreachable;
w4.trace(buf); // 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) { switch (mara_direction) {
.Up => { .Up => {
camera.x -= mara_speed; camera.y -= mara_speed;
mara_box.base.y -= mara_speed;
}, },
.Down => { .Down => {
camera.x += mara_speed; camera.y += mara_speed;
mara_box.base.y += mara_speed;
}, },
.Left => { .Left => {
camera.y -= mara_speed; camera.x -= mara_speed;
mara_box.base.x -= mara_speed;
}, },
.Right => { .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 { export fn update() void {
defer frame_count += 1; defer frame_count += 1;
if (frame_count % 60 == 0) {
done = false;
}
switch (state) { switch (state) {
.Title => title() catch unreachable, .Title => title() catch unreachable,
@ -281,6 +298,7 @@ export fn update() void {
fn gameplay() !void { fn gameplay() !void {
defer done = true; defer done = true;
defer clampCamera();
if (sound_timer != 0) { if (sound_timer != 0) {
sound_timer -= 1; sound_timer -= 1;
} }
@ -292,7 +310,7 @@ fn gameplay() !void {
if (!tframe.enabled) { if (!tframe.enabled) {
move_mara(gamepad); move_mara(gamepad);
} else { } else {
tframe.update(gamepad); tframe.update(gamepad);
} }
for (map.coll) |box| { 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; mara_box.base.x = 0;
bonk(); bonk();
} }
if (mara_box.base.y <= 0) { if (mara_box.base.y < 0) {
mara_box.base.y = 0; mara_box.base.y = 0;
bonk(); bonk();
} }
@ -375,7 +393,31 @@ fn draw_mara() void {
}; };
var step: u32 = if (mara_frame) 1 else 0; 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 { pub fn panic(msg: []const u8, st: ?*std.builtin.StackTrace) noreturn {

View File

@ -17,7 +17,7 @@ pub fn get_tile(x: i32, y: i32) u9 {
if (x >= width or y >= height) { if (x >= width or y >= height) {
return 0; 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 tileset: Tileset = .Dungeon;
pub const ts_width = 14; pub const ts_width = 14;

View File

@ -17,7 +17,7 @@ pub fn get_tile(x: i32, y: i32) u9 {
if (x >= width or y >= height) { if (x >= width or y >= height) {
return 0; 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 tileset: Tileset = .Rpg;
pub const ts_width = 34; pub const ts_width = 34;

View File

@ -44,7 +44,11 @@ pub const Rect = packed struct {
} }
pub fn collides(this: @This(), other: @This()) bool { 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;
} }
}; };

View File

@ -31,7 +31,7 @@ out += `pub fn get_tile(x: i32, y: i32) u9 {
if (x >= width or y >= height) { if (x >= width or y >= height) {
return 0; return 0;
} }
return data[@intCast(u32, x) * height + @intCast(u32, y)]; return data[@intCast(u32, y) * width + @intCast(u32, x)];
} }
`; `;