mindcraft/src/agent/npc/data.js

50 lines
1.4 KiB
JavaScript
Raw Normal View History

2024-03-05 12:05:46 -08:00
export class NPCData {
constructor() {
this.goals = [];
2024-04-23 20:47:01 -07:00
this.curr_goal = null;
2024-03-05 16:40:06 -08:00
this.built = {};
2024-03-20 16:03:16 -07:00
this.home = null;
2024-04-23 20:47:01 -07:00
this.do_routine = true;
2024-04-24 13:36:26 -07:00
this.do_set_goal = true;
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;
2024-04-23 20:47:01 -07:00
if (this.curr_goal)
obj.curr_goal = this.curr_goal;
2024-03-06 12:18:00 -08:00
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-04-23 20:47:01 -07:00
obj.do_routine = this.do_routine;
2024-04-24 13:36:26 -07:00
obj.do_set_goal = this.do_set_goal;
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-04-23 20:47:01 -07:00
if (obj.curr_goal)
npc.curr_goal = obj.curr_goal;
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-04-23 20:47:01 -07:00
if (obj.do_routine !== undefined)
npc.do_routine = obj.do_routine;
2024-04-24 13:36:26 -07:00
if (obj.do_set_goal !== undefined)
npc.do_set_goal = obj.do_set_goal;
2024-03-05 12:05:46 -08:00
return npc;
}
}