-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTsamagotchi.ts
241 lines (208 loc) · 6.03 KB
/
Tsamagotchi.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
export class Tsamagotchi
{
public name : string;
private _age : number;
private _birthdate : Date;
private _health : number;
private _satiety : number;
private _happiness : number;
private _isAlive : boolean;
private _foodLimit : number;
/**
* Create a Tsamagotchi instance.
*
* @class
*
* @param {string} [name="Your new Tsamagotchi"] - The name of your Tsamagotchi.
* @param {number} [foodLimit=100] - The amount of food your Tsamagotchi can consume.
*/
constructor(name : string = "Your new Tsamagotchi", foodLimit : number = 100)
{
this.name = name;
this._age = 0;
this._birthdate = new Date();
this._health = 100;
this._satiety = 100;
this._happiness = 100;
this._isAlive = true;
this._foodLimit = foodLimit;
}
/**
* Get the age of the Tsamagotchi.
*
* @returns {number} The age of the Tsamagotchi.
*/
get age() : number
{
return this._age;
}
/**
* Get the birthdate of the Tsamagotchi.
*
* @returns {Date} The birthdate of the Tsamagotchi.
*/
get birthdate() : Date
{
return this._birthdate;
}
/**
* Get the health of the Tsamagotchi.
*
* @returns {number} The health of the Tsamagotchi.
*/
get health() : number
{
return this._health;
}
/**
* Get the satiety level of the Tsamagotchi.
*
* @return {number} The satiety level of the Tsamagotchi.
*/
get satiety() : number
{
return this._satiety;
}
/**
* Get the happiness level of the Tsamagotchi.
*
* @returns {number} The happiness level of the Tsamagotchi.
*/
get happiness() : number
{
return this._happiness;
}
/**
* Check if the Tsamagotchi is still alive.
*
* @returns {boolean} Wether the Tsamagotchi is still alive or not.
*/
get isAlive() : boolean
{
return this._isAlive;
}
/**
* Get the food limit of the Tsamagotchi.
*
* @returns {number} The amount of food the Tsamagotchi can consume.
*/
get foodLimit() : number
{
return this._foodLimit;
}
/**
* Feed the Tsamagotchi a certain amount of food. If the Tsamagotchi eats too much, its health will go down.
*
* @param {number} [amount=1] - The amount of food to feed the Tsamagotchi.
*
* @returns {number} The satiety status of the Tsamagotchi.
*/
feed(amount : number = 1) : number
{
if (!this.isAlive) return this.satiety;
this._satiety += amount;
if (this.satiety > this.foodLimit)
{
this.decreaseHealth( this.satiety - this.foodLimit );
this._satiety = this.foodLimit;
}
return this.satiety;
}
/**
* Mature the Tsamagotchi by a certain amount.
*
* @param {number} [amount=1] - The amount the Tsamagotchi needs to mature.
*
* @returns {number} The new age of the Tsamagotchi.
*/
mature(amount : number = 1) : number
{
if (!this.isAlive) return this.health;
if (this.isAlive)
{
this._age += amount;
this.digest(amount);
this.decreaseHappiness(amount);
}
return this.health
}
/**
* Make the Tsamagotchi digest a certain amount of its satiety level. If the satiety level reaches zero or lower, the Tsamagotchi loses health.
*
* @param {number} [amount=1] The amount that needs to be digested.
* @returns {number} The current satiety level of the Tsamagotchi.
*/
digest(amount : number = 1)
{
if (!this.isAlive) return this.satiety;
this._satiety -= amount;
if (this.satiety < 0)
{
this.decreaseHealth( Math.abs(this.satiety) );
this._satiety = 0;
}
return this.satiety;
}
/**
* Heal the Tsamagotchi by a certain amount.
*
* @param {number} [amount=1] - The amount of health that should be healed.
* @returns {number} The new health status of the Tsamagotchi.
*/
heal(amount : number = 1) : number
{
if (!this.isAlive) return this.health;
this._health += amount;
return this.health;
}
/**
* Give the Tsamagotchi a certain amount of love.
*
* @param {number} [amount=1] - The amount of love to give.
* @returns {number} The currenct happiness status of the Tsamagotchi.
*/
love(amount : number = 1) : number
{
if (!this.isAlive) return this.happiness;
this._happiness += amount;
return this.happiness;
}
/**
* Decrease the happiness of the Tsamagotchi. If its happiness reaches zero, it will lose health.
*
* @param {number} [amount=1] - The amount the happiness should be decreased with.
* @returns {number} The currenct happiness level of the Tsamagotchi.
*/
decreaseHappiness(amount : number = 1) : number
{
if (!this.isAlive) return this.happiness;
this._happiness -= amount;
if (this.happiness <= 0)
{
this.decreaseHealth( Math.abs(this.happiness) + 1 );
this._happiness = 0;
}
return this.happiness;
}
/**
* Decrease the health of the Tsamagotchi by a certain amount. If the health of the Tsamagotchi reaches zero, the Tsamagotchi dies.
*
* @param {number} [amount=1] - The amount to decrease the health with.
* @returns {number} The currency health status of the Tsamagotchi.
*/
decreaseHealth(amount : number = 1)
{
if (!this.isAlive) return this.health;
this._health -= amount;
if (this.health <= 0) this.die();
return this.health;
}
/**
* Make the Tsamagotchi die. Too bad, you tried.
*/
die()
{
this._isAlive = false;
this._health = 0;
}
}