UNPKG

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

108 lines (105 loc) 3.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.generate = void 0; const types_1 = require("../types"); /** TODO: - Support inheritance - Support importing Schema dependencies */ const typeMaps = { "string": "string", "number": "number", "boolean": "boolean", "int8": "number", "uint8": "number", "int16": "number", "uint16": "number", "int32": "number", "uint32": "number", "int64": "number", "uint64": "number", "float32": "number", "float64": "number", }; const distinct = (value, index, self) => self.indexOf(value) === index; function generate(context, options) { return context.classes.map(klass => ({ name: klass.name + ".lua", content: generateClass(klass, options.namespace, context.classes) })); } exports.generate = generate; function generateClass(klass, namespace, allClasses) { const allRefs = []; klass.properties.forEach(property => { let type = property.type; // keep all refs list if ((type === "ref" || type === "array" || type === "map")) { allRefs.push(property); } }); // TOOD: inheritance return `${(0, types_1.getCommentHeader)().replace(/\/\//mg, "--")} local schema = require 'colyseus.serialization.schema.schema' ${allRefs. filter(ref => ref.childType && typeMaps[ref.childType] === undefined). map(ref => ref.childType). concat((0, types_1.getInheritanceTree)(klass, allClasses, false).map(klass => klass.name)). filter(distinct). map(childType => `local ${childType} = require '${(namespace ? `${namespace}.` : '')}${childType}'`). join("\n")} local ${klass.name} = schema.define({ ${klass.properties.map(prop => generatePropertyDeclaration(prop)).join(",\n")}, ["_fields_by_index"] = { ${klass.properties.map(prop => `"${prop.name}"`).join(", ")} }, }) return ${klass.name} `; // ["on_change"] = function(changes) // -- on change logic here // end, // ["on_add"] = function() // -- on add logic here // end, // ["on_remove"] = function() // -- on remove logic here // end, } function generatePropertyDeclaration(prop) { let typeArgs; if (prop.childType) { const isUpcaseFirst = prop.childType.match(/^[A-Z]/); if (isUpcaseFirst) { typeArgs += `${prop.childType}`; } else { typeArgs += `"${prop.childType}"`; } if (prop.type === "ref") { typeArgs = (isUpcaseFirst) ? `${prop.childType}` : `"${prop.childType}"`; } else { typeArgs = (isUpcaseFirst) ? `{ ${prop.type} = ${prop.childType} }` : `{ ${prop.type} = "${prop.childType}" }`; } } else { typeArgs = `"${prop.type}"`; } return ` ["${prop.name}"] = ${typeArgs}`; } // function generatePropertyInitializer(prop: Property) { // let initializer = ""; // if(prop.type === "ref") { // initializer = `new ${prop.childType}()`; // } else if(prop.type === "array") { // initializer = `new schema.ArraySchema()`; // } else if(prop.type === "map") { // initializer = `new schema.MapSchema()`; // } // return `this.${prop.name} = ${initializer}`; // } //# sourceMappingURL=lua.js.map