How to import JSON object with functions inside?
Good day,
I am playing with JS.Class right now and I'm making a text-based game. For now, it is executed in a browser but eventually, it will be executed in a Node.js environment managing sockets and ANSI colors supports for players using a telnet-like client.
I have created a few classes and one of them define Characters (which is either a real player or an in-game character).
For a real player, the code to create a new one looks like this: new Character( "userId", { /* player's options/settings/parameters here*/ }, socket );
Example:
new Character( "000001", {
"Name" : "Cybrix",
"Zone" : "000001-000003",
"Stealth" : false,
"Fighting" : false,
"Blind" : false,
"Sleeping" : false
}, socket );
More than often, the in-game characters should have differents and uniques methods that I use to simulate an event-like behavior.
Example:
new Character( "000001", {
"Name" : "Large Dragon Boss",
"Zone" : "000001-000003",
"Stealth" : false,
"Fighting" : false,
"Blind" : false,
"Sleeping" : true,
"onArrive" : function( target ) {
/* do something unique to this character. Eg: attack target */
}
}, undefined );
The question:
I would like to find an efficient way to load all the "uniques" in-game characters on demand (which happens usually when a new area is loaded). that will support functions. I really tho开发者_运维知识库ught about using AJAX but that would be invalid JSON format.
Any tips on how to achieve this or what should I change in my "game" design to achieve this?
I, for sure, need a solution that will both work in my browser and can be ported nicely to Node.js.
Thank you!
(PS: Sorry for my very specific problem that might not help anyone else) (PS 2: I don't know how what question to use in the Title)
Can these characters be split into types of characters. You could have your character types inherit from Character
and each implement their own onArrive
method. Something like:
function Dude(){};
Dude.prototype = new Character // inherit from character
Dude.prototoype.onArrive = function(){
// do some stuff
}
And so on.
You can't have a function defined in your JSON returned from a server, nor do you want to. That would be too tightly coupled. Just define object prototypes that inherit and create character types that have the appropriate 'features' (or behaviour)
Additional
I'm a bit confused as to what you're looking for, but let me take another stab. IF you have some data being exchanged between a few servers, (character data) you want to be able to instantiate objects of a certain type. Assuming this data knows its type (ie add this as a property to the character JSON) then you can instantiate an object of any type. Using the above prototype declarations, you can create as many types of Character that you want. You could then instantiate this type through some factory method. For instance
var Factory = {
new: function(attrs){
return new classes[attrs.type](attrs);
}
};
var classes = {
dude: Dude,
another: Another,
blah: Lah,
};
Assuming that you've defined Dude, Another, Lah as other prototypes of Character
and they each implement some onAdd
method (or just use the default defined in Character
. Then you can use it like so:
character = Factory.new({
type: 'dude',
"Name" : "Cybrix",
"Zone" : "000001-000003",
"Stealth" : false,
"Fighting" : false,
"Blind" : false,
"Sleeping" : false
});
character.onAdd();
精彩评论