Created
September 18, 2014 01:18
-
-
Save south-str/d522069d09721306570d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* *** ship ***************************************************************** */ | |
function ship(){ | |
var slot1 = new equip(); | |
var slot2 = new equip(); | |
var slot3 = new equip(); | |
var slot4 = new equip(); | |
this.firepower = 0; | |
this.topedo = 0; | |
this.antisubmarine = 0; | |
this.equipments = { | |
slot1:{ | |
equip:slot1, | |
mount:0}, | |
slot2:{ | |
equip:slot2, | |
mount:0}, | |
slot3:{ | |
equip:slot3, | |
mount:0}, | |
slot4:{ | |
equip:slot4, | |
mount:0} | |
}; | |
} | |
//カプセル化のためにアクセサ(getter,setter)は必要。 | |
ship.prototype.getFirepower = function(){ | |
return this.firepower; | |
} | |
ship.prototype.setFirepower = function(fp){ | |
(! numericCheck(fp) || fp < 0) ? this.firepower = 0 : this.firepower = parseInt(fp); | |
} | |
ship.prototype.getTopedo = function(){ | |
return this.topedo; | |
} | |
ship.prototype.setTopedo = function(to){ | |
(! numericCheck(to) || to < 0) ? this.topedo = 0: this.topedo = parseInt(to); | |
} | |
ship.prototype.getMount = function(x){ | |
var slots = 4; | |
if(numericCheck(x) && x >= 0 && x < slots){ | |
switch (x){ | |
case 0: | |
return this.equipments.slot1.mount; | |
break; | |
case 1: | |
return this.equipments.slot2.mount; | |
break; | |
case 2: | |
return this.equipments.slot3.mount; | |
break; | |
case 3: | |
return this.equipments.slot4.mount; | |
break; | |
default: | |
return null; | |
} | |
}else{ | |
return null; | |
} | |
} | |
ship.prototype.setMount = function(mo, slot){ | |
var slots = 4; | |
var moNumCheck = (numericCheck(mo) && mo > 0) ? true : false; | |
var slotNumCheck = (numericCheck(slot) && slot >= 0 && slot < slots) ? true : false; | |
if(moNumCheck && slotNumCheck){ | |
switch (slot){ | |
case 0: | |
this.equipments.slot1.mount = parseInt(mo); | |
break; | |
case 1: | |
this.equipments.slot2.mount = parseInt(mo); | |
break; | |
case 2: | |
this.equipments.slot3.mount = parseInt(mo); | |
break; | |
case 3: | |
this.equipments.slot4.mount = parseInt(mo); | |
break; | |
default: | |
return null; | |
} | |
} | |
} | |
ship.prototype.getAllProp = function(){ | |
var self = this; | |
var keys = Object.keys(this); | |
var list = keys.map(function(x){ | |
return self[x]; | |
}); | |
return list; | |
} | |
ship.prototype.getEquipFirepower = function(){ | |
return [this.equipments.slot1.equip.getFirepower(), | |
this.equipments.slot2.equip.getFirepower(), | |
this.equipments.slot3.equip.getFirepower(), | |
this.equipments.slot4.equip.getFirepower()]; | |
} | |
ship.prototype.sumEquipFirepower = function(){ | |
var fp = [this.equipments.slot1.equip.getFirepower(), | |
this.equipments.slot2.equip.getFirepower(), | |
this.equipments.slot3.equip.getFirepower(), | |
this.equipments.slot4.equip.getFirepower()]; | |
return fp.reduce(function(x,y){return x + y;}); | |
} | |
ship.prototype.getEquipAntisub = function(){ | |
return [this.equipments.slot1.equip.getAntisubArray(), | |
this.equipments.slot2.equip.getAntisubArray(), | |
this.equipments.slot3.equip.getAntisubArray(), | |
this.equipments.slot4.equip.getAntisubArray()]; | |
} | |
ship.prototype.sumSurvAntisub = function(){ | |
var antisubs = [this.equipments.slot1.equip.getAntisubmarine(), | |
this.equipments.slot2.equip.getAntisubmarine(), | |
this.equipments.slot3.equip.getAntisubmarine(), | |
this.equipments.slot4.equip.getAntisubmarine()]; | |
var types = [this.equipments.slot1.equip.getType(), | |
this.equipments.slot2.equip.getType(), | |
this.equipments.slot3.equip.getType(), | |
this.equipments.slot4.equip.getType()]; | |
return antisubs.map(function(x,y){return types[y] == 1 ? x : 0;}).reduce(function(x,y){return x + y;}); | |
} | |
ship.prototype.sumNotSurvAntisub = function(){ | |
var antisubs = [this.equipments.slot1.equip.getAntisubmarine(), | |
this.equipments.slot2.equip.getAntisubmarine(), | |
this.equipments.slot3.equip.getAntisubmarine(), | |
this.equipments.slot4.equip.getAntisubmarine()]; | |
var types = [this.equipments.slot1.equip.getType(), | |
this.equipments.slot2.equip.getType(), | |
this.equipments.slot3.equip.getType(), | |
this.equipments.slot4.equip.getType()]; | |
return antisubs.map(function(x,y){return types[y] != 1 ? x : 0;}).reduce(function(x,y){return x + y;}); | |
} | |
ship.prototype.getEquipBomb = function(){ | |
return [this.equipments.slot1.equip.getBomb(), | |
this.equipments.slot2.equip.getBomb(), | |
this.equipments.slot3.equip.getBomb(), | |
this.equipments.slot4.equip.getBomb()]; | |
} | |
/* *** equip **************************************************************** */ | |
function equip() { | |
this.firepower = 0; | |
this.topedo = 0; | |
this.antisubmarine = 0; | |
this.bomb = 0; | |
this.type = 0; | |
} | |
equip.prototype.getFirepower = function(){ | |
return this.firepower; | |
} | |
equip.prototype.setFirepower = function(fp){ | |
(! numericCheck(fp) || fp < 0) ? this.firepower = 0 : this.firepower = parseInt(fp); | |
} | |
equip.prototype.getTopedo = function(){ | |
return this.topedo; | |
} | |
equip.prototype.setTopedo = function(to){ | |
(! numericCheck(to) || to < 0) ? this.topedo = 0 : this.topedo = parseInt(to); | |
} | |
equip.prototype.getAntisubmarine = function(){ | |
return this.antisubmarine; | |
} | |
equip.prototype.setAntisubmarine = function(as){ | |
(! numericCheck(as) || as < 0) ? this.antisubmarine = 0 : this.antisubmarine = parseInt(as); | |
} | |
equip.prototype.getBomb = function(){ | |
return this.bomb; | |
} | |
equip.prototype.setBomb = function(bo){ | |
(! numericCheck(bo) || bo < 0) ? this.bomb = 0 : this.bomb = parseInt(bo); | |
} | |
equip.prototype.getType = function(){ | |
return this.type; | |
} | |
equip.prototype.setType = function(ty){ | |
(! numericCheck(ty) || ty < 0) ? this.type = 0 : this.type = parseInt(ty); | |
} | |
equip.prototype.getAntisubArray = function(){ | |
return [this.antisubmarine, | |
this.type]; | |
} | |
/* *** utility ************************************************************** */ | |
function numericCheck(x){ | |
return (isFinite(x)) ? true : false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment