can someone please help me simplify the following code and email me at bigbrother328@cox.net, I want it so that all the monsters and moves are defined before hand and then doing something like YourMonster = FireMonster (that would be if they picked fire type), same with moves, for example something like (YourMonster.move1 = Punch)
Sounds like you're too lazy to go through the code, make changes and simplify the source yourself. What makes you think anyone here would want to do it for free? If you would like to pay somebody you should go to a freelancing web-site or contact somebody directly.
Sounds like you're too lazy to go through the code, make changes and simplify the source yourself. What makes you think anyone here would want to do it for free? If you would like to pay somebody you should go to a freelancing web-site or contact somebody directly.
I posted on these forums for help, I did not say I would not do it myself (and i didn't ask you to do it for me), I am new to this language and still learning, I don't know how to simplify it, i just need someone to show me what i need to do in order to simplify the code, any help would be great, please don't criticize my thread.
i would use object literals and a merge function to drastically cut down on the # of chars used...
or at least use an object to get all of the dots except one as a single word:
Code:
var him=OppMonster['Move'][0];
him['MoveName']="Punch";
him['MoveType']="Normal";
him['MoveCategory']="Physical";
him['MovePower']=20;
him['MoveAccuracy']=100;
him['MovePP']=10;
and "him['MoveType']" is wordier than "him.MoveType" - if no spaces in key, use dots...
you can also nest some of thos repetititve IFs using ternary:
Code:
var mt=GetMoveType(Attacker), dt= GetType(Defender) ;
//then for each move type, you can do in one short line:
if ( mt === "Fire"){ return dt=== "Water" ? 0.5 : 2.0;}
instead of
Code:
if (GetMoveType(Attacker) === "Fire" && GetType(Defender) === "Water")
return 0.5;
if (GetMoveType(Attacker) === "Fire" && GetType(Defender) === "Nature")
return 2.0;
long-term, moving those values into objects instead of fork code is going to make it a lot easier to modify and expand the game later.
from anywhere, you can then do handy things like "var moveTypes=Object.keys(cmLUT)" to grab an array of all move types, meaning you won't have to cut and paste squat to expand.
a reference is always going to run faster than a calculation, and each if and case is a calculation.
i handle unknown values using the Look-Up-Table object ( .Default) and default operators ( || ), which only execute if needed instead of each time the function executes.
the function now has no ifs or forks, so it's much faster to execute, and has been reduced to one line.
can you see how that's more efficient and flexible ?
EDIT:
as a bonus, you'll get to brag that you use OOP instead of procedural design in your game.
by the way is there any way i could maybe create a function beforehand and make each monster a member of the function, if so can you please show me what to do, because if i can do this it will save me the trouble of coding the same monsters and moves, over and over again for each player, and drastically cut down code
for clarification on me coding the same thing over and over again and how doing this could save a ton of code
/**
* Monster (constructor)
* use:
* new Monster('nameStr', {custom: data});
* args:
* [required] name (string)
* [optional] custom (object)
* description:
* Given at least a name, Monster will create a new monster. Providing custom data will override default data. Custom data that is not found in _default will be ignored.
**/
function Monster (name, custom) {
var _default = {
life: 10,
scariness: 3
};
var custom = custom || {};
var i;
if (name === void 0) { // void 0 is undefined..
throw "Monsters require a name.";
}
this.name = name;
for (i in _default) {
this[i] = custom[i] || _default[i];
}
};
So basically, you place all of the properties shared across all monsters inside of the _default object. If you want to create a monster with different attributes, you can set them when invoking the constructor with 'new', passing the custom attributes to the 'custom' argument.
Here's an example of using 'new' to create a vampire and a ghost:
Code:
var dracula = new Monster('vampire', {scariness: 5});
var casper = new Monster('ghost');
Dracula is a particularly scary monster, so we bumped the default scariness from 3, up to 5. Casper, on the other hand, is a very average monster. Casper's scariness in this example remains the default- which is 3. Both monsters retain a 'life' value of 10- because of the default.
This isn't very interesting yet. Monsters can take action. For sake of simplicity, I'll make an action that affects two monsters. I want every monster to have the ability to 'scare' other monsters.
So basically, what we would do is change the prototype of Monster- and it will affect all monsters.
Code:
Monster.prototype.scare = function (m) {
m.life -= this.scariness;
};
We could technically define this after we declare the monsters using 'new' and it will still affect them. But it's much more clear to put this directly underneith the Monster constructor- because it extends Monster's prototype (for clearity).
We take an arg 'm' which we assume to be a Monster instance. If we say:
Code:
dracula.scare(casper)
.. then we want casper's (m becomes casper) life to be negatively influenced by dracula's scare. 'this' belongs to the monster who decided to scare- that monster is clearly dracula because we said 'dracula.scare'. In my opinion, this is very clear and easy-to-read code. It's also got a little elegance, but not as much as you might find in JavaScript if you decided to go the functional route. Since you like C++, though, I think you'll like this pattern better than the functional alternatives.
Hopefully that helps you with classical OOP in JavaScript. Notice private variables are only possible with closure- that is, using 'var' to define variables in a function, so that the 'var' is only accessible in that function and child functions/methods.
Bookmarks