mindcraft/src/agent/npc/data.js

37 lines
966 B
JavaScript
Raw Normal View History

2024-03-05 12:05:46 -08:00
export class NPCData {
constructor() {
this.goals = [];
2024-03-05 16:40:06 -08:00
this.built = {};
2024-03-20 16:03:16 -07:00
this.home = null;
2024-03-05 12:05:46 -08:00
}
toObject() {
2024-03-06 12:18:00 -08:00
let obj = {};
if (this.goals.length > 0)
obj.goals = this.goals;
if (Object.keys(this.built).length > 0)
obj.built = this.built;
2024-03-20 16:03:16 -07:00
if (this.home)
obj.home = this.home;
2024-03-06 12:18:00 -08:00
return obj;
2024-03-05 12:05:46 -08:00
}
static fromObject(obj) {
if (!obj) return null;
let npc = new NPCData();
2024-03-06 12:18:00 -08:00
if (obj.goals) {
npc.goals = [];
for (let goal of obj.goals) {
if (typeof goal === 'string')
npc.goals.push({name: goal, quantity: 1});
else
npc.goals.push({name: goal.name, quantity: goal.quantity});
}
}
2024-03-05 16:40:06 -08:00
if (obj.built)
npc.built = obj.built;
2024-03-20 16:03:16 -07:00
if (obj.home)
npc.home = obj.home;
2024-03-05 12:05:46 -08:00
return npc;
}
}