1

I am going to try to explain this the best way I can, so here goes. In my project, I'm making a video game and at the moment, I'm writing the skills the player is going to use. However, there is a huge, glaring problem I see. That is, I'm setting the skills so when the player fights a dragon, the player is going to take off some of the dragon's HP, etc. But what happens when the player is using those same skills vs. a goblin or some other creature? It won't work correctly since the skills are linked to specifically to a dragon as I currently have it in my code.

I decided to use objects with all the monsters in them, but I don't know how to centralize it so that I can only reference one object code, which will then look for what monster I'm fighting. That way, the skill is stream-lined and I don't have to write a new skill for each fight. How can I set this up?

// 1. CHARACTER OBJECTS

function player(hp, hpcap, mana, manacap, energy, energycap, atb) {
    this.hp = hp;
    this.hpcap = hpcap;
    this.mana = mana;
    this.manacap = manacap;
    this.energy = energy;
    this.energycap = energycap;
    this.atb = atb;
}

function playerStats(strength, armor, magicdamage, magicresistance, precision, parry, critical, manaregen, energyregen) {
    this.strength = strength;
    this.armor = armor;
    this.magicdamage = magicdamage;
    this.magicresistance = magicresistance;
    this.precision = precision;
    this.parry = parry;
    this.critical = critical;
    this.manaregen = manaregen;
    this.energyregen = energyregen;
}

function npc(hp, mana, energy, atb) {
    this.hp = hp;
    this.mana = mana;
    this.energy = energy;
    this.atb = atb;
}

function npcStats(strength, armor, magicdamage, magicresistance, precision, parry, critical, manaregen, energyregen) {
    this.strength = strength;
    this.armor = armor;
    this.magicdamage = magicdamage;
    this.magicresistance = magicresistance;
    this.precision = precision;
    this.parry = parry;
    this.critical = critical;
    this.manaregen = manaregen;
    this.energyregen = energyregen;
}

// 2. GLOBAL PLAYER/NPC VARIABLES

var character = new player(1, 100, 50, 50, 75, 75, 6);
var cs = new playerStats(20, 1, 10, 1, 30, 10, 10, 1, 1);
var dragon = new npc(250, 80, 75, 6);
var dragonstats = new npcStats(15, 3, 15, 5, 40, 10, 10, 2, 2);
4

1 回答 1

2

您可以创建一个全局可用的命名空间:

var yourNameSpace = {};

创建monsters属性:

yourNameSpace.monsters = {};

然后添加怪物的键值对:

yourNameSpace.monsters.dragon = new npc(250, 80, 75, 6);
yourNameSpace.monsters.threeToedMegaSloth = new npc(350, 60, 15, 6);
//etc..

然后你可以像这样引用你的怪物:

yourNameSpace.monsters["dragon"];

或者

yourNameSpace.monsters.dragon;
于 2013-10-18T03:15:47.903 回答