Feast 2.0!
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

73 linhas
2.5 KiB

  1. import { TargetDrainedVigorCondition } from '@/game/combat/conditions'
  2. import { Creature } from '@/game/creature'
  3. import { LogEntry, LogLines, nilLog } from "@/game/interface"
  4. import { Container } from '@/game/vore'
  5. import { Action } from './combat'
  6. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  7. abstract class Relay<Sender, EventMap extends { [name: string]: any }> {
  8. private subscriptions: { [K in keyof EventMap]: Array<(sender: Sender, args: EventMap[K]) => LogEntry> }
  9. constructor (private eventNames: Array<keyof EventMap>) {
  10. const partialSubscriptions: Partial<{ [K in keyof EventMap]?: Array<(sender: Sender, args: EventMap[K]) => LogEntry>}> = {}
  11. this.eventNames.forEach(name => {
  12. partialSubscriptions[name] = []
  13. })
  14. this.subscriptions = partialSubscriptions as Required<{ [K in keyof EventMap]: Array<(sender: Sender, args: EventMap[K]) => LogEntry> }>
  15. }
  16. subscribe<K extends keyof EventMap> (name: K, callback: (sender: Sender, args: EventMap[K]) => LogEntry): void {
  17. if (this.subscriptions[name] === undefined) {
  18. this.subscriptions[name] = []
  19. }
  20. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  21. this.subscriptions[name]!.push(callback)
  22. }
  23. dispatch<K extends keyof EventMap> (name: K, sender: Sender, args: EventMap[K]): LogEntry {
  24. const subscriptionList = this.subscriptions[name]
  25. if (subscriptionList !== undefined) {
  26. return new LogLines(...subscriptionList.map(sub => sub(sender, args)))
  27. } else {
  28. return nilLog
  29. }
  30. }
  31. connect<ParentEventMap extends EventMap> (parent: Relay<Sender, ParentEventMap>): void {
  32. this.eventNames.forEach(name => {
  33. parent.subscribe(name, (sender, args) => this.dispatch(name, sender, args))
  34. })
  35. }
  36. }
  37. type VoreMap = {
  38. "onEaten": { prey: Creature };
  39. "onReleased": { prey: Creature };
  40. "onEntered": { prey: Creature };
  41. "onExited": { prey: Creature };
  42. "onDigested": { prey: Creature };
  43. "onAbsorbed": { prey: Creature };
  44. }
  45. export class VoreRelay extends Relay<Container, VoreMap> {
  46. constructor () {
  47. super(["onEaten", "onReleased", "onEntered", "onExited", "onDigested", "onAbsorbed"])
  48. }
  49. }
  50. type ActionMap = {
  51. "onActionAttempt": { user: Creature; target: Creature; action: Action };
  52. "onActionFail": { user: Creature; target: Creature; action: Action };
  53. "onActionSuccess": { user: Creature; target: Creature; action: Action };
  54. }
  55. export class ActionRelay extends Relay<Action, ActionMap> {
  56. constructor () {
  57. super([
  58. "onActionAttempt",
  59. "onActionFail",
  60. "onActionSuccess"
  61. ])
  62. }
  63. }