Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Яковлева Анастасия #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
62 changes: 54 additions & 8 deletions src/TaskQueue.js
Original file line number Diff line number Diff line change
@@ -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});
Expand All @@ -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);
});
Expand All @@ -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;
213 changes: 198 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
6 changes: 3 additions & 3 deletions src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ body {
opacity: 0.0;
}

.cardSignal {
transform: rotateY(180deg);
}
/*.cardSignal {*/
/* transform: rotateY(180deg);*/
/*}*/

.cardSignal.damage,
.playerSignal.damage {
Expand Down