Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script type="text/javascript">
- function w(message) {
- console.log(message);
- }
- function Skill(name, damage, frequency, initialCooldown) {
- this.name = name;
- this.damage = damage;
- this.frequency = frequency; // how often this skill is used
- this.cooldown = initialCooldown || 0; // the time when this skill will be ready to be used/reused
- this.used = 0; // the time when this skill was last used
- }
- Skill.prototype.toString = function() {
- return this.name + " " + this.damage + "/" + this.frequency;
- };
- Skill.prototype.use = function() {
- this.used = this.cooldown;
- this.cooldown += this.frequency;
- };
- function Champion(name, hp, startCooldownOn, doListener) {
- this.name = name;
- this.hp = hp;
- this.skills = [];
- this.doListener = doListener || function(){};
- }
- Champion.prototype.installSkill = function(skill) {
- this.skills.push(skill);
- };
- Champion.prototype.getShortestCooldownSkill = function() {
- if(this.skills.length === 0)
- w("No skill has been installed.");
- var shortestCooldownSkill = this.skills[0];
- this.skills.forEach(function(skill) {
- if(skill.cooldown < shortestCooldownSkill.cooldown)
- shortestCooldownSkill = skill;
- }.bind(this));
- return shortestCooldownSkill;
- };
- Champion.prototype.do = function() {
- var skills = [];
- do {
- var skill = this.getShortestCooldownSkill()
- skill.use();
- skills.push(skill);
- /*
- * Is the time when the skill was last used the same as when the next
- * skill will be ready? Then they're being called at the same time
- */
- } while(skill.used === this.getShortestCooldownSkill().cooldown);
- // calls the listener passing all the skills that are being called at this instant
- this.doListener(skills);
- return skill;
- };
- Champion.prototype.addDoListener = function(doListener) {
- this.doListener = doListener;
- };
- function doListener(skills) {
- var text = "";
- var damage = 0;
- var time = skills[0].used + "s";
- var tr = document.createElement("tr");
- // create and set this static variable to 0 if it hasn't been created yet
- doListener.totalDamage = doListener.totalDamage || 0;
- skills.forEach(function(skill) {
- text += skill.toString() + "<br>";
- damage += skill.damage;
- });
- doListener.totalDamage += damage;
- tr.innerHTML = "<td>" + time + "</td><td>" + text + "</td><td>" + damage + "</td>";
- document.getElementById("battleLog").getElementsByTagName("tbody")[0].appendChild(tr);
- document.getElementById("totalDamage").innerHTML = doListener.totalDamage;
- document.getElementById("totalTime").innerHTML = time;
- }
- var Ashe = new Champion("Alistar", 1000);
- Ashe.addDoListener(doListener);
- Ashe.installSkill(new Skill("Dashing Hoof", 50, 2));
- Ashe.installSkill(new Skill("Metal Horn", 30, 1));
- Ashe.installSkill(new Skill("Burning Milk", 5, 0.5));
- </script>
- <style type="text/css">
- table {border: 1px solid black;}
- td {border:1px solid black;text-align:center;padding:2px 30px;}
- tbody tr:nth-child(odd) {background-color:#eee;}
- tfoot {font-weight:bold;background-color:#aaa;}
- </style>
- <input type="button" value="Atacar" id="atacar" onclick="Ashe.do()" />
- <table id="battleLog">
- <thead>
- <tr>
- <th>Time</th>
- <th>Skill(s)' Names Damage / Frequency</th>
- <th>Damage</th>
- </tr>
- </thead>
- <tfoot>
- <tr>
- <td id="totalTime"></td>
- <td></td>
- <td id="totalDamage"></td>
- </tr>
- </tfoot>
- <tbody>
- </tbody>
- </table>
Advertisement
Add Comment
Please, Sign In to add comment