Skip to content

Commit

Permalink
Reduce code duplication on createPet function
Browse files Browse the repository at this point in the history
  • Loading branch information
Technickel-Dev committed Oct 17, 2022
1 parent 53fbfcd commit bef9d03
Showing 1 changed file with 46 additions and 132 deletions.
178 changes: 46 additions & 132 deletions src/panel/pets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1049,137 +1049,51 @@ 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");
}

0 comments on commit bef9d03

Please sign in to comment.