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

MNG-96 pokemon attack #32

Merged
merged 6 commits into from
Feb 13, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
73 changes: 59 additions & 14 deletions src/fightPage/buttonsEventListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { createActivePlayer } from "./createActivePlayer";
import { createHPBars } from "./createHPBars";
import { createFightPagePokeballs } from "./createFightPagePokeballs";
import { updateMovesList } from "./updateMovesList";
import { Fight } from "../fightClass"
import { PokemonMove } from "../pokemonClass";

export const actionsButtonEventListener = (
//player: Player,
Expand All @@ -13,6 +15,21 @@ export const actionsButtonEventListener = (
const battleButtons = document.getElementsByClassName(
"battleButton"
)! as HTMLCollectionOf<HTMLElement>;

animationButtonsEntry(battleButtons);
if (checkIfPokeWasDefeated(gameHandler)) {
console.log("Defeated");
gameHandler.generateSwitchButtons(false)
} else {
prepareActions(gameHandler, battleButtons)
};
}

const prepareActions = (
gameHandler: GameHandler,
battleButtons: HTMLCollectionOf<HTMLElement>
) => {

const attackButton = document.querySelector(
"#attackButton"
)! as HTMLDivElement;
Expand All @@ -22,8 +39,6 @@ export const actionsButtonEventListener = (
const mangoButton = document.querySelector("#mangoButton")! as HTMLDivElement;
const mango = document.querySelector("#mango")! as HTMLDivElement;

animationButtonsEntry(battleButtons);

attackButton.addEventListener("click", () => {
animationButtonsExit(battleButtons);
setTimeout(() => {
Expand All @@ -33,7 +48,7 @@ export const actionsButtonEventListener = (
switchButton.addEventListener("click", () => {
animationButtonsExit(battleButtons);
setTimeout(() => {
gameHandler.generateSwitchButtons();
gameHandler.generateSwitchButtons(true);
}, 1000);
});

Expand All @@ -43,7 +58,7 @@ export const actionsButtonEventListener = (
mango.innerHTML = "0";
mangoButton.classList.add("disabledButton");
}
};
}

export const attacksButtonEventListener = (gameHandler: GameHandler) => {
const attackButtonOne = document.querySelector(
Expand All @@ -57,13 +72,28 @@ export const attacksButtonEventListener = (gameHandler: GameHandler) => {
)! as HTMLCollectionOf<HTMLElement>;
const backButton = document.querySelector("#backButton")! as HTMLDivElement;

const moveOne = gameHandler.currentPlayer.getActivePokemon.moves[0];
const moveTwo = gameHandler.currentPlayer.getActivePokemon.moves[1];
animationButtonsEntry(battleButtons);
magicFunction(attackButtonOne, battleButtons, gameHandler, attack);
magicFunction(attackButtonTwo, battleButtons, gameHandler, attack);
magicFunction(attackButtonOne, battleButtons, gameHandler, attack, moveOne);
Copy link
Collaborator

@AleksandraCyp AleksandraCyp Feb 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const pokemon = gameHandler.currentPlayer.getActivePokemon;
const move = pokemon.moves.find(move => move.moveName === e.currentTarget.textContent)

Maybe instead of adding moves to magic funtion and event listeners, you could use the following code in attack function? (pass event as parameter to the attack function?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I used getting move by event.
I will merge it after you pull request will be on master.

magicFunction(attackButtonTwo, battleButtons, gameHandler, attack, moveTwo);
magicFunction(backButton, battleButtons, gameHandler);
};

export const switchButtonEventListener = (gameHandler: GameHandler) => {
const checkIfPokeWasDefeated = (
gameHandler: GameHandler
) => {
if (gameHandler.currentPlayer.getActivePokemon.isAlive()) {
return false
}
return true
}


export const switchButtonEventListener = (
gameHandler: GameHandler,
backEnabled: boolean
) => {
const switchButtonOne = document.querySelector(
"#switchButtonOne"
)! as HTMLDivElement;
Expand All @@ -76,22 +106,30 @@ export const switchButtonEventListener = (gameHandler: GameHandler) => {
const backButton = document.querySelector("#backButton")! as HTMLDivElement;

animationButtonsEntry(battleButtons);

magicFunction(switchButtonOne, battleButtons, gameHandler, switchPoke);
magicFunction(switchButtonTwo, battleButtons, gameHandler, switchPoke);
magicFunction(backButton, battleButtons, gameHandler);
if (switchButtonOne !== null) {
magicFunction(switchButtonOne, battleButtons, gameHandler, switchPoke);
}
if (switchButtonTwo !== null) {
magicFunction(switchButtonTwo, battleButtons, gameHandler, switchPoke);
}
if (backButton !== null && backEnabled) {
magicFunction(backButton, battleButtons, gameHandler);
}
};

const magicFunction = (
button: HTMLDivElement,
buttons: HTMLCollectionOf<HTMLElement>,
gameHandler: GameHandler,
functionToCall?: any
functionToCall?: any,
additionalParameter?: any
) => {
button.addEventListener("click", (e) => {
console.log(`${button.innerText} used!`);
if (functionToCall) {
functionToCall(gameHandler, e);
additionalParameter
? functionToCall(gameHandler, additionalParameter, e)
: functionToCall(gameHandler, e);
updateMovesList(gameHandler, functionToCall, e);
createActivePokemon(gameHandler);
createHPBars(gameHandler.playerOne, gameHandler.playerTwo);
Expand Down Expand Up @@ -122,8 +160,15 @@ const animationButtonsExit = (buttons: HTMLCollectionOf<HTMLElement>) => {
}
};

export const attack = (gameHandler: GameHandler) => {
export const attack = (gameHandler: GameHandler, move: PokemonMove) => {
console.log("bum bum bach!");
const fight = new Fight()
const attackingPoke = gameHandler.currentPlayer.getActivePokemon;
const defendingPoke = gameHandler.opponentPlayer.getActivePokemon;
console.log("Before attack ", `${defendingPoke.name} has ${defendingPoke.currentHP}`);
const damage: number = fight.fight(attackingPoke, defendingPoke, move)
console.log(`${attackingPoke.name} did ${damage} with ${move.moveName} to ${defendingPoke.name}`);
console.log("After attack ", `${defendingPoke.name} has ${defendingPoke.currentHP}`);
};

export const switchPoke = (gameHandler: GameHandler, event: Event) => {
Expand Down
23 changes: 17 additions & 6 deletions src/gameHandlerClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,25 @@ export class GameHandler {
attacksButtonEventListener(this);
}

generateSwitchButtons(): void {
generateSwitchButtons(backEnabled: boolean): void {
const player = this.currentPlayer;
console.log("Switch choose modal");

const actionContainer = document.querySelector("#actionModals")!;
actionContainer.innerHTML = `
<div class="button battleButton" id="switchButtonOne">${player.notActivePokemons[0].name}</div>
<div class="button battleButton" id="switchButtonTwo">${player.notActivePokemons[1].name}</div>
<div class="button battleButton" id="backButton">back</div>`;
switchButtonEventListener(this);
let innHTML: string = "";
const buttonIds: string[] = ["switchButtonOne", "switchButtonTwo"];
let pokeId: number = 0;
for (let pokemon of player.notActivePokemons) {
if (pokemon.isAlive()) {
innHTML += `<div class="button battleButton" id="${[buttonIds[pokeId]]}">${pokemon.name}</div>`
pokeId++
}
}
if (backEnabled) {
innHTML += `<div class='button battleButton" id="backButton">back</div>`
innHTML += `<div class='button battleButton" id="backButton">back</div>`
}
actionContainer.innerHTML = innHTML;
switchButtonEventListener(this, backEnabled);
}
}