1
1

Perk.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /// <reference path="../World/Classes/Save/StoredVariable.ts" />
  2. class Perk extends StoredVariable<boolean> {
  3. public forcedStatus : ((e : Perk) => boolean | void) = () => { return undefined };
  4. public description : Say | string | ((perk : Perk) => Say | string) = "Undefined";
  5. public name : string;
  6. public confirmPicked : (() => void) = () => {};
  7. constructor (id : string) {
  8. super({
  9. id : "Perk_" + id,
  10. value : false
  11. });
  12. this.name = id;
  13. Perk.storePerk(this);
  14. }
  15. public isEnabled (valueOnly? : boolean) : boolean {
  16. if (valueOnly != true) {
  17. let forced = this.forcedStatus(this);
  18. if (forced != undefined) {
  19. return <boolean> forced;
  20. }
  21. }
  22. return this.value;
  23. }
  24. public isForced () {
  25. return this.forcedStatus(this) != undefined;
  26. }
  27. public getDescription () {
  28. if (typeof this.description == "function") {
  29. return this.description(this);
  30. } else {
  31. return this.description;
  32. }
  33. }
  34. public static perks : {[id : string] : Perk} = {};
  35. public static storePerk (perk : Perk) {
  36. Perk.perks[perk.id] = perk;
  37. }
  38. public static getPerk (id : string) {
  39. return Perk.perks[id];
  40. }
  41. public static getPerks () : Array<Perk> {
  42. let perks = [];
  43. for (let id in Perk.perks) {
  44. perks.push(Perk.perks[id]);
  45. }
  46. perks.sort((a : Perk, b : Perk) => {
  47. let na = a.name.toUpperCase();
  48. let nb = b.name.toUpperCase();
  49. if (na < nb) return -1;
  50. if (na > nb) return 1;
  51. return 0;
  52. });
  53. return perks;
  54. }
  55. public static updatePerks() {
  56. for (let id in Perk.perks) {
  57. let perk = Perk.perks[id];
  58. if (perk.isForced()) {
  59. perk.value = <boolean> perk.forcedStatus(perk);
  60. }
  61. }
  62. }
  63. }