From 6df86f7293bde32e4fec70e5ddde4bd9d3a48194 Mon Sep 17 00:00:00 2001 From: philya71828 Date: Sun, 29 Aug 2021 14:36:35 +0500 Subject: [PATCH 1/2] Task done 1-6 --- src/TaskQueue.js | 62 ++++++++--------- src/index.html | 3 +- src/index.js | 172 ++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 193 insertions(+), 44 deletions(-) diff --git a/src/TaskQueue.js b/src/TaskQueue.js index f3a9c6a..dc8dfec 100644 --- a/src/TaskQueue.js +++ b/src/TaskQueue.js @@ -1,10 +1,34 @@ -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); + } +} + +export default 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 +45,7 @@ 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; +} \ No newline at end of file diff --git a/src/index.html b/src/index.html index c47893c..4802c6d 100644 --- a/src/index.html +++ b/src/index.html @@ -96,13 +96,14 @@
10 / 10
+
World
Wide
West
-
+ diff --git a/src/index.js b/src/index.js index a01f912..145ac4e 100644 --- a/src/index.js +++ b/src/index.js @@ -27,30 +27,182 @@ function getCreatureDescription(card) { return 'Существо'; } - +//Класс Creature +class Creature extends Card +{ + getDescriptions() + { + return [getCreatureDescription(this), super.getDescriptions()]; + } +}; // Основа для утки. -function Duck() { +/*function Duck() { this.quacks = function () { console.log('quack') }; this.swims = function () { console.log('float: both;') }; -} - +}*/ +// Класс Duck +class Duck extends Creature +{ + constructor(name, maxPower, image) + { + super(name||'Мирная утка', maxPower||2, image); + } + quacks() { console.log('quack') }; + swims() { console.log('float: both;') }; +}; // Основа для собаки. -function Dog() { +/*function Dog() { +}*/ +// Класс Dog +class Dog extends Creature +{ + constructor(name, maxPower, image) + { + super(name||'Пес-бандит', maxPower||3, image); + } +}; + +// Класс Trasher +class Trasher extends Dog +{ + constructor(name, maxPower, image) + { + super(name||'Громила', maxPower||5, image); + } + + quacks() { console.log('quack') }; + swims() { console.log('float: both;') }; + + modifyTakenDamage(value, fromCard, gameContext, continuation) + { + this.view.signalAbility(() => { + super.modifyTakenDamage(value - 1, fromCard, gameContext, continuation); + }); + }; + + getDescriptions() + { + const rez = super.getDescriptions(); + rez.unshift('Урон на 1 меньше', '

'); + return rez; + }; +}; + +// Класс Gatling +class Gatling extends Creature +{ + constructor(name, maxPower, image) + { + super(name||'Гатлинг', maxPower||6, image); + } + + modifyDealedDamageToCreature (value, toCard, gameContext, continuation) + { + this.view.signalAbility(() => { + super.modifyDealedDamageToCreature (2, toCard, gameContext, continuation); + }); + } + + attack (gameContext, continuation) + { + const taskQueue = new TaskQueue(); + const {currentPlayer, oppositePlayer, position, updateView} = gameContext; + taskQueue.push(onDone => this.view.showAttack(onDone)); + for(let pos = 0; pos < oppositePlayer.table.length; pos++) + { + //taskQueue.push(onDone => this.view.showAttack(onDone)); + taskQueue.push(onDone => { + const oppositeCard = oppositePlayer.table[pos]; + if (oppositeCard) { + this.dealDamageToCreature(this.currentPower, oppositeCard, gameContext, onDone); + } + }); + } + + taskQueue.continueWith(continuation); + }; } +// Класс Lad +class Lad extends Dog +{ + constructor(name, maxPower, image) + { + super(name||'Браток', maxPower||2, image); + } + + static getInGameCount() { return this.inGameCount || 0; } + static setInGameCount(value) { this.inGameCount = value; } + + doAfterComingIntoPlay(gameContext, continuation) + { + Lad.setInGameCount(Lad.getInGameCount() + 1); + super.doAfterComingIntoPlay(gameContext, continuation); + }; + + doBeforeRemoving(continuation) + { + Lad.setInGameCount(Lad.getInGameCount() - 1); + super.doBeforeRemoving(continuation); + }; + + static getBonus() + { + let countLad = this.getInGameCount(); + return Math.round(countLad * (countLad + 1) / 2); + } + + modifyTakenDamage(value, fromCard, gameContext, continuation) + { + this.view.signalAbility(() => { + super.modifyTakenDamage(value - Lad.getBonus(), fromCard, gameContext, continuation); + }); + }; + + modifyDealedDamageToCreature (value, toCard, gameContext, continuation) + { + this.view.signalAbility(() => { + super.modifyDealedDamageToCreature (value + Lad.getBonus(), toCard, gameContext, continuation); + }); + } + + getDescriptions() + { + if (Lad.prototype.hasOwnProperty('modifyDealedDamageToCreature') || Lad.prototype.hasOwnProperty('modifyTakenDamage')) + { + const rez = super.getDescriptions(); + rez.unshift('Чем их больше, тем они сильнее', '

