text triggers

Signed-off-by: Christine Dodrill <me@christine.website>
This commit is contained in:
Cadey Ratio 2022-07-01 18:05:55 -04:00
parent f06bdefbe3
commit 53e8c52da1
8 changed files with 263 additions and 105 deletions

View File

@ -11,7 +11,7 @@
"infinite":false,
"layers":[
{
"data":[1, 1, 1, 1, 1, 19, 20, 21, 1, 1, 69, 1, 1, 1, 1, 36, 53, 38, 1, 1, 1, 1, 1, 1, 86, 88, 104, 1, 1, 17, 1, 1, 1, 1, 105, 1, 1, 1, 1, 1, 1, 69, 1, 1, 105, 1, 1, 1, 81, 1, 1, 1, 1, 1, 105, 67, 66, 97, 98, 99, 1, 68, 1, 1, 105, 66, 67, 114, 115, 116, 1, 103, 88, 88, 106, 1, 1, 100, 118, 101, 1, 1, 1, 1, 103, 88, 88, 88, 107, 88, 11, 13, 1, 12, 1, 1, 1, 1, 1, 1],
"data":[1, 1, 1, 18, 1, 19, 20, 21, 1, 18, 69, 1, 35, 52, 1, 36, 53, 38, 35, 52, 1, 18, 1, 18, 86, 88, 104, 1, 18, 17, 35, 52, 35, 52, 105, 1, 18, 35, 92, 1, 18, 69, 1, 1, 105, 35, 52, 1, 81, 1, 52, 35, 1, 18, 105, 67, 66, 97, 98, 99, 1, 68, 35, 52, 105, 66, 67, 114, 115, 116, 18, 103, 88, 88, 106, 1, 1, 100, 118, 101, 52, 1, 1, 1, 103, 88, 88, 88, 107, 88, 11, 13, 1, 12, 1, 69, 1, 18, 35, 52],
"height":10,
"id":1,
"name":"map",
@ -217,17 +217,17 @@
{
"height":20,
"id":30,
"name":"",
"name":"sign",
"properties":[
{
"name":"act_face",
"type":"string",
"value":"Up"
},
{
"name":"dialogue",
"type":"string",
"value":"Test message!"
},
{
"name":"direction",
"type":"string",
"value":"Up"
}],
"rotation":0,
"type":"",
@ -235,6 +235,72 @@
"width":16,
"x":16,
"y":96
},
{
"height":16,
"id":31,
"name":"gravestone 1",
"properties":[
{
"name":"dialogue",
"type":"string",
"value":"Ded."
},
{
"name":"direction",
"type":"string",
"value":"Up"
}],
"rotation":0,
"type":"",
"visible":true,
"width":7,
"x":82.64,
"y":103
},
{
"height":16,
"id":32,
"name":"gravestone 2",
"properties":[
{
"name":"dialogue",
"type":"string",
"value":"Moar ded."
},
{
"name":"direction",
"type":"string",
"value":"Up"
}],
"rotation":0,
"type":"",
"visible":true,
"width":7,
"x":96.18,
"y":103.36
}],
"opacity":1,
"type":"objectgroup",
"visible":true,
"x":0,
"y":0
},
{
"draworder":"topdown",
"id":4,
"name":"spc",
"objects":[
{
"height":16,
"id":33,
"name":"mara_start",
"rotation":0,
"type":"",
"visible":true,
"width":16,
"x":32,
"y":128
}],
"opacity":1,
"type":"objectgroup",
@ -242,8 +308,8 @@
"x":0,
"y":0
}],
"nextlayerid":4,
"nextobjectid":31,
"nextlayerid":5,
"nextobjectid":34,
"orientation":"orthogonal",
"renderorder":"right-down",
"tiledversion":"1.4.3",

View File

