337 lines
7.5 KiB
Zig
337 lines
7.5 KiB
Zig
const w4 = @import("./wasm4.zig");
|
|
const sh0rk = @import("./sh0rk.zig");
|
|
const sprites = @import("./sprites.zig");
|
|
const palette = @import("./palette.zig");
|
|
const tframe = @import("./tframe.zig");
|
|
const overhead = @import("./overhead.zig");
|
|
|
|
const std = @import("std");
|
|
const fmt = std.fmt;
|
|
|
|
const Direction = sh0rk.Direction;
|
|
const Point = sh0rk.Point;
|
|
const Rect = sh0rk.Rect;
|
|
|
|
const map = @import("./maps/overworld.zig");
|
|
|
|
var frame_count: u32 = 0;
|
|
var mara_direction: Direction = Direction.Right;
|
|
var mara_frame: bool = false;
|
|
var mara_box: Rect = Rect{ .base = map.start_point, .width = 16, .height = 16 };
|
|
var mara_speed: i16 = 0;
|
|
var old_speed: i16 = 0;
|
|
|
|
const screen_width = 20;
|
|
const screen_height = 20;
|
|
const tile_size: i16 = 8;
|
|
|
|
var sound_timer: u8 = 0;
|
|
|
|
var textBuf: [160]u8 = undefined;
|
|
var done: bool = false;
|
|
|
|
var camera = Point{ .x = 0, .y = 0 };
|
|
|
|
fn world_to_screen(p: Point) Point {
|
|
return p.sub(camera);
|
|
}
|
|
|
|
fn screen_to_world(p: Point) Point {
|
|
p.add(camera);
|
|
}
|
|
|
|
export fn start() void {
|
|
if (sh0rk.state == .Gameplay) {
|
|
palette.mist();
|
|
} else {
|
|
palette.tamtam();
|
|
}
|
|
}
|
|
|
|
fn bonk() void {
|
|
if (sound_timer != 0) {
|
|
return;
|
|
}
|
|
|
|
w4.tone(
|
|
w4.Tone.Frequency{ .start = 220, .end = 40 },
|
|
w4.Tone.Duration{
|
|
.attack = 0,
|
|
.decay = 0,
|
|
.sustain = 6,
|
|
.release = 6,
|
|
},
|
|
w4.Tone.Volume{
|
|
.sustain = 100,
|
|
.attack = 100,
|
|
},
|
|
w4.Tone.Flags{
|
|
.channel = .triangle,
|
|
.pulse_duty = .@"1/4",
|
|
},
|
|
);
|
|
|
|
sound_timer = 30;
|
|
}
|
|
|
|
fn map_colors() void {
|
|
switch (map.tileset) {
|
|
.Dungeon => {
|
|
w4.m.colors.* = .{
|
|
._0 = .p2,
|
|
._1 = .p0,
|
|
._2 = .p1,
|
|
._3 = .p3,
|
|
};
|
|
},
|
|
.Rpg => {
|
|
w4.m.colors.* = .{
|
|
._0 = .p3,
|
|
._1 = .p2,
|
|
._2 = .p1,
|
|
._3 = .p0,
|
|
};
|
|
},
|
|
}
|
|
}
|
|
|
|
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();
|
|
|
|
var startCol = @divTrunc(camera.x, tile_size);
|
|
var endCol = startCol + screen_width;
|
|
var startRow = @divTrunc(camera.y, tile_size);
|
|
var endRow = startRow + screen_height;
|
|
|
|
var col: i16 = startCol;
|
|
while (col < endCol + 1) {
|
|
var row: i16 = startRow;
|
|
defer col += 1;
|
|
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);
|
|
// }
|
|
|
|
draw_tile(tile, scr.x, scr.y);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn draw_tile(tile: u32, x: i32, y: i32) void {
|
|
if (tile == 0) {
|
|
w4.rect(x, y, 8, 8);
|
|
return;
|
|
}
|
|
|
|
var tileX = (tile - 1) % map.ts_width;
|
|
var tileY = (tile - 1) / map.ts_width;
|
|
|
|
switch (map.tileset) {
|
|
.Rpg => w4.blitSub(
|
|
&sprites.kenney_rpg,
|
|
x,
|
|
y,
|
|
8,
|
|
8,
|
|
tileX * 8,
|
|
tileY * 8,
|
|
sprites.kenney_rpg_width,
|
|
w4.BlitFlags{ .two_bits = true },
|
|
),
|
|
.Dungeon => w4.blitSub(
|
|
&sprites.dungeon,
|
|
x,
|
|
y,
|
|
8,
|
|
8,
|
|
tileX * 8,
|
|
tileY * 8,
|
|
sprites.dungeon_width,
|
|
w4.BlitFlags{ .two_bits = true },
|
|
),
|
|
}
|
|
}
|
|
|
|
fn move_mara(gamepad: w4.GamePad) void {
|
|
if (mara_speed > 0) {
|
|
mara_speed -= 1;
|
|
}
|
|
|
|
if (gamepad.up) {
|
|
mara_direction = Direction.Up;
|
|
mara_speed = 1;
|
|
camera.y -= mara_speed;
|
|
mara_box.base.y -= mara_speed;
|
|
} else if (gamepad.down) {
|
|
mara_direction = Direction.Down;
|
|
mara_speed = 1;
|
|
camera.y += mara_speed;
|
|
mara_box.base.y += mara_speed;
|
|
} else if (gamepad.left) {
|
|
mara_direction = Direction.Left;
|
|
mara_speed = 1;
|
|
camera.x -= mara_speed;
|
|
mara_box.base.x -= mara_speed;
|
|
} else if (gamepad.right) {
|
|
mara_direction = Direction.Right;
|
|
mara_speed = 1;
|
|
camera.x += mara_speed;
|
|
mara_box.base.x += mara_speed;
|
|
}
|
|
}
|
|
|
|
export fn update() void {
|
|
defer frame_count += 1;
|
|
if (frame_count % 60 == 0) {
|
|
done = false;
|
|
}
|
|
|
|
switch (sh0rk.state) {
|
|
.Title => overhead.title() catch unreachable,
|
|
.StoryDump => overhead.storydump() catch unreachable,
|
|
.Gameplay => gameplay() catch unreachable,
|
|
}
|
|
}
|
|
|
|
fn gameplay() !void {
|
|
defer done = true;
|
|
defer clampCamera();
|
|
if (sound_timer != 0) {
|
|
sound_timer -= 1;
|
|
}
|
|
|
|
drawMap() catch unreachable;
|
|
|
|
const gamepad = w4.m.gamepads[0];
|
|
|
|
if (!tframe.enabled) {
|
|
move_mara(gamepad);
|
|
} else {
|
|
tframe.update(gamepad);
|
|
}
|
|
|
|
for (map.coll) |box| {
|
|
if (mara_box.collides(box)) {
|
|
switch (mara_direction) {
|
|
.Up => {
|
|
mara_box.base.y += mara_speed;
|
|
},
|
|
.Down => {
|
|
mara_box.base.y -= mara_speed;
|
|
},
|
|
.Left => {
|
|
mara_box.base.x += mara_speed;
|
|
},
|
|
.Right => {
|
|
mara_box.base.x -= mara_speed;
|
|
},
|
|
}
|
|
mara_speed = 0;
|
|
bonk();
|
|
}
|
|
}
|
|
|
|
for (map.triggers) |trig| {
|
|
if (mara_box.collides(trig.aura) and mara_direction == trig.direction and gamepad.b) {
|
|
tframe.set_text(trig.dialogue);
|
|
}
|
|
}
|
|
|
|
if (mara_box.base.x < 0) {
|
|
mara_box.base.x = 0;
|
|
bonk();
|
|
}
|
|
|
|
if (mara_box.base.y < 0) {
|
|
mara_box.base.y = 0;
|
|
bonk();
|
|
}
|
|
|
|
draw_mara();
|
|
}
|
|
|
|
fn draw_glaceon(p: Point) void {
|
|
palette.mist();
|
|
w4.m.colors.* = .{
|
|
._0 = .p0,
|
|
._1 = .p1,
|
|
._2 = .p3,
|
|
._3 = .transparent,
|
|
};
|
|
|
|
w4.blit(&sprites.glaceon, p.x, p.y, sprites.glaceon_width, sprites.glaceon_height, w4.BlitFlags{ .two_bits = true });
|
|
}
|
|
|
|
fn draw_mara() void {
|
|
var flags: w4.BlitFlags = w4.BlitFlags{
|
|
.two_bits = true,
|
|
};
|
|
|
|
w4.m.colors.* = .{
|
|
._0 = .transparent,
|
|
._1 = .p0,
|
|
._2 = .p1,
|
|
._3 = .p3,
|
|
};
|
|
|
|
if (mara_direction == Direction.Left) {
|
|
flags.flip_x = true;
|
|
}
|
|
|
|
if (frame_count % 16 == 0 and mara_speed > 0) {
|
|
mara_frame = !mara_frame;
|
|
}
|
|
|
|
var frame: u32 = switch (mara_direction) {
|
|
.Left => 4,
|
|
.Right => 4,
|
|
.Up => 2,
|
|
.Down => 0,
|
|
};
|
|
var step: u32 = if (mara_frame) 1 else 0;
|
|
|
|
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 {
|
|
_ = st;
|
|
w4.trace("!!! PANIC !!!");
|
|
w4.trace(msg);
|
|
unreachable;
|
|
}
|