Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 30 additions & 31 deletions src/TaskQueue.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;
173 changes: 164 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
];


Expand Down