'); + return rez; + } + else + { + return super.getDescriptions(); + } + }; +}; + // Колода Шерифа, нижнего игрока. const seriffStartDeck = [ - new Card('Мирный житель', 2), - new Card('Мирный житель', 2), - new Card('Мирный житель', 2), + new Gatling(), + new Duck(), + new Duck(), + new Duck(), + new Gatling(), + new Duck(), ]; // Колода Бандита, верхнего игрока. const banditStartDeck = [ - new Card('Бандит', 3), + new Dog(), + new Dog(), + new Trasher(), + new Lad(), + new Dog(), + new Lad(), + new Dog(), ]; @@ -58,7 +210,7 @@ const banditStartDeck = [ const game = new Game(seriffStartDeck, banditStartDeck); // Глобальный объект, позволяющий управлять скоростью всех анимаций. -SpeedRate.set(1); +SpeedRate.set(0.5); // Запуск игры. game.play(false, (winner) => { From b3e586efef5d21c4aa7daab2c06c99116bc5f74b Mon Sep 17 00:00:00 2001 From: philya71828 Date: Sun, 29 Aug 2021 19:26:56 +0500 Subject: [PATCH 2/2] Task done --- src/index.js | 158 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 141 insertions(+), 17 deletions(-) diff --git a/src/index.js b/src/index.js index 145ac4e..ffc5d5c 100644 --- a/src/index.js +++ b/src/index.js @@ -29,11 +29,21 @@ function getCreatureDescription(card) { //Класс Creature class Creature extends Card -{ - getDescriptions() +{ constructor(name, maxPower, image) { + super (name, maxPower, image); + this._currentPower = maxPower; + } + + getDescriptions() { return [getCreatureDescription(this), super.getDescriptions()]; } + + set currentPower(value) { + value > this.maxPower ? this._currentPower = this.maxPower : this._currentPower = value; + } + + get currentPower() {return this._currentPower;} }; // Основа для утки. @@ -84,11 +94,17 @@ class Trasher extends Dog getDescriptions() { - const rez = super.getDescriptions(); - rez.unshift('Урон на 1 меньше', '

'); - return rez; + if (Trasher.prototype.hasOwnProperty('modifyTakenDamage')) + { + const rez = super.getDescriptions(); + rez.unshift('Урон на 1 меньше', '