@ -1,21 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.4" tiledversion="1.4.3" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="16" tileheight="16" infinite="0" nextlayerid="4" nextobjectid="31">
<map version="1.4" tiledversion="1.4.3" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="16" tileheight="16" infinite="0" nextlayerid="5" nextobjectid="34">
<editorsettings>
<export target="RPGmap.json" format="json"/>
</editorsettings>
<tileset firstgid="1" source="../sprites/kenney_rpg.tsx"/>
<layer id="1" name="map" width="10" height="10">
<data encoding="csv">
1,1,1,1,1,19,20,21,1,1,
69,1,1,1,1,36,53,38,1,1,
1,1,1,1,86,88,104,1,1,17,
1,1,1,1,105,1,1,1,1,1,
1,69,1,1,105,1,1,1,81,1,
1,1,1,1,105,67,66,97,98,99,
1,68,1,1,105,66,67,114,115,116,
1,103,88,88,106,1,1,100,118,101,
1,1,1,1,103,88,88,88,107,88,
11,13,1,12,1,1,1,1,1,1
1,1,1,18,1,19,20,21,1,18,
69,1,35,52,1,36,53,38,35,52,
1,18,1,18,86,88,104,1,18,17,
35,52,35,52,105,1,18,35,92,1,
18,69,1,1,105,35,52,1,81,1,
52,35,1,18,105,67,66,97,98,99,
1,68,35,52,105,66,67,114,115,116,
18,103,88,88,106,1,1,100,118,101,
52,1,1,1,103,88,88,88,107,88,
11,13,1,12,1,69,1,18,35,52
</data>
</layer>
<objectgroup id="2" name="coll">
@ -37,11 +37,26 @@
<object id="29" x="51.8182" y="154" width="8" height="5.81818"/>
</objectgroup>
<objectgroup id="3" name="act">
<object id="30" x="16" y="96" width="16" height="20">
<object id="30" name="sign" x="16" y="96" width="16" height="20">
<properties>
<property name="act_face" value="Direction.Up"/>
<property name="dialogue" value="Test message!"/>
<property name="direction" value="Up"/>
</properties>
</object>
<object id="31" name="gravestone 1" x="82.64" y="103" width="7" height="16">
<properties>
<property name="dialogue" value="Ded."/>
<property name="direction" value="Up"/>
</properties>
</object>
<object id="32" name="gravestone 2" x="96.18" y="103.36" width="7" height="16">
<properties>
<property name="dialogue" value="Moar ded."/>
<property name="direction" value="Up"/>
</properties>
</object>
</objectgroup>
<objectgroup id="4" name="spc">
<object id="33" name="mara_start" x="32" y="128" width="16" height="16"/>
</objectgroup>
</map>

View File

