Deeper writing parsing with getters

Programming this is fairly easy. The harder part is having the forethought to know exactly what I’m going to need.
I know that with items I need to have a way to organize them on a player, track them efficiently on the server(to prevent duping, spawning from thin air, etc), and what sparked getting this done is that I also wanted to make it so Events can modify your clothes – which are an item.
There was quite a lot of doing over that I had to do when in the middle of working on it I kept thinking of other things I needed, that what I was writing wouldn’t work with. But now it’s looking good.

The end result in all this recoding is pretty simple at the moment. You get NPCs that can damage your clothes, tearing them, or adding adjectives to them such as “sticky”. How you get there is a little complicated. Lets look at a clothing item object:
{
'id':5001,
'name':'Safari Jacket',
'desc':'safari jacket',
'shortDesc':'jacket',
'removal':'unbutton',
'state':3,
'states':['barely intact', 'torn', 'lightly torn', 'normal'],
'adjs':[]
}

‘id’ is just how it’s indexed. That’s in line with the “efficiently track on the server”. The rest is considered safe for client modding.
For the rest, we have ‘removal’ which is used to tell how it’s taken off, to build a clothes removing phrase. State is a more agnostic word for durability. The states is an array, to match a word to the number, where different descriptions can be set for different states(not all clothing/armor tears, after all). Adjs is an array storing adjectives that are added to it.
Then there is the matter of getters. Getters are a function that are run when a property is, well, gotten. Instead of setting an “is character clothed?” property as true/false at the time when clothes are being set on them, I can define a getter that checks for clothes and returns the true or false. I can also use it to build more detailed phrases, instead of setting “top clothing” to a “safari jacket” string.
var compoundDesc = function(desc, adjs, state, states){
state = state || false;
var length = adjs.length;
if( state && state != states.length - 1 ){
desc = states[state]+' '+desc;
length += 1;
}
if( adjs.length > 0 ){
adjs.forEach(function(adj, i){
var sep = length > 1 && i != adjs.length-length ? ', ' : ' ';
sep = i == adjs.length-length+1 ? ', and ' : sep;
sep = i == adjs.length-length+1 && length == 2 ? ' and ' : sep;
desc = adj + sep + desc;
});
}
return desc;
}
yourChar.descs.__defineGetter__('top', function(){
var obj = yourChar.clothing.top;
return compoundDesc(obj.desc, obj.adjs, obj.state, obj.states);
});

There’s two things here, a function I made to combine the adjectives, state, and description together, and the getter itself that’s calling that commonly used function.
The function is adding it all together to make proper English out of all these words and phrases. If there is just a state, you get “light torn safari jacket”, an adjective and state “sticky and lightly torn safari jacket”, and with 3 or more “cum covered, sticky, and lightly torn safari jacket”.
It’s the writer parser that grabs that property, returning that whenever %top is there.

Now, that’s just an example for one item. Similar will eventually be used all over. These make things a lot easier in that I can simply set a character’s top to a certain item, or to referencing their chest, and it automatically updates many other descriptors and variables used in so many different areas. It has the end result as if there were a chain reaction of changes just from one thing being changed.

Bookmark the permalink.