artificer/api/schemas/map.zig

35 lines
1.1 KiB
Zig

const std = @import("std");
const Store = @import("../store.zig");
const Position = @import("../position.zig");
const json_utils = @import("../json_utils.zig");
const json = std.json;
const Allocator = std.mem.Allocator;
const Map = @This();
const MapContent = @import("./map_content.zig");
name: []u8,
skin: []u8,
position: Position,
content: ?MapContent,
pub fn parse(store: *Store, obj: json.ObjectMap, allocator: Allocator) !Map {
const content = json_utils.getObject(obj, "content");
const x = json_utils.getInteger(obj, "x") orelse return error.MissingProperty;
const y = json_utils.getInteger(obj, "y") orelse return error.MissingProperty;
return Map{
.name = (try json_utils.dupeString(allocator, obj, "name")) orelse return error.MissingProperty,
.skin = (try json_utils.dupeString(allocator, obj, "skin")) orelse return error.MissingProperty,
.position = Position.init(x, y),
.content = if (content) |c| try MapContent.parse(store, c) else null
};
}
pub fn deinit(self: Map, allocator: Allocator) void {
allocator.free(self.name);
allocator.free(self.skin);
}