Skip to content

Commit efbc0a1

Browse files
committed
basic ability/skill/whatever class with mechanics like cooldowns and stuff
1 parent 55b73ff commit efbc0a1

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

gameObjects/Ability.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
const ABILITY_USE_ALL_CHARGE = -1;
3+
class Ability
4+
{
5+
rate = 120; // default: twice a second
6+
charge = 8;
7+
maxcharge = 8;
8+
chargeused = 1;
9+
base_recharge = 1; // per second
10+
user = null;
11+
cdtimer =0;
12+
constructor(user)
13+
{
14+
this.user = user;
15+
}
16+
use()
17+
{
18+
if(this.cdtimer>0)
19+
{
20+
return;
21+
}
22+
if(this.chargeused==ABILITY_USE_ALL_CHARGE)
23+
{
24+
this.apply(this.charge);
25+
this.charge=0;
26+
this.cdtimer+=(60/this.rate);
27+
}
28+
else
29+
{
30+
if(this.charge>=this.chargeused)
31+
{
32+
this.apply();
33+
this.charge-=this.chargeused;
34+
this.cdtimer+=(60/this.rate);
35+
}
36+
}
37+
}
38+
39+
update(dT)
40+
{
41+
this.charge += dT*this.base_recharge;
42+
this.cdtimer-=dT;
43+
if(this.charge>this.maxcharge)
44+
{
45+
this.charge=this.maxcharge;
46+
}
47+
if(this.cdtimer<0)
48+
{
49+
this.cdtimer=0;
50+
}
51+
}
52+
53+
apply(charge=0)
54+
{
55+
56+
}
57+
}

0 commit comments

Comments
 (0)