Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function keys(object) {
- return Object.keys(object);
- }
- let queue = {
- attackQueue: [],
- add: function(object) {
- let propName = keys(object), array = [], outerArr = [];
- for (let i = 0; i < propName.length; i++) {
- if (propName[i] === "name") {
- outerArr.push(object[propName[i]]);
- } else if (propName[i] === "command" || propName[i] === "hitChance") {
- array.push([propName[i], object[propName[i]]]);
- }
- }
- outerArr.push(array);
- queue.attackQueue.push(outerArr);
- },
- remove: function(object) {
- for (let i = 0; i < queue.attackQueue.length; i++) {
- if (object.name === queue.attackQueue[i][0]) {
- queue.attackQueue.splice([i], 1);
- }
- }
- },
- sort: function() {
- queue.attackQueue.sort(function(a, b) {
- if (a[1][0][1] < b[1][0][1]) {
- return 1;
- }
- })
- },
- removeFirst: function() {
- queue.attackQueue.shift();
- },
- reset: function() {
- queue.attackQueue = [];
- }
- }
- function executeRound() {
- let length = queue.attackQueue.length, arr = queue.attackQueue;
- while (queue.attackQueue.length !== 0) {
- window[arr[0][1][1][1].toLowerCase()](); // I used toLowerCase just in case the commands would have been wrote in upperCase
- console.log("removed entity " + arr[0][0] + " !");
- queue.removeFirst();
- }
- }
- function attack() {
- console.log("attack");
- }
- function defend() {
- console.log("defend");
- }
- // example
- let player1 = {
- name: "player1",
- hitChance: 9000,
- command: "attack"
- }
- // add player1 to the queue
- queue.add(player1);
- // execute function "executeRound"
- executeRound();
- console.log(queue.attackQueue) // --> "removed entitiy player1 !"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement