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
46 changes: 46 additions & 0 deletions .idea/workspace.xml

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

242 changes: 208 additions & 34 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,231 @@
import Card from './Card.js';
import Game from './Game.js';
import TaskQueue from './TaskQueue.js';
import SpeedRate from './SpeedRate.js';
import Card from "./Card.js";
import Game from "./Game.js";
import TaskQueue from "./TaskQueue.js";
import SpeedRate from "./SpeedRate.js";
class Creature extends Card {
constructor(name, power) {
super(name, power);
this._currentPower = power;
}
get currentPower() {
return this._currentPower;
}

// Отвечает является ли карта уткой.
function isDuck(card) {
return card && card.quacks && card.swims;
set currentPower(value) {
if (value > this.maxPower) {
this._currentPower = this.maxPower;
} else {
this._currentPower = value;
}
}

getDescriptions() {
return [super.getDescriptions(), getCreatureDescription(this)];
}
}
class Duck extends Creature {
constructor(name, power) {
super(name ?? "Мирная утка", power ?? 2);
}

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

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

// Дает описание существа по схожести с утками и собаками
function getCreatureDescription(card) {
if (isDuck(card) && isDog(card)) {
return 'Утка-Собака';
class Dog extends Creature {
constructor(name, power) {
super(name ?? "Пес-бандит", power ?? 3);
}
if (isDuck(card)) {
return 'Утка';
}

class Trasher extends Dog {
constructor(name, power) {
super(name ?? "Громила", power ?? 5);
}
if (isDog(card)) {
return 'Собака';

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

getDescriptions() {
const result = super.getDescriptions();
result.unshift("Громила");
return result;
}
}
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((onDone) => this.view.showAttack(onDone));
taskQueue.push((onDone) =>
this.dealDamageToCreature(2, card, gameContext, onDone),
);
}

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

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

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

static getBonus() {
const count = this.getInGameCount();
return count * (count + 1) / 2;
}

doAfterComingIntoPlay(gameContext, continuation) {
Lad.setInGameCount(Lad.getInGameCount() + 1);
super.doAfterComingIntoPlay(gameContext, continuation);
}
return 'Существо';

doBeforeRemoving(continuation) {
Lad.setInGameCount(Lad.getInGameCount() - 1);
super.doBeforeRemoving(continuation);
}

modifyDealedDamageToCreature(value, toCard, gameContext, continuation) {
super.modifyDealedDamageToCreature(value + Lad.getBonus(), toCard, gameContext, continuation);
}

modifyTakenDamage(value, fromCard, gameContext, continuation) {
super.modifyTakenDamage(value - Lad.getBonus(), fromCard, gameContext, continuation);
}

getDescriptions() {
const descriptions = super.getDescriptions();
if (Lad.prototype.hasOwnProperty("modifyDealedDamageToCreature")
&& Lad.prototype.hasOwnProperty("modifyTakenDamage")) {
descriptions.push("Чем их больше, тем они сильнее");
}
return descriptions;
}

}
class Rogue extends Creature {
static toSteal = ["modifyDealedDamageToCreature", "modifyDealedDamageToPlayer", "modifyTakenDamage"];

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

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

// Основа для утки.
function Duck() {
this.quacks = function () { console.log('quack') };
this.swims = function () { console.log('float: both;') };
gameContext.updateView();
super.modifyTakenDamage(value, fromCard, gameContext, continuation);
}
}

class Brewer extends Duck {

// Основа для собаки.
function Dog() {
constructor(name, power) {
super(name ?? "Пивовар", power ?? 2);
this._currentPower = power ?? 2;
}

modifyDealedDamageToCreature(value, toCard, gameContext, continuation) {
const cards = gameContext.currentPlayer.table.concat(gameContext.oppositePlayer.table);
for (const card of cards) {
if (isDuck(card)) {
card.view.signalHeal(function () {
card.maxPower += 1;
card.currentPower += 2;
card.updateView();
});
}
}
super.modifyDealedDamageToCreature(value, toCard, gameContext, continuation);
}
}

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

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

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

class Nemo extends Creature {
constructor(name, power) {
super(name ?? "Немо", power ?? 4);
}

doBeforeAttack(gameContext, continuation) {
const {currentPlayer, oppositePlayer, position, updateView} = gameContext;
const oppositeCard = oppositePlayer.table[position];
const prototype = Object.getPrototypeOf(oppositeCard);
Object.setPrototypeOf(this, prototype);
gameContext.updateView();
this.doBeforeAttack(gameContext, continuation);
}
}
function isDuck(card) {
return card && card.quacks && card.swims;
}
function isDog(card) {
return card instanceof Dog;
}
// Дает описание существа по схожести с утками и собаками
function getCreatureDescription(card) {
if (isDuck(card) && isDog(card)) {
return "Утка-Собака";
}
if (isDuck(card)) {
return "Утка";
}
if (isDog(card)) {
return "Собака";
}
return "Существо";
}
// Колода Шерифа, нижнего игрока.
const seriffStartDeck = [
new Card('Мирный житель', 2),
new Card('Мирный житель', 2),
new Card('Мирный житель', 2),
new Nemo(),
];

// Колода Бандита, верхнего игрока.
const banditStartDeck = [
new Card('Бандит', 3),
new Brewer(),
new Brewer(),
];


Expand All @@ -59,8 +234,7 @@ const game = new Game(seriffStartDeck, banditStartDeck);

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

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