Skip to content

Instantly share code, notes, and snippets.

@Caballerog
Created June 14, 2024 11:17
Show Gist options
  • Save Caballerog/8159dc049880d4af0d52cd4e6ddaf1ee to your computer and use it in GitHub Desktop.
Save Caballerog/8159dc049880d4af0d52cd4e6ddaf1ee to your computer and use it in GitHub Desktop.
import { Clonable } from "./clonable";
export class VirtualPet implements Clonable {
name: string;
color: string;
abilities: string[];
energyLevel: number = 0;
constructor(name: string, color: string, abilities: string[], energyLevel: number = 100) {
this.name = name;
this.color = color;
this.abilities = abilities;
this.calculateEnergy(3000);
}
private calculateEnergy(milliseconds: number) {
const start = new Date().getTime();
let end = start;
console.log(`Initializing ${this.name}...`);
while (end < start + milliseconds) {
end = new Date().getTime();
}
this.energyLevel = 100;
}
displayInfo() {
console.log(`Name: ${this.name}, Color: ${this.color}, Abilities: ${this.abilities.join(', ')}, Energy Level: ${this.energyLevel}`);
}
clone(): this {
console.log(`\nCloning ${this.name}...`)
const clone = Object.create(this);
clone.abilities = [...this.abilities]; // Clone the abilities array
return clone;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment