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
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/4-west.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 26 additions & 29 deletions src/TaskQueue.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const TaskQueue = function() {
function 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 {
Expand All @@ -18,38 +17,36 @@ const TaskQueue = function() {
dispose
});
}
runNextTask(this);
nextTask(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();
function nextTask(taskQueue) {
if (taskQueue.running || taskQueue.tasks.length === 0) {
return;
}
taskQueue.running = true;
const currentTask = taskQueue.tasks.shift();

if (task.runAndContinue) {
setTimeout(() => {
task.runAndContinue(() => {
task.dispose && task.dispose();
taskQueue.running = false;
if (currentTask.runAndContinue) {
setTimeout(() => {
currentTask.runAndContinue(() => {
currentTask.dispose && currentTask.dispose();
taskQueue.running = false;

setTimeout(() => {
runNextTask(taskQueue);
});
setTimeout(() => {
nextTask(taskQueue);
});
}, 0);
}
else {
runNextTask(taskQueue);
}
});
}, 0);
}
else {
nextTask(taskQueue);
}
}

return TaskQueue;
}();

export default TaskQueue;
nextTask(new TaskQueue());
202 changes: 181 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import Card from './Card.js';
import Game from './Game.js';
import TaskQueue from './TaskQueue.js';
//import TaskQueue from './TaskQueue.js';
import SpeedRate from './SpeedRate.js';

// Отвечает является ли карта уткой.
function isDuck(card) {
return card && card.quacks && card.swims;
}

// Отвечает является ли карта собакой.
function isDog(card) {
return card instanceof Dog;
}

// Дает описание существа по схожести с утками и собаками
function getCreatureDescription(card) {
if (isDuck(card) && isDog(card)) {
Expand All @@ -27,40 +25,202 @@ function getCreatureDescription(card) {
return 'Существо';
}

class Creature extends Card {
constructor(name, maxPower) {
super(name, maxPower);
}

// setCurrentPower(value) {
// this.currentPower = Math.min(this.maxPower, this.currentPower + value);
// }

getDescriptions() {
return [getCreatureDescription(this), ...super.getDescriptions()]
}
}

class Duck extends Creature {
constructor(name, power) {
super(name ?? "Мирная утка", power ?? 2);
}

quacks() {
console.log('quack')
};

swims() {
console.log('float: both;')
};
}

class Dog extends Creature {
constructor(name, power) {
super(name ?? "Пес-бандит", power ?? 3);
}
}

class Trasher extends Dog {
constructor(name, power) {
super(name ?? "Громила", power ?? 5);
}

modifyTakenDamage(value, fromCard, gameContext, continuation) {
this.view.signalAbility(() => continuation(value - 1));
}

getDescriptions() {
let descriptions = [];
if (Trasher.prototype.hasOwnProperty('modifyTakenDamage')) {
descriptions = ["Урон меньше на 1"];
}
return descriptions.concat([...super.getDescriptions()]);
}
}

class Gatling extends Creature {
constructor(name, power) {
super(name ?? "Гатлинг", power ?? 6);
}

// attack(gameContext, continuation) {
// let taskQueue = new TaskQueue();
// let oppositePlayer = gameContext.oppositePlayer;
// for (let card of oppositePlayer.table) {
// taskQueue.push((attack) => this.view.showAttack(attack));
// taskQueue.push((onDone) => this.dealDamageToCreature(2, card, gameContext, onDone));
// }
// taskQueue.continueWith(continuation);
// game.updateView();
// };
}

class Lad extends Dog {
constructor(name, power) {
super(name ?? "Браток", power ?? 2);
}

static getInGameCount() {
return this.inGameCount || 0;
}

// static setInGameCount(value) {
// this.inGameCount = value;
// }

static getBonus() {
return this.getInGameCount() * (this.getInGameCount() + 1) / 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 = super.getDescriptions();
if (Lad.prototype.hasOwnProperty("modifyDealedDamageToCreature") && Lad.prototype.hasOwnProperty("modifyTakenDamage")) {
descriptions.push("Чем их больше, тем они сильнее");
}
return descriptions;
}
}

class Rogue extends Creature {
constructor(name, power) {
super(name ?? "Изгой", power ?? 2);
}

static stealer = ["modifyDealedDamageToCreature", "modifyDealedDamageToPlayer", "modifyTakenDamage"];

modifyTakenDamage(value, fromCard, gameContext, continuation) {
const prototype = Object.getPrototypeOf(fromCard);
for (const property of Rogue.stealer) {
if (prototype.hasOwnProperty(property)) {
this[property] = prototype[property];
delete prototype[property];
}
}

gameContext.updateView();
super.modifyTakenDamage(value, fromCard, gameContext, continuation);
}

// Основа для утки.
function Duck() {
this.quacks = function () { console.log('quack') };
this.swims = function () { console.log('float: both;') };
getDescriptions() {
let descriptions = ["Ворует способности врагов"];
return descriptions.concat([...super.getDescriptions()]);
}
}

class Brewer extends Duck {
constructor(name, power) {
super(name ?? "Пивовар", power ?? 2);
}

// Основа для собаки.
function Dog() {
// doBeforeAttack(gameContext, continuation) {
// let taskQueue = new TaskQueue();
// let {currentPlayer, oppositePlayer} = gameContext;
// let cards = currentPlayer.table.concat(oppositePlayer.table);
// for (let card of cards) {
// if (isDuck(card)) {
// card.maxPower += 1;
// card.setCurrentPower(2);
// taskQueue.push(onDone => card.view.signalHeal(onDone));
// card.updateView();
// }
// }
// taskQueue.continueWith(continuation);
// }
}

class PseudoDuck extends Dog {
constructor(name, power) {
super(name ?? "Псевдоутка", power ?? 3);
}

// Колода Шерифа, нижнего игрока.
const seriffStartDeck = [
new Card('Мирный житель', 2),
new Card('Мирный житель', 2),
new Card('Мирный житель', 2),
];
quacks() {console.log('quack')};
swims() {console.log('float: both;')};
}

// Колода Бандита, верхнего игрока.
const banditStartDeck = [
new Card('Бандит', 3),
];
class Nemo extends Creature {
constructor(name, power) {
super(name ?? "Немо", power ?? 4);
}

// doBeforeAttack(gameContext, continuation) {
// let taskQueue = new TaskQueue();
// let {oppositePlayer, position} = gameContext;
// let oppositeCard = oppositePlayer.table[position];
//
// if (oppositeCard) {
// Object.setPrototypeOf(this, Object.getPrototypeOf(oppositeCard));
// gameContext.updateView();
// }
// taskQueue.continueWith(continuation);
// }
}

// Создание игры.
const seriffStartDeck = [new Duck(), new Duck(), new Gatling(), new Nemo()];
const banditStartDeck = [new Trasher(), new Brewer(), new Lad(), new PseudoDuck()];
const game = new Game(seriffStartDeck, banditStartDeck);

// Глобальный объект, позволяющий управлять скоростью всех анимаций.
SpeedRate.set(1);
SpeedRate.set(2);

// Запуск игры.
game.play(false, (winner) => {
alert('Победил ' + winner.name);
});
});