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

Reduce code duplication on createPet function #270

Merged
Merged
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
168 changes: 36 additions & 132 deletions src/panel/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1049,137 +1049,41 @@ export function createPet(
if (name === undefined || name === null || name === '') {
throw new InvalidPetException('name is undefined');
}
if (petType === 'totoro') {
return new Totoro(
el,
collision,
speech,
size,
left,
bottom,
petRoot,
floor,
name,
PetSpeed.normal,
);
}
if (petType === 'cat') {
return new Cat(
el,
collision,
speech,
size,
left,
bottom,
petRoot,
floor,
name,
PetSpeed.normal,
);
} else if (petType === 'dog') {
return new Dog(
el,
collision,
speech,
size,
left,
bottom,
petRoot,
floor,
name,
PetSpeed.normal,
);
} else if (petType === 'snake') {
return new Snake(
el,
collision,
speech,
size,
left,
bottom,
petRoot,
floor,
name,
PetSpeed.verySlow,
);
} else if (petType === 'clippy') {
return new Clippy(
el,
collision,
speech,
size,
left,
bottom,
petRoot,
floor,
name,
PetSpeed.slow,
);
} else if (petType === 'cockatiel') {
return new Cockatiel(
el,
collision,
speech,
size,
left,
bottom,
petRoot,
floor,
name,
PetSpeed.normal,
);
} else if (petType === 'crab') {
return new Crab(
el,
collision,
speech,
size,
left,
bottom,
petRoot,
floor,
name,
PetSpeed.slow,
);
} else if (petType === 'rubber-duck') {
return new RubberDuck(
el,
collision,
speech,
size,
left,
bottom,
petRoot,
floor,
name,
PetSpeed.fast,
);
} else if (petType === 'zappy') {
return new Zappy(
el,
collision,
speech,
size,
left,
bottom,
petRoot,
floor,
name,
PetSpeed.veryFast,
);
} else if (petType === 'rocky') {
return new Rocky(
el,
collision,
speech,
size,
left,
bottom,
petRoot,
floor,
name,
PetSpeed.still,
);

const standardPetArguments: [
HTMLImageElement,
HTMLDivElement,
HTMLDivElement,
PetSize,
number,
number,
string,
number,
string,
] = [el, collision, speech, size, left, bottom, petRoot, floor, name];

switch (petType) {
case PetType.cat:
return new Cat(...standardPetArguments, PetSpeed.normal);
case PetType.dog:
return new Dog(...standardPetArguments, PetSpeed.normal);
case PetType.crab:
return new Crab(...standardPetArguments, PetSpeed.slow);
case PetType.clippy:
return new Clippy(...standardPetArguments, PetSpeed.slow);
case PetType.totoro:
return new Totoro(...standardPetArguments, PetSpeed.normal);
case PetType.snake:
return new Snake(...standardPetArguments, PetSpeed.verySlow);
case PetType.rubberduck:
return new RubberDuck(...standardPetArguments, PetSpeed.fast);
case PetType.zappy:
return new Zappy(...standardPetArguments, PetSpeed.veryFast);
case PetType.rocky:
return new Rocky(...standardPetArguments, PetSpeed.still);
case PetType.cockatiel:
return new Cockatiel(...standardPetArguments, PetSpeed.normal);
default:
throw new InvalidPetException("Pet type doesn't exist");
}
throw new InvalidPetException("Pet type doesn't exist");
}