diff --git a/src/TaskQueue.js b/src/TaskQueue.js index f3a9c6a..9a1f28c 100644 --- a/src/TaskQueue.js +++ b/src/TaskQueue.js @@ -1,10 +1,33 @@ -const TaskQueue = function() { - function TaskQueue() { +function runNextTask(taskQueue) { + if (taskQueue.running || taskQueue.tasks.length === 0) { + return; + } + taskQueue.running = true; + const task = taskQueue.tasks.shift(); + + if (task.runAndContinue) { + setTimeout(() => { + task.runAndContinue(() => { + task.dispose && task.dispose(); + taskQueue.running = false; + + setTimeout(() => { + runNextTask(taskQueue); + }); + }); + }, 0); + } + else { + runNextTask(taskQueue); + } +} + +class TaskQueue{ + constructor(){ this.tasks = []; this.running = false; } - - TaskQueue.prototype.push = function(run, dispose, duration) { + push(run, dispose, duration) { if (duration === undefined || duration === null) { this.tasks.push({runAndContinue: run, dispose}); } else { @@ -21,35 +44,9 @@ const TaskQueue = function() { runNextTask(this); }; - TaskQueue.prototype.continueWith = function(action) { + continueWith(action) { this.push(action, null, 0); }; - - function runNextTask(taskQueue) { - if (taskQueue.running || taskQueue.tasks.length === 0) { - return; - } - taskQueue.running = true; - const task = taskQueue.tasks.shift(); - - if (task.runAndContinue) { - setTimeout(() => { - task.runAndContinue(() => { - task.dispose && task.dispose(); - taskQueue.running = false; - - setTimeout(() => { - runNextTask(taskQueue); - }); - }); - }, 0); - } - else { - runNextTask(taskQueue); - } - } - - return TaskQueue; -}(); +} export default TaskQueue; diff --git a/src/index.js b/src/index.js index a01f912..87790ba 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,72 @@ import Game from './Game.js'; import TaskQueue from './TaskQueue.js'; import SpeedRate from './SpeedRate.js'; +class Creature extends Card{ + getDescriptions(){ + let str1 = super.getDescriptions(); + let str2 = getCreatureDescription(this); + return[str1, str2]; + } + +} + +class Duck extends Creature{ + constructor(name = "Мирная утка", maxPower = 2){ + super(name, maxPower) + } + quacks() { console.log('quack') }; + swims() { console.log('float: both;') }; +} + +class BigDuck extends Duck{ + constructor(name = "Большая утка", maxPower = 4){ + super(name, maxPower) + } +} + +class Dog extends Creature{ + constructor(name = "Пес-бандит", maxPower = 3){ + super(name, maxPower) + } +} + +class Trasher extends Dog{ + constructor(name = "Громила", maxPower = 4){ + super(name, maxPower) + } + modifyDealedDamageToCreature (value, toCard, gameContext, continuation) { + this.view.signalAbility(() => {continuation(value - 1)}); + }; + getDescriptions() { + return ['Входящий урон уменьшен на 1', + super.getDescription(), + + ]; + }; +} + +class Gatling extends Creature{ + constructor(name = "Гатлинг", maxPower = 6){ + super(name, maxPower) + } + attack(gameContext, continuation) { + const taskQueue = new TaskQueue(); + + const {currentPlayer, oppositePlayer, position, updateView} = gameContext; + for(let i = 0; i this.view.showAttack(onDone)); + taskQueue.push(onDone => { + + if (element) { + this.dealDamageToCreature(2, element, gameContext, onDone); + } + }); + } + taskQueue.continueWith(continuation); + }; +} + // Отвечает является ли карта уткой. function isDuck(card) { return card && card.quacks && card.swims; @@ -27,30 +93,20 @@ function getCreatureDescription(card) { return 'Существо'; } - - -// Основа для утки. -function Duck() { - this.quacks = function () { console.log('quack') }; - this.swims = function () { console.log('float: both;') }; -} - - -// Основа для собаки. -function Dog() { -} - - // Колода Шерифа, нижнего игрока. const seriffStartDeck = [ - new Card('Мирный житель', 2), - new Card('Мирный житель', 2), - new Card('Мирный житель', 2), + new Duck(), + new Duck(), + new Gatling(), + new BigDuck(), ]; // Колода Бандита, верхнего игрока. const banditStartDeck = [ - new Card('Бандит', 3), + new Trasher(), + new Dog(), + new Dog(), + new Dog(), ];