Unverified Commit 85130e01 authored by Michal Čaniga's avatar Michal Čaniga
Browse files

Consume only one powerup with given id

parent 18392c4f
...@@ -5,7 +5,7 @@ import { maxHearts } from '../../data/constants'; ...@@ -5,7 +5,7 @@ import { maxHearts } from '../../data/constants';
import { Food } from '../../data/food'; import { Food } from '../../data/food';
import { SetUserData } from '../../utils/commonTypes'; import { SetUserData } from '../../utils/commonTypes';
import { Powerup } from './Powerup'; import { consumePowerup, Powerup } from './Powerup';
type FoodPowerupProps = { type FoodPowerupProps = {
setHearts: (hearts: number) => void; setHearts: (hearts: number) => void;
...@@ -37,7 +37,7 @@ export const FoodPowerup = ({ ...@@ -37,7 +37,7 @@ export const FoodPowerup = ({
prev !== undefined prev !== undefined
? { ? {
...prev, ...prev,
foodIds: prev.foodIds.filter(id => id !== food.id) foodIds: consumePowerup(prev.pokeballIds, food.id)
} }
: undefined : undefined
); );
......
...@@ -10,7 +10,7 @@ import { ...@@ -10,7 +10,7 @@ import {
} from '../../utils/commonTypes'; } from '../../utils/commonTypes';
import { PokemonOnCanvas } from '../../utils/pokemonFetcher'; import { PokemonOnCanvas } from '../../utils/pokemonFetcher';
import { Powerup } from './Powerup'; import { consumePowerup, Powerup } from './Powerup';
type PokeballPowerupProps = { type PokeballPowerupProps = {
pokeball: Pokeball; pokeball: Pokeball;
...@@ -43,7 +43,7 @@ export const PokeballPowerup = ({ ...@@ -43,7 +43,7 @@ export const PokeballPowerup = ({
prev !== undefined prev !== undefined
? { ? {
...prev, ...prev,
pokeballIds: prev.pokeballIds.filter(id => id !== pokeball.id) pokeballIds: consumePowerup(prev.pokeballIds, pokeball.id)
} }
: undefined : undefined
); );
......
...@@ -7,6 +7,11 @@ type PowerupProps = { ...@@ -7,6 +7,11 @@ type PowerupProps = {
style?: React.CSSProperties; style?: React.CSSProperties;
}; };
export const consumePowerup = (ids: number[], powerupId: number) => {
const powerupIdx = ids.findIndex(id => id === powerupId);
return [...ids.slice(0, powerupIdx), ...ids.slice(powerupIdx + 1)];
};
export const Powerup = ({ icon, onClick, style }: PowerupProps) => ( export const Powerup = ({ icon, onClick, style }: PowerupProps) => (
<Button <Button
shape="circle" shape="circle"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment