Feast 2.0!
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

54 行
2.0 KiB

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