Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. function keys(object) {
  2. return Object.keys(object);
  3. }
  4.  
  5. let queue = {
  6. attackQueue: [],
  7.  
  8. add: function(object) {
  9. let propName = keys(object), array = [], outerArr = [];
  10.  
  11. for (let i = 0; i < propName.length; i++) {
  12. if (propName[i] === "name") {
  13. outerArr.push(object[propName[i]]);
  14. } else if (propName[i] === "command" || propName[i] === "hitChance") {
  15. array.push([propName[i], object[propName[i]]]);
  16. }
  17. }
  18. outerArr.push(array);
  19. queue.attackQueue.push(outerArr);
  20. },
  21.  
  22. remove: function(object) {
  23.  
  24. for (let i = 0; i < queue.attackQueue.length; i++) {
  25. if (object.name === queue.attackQueue[i][0]) {
  26. queue.attackQueue.splice([i], 1);
  27. }
  28. }
  29. },
  30.  
  31. sort: function() {
  32. queue.attackQueue.sort(function(a, b) {
  33. if (a[1][0][1] < b[1][0][1]) {
  34. return 1;
  35. }
  36. })
  37. },
  38.  
  39. removeFirst: function() {
  40. queue.attackQueue.shift();
  41. },
  42.  
  43. reset: function() {
  44. queue.attackQueue = [];
  45. }
  46. }
  47.  
  48. function executeRound() {
  49. let length = queue.attackQueue.length, arr = queue.attackQueue;
  50.  
  51. while (queue.attackQueue.length !== 0) {
  52. window[arr[0][1][1][1].toLowerCase()](); // I used toLowerCase just in case the commands would have been wrote in upperCase
  53. console.log("removed entity " + arr[0][0] + " !");
  54. queue.removeFirst();
  55. }
  56. }
  57.  
  58. function attack() {
  59. console.log("attack");
  60. }
  61.  
  62. function defend() {
  63. console.log("defend");
  64. }
  65.  
  66. // example
  67. let player1 = {
  68. name: "player1",
  69. hitChance: 9000,
  70. command: "attack"
  71. }
  72.  
  73. // add player1 to the queue
  74. queue.add(player1);
  75.  
  76. // execute function "executeRound"
  77. executeRound();
  78.  
  79. console.log(queue.attackQueue) // --> "removed entitiy player1 !"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement