From edcc640f3a8f99e7b9459824dc9834f3dd264430 Mon Sep 17 00:00:00 2001 From: ihatememore Date: Wed, 16 Jun 2021 19:51:53 +0500 Subject: [PATCH] 1-10 --- src/TaskQueue.js | 62 ++++++++++++-- src/index.js | 213 +++++++++++++++++++++++++++++++++++++++++++---- src/styles.css | 6 +- 3 files changed, 255 insertions(+), 26 deletions(-) diff --git a/src/TaskQueue.js b/src/TaskQueue.js index f3a9c6a..e14576e 100644 --- a/src/TaskQueue.js +++ b/src/TaskQueue.js @@ -1,9 +1,8 @@ -const TaskQueue = function() { +/*const TaskQueue = function() { function TaskQueue() { this.tasks = []; this.running = false; } - TaskQueue.prototype.push = function(run, dispose, duration) { if (duration === undefined || duration === null) { this.tasks.push({runAndContinue: run, dispose}); @@ -20,24 +19,20 @@ const TaskQueue = function() { } runNextTask(this); }; - TaskQueue.prototype.continueWith = function(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); }); @@ -48,8 +43,59 @@ const TaskQueue = function() { runNextTask(taskQueue); } } - return TaskQueue; -}(); +}();*/ + +class TaskQueue { + constructor(props) { + this.tasks = []; + this.running = false; + } + + push (run, dispose, duration) { + if (duration === undefined || duration === null) { + this.tasks.push({runAndContinue: run, dispose}); + } else { + this.tasks.push({ + runAndContinue: (continuation) => { + run(); + setTimeout(() => { + continuation(); + }, duration); + }, + dispose + }); + } + this.#runNextTask(this); + } + + continueWith (action) { + this.push(action, null, 0); + }; + + #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(() => { + this.#runNextTask(taskQueue); + }); + }); + }, 0); + } + else { + this.#runNextTask(taskQueue); + } + } +} export default TaskQueue; diff --git a/src/index.js b/src/index.js index a01f912..f63d2a6 100644 --- a/src/index.js +++ b/src/index.js @@ -27,40 +27,223 @@ function getCreatureDescription(card) { return 'Существо'; } +class Creature extends Card { + constructor(name, maxPower, image) { + super(name, maxPower, image); + } + get power() { + return this.currentPower; + } -// Основа для утки. -function Duck() { - this.quacks = function () { console.log('quack') }; - this.swims = function () { console.log('float: both;') }; + set power(value) { + this.currentPower = Math.min(this.maxPower, this.currentPower + value); + } + + getDescriptions() { + return [getCreatureDescription(this), super.getDescriptions()] + } } +// Основа для утки. +class Duck extends Creature { + constructor(name = "Мирная утка", maxPower = 2, image) { + super(name, maxPower); + } + + quacks() { + console.log('quack') + }; + + swims() { + console.log('float: both;') + }; +} // Основа для собаки. -function Dog() { +class Dog extends Creature { + constructor(name = "Пес-бандит", maxPower = 3, image) { + super(name, maxPower); + } +} + +class Trasher extends Dog { + constructor(name = "Большой злой дядя", maxPower = 5, image) { + super(name, maxPower, image); + } + + modifyTakenDamage(value, toCard, gameContext, continuation) { + this.view.signalAbility(() => continuation(value - 1)) + } + +} + +class Gatling extends Creature { + constructor(name = "Рембо с БОЛЬШОЙ пушкой", maxPower = 6, image) { + super(name, maxPower); + } + + attack(gameContext, continuation) { + const taskQueue = new TaskQueue(); + + const {currentPlayer, oppositePlayer, position, updateView} = gameContext; + + taskQueue.push(onDone => this.view.showAttack(onDone)); + const enemies = gameContext.oppositePlayer.table; + for (const enemy of gameContext.oppositePlayer.table) { + if (enemies) { + taskQueue.push(onDone => { + this.dealDamageToCreature(this.currentPower, enemy, gameContext, onDone); + }); + } else { + taskQueue.push(onDone => this.dealDamageToPlayer(1, gameContext, onDone)); + } + } + + taskQueue.continueWith(continuation); + } +} + +class Lad extends Dog { + static #count = 0; + + static getBonus() { + return Lad.#count * (Lad.#count + 1) / 2; + } + + constructor(name = "Бригада, туудуду", maxPower = 2, image) { + super(name, maxPower, image); + } + + doAfterComingIntoPlay(gameContext, continuation) { + Lad.#count++; + super.doAfterComingIntoPlay(gameContext, continuation); + } + + modifyTakenDamage(value, toCard, gameContext, continuation) { + this.view.signalAbility(() => + super.modifyTakenDamage(value - Lad.getBonus(), toCard, gameContext, continuation)); + } + + doBeforeRemoving(gameContext, continuation) { + Lad.#count--; + super.doBeforeRemoving(gameContext, continuation); + } + + modifyDealedDamageToCreature(value, toCard, gameContext, continuation) { + super.modifyDealedDamageToCreature(value + Lad.getBonus(), toCard, gameContext, continuation) + } + + getDescriptions() { + if (Lad.prototype.hasOwnProperty('modifyDealedDamageToCreature')) + return ["Бригада, вместе мы сильнее ", ...super.getDescriptions()]; + return super.getDescriptions(); + } +} + +class Rogue extends Creature { + constructor(name = "Воришка Баллов", maxPower = 2, image) { + super(name, maxPower, image); + } + + doBeforeAttack (gameContext, continuation) { + const {currentPlayer, oppositePlayer, position, updateView} = gameContext; + + if(oppositePlayer.table[position]) { + let cardOpponent = Object.getPrototypeOf(oppositePlayer.table[position]); + if (cardOpponent.modifyDealedDamageToCreature) { + this["modifyDealedDamageToCreature"] = cardOpponent["modifyDealedDamageToCreature"]; + delete cardOpponent["modifyDealedDamageToCreature"]; + } + if (cardOpponent.modifyTakenDamage) { + this["modifyTakenDamage"] = cardOpponent["modifyTakenDamage"]; + delete cardOpponent["modifyTakenDamage"]; + } + if (cardOpponent.modifyDealedDamageToPlayer) { + this["modifyDealedDamageToPlayer"] = cardOpponent["modifyDealedDamageToPlayer"]; + delete cardOpponent["modifyDealedDamageToPlayer"]; + } + } + + updateView(); + continuation(); + }; } +class Brewer extends Duck { + constructor(name = "Пивовар Жигуля", maxPower = 2, image) { + super(name, maxPower); + } + + doBeforeAttack (gameContext, continuation) { + const {currentPlayer, oppositePlayer, position, updateView} = gameContext; + let allCards = currentPlayer.table.concat(oppositePlayer.table) + + for (let card of allCards) { + if (isDuck(card)) { + card.maxPower += 1; + card.power += 2; + } + } + + this.view.signalHeal(continuation) + + updateView(); + }; +} + +class PseudoDuck extends Dog { + constructor(name = "Псайдак", maxPower = 3, image) { + super(name, maxPower); + } + + quacks() { + console.log('quack') + }; + + swims() { + console.log('float: both;') + }; +} + +class Nemo extends Creature { + constructor(name = "Немо", maxPower = 4, image) { + super(name, maxPower); + } + + getDescriptions() { + return ["The one without a name without an honest heart as compass", ...super.getDescriptions()]; + } + + doBeforeAttack (gameContext, continuation) { + const {currentPlayer, oppositePlayer, position, updateView} = gameContext; + + if(oppositePlayer.table[position]) { + let cardOpponent = Object.getPrototypeOf(oppositePlayer.table[position]); + Object.setPrototypeOf(this, cardOpponent); + } + + updateView(); + continuation( this.doBeforeAttack(gameContext, continuation)); + + }; +} -// Колода Шерифа, нижнего игрока. const seriffStartDeck = [ - new Card('Мирный житель', 2), - new Card('Мирный житель', 2), - new Card('Мирный житель', 2), + new Nemo(), ]; - -// Колода Бандита, верхнего игрока. const banditStartDeck = [ - new Card('Бандит', 3), + new Brewer(), + new Brewer(), ]; - // Создание игры. const game = new Game(seriffStartDeck, banditStartDeck); // Глобальный объект, позволяющий управлять скоростью всех анимаций. -SpeedRate.set(1); +SpeedRate.set(1.5); // Запуск игры. game.play(false, (winner) => { alert('Победил ' + winner.name); -}); +}); \ No newline at end of file diff --git a/src/styles.css b/src/styles.css index 5715729..c304933 100644 --- a/src/styles.css +++ b/src/styles.css @@ -212,9 +212,9 @@ body { opacity: 0.0; } -.cardSignal { - transform: rotateY(180deg); -} +/*.cardSignal {*/ +/* transform: rotateY(180deg);*/ +/*}*/ .cardSignal.damage, .playerSignal.damage {