Skip to content

Instantly share code, notes, and snippets.

@susanndelgado
Last active December 20, 2015 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save susanndelgado/6040887 to your computer and use it in GitHub Desktop.
Save susanndelgado/6040887 to your computer and use it in GitHub Desktop.
Revising a friends code to make it more reusable. This example uses a little bit of modular coding and a little bit of Parasitic Inheritance and Overriding Members concepts.
var ATTACKS = (function (self) {
var moveList = {
slam: {
name: "Slam",
power: 20,
physical: true,
magic: false,
atkdelay: 400,
atkhold: 0,
special: null
},
slash: {
name: "Slash",
power: 25,
physical: true,
magic: false,
atkdelay: 400,
atkhold: 0,
special: true
},
flash: {
name: "Flash",
power: 10,
physical: false,
magic: true,
atkdelay: 400,
atkhold: 500,
special: null
},
iceball: {
name: "IceBall",
power: 35,
physical: false,
magic: true,
atkdelay: 700,
atkhold: 900,
special: true
},
shadowdash: {
name: "ShadowDash",
power: 50,
physical: false,
magic: true,
atkdelay: 2000,
atkhold: 900,
special: true
},
finisher: {
name: "Finisher",
power: 100,
physical: false,
magic: true,
atkdelay: 700,
atkhold: 900,
special: true
}
};
function getSelectedMove(att) {
var selected = moveList[att];
return selected;
}
return {
getMoves: function (att) {
//console.log(moveList[att]);
var result = getSelectedMove(att);
return result;
}
};
}(ATTACKS || {}));
var createCharacter = function (name, type, level, attack, life, maxlife, m, s) {
var char = {
name: name,
type: type,
level: level,
attack: attack,
life: life,
maxlife: maxlife,
moves: getmoves(m),
src: s
};
function getmoves(makeList) {
var temp = {};
for (var prop in makeList) {
var move = ATTACKS.getMoves(prop);
temp[prop] = move;
}
return temp;
}
return char;
};
var createNPC = function (name, type, level, attack, life, maxlife, m, s, num){
var npc = createCharacter(name, type, level, attack, life, maxlife, m, s);
npc.num = num;
//var cpuFunct = Object.getOwnPropertyDescriptor(npc, "funct");
Object.defineProperty(npc, "funct", {
get : function(){
var choseCpuAttack = Math.floor(Math.random()* this.num);
return choseCpuAttack;
}
});
return npc;
};
var hero = createCharacter("Hero Boy", "hero", 1, 10, 300, 300,
{
shadowdash: '',
iceball: ''
}
, "img.png");
console.log("hero is created from createCharacter");
console.log(hero);
var enemy = createNPC("Evil Man", "enemy", 1, 10, 300, 300,
{
slam: '',
flash : '',
finisher: ''
}, "img.png", 3);
console.log("enemy is created from createNPC which inherits from createCharacter");
console.log(enemy);
function NPCActions(char){
var mov = char.moves;
var choseCPUAttack = char.funct;
var arr = [];
for (var prop in mov){
arr.push(prop);
}
console.log("enemy props : " +arr);
var a = char.moves[arr[choseCPUAttack]];
console.log("Selected enemy prop : ");
console.log(a);
}
NPCActions(enemy);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment