Guest User

game calculator

a guest
Jan 6th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script type="text/javascript">
  2. function w(message) {
  3.     console.log(message);
  4. }
  5.  
  6. function Skill(name, damage, frequency, initialCooldown) {
  7.     this.name = name;
  8.     this.damage = damage;
  9.     this.frequency = frequency; // how often this skill is used
  10.     this.cooldown = initialCooldown || 0; // the time when this skill will be ready to be used/reused
  11.     this.used = 0; // the time when this skill was last used
  12. }
  13.  
  14. Skill.prototype.toString = function() {
  15.     return this.name + " " + this.damage + "/" + this.frequency;
  16. };
  17.  
  18. Skill.prototype.use = function() {
  19.     this.used = this.cooldown;
  20.     this.cooldown += this.frequency;
  21. };
  22.  
  23. function Champion(name, hp, startCooldownOn, doListener) {
  24.     this.name = name;
  25.     this.hp = hp;
  26.     this.skills = [];
  27.     this.doListener = doListener || function(){};
  28. }
  29.  
  30. Champion.prototype.installSkill = function(skill) {
  31.     this.skills.push(skill);
  32. };
  33.  
  34. Champion.prototype.getShortestCooldownSkill = function() {
  35.     if(this.skills.length === 0)
  36.         w("No skill has been installed.");
  37.    
  38.     var shortestCooldownSkill = this.skills[0];
  39.     this.skills.forEach(function(skill) {
  40.         if(skill.cooldown < shortestCooldownSkill.cooldown)
  41.             shortestCooldownSkill = skill;
  42.     }.bind(this));
  43.     return shortestCooldownSkill;
  44. };
  45.  
  46. Champion.prototype.do = function() {
  47.     var skills = [];
  48.     do {
  49.         var skill = this.getShortestCooldownSkill()
  50.         skill.use();
  51.         skills.push(skill);
  52.         /*
  53.          * Is the time when the skill was last used the same as when the next
  54.          * skill will be ready? Then they're being called at the same time
  55.          */
  56.     } while(skill.used === this.getShortestCooldownSkill().cooldown);
  57.     // calls the listener passing all the skills that are being called at this instant
  58.     this.doListener(skills);
  59.     return skill;
  60. };
  61.  
  62. Champion.prototype.addDoListener = function(doListener) {
  63.     this.doListener = doListener;
  64. };
  65.  
  66. function doListener(skills) {
  67.     var text = "";
  68.     var damage = 0;
  69.     var time = skills[0].used + "s";
  70.     var tr = document.createElement("tr");
  71.     // create and set this static variable to 0 if it hasn't been created yet
  72.     doListener.totalDamage = doListener.totalDamage || 0;
  73.    
  74.     skills.forEach(function(skill) {
  75.         text += skill.toString() + "<br>";
  76.         damage += skill.damage;
  77.     });
  78.     doListener.totalDamage += damage;
  79.    
  80.     tr.innerHTML = "<td>" + time + "</td><td>" + text + "</td><td>" + damage + "</td>";
  81.     document.getElementById("battleLog").getElementsByTagName("tbody")[0].appendChild(tr);
  82.     document.getElementById("totalDamage").innerHTML = doListener.totalDamage;
  83.     document.getElementById("totalTime").innerHTML = time;
  84. }
  85.  
  86. var Ashe = new Champion("Alistar", 1000);
  87. Ashe.addDoListener(doListener);
  88. Ashe.installSkill(new Skill("Dashing Hoof", 50, 2));
  89. Ashe.installSkill(new Skill("Metal Horn", 30, 1));
  90. Ashe.installSkill(new Skill("Burning Milk", 5, 0.5));
  91. </script>
  92. <style type="text/css">
  93.     table {border: 1px solid black;}
  94.     td {border:1px solid black;text-align:center;padding:2px 30px;}
  95.     tbody tr:nth-child(odd) {background-color:#eee;}
  96.     tfoot {font-weight:bold;background-color:#aaa;}
  97.    
  98. </style>
  99. <input type="button" value="Atacar" id="atacar" onclick="Ashe.do()" />
  100. <table id="battleLog">
  101.     <thead>
  102.         <tr>
  103.             <th>Time</th>
  104.             <th>Skill(s)' Names Damage / Frequency</th>
  105.             <th>Damage</th>
  106.         </tr>
  107.     </thead>
  108.     <tfoot>
  109.         <tr>
  110.             <td id="totalTime"></td>
  111.             <td></td>
  112.             <td id="totalDamage"></td>
  113.         </tr>
  114.     </tfoot>
  115.     <tbody>
  116.     </tbody>
  117. </table>
Advertisement
Add Comment
Please, Sign In to add comment