-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathentitymodulebuilder.js
More file actions
114 lines (84 loc) · 3.13 KB
/
entitymodulebuilder.js
File metadata and controls
114 lines (84 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// ****
// MODULE OBJECT
// ****
// an entity module is associated to one, and only one entity in the environment
// its behavior has to be implemented exclusively in the respondToMessage function
function EntityModule(entity_module_api) {
// which entity this module is attached to (defined when added to environment collection)
this.entity_id = "";
// a reference to the module API
this.API = entity_module_api;
// rank of the module on the entity it is attached to
this.rank = -1;
// this function has to be overriden to implement custom behavior
// message is a string and data is an object
this.respondToMessage = function(message, data) {
switch(message) {
// inspector panel request
// we send back to the socket the contents of our block
case "inspector_panel":
data.socket.emit("inspector_panel_block", {
elements: [
this.API.outputPlainText("This is a blank module")
],
rank: this.rank
});
break;
}
};
// this function has to be overriden to implement rendering
// drawing instructions must be added to the list with the API
this.appendDrawingInstructions = function(instructions_list) {};
}
// ****
// MODULE CREATION
// ****
// this static object adds new modules to the environment
function EntityModuleBuilder(entity_module_api) {
// a dictionary of all the module build functions
this.module_build_functions = {};
// module descriptions
this.module_descriptions = {};
// a reference to the module API
this.module_api = entity_module_api;
}
// todo: store which module registered for which channel
// entity_id is a string, module_name is a string
// the created module is returned
EntityModuleBuilder.prototype.createModule = function(module_name) {
// checking if a build function is available
if(!this.module_build_functions[module_name]) {
console.error("no builder found for module: "+module_name);
return null;
}
// module creation
var new_module = new EntityModule(this.module_api);
this.module_build_functions[module_name](new_module);
new_module.name = module_name;
return new_module;
};
// returns the module type description
EntityModuleBuilder.prototype.getDescription = function(module_name) {
return this.module_descriptions[module_name];
};
// this is used to register custom modules in the builder
// module_name is a string
// description is a string
// build_function is the function that will be called when a new module of that type is built;
// a blank EntityModule is passed as an argument to this function
// channels is an array of string, representing messages for which the module registers
// (this is not yet functional)
EntityModuleBuilder.prototype.registerModule = function(module_name, description, build_function, channels) {
// foolproof check
if(this.module_build_functions[module_name]) {
console.error("module already registered: "+module_name);
return;
}
// registering build function & description
this.module_build_functions[module_name] = build_function;
this.module_descriptions[module_name] = description;
};
// export module
module.exports = function(entity_module_api) {
return new EntityModuleBuilder(entity_module_api);
};