@ -11,10 +11,12 @@ const Direction = sh0rk.Direction;
const Point = sh0rk.Point;
const Rect = sh0rk.Rect;
const map = @import("./maps/rpg.zig");
var frame_count: u32 = 0;
var mara_direction: Direction = Direction.Right;
var mara_frame: bool = false;
var mara_box: Rect = Rect{.base = Point{.x = 20, .y = 40}, .width = 16, .height = 16};
var mara_box: Rect = Rect{.base = map.start_point, .width = 16, .height = 16};
var mara_speed: i32 = 0;
var screen = Rect{.base = Point{.x = 0, .y = 0}, .width = 160, .height = 160};
@ -23,16 +25,10 @@ var sound_timer: u8 = 0;
var textBuf: [160]u8 = undefined;
var done: bool = true;
var text_frame: bool = false;
var curr_text: []const u8 = undefined;
var text_index: usize = 0;
var text_counter: u4 = 0;
export fn start() void {
palette.mist();
}
const map = @import("./maps/rpg.zig");
fn bonk() void {
if (sound_timer != 0) {
@ -83,10 +79,10 @@ fn drawMap() !void {
var tileX = tile % map.ts_width;
var tileY = tile / map.ts_width;
if (!done) {
var buf = fmt.bufPrint(&textBuf, "{},{}: {}: {},{}", .{col, row, tile, tileX, tileY}) catch unreachable;
w4.trace(buf);
}
// if (!done) {
// var buf = fmt.bufPrint(&textBuf, "{},{}: {}: {},{}", .{col, row, tile, tileX, tileY}) catch unreachable;
// w4.trace(buf);
// }
w4.blitSub(
&sprites.kenney_rpg,
@ -154,40 +150,11 @@ export fn update() void {
const gamepad = w4.m.gamepads[0];
const old_speed = mara_speed;
move_mara(gamepad);
if (gamepad.b) {
var buf = fmt.bufPrint(&textBuf, "{},{}", .{mara_box.base.x, mara_box.base.y}) catch unreachable;
w4.trace(buf);
}
if (gamepad.b and !text_frame and mara_direction == Direction.Up and mara_box.base.equals(Point{.x = 16, .y = 112})) {
text_frame = true;
curr_text = "Test message!";
text_index = 0;
text_counter = 4;
}
if (text_frame) {
tframe.draw();
w4.m.colors._0 = .p3;
w4.m.colors._1 = .p0;
w4.text(curr_text[0..text_index], 12, 140);
if (gamepad.a) {
w4.trace("dismissing");
text_frame = false;
text_index = 0;
curr_text = "";
}
if (text_index < curr_text.len) {
text_counter -= 1;
if (text_counter == 0) {
text_index += 1;
text_counter = 4;
}
}
if (!tframe.enabled) {
move_mara(gamepad);
} else {
tframe.update(gamepad);
}
for (map.coll) |box| {
@ -211,6 +178,12 @@ export fn update() void {
}
}
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();

View File

@ -1,7 +1,9 @@
// Generated by ./src/tools/genmap.js ./maps/RPGmap.json, DO NOT EDIT
const sh0rk = @import("../sh0rk.zig");
const Direction = sh0rk.Direction;
const Point = sh0rk.Point;
const Trigger = sh0rk.Trigger;
const Rect = sh0rk.Rect;
pub const width = 10;
@ -11,16 +13,16 @@ pub const ts_width = 17;
pub const ts_height = 8;
pub const data = [100]u8{
0, 0, 0, 0, 0, 18, 19, 20, 0, 0, 68,
0, 0, 0, 0, 35, 52, 37, 0, 0, 0,
0, 0, 0, 85, 87, 103, 0, 0, 16, 0,
0, 0, 0, 104, 0, 0, 0, 0, 0, 0,
68, 0, 0, 104, 0, 0, 0, 80, 0, 0,
0, 0, 0, 104, 66, 65, 96, 97, 98, 0,
67, 0, 0, 104, 65, 66, 113, 114, 115, 0,
102, 87, 87, 105, 0, 0, 99, 117, 100, 0,
0, 0, 0, 17, 0, 18, 19, 20, 0, 17, 68,
0, 34, 51, 0, 35, 52, 37, 34, 51, 0,
17, 0, 17, 85, 87, 103, 0, 17, 16, 34,
51, 34, 51, 104, 0, 17, 34, 91, 0, 17,
68, 0, 0, 104, 34, 51, 0, 80, 0, 51,
34, 0, 17, 104, 66, 65, 96, 97, 98, 0,
67, 34, 51, 104, 65, 66, 113, 114, 115, 17,
102, 87, 87, 105, 0, 0, 99, 117, 100, 51,
0, 0, 0, 102, 87, 87, 87, 106, 87, 10,
12, 0, 11, 0, 0, 0, 0, 0, 0,
12, 0, 11, 0, 68, 0, 17, 34, 51,
};
pub const coll = [_]Rect{
@ -42,3 +44,11 @@ pub const coll = [_]Rect{
Rect{.base = Point{.x = 52, .y = 154}, .width = 8, .height = 6},
};
pub const triggers = [_]Trigger{
Trigger{.aura = Rect{.base = Point{.x = 16, .y = 96}, .width = 16, .height = 20}, .direction = Direction.Up, .dialogue = "Test message!"}, // sign
Trigger{.aura = Rect{.base = Point{.x = 83, .y = 103}, .width = 7, .height = 16}, .direction = Direction.Up, .dialogue = "Ded."}, // gravestone 1
Trigger{.aura = Rect{.base = Point{.x = 96, .y = 103}, .width = 7, .height = 16}, .direction = Direction.Up, .dialogue = "Moar ded."}, // gravestone 2
};
pub const start_point = Point{ .x = 32, .y = 128 };

View File

@ -1,7 +1,9 @@
// Generated by ./src/tools/genmap.js ./maps/TestMap.json, DO NOT EDIT
const sh0rk = @import("../sh0rk.zig");
const Direction = sh0rk.Direction;
const Point = sh0rk.Point;
const Trigger = sh0rk.Trigger;
const Rect = sh0rk.Rect;
pub const width = 10;
@ -10,19 +12,6 @@ pub const height = 10;
pub const ts_width = 10;
pub const ts_height = 5;
pub const data = [100]u8{
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 17, 28, 28, 28, 28, 28,
28, 28, 28, 28, 37, 28, 28, 28, 28, 0,
1, 2, 3, 1, 47, 3, 1, 2, 3, 5,
49, 49, 49, 49, 49, 49, 21, 49, 22, 15,
21, 49, 22, 49, 42, 49, 49, 49, 49, 25,
40, 49, 49, 49, 49, 49, 21, 49, 49, 5,
49, 49, 49, 21, 49, 49, 49, 41, 49,
};
pub const coll = [_]Rect{
Rect{.base = Point{.x = 0, .y = 80}, .width = 160, .height = 80},
Rect{.base = Point{.x = 80, .y = 48}, .width = 16, .height = 32},

View File

@ -41,6 +41,12 @@ pub const Direction = enum(u2) {
Right,
};
pub const Trigger = struct {
aura: Rect,
direction: Direction,
dialogue: []const u8,
};
pub const Tile = packed struct {
x: u4,
y: u4,

View File

@ -2,7 +2,12 @@ const sh0rk = @import("./sh0rk.zig");
const w4 = @import("./wasm4.zig");
const sprites = @import("./sprites.zig");
pub fn draw() void {
pub var enabled: bool = false;
var curr_text: []const u8 = undefined;
var text_index: usize = 0;
var text_counter: u4 = 0;
fn frame() void {
w4.m.colors.* = .{
._0 = .p0,
._1 = .p1,
@ -158,4 +163,39 @@ pub fn draw() void {
w4.BlitFlags{.two_bits = true},
);
}
}
pub fn set_text(text: []const u8) void {
enabled = true;
curr_text = text;
text_index = 0;
text_counter = 15;
}
pub fn update(gamepad: w4.GamePad) void {
frame();
w4.m.colors._0 = .p3;
w4.m.colors._1 = .p0;
w4.text(curr_text[0..text_index], 12, 140);
if (gamepad.a) {
w4.trace("dismissing");
enabled = false;
text_index = 0;
curr_text = "";
}
if (text_index < curr_text.len) {
text_counter -= 1;
if (gamepad.b) {
text_counter = 0;
}
if (text_counter == 0) {
text_index += 1;
text_counter = 4;
}
}
}

View File

@ -17,6 +17,7 @@ out += `const sh0rk = @import("../sh0rk.zig");\n`;
out += `const Direction = sh0rk.Direction;\n`;
out += `const Point = sh0rk.Point;\n`;
out += `const Trigger = sh0rk.Trigger;\n`;
out += `const Rect = sh0rk.Rect;\n\n`;
out += `pub const width = ${map.width};\n`;
@ -48,23 +49,23 @@ map.tilesets.forEach(tset => {
out += `pub const ts_width = ${tilesheet.width};\n`;
out += `pub const ts_height = ${tilesheet.height};\n\n`;
out += `pub const data = [${tiles.data.length}]u8{\n `;
const genMap = (tiles) => {
out += `pub const data = [${tiles.data.length}]u8{\n `;
tiles.data.forEach((tile, index) => {
tile -= 1;
out += `${tile},`;
if (index !== 0 && index % 10 === 0) {
out += "\n ";
} else {
out += " ";
}
});
tiles.data.forEach((tile, index) => {
tile -= 1;
out += `${tile},`;
if (index !== 0 && index % 10 === 0) {
out += "\n ";
} else {
out += " ";
}
});
out += "\n};\n\n";
if (map.layers.length >= 2) {
let coll = map.layers[1];
out += "\n};\n\n";
};
const genColl = coll => {
out += `pub const coll = [_]Rect{\n`;
coll.objects.forEach(obj => {
@ -73,10 +74,68 @@ if (map.layers.length >= 2) {
const width = Math.round(obj.width);
const height = Math.round(obj.height);
out += ` Rect{.base = Point{.x = ${x}, .y = ${y}}, .width = ${width}, .height = ${height}},\n`;
let comment = "";
if (obj.name !== "") {
comment = ` // ${obj.name}`;
}
out += ` Rect{.base = Point{.x = ${x}, .y = ${y}}, .width = ${width}, .height = ${height}},${comment}\n`;
});
out += "};\n\n";
}
};
const genAct = act => {
out += `pub const triggers = [_]Trigger{\n`;
act.objects.forEach(obj => {
const x = Math.round(obj.x);
const y = Math.round(obj.y);
const width = Math.round(obj.width);
const height = Math.round(obj.height);
let direction = "";
let dialogue = "";
obj.properties.forEach(prop => {
if (prop.name === "direction") {
direction = prop.value;
}
if (prop.name === "dialogue") {
dialogue = prop.value;
}
});
if (direction === "" || dialogue === "") {
throw new Error("Missing direction or dialogue");
}
out += ` Trigger{.aura = Rect{.base = Point{.x = ${x}, .y = ${y}}, .width = ${width}, .height = ${height}}, .direction = Direction.${direction}, .dialogue = "${dialogue}"}, // ${obj.name}\n`;
});
out += "};\n\n";
};
map.layers.forEach(layer => {
if (layer.name == "map") {
genMap(layer);
}
if (layer.name == "coll") {
genColl(layer);
}
if (layer.name == "act") {
genAct(layer);
}
if (layer.name == "spc") {
layer.objects.forEach(obj => {
if (obj.name === "mara_start") {
out += `pub const start_point = Point{ .x = ${Math.round(obj.x)}, .y = ${Math.round(obj.y)} };\n\n`;
}
});
}
});
await Deno.writeTextFile(outFname, out);