2017-04-09 113 views
0

我正在開發js遊戲,我覺得我正在重新獲得大量的代碼。無論如何要讓代碼更加優化或使其成爲一個大功能?我希望這是足夠的代碼,如果沒有,我會讓所有的代碼jfiddle。你怎麼能幹這個代碼?

// Shop, Double coffee 
$("body").on("click", "#shopUpgradeDoublepoints", function() { 
    if(storeDoublePoints === false && totalClicks >= storeDoublePointsCost) { 
     storeDoublePoints = true; 
     storeReleventCheckMultiplier = true; 
     storeCurrentActive = "storeDoublePoints"; 
     storeBoughtItems.push("upgradeDoublePoints"); 
     totalClicks -= storeDoublePointsCost; 
     storeReleventMultiplier(); 
     $("#shopUpgradeDoublepoints").fadeOut(); 
     $(".clicks").html(totalClicks); 
     console.log("Unlocked doublepoints for " + storeDoublePointsCost + " coffee. Total coffee: " + totalClicks); 
    } 
}); 

// Shop, Triple coffee 
$("body").on("click", "#shopUpgradeTriplepoints", function() { 
    if(storeTriplePoints === false && totalClicks >= storeTriplePointsCost) { 
     storeTriplePoints = true; 
     storeCurrentActive = "storeTriplePoints"; 
     storeBoughtItems.push("upgradeTriplePoints"); 
     totalClicks -= storeTriplePointsCost; 
     storeReleventMultiplier(); 
     $(".clicks").html(totalClicks); 
     $("#shopUpgradeTriplepoints").fadeOut(); 
     console.log("Unlocked triplepoints for " + storeTriplePointsCost + " coffee. Total coffee: " + totalClicks); 
    } 
}); 

// Shop, Quadruple coffee 
$("body").on("click", "#shopUpgradeQuadruplepoints", function(){ 
    if(storeQuadruplePoints === false && totalClicks >= storeQuadruplePointsCost) { 
     storeQuadruplePoints = true; 
     storeCurrentActive = "storeQuadruplePoints"; 
     storeBoughtItems.push("upgradeQuadruplePoints"); 
     totalClicks -= storeQuadruplePointsCost; 
     storeReleventMultiplier(); 
     $("#shopUpgradeQuadruplepoints").fadeOut(); 
     $(".clicks").html(totalClicks); 
     console.log("Unlocked quadruplepoints for " + storeQuadruplePointsCost + " coffee. Total coffee: " + totalClicks); 
    } 
}); 
+4

如果你希望你的工作代碼審查你應該看看http://codereview.stackexchange.com/ –

+0

只是這段代碼 – Aphryv

+0

這個問題屬於codereview – zurfyx

回答

1

我想你可以把對象的數組,所有的數據,然後遍歷它們和過程在其中一個功能,這樣的事情,

var items = [{ 
    name: "Double", 
    points: false, 
    pointsCost: 1234 
}, { 
    name: "Triple", 
    points: true, 
    pointsCost: 1234 
}, { 
    name: "Quadruple", 
    points: false, 
    pointsCost: 1234 
}]; 


for (var i = 0; i < items.length; i++) { 
    processItem(items[i]); 
} 

function processItem(item) { 
    var name = item.name + "points"; 
    var id = "#showUpgrade" + name; 
    $("body").on("click", id, function() { 
     if (!item.points && totalClicks >= item.pointsCost) { 
      item.points = true; 
      storeCurrentActive = "store" + name; 
      storeBoughtItems.push("upgrade" + name); 
      totalClicks -= item.cost; 
      storeReleventMultiplier(); 
      $(".clicks").html(totalClicks); 
      $(id).fadeOut(); 
      console.log(`Unlocked ${name} for ${item.cost} coffee. Total coffee: ${totalClicks}`); 
      //console.log("Unlocked " + name + " for " + item.cost + " coffee. Total coffee: " + totalClicks); 
     } 
    }); 
}