'); + return rez; + } else + { + return super.getDescriptions(); + } }; -}; +} // Класс Gatling class Gatling extends Creature @@ -183,34 +199,142 @@ class Lad extends Dog }; }; +// Класс Rogue +class Rogue extends Creature +{ + constructor(name, maxPower, image) + { + super(name||'Изгой', maxPower||2, image); + } + + // Забирает способности у типа карты противника + doBeforeAttack(gameContext, continuation) + { + const {currentPlayer, oppositePlayer, position, updateView} = gameContext; + if (position < oppositePlayer.table.length) + { + let objPrototype = Object.getPrototypeOf(oppositePlayer.table[position]); + let arrPr = ['modifyDealedDamageToCreature', 'modifyDealedDamageToPlayer', 'modifyTakenDamage']; + for (let i = 0; i < arrPr.length; i++) + if (objPrototype.hasOwnProperty(arrPr[i])) + { + // добавляем способность изгою, но не в прототип, чтобы не наследовалась + currentPlayer.table[position][arrPr[i]] = objPrototype[arrPr[i]]; + // удаляем способность у класса карты соперника + delete objPrototype[arrPr[i]]; + // запоминаем, у кого отняли способности + currentPlayer.table[position].takeAway = oppositePlayer.table[position].name; + } + updateView(); + } + continuation(); + }; + + getDescriptions() + { + let str = 'Отнимаю способности' + if (this.takeAway) str = 'Отнял способности у карты \"' + this.takeAway + '\"'; + const rez = super.getDescriptions(); + rez.unshift(str, '

'); + return rez; + }; +} + +// Класс Brewer +class Brewer extends Duck +{ + constructor(name, maxPower, image) + { + super(name||'Пивовар', maxPower||2, image); + } + + // Раздача "пива" + doBeforeAttack(gameContext, continuation) + { + const {currentPlayer, oppositePlayer, position, updateView} = gameContext; + let arrCard = currentPlayer.table.concat(oppositePlayer.table); + for (let i = 0; i < arrCard.length; i++) + if (isDuck(arrCard[i])) + arrCard[i].view.signalHeal (() => { + arrCard[i].maxPower++; + arrCard[i].currentPower += 2; + arrCard[i].updateView(); + }); + continuation(); + }; + + getDescriptions() + { + const rez = super.getDescriptions(); + rez.unshift('Раздаю уткам способности', '

'); + return rez; + }; + +}; + +// Класс PseudoDuck +class PseudoDuck extends Dog +{ + constructor(name, maxPower, image) + { + super(name||'Псевдоутка', maxPower||3, image); + } + + quacks() { console.log('quack') }; + swims() { console.log('float: both;') }; +} + +// Класс Nemo +class Nemo extends Creature +{ + constructor(name, maxPower, image) + { + super(name||'Немо', maxPower||4, image); + } + + // Крадет прототип у карты противника, и теряет свою способность воровать + doBeforeAttack(gameContext, continuation) + { + const {currentPlayer, oppositePlayer, position, updateView} = gameContext; + if (position < oppositePlayer.table.length) + { + let proto = Object.getPrototypeOf(oppositePlayer.table[position]); + Object.setPrototypeOf(this, proto); + updateView(); + this.doBeforeAttack(gameContext, continuation); + } else continuation(); + }; +} + // Колода Шерифа, нижнего игрока. const seriffStartDeck = [ - new Gatling(), - new Duck(), - new Duck(), new Duck(), - new Gatling(), new Duck(), + new Rogue(), + new Rogue(), + new Brewer(), + new Brewer(), + new Nemo(), ]; // Колода Бандита, верхнего игрока. const banditStartDeck = [ - new Dog(), - new Dog(), - new Trasher(), new Lad(), - new Dog(), new Lad(), - new Dog(), + new Lad(), + new Dog(), + new Dog(), + new PseudoDuck(), + new Brewer(), + new Brewer(), ]; - // Создание игры. const game = new Game(seriffStartDeck, banditStartDeck); // Глобальный объект, позволяющий управлять скоростью всех анимаций. -SpeedRate.set(0.5); +SpeedRate.set(1); // Запуск игры. game.play(false, (winner) => {