javascript - How to import JSON object with functions inside? -
good day,
i playing js.class right , i'm making text-based game. now, executed in browser eventually, executed in node.js environment managing sockets , ansi colors supports players using telnet-like client.
i have created few classes , 1 of them define characters (which either real player or in-game character).
for real player, code create new 1 looks 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 often, in-game characters should have differents , uniques methods use simulate 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 ) { /* unique character. eg: attack target */ } }, undefined );
the question:
i find efficient way load "uniques" in-game characters on demand (which happens when new area loaded). support functions. thought using ajax invalid json format.
any tips on how achieve or should change in "game" design achieve this?
i, sure, need solution both work in browser , can ported nicely node.js.
thank you!
(ps: sorry specific problem might not else) (ps 2: don't know how question use in title)
can these characters split types of characters. have character types inherit character
, each implement own onarrive
method. like:
function dude(){}; dude.prototype = new character // inherit character dude.prototoype.onarrive = function(){ // stuff }
and on.
you can't have function defined in json returned server, nor want to. tightly coupled. define object prototypes inherit , create character types have appropriate 'features' (or behaviour)
additional
i'm bit confused you're looking for, let me take stab. if have data being exchanged between few servers, (character data) want able instantiate objects of type. assuming data knows type (ie add property character json) can instantiate object of type. using above prototype declarations, can create many types of character want. instantiate type through factory method. instance
var factory = { new: function(attrs){ return new classes[attrs.type](attrs); } }; var classes = { dude: dude, another: another, blah: lah, };
assuming you've defined dude, another, lah other prototypes of character
, each implement onadd
method (or use default defined in character
. can use so:
character = factory.new({ type: 'dude', "name" : "cybrix", "zone" : "000001-000003", "stealth" : false, "fighting" : false, "blind" : false, "sleeping" : false }); character.onadd();
Comments
Post a Comment