diff --git a/src/TaskQueue.js b/src/TaskQueue.js index f3a9c6a..d58c570 100644 --- a/src/TaskQueue.js +++ b/src/TaskQueue.js @@ -1,10 +1,35 @@ -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 +46,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..2a90247 100644 --- a/src/index.js +++ b/src/index.js @@ -27,30 +27,185 @@ function getCreatureDescription(card) { return 'Существо'; } +class Creature extends Card{ + constructor(name,power,image){ + super(name,power,image); + } + getDescriptions(){ + return [getCreatureDescription(this),...super.getDescriptions()] + } +}; + // Основа для утки. -function Duck() { - this.quacks = function () { console.log('quack') }; - this.swims = function () { console.log('float: both;') }; -} +class Duck extends Creature { + constructor(name='Мирная утка', power=2, image=''){ + super(name,power,image) + } + quacks(){ + console.log('quack') + }; + swims () { + console.log('float: both;') + }; +}; + + // Основа для собаки. -function Dog() { +class Dog extends Creature { + constructor(name='Пес-Бандит',power=3,image=''){ + super(name,power,image); + } + +}; + +class Trasher extends Dog{ + constructor(){ + super('Громила', 5,''); + } + + modifyTakenDamage(value, fromCard, gameContext, continuation){ + this.view.signalAbility(() => continuation(value - 1)); + } + + getDescriptions(){ + return ['Получает на 1 урон меньше ', super.getDescriptions()] + } +}; + +class Gatling extends Creature{ + constructor(){ + super('Гатлинг',6,''); + } + + attack (gameContext, continuation) { + let taskQueue = new TaskQueue(); + let oppositePlayer = gameContext.oppositePlayer; + for (let card of oppositePlayer.table) { + taskQueue.push((onDone) => this.view.showAttack(onDone)); + taskQueue.push((onDone) => + this.dealDamageToCreature(2, card, gameContext, onDone) + ); + } + taskQueue.continueWith(continuation); + } +}; + +class Lad extends Dog{ + static getInGameCount() { return this.inGameCount || 0; } + static setInGameCount(value) { this.inGameCount = value; } + static getBonus() { return this.getInGameCount() * (this.getInGameCount() + 1) / 2} + constructor(){ + super('Браток',2,''); + } + doAfterComingIntoPlay(gameContext, continuation) { + Lad.setInGameCount(Lad.getInGameCount() + 1); + continuation(); + } + + doBeforeRemoving(continuation) { + Lad.setInGameCount(Lad.getInGameCount() - 1); + continuation(); + }; + + modifyTakenDamage(value, fromCard, gameContext, continuation) { + this.view.signalAbility(() => continuation(value - Lad.getBonus())); + } + + modifyDealedDamageToCreature(value, toCard, gameContext, continuation) { + continuation(value + Lad.getBonus()); + } + + getDescriptions() { + let descriptions = []; + if (Lad.prototype.hasOwnProperty('modifyDealedDamageToCreature') || + Lad.prototype.hasOwnProperty('modifyTakenDamage')) { + descriptions = ["Чем их больше, тем они сильнее"]; + } + return descriptions.concat([...super.getDescriptions()]); + } + +}; + +class Rogue extends Creature { + constructor(name = "Изгой", maxPower = 2, image) { + super(name, maxPower, image); + } + + doBeforeAttack(gameContext, continuation){ + const {currentPlayer, oppositePlayer, position, updateView} = gameContext; + //Изгой похищает эти способности: modifyDealedDamageToCreature, modifyDealedDamageToPlayer, modifyTakenDamage + + let cardOpponent = oppositePlayer.table[position]; + if (cardOpponent) { + let prototype = Object.getPrototypeOf(cardOpponent); + if (prototype.modifyDealedDamageToCreature) { + this.modifyDealedDamageToCreature = prototype.modifyDealedDamageToCreature; + delete prototype.modifyDealedDamageToCreature; + } + + if (prototype.modifyDealedDamageToPlayer) { + this.modifyDealedDamageToPlayer = prototype.modifyDealedDamageToPlayer; + delete prototype.modifyDealedDamageToPlayer; + } + + if (prototype.modifyTakenDamage) { + this.modifyTakenDamage = prototype.modifyTakenDamage; + delete prototype.modifyTakenDamage; + } + } + + updateView(); + continuation(); + } + + getDescriptions() { + return ["Ворует силы", ...super.getDescriptions()]; + } +} + +class Brewer extends Duck { + constructor(name = "Пивовар", maxPower = 2) { + super(name, maxPower); + } + + doBeforeAttack(gameContext, continuation) { + let taskQueue = new TaskQueue(); + const cards = gameContext.currentPlayer.table.concat(gameContext.oppositePlayer.table); + + for(let card of cards) { + + if(isDuck(card)) { + card.view.signalHeal(); + card.maxPower += 1; + card.currentPower += 2; + card.updateView(); + } + } + + taskQueue.continueWith(continuation); + } } // Колода Шерифа, нижнего игрока. const seriffStartDeck = [ - new Card('Мирный житель', 2), - new Card('Мирный житель', 2), - new Card('Мирный житель', 2), + new Duck(), + new Gatling(), + new Brewer(), + new Rogue() + ]; // Колода Бандита, верхнего игрока. const banditStartDeck = [ - new Card('Бандит', 3), + new Trasher(), + new Lad(), + new Lad(), + new Lad() ];