dcl-npc-toolkit-ai-version
Version:
A collection of tools for creating Non-Player-Characters (NPCs). These are capable of having conversations with the player, and play different animations. AI usage is added atop of it
166 lines • 7.2 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Reflection = exports.ReflectionType = exports.ReflectionField = void 0;
const annotations_1 = require("./annotations");
const Schema_1 = require("./Schema");
const ArraySchema_1 = require("./types/ArraySchema");
const typeRegistry_1 = require("./types/typeRegistry");
const reflectionContext = { context: new annotations_1.Context() };
/**
* Reflection
*/
class ReflectionField extends Schema_1.Schema {
}
exports.ReflectionField = ReflectionField;
__decorate([
(0, annotations_1.type)("string", reflectionContext),
__metadata("design:type", String)
], ReflectionField.prototype, "name", void 0);
__decorate([
(0, annotations_1.type)("string", reflectionContext),
__metadata("design:type", String)
], ReflectionField.prototype, "type", void 0);
__decorate([
(0, annotations_1.type)("number", reflectionContext),
__metadata("design:type", Number)
], ReflectionField.prototype, "referencedType", void 0);
class ReflectionType extends Schema_1.Schema {
constructor() {
super(...arguments);
this.fields = new ArraySchema_1.ArraySchema();
}
}
exports.ReflectionType = ReflectionType;
__decorate([
(0, annotations_1.type)("number", reflectionContext),
__metadata("design:type", Number)
], ReflectionType.prototype, "id", void 0);
__decorate([
(0, annotations_1.type)([ReflectionField], reflectionContext),
__metadata("design:type", ArraySchema_1.ArraySchema)
], ReflectionType.prototype, "fields", void 0);
class Reflection extends Schema_1.Schema {
constructor() {
super(...arguments);
this.types = new ArraySchema_1.ArraySchema();
}
static encode(instance) {
const rootSchemaType = instance.constructor;
const reflection = new Reflection();
reflection.rootType = rootSchemaType._typeid;
const buildType = (currentType, schema) => {
for (let fieldName in schema) {
const field = new ReflectionField();
field.name = fieldName;
let fieldType;
if (typeof (schema[fieldName]) === "string") {
fieldType = schema[fieldName];
}
else {
const type = schema[fieldName];
let childTypeSchema;
//
// TODO: refactor below.
//
if (Schema_1.Schema.is(type)) {
fieldType = "ref";
childTypeSchema = schema[fieldName];
}
else {
fieldType = Object.keys(type)[0];
if (typeof (type[fieldType]) === "string") {
fieldType += ":" + type[fieldType]; // array:string
}
else {
childTypeSchema = type[fieldType];
}
}
field.referencedType = (childTypeSchema)
? childTypeSchema._typeid
: -1;
}
field.type = fieldType;
currentType.fields.push(field);
}
reflection.types.push(currentType);
};
const types = rootSchemaType._context?.types;
for (let typeid in types) {
const type = new ReflectionType();
type.id = Number(typeid);
buildType(type, types[typeid]._definition.schema);
}
return reflection.encodeAll();
}
static decode(bytes, it) {
const context = new annotations_1.Context();
const reflection = new Reflection();
reflection.decode(bytes, it);
const schemaTypes = reflection.types.reduce((types, reflectionType) => {
const schema = class _ extends Schema_1.Schema {
};
const typeid = reflectionType.id;
types[typeid] = schema;
context.add(schema, typeid);
return types;
}, {});
reflection.types.forEach((reflectionType) => {
const schemaType = schemaTypes[reflectionType.id];
reflectionType.fields.forEach(field => {
if (field.referencedType !== undefined) {
let fieldType = field.type;
let refType = schemaTypes[field.referencedType];
// map or array of primitive type (-1)
if (!refType) {
const typeInfo = field.type.split(":");
fieldType = typeInfo[0];
refType = typeInfo[1];
}
if (fieldType === "ref") {
(0, annotations_1.type)(refType, { context })(schemaType.prototype, field.name);
}
else {
(0, annotations_1.type)({ [fieldType]: refType }, { context })(schemaType.prototype, field.name);
}
}
else {
(0, annotations_1.type)(field.type, { context })(schemaType.prototype, field.name);
}
});
});
const rootType = schemaTypes[reflection.rootType];
const rootInstance = new rootType();
/**
* auto-initialize referenced types on root type
* to allow registering listeners immediatelly on client-side
*/
for (let fieldName in rootType._definition.schema) {
const fieldType = rootType._definition.schema[fieldName];
if (typeof (fieldType) !== "string") {
rootInstance[fieldName] = (typeof (fieldType) === "function")
? new fieldType() // is a schema reference
: new ((0, typeRegistry_1.getType)(Object.keys(fieldType)[0])).constructor(); // is a "collection"
}
}
return rootInstance;
}
}
exports.Reflection = Reflection;
__decorate([
(0, annotations_1.type)([ReflectionType], reflectionContext),
__metadata("design:type", ArraySchema_1.ArraySchema)
], Reflection.prototype, "types", void 0);
__decorate([
(0, annotations_1.type)("number", reflectionContext),
__metadata("design:type", Number)
], Reflection.prototype, "rootType", void 0);
//# sourceMappingURL=Reflection.js.map