12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- /*
- Mana
- A Magic Component
- Version: 1.0
- Docs:
- - mana(maximum=0)
- - add()
- - mp() number
- - setMP(mp=0)
- - maxMP() number
- - setMaxMP(mp=0)
- - gain(mp=1)
- - cast(mp=1)
- - canCast(mp=1) bool
- - tryCast(mp=1) bool
- - onGain(function)
- - onCast(function)
- > mana renders information in debug.inspect mode
- */
- function mana(max_mana=0) {
- let val = 0
- let max_val = max_mana
- return {
- id: "mana",
- add() {
- val = max_val;
- },
- mp() {
- return val;
- },
- setMP(mp=0) {
- val = mp;
- if (val > max_val) {
- val = max_val;
- }
- if (val < 0) {
- val = 0;
- }
- },
- maxMP() {
- return max_val;
- },
- setMaxMP(mp=0) {
- max_val = mp;
- if (max_val < 0) {
- max_val = 0;
- }
- if (val > max_val) {
- val = max_val;
- }
- },
- gain(mp=1) {
- if (this.mp() >= this.maxMP()) {
- return;
- } else if (this.mp() + mp <= this.maxMP()) {
- this.setMP(this.mp()+mp);
- this.trigger("gain", mp);
- } else {
- let diff = this.maxMP() - (this.mp()+mp);
- this.setMP(this.mp()+mp);
- this.trigger("gain", diff);
- }
- },
- cast(mp=1) {
- if (this.mp() - mp >= 0) {
- this.setMP(this.mp()-mp);
- this.trigger("cast", mp);
- }
- },
- canCast(mp=1) {
- return (this.mp() >= mp);
- },
- tryCast(mp=1) {
- if (this.canCast(mp)) {
- this.cast(mp);
- return true;
- } else {
- return false;
- }
- },
- onGain(func) {
- this.on("gain", func);
- },
- onCast(func) {
- this.on("cast", func);
- },
- inspect() {
- return "MP: " + this.mp() + "/" + this.maxMP();
- }
- }
- }
|