From b09157538d557e80353dce851b9e897254c66845 Mon Sep 17 00:00:00 2001 From: LoafyLemon Date: Tue, 17 May 2022 22:05:27 +0100 Subject: [PATCH] Event Queue #1 * Added EventQueue system for events with requirements, and timers. --- game/scripts/events/DayNight.rpy | 3 +- game/scripts/events/Quests.rpy | 1 + game/scripts/events/queue.rpy | 88 ++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 game/scripts/events/queue.rpy diff --git a/game/scripts/events/DayNight.rpy b/game/scripts/events/DayNight.rpy index 2f9f8bf3..e41a7906 100644 --- a/game/scripts/events/DayNight.rpy +++ b/game/scripts/events/DayNight.rpy @@ -95,8 +95,9 @@ label day_start: if not first_random_twins: $ twins_interest = True - # Deliver mail + # Pass time $ mailbox.tick() + $ eventqueue.tick() # Update map locations call set_her_map_location() diff --git a/game/scripts/events/Quests.rpy b/game/scripts/events/Quests.rpy index f3c609fc..95830b17 100644 --- a/game/scripts/events/Quests.rpy +++ b/game/scripts/events/Quests.rpy @@ -357,6 +357,7 @@ label quests: # (Optional) Genie gets caught by Snape. (E0 can play either before or after E1) jump potions_intro_E0 + $ eventqueue.start() # All quest events should somehow end with a jump to the main room day/night cycle # If no quest event is triggered, resume normally from the main room diff --git a/game/scripts/events/queue.rpy b/game/scripts/events/queue.rpy new file mode 100644 index 00000000..f2e38647 --- /dev/null +++ b/game/scripts/events/queue.rpy @@ -0,0 +1,88 @@ + +init -1 python: + class EventQueue(object): + def __init__(self): + self.queue = [] + self.freeze = False + + def get_events(self, raw=False): + return self.queue if raw else [x for x in self.queue if x.wait < 1] + + def freeze(self): + self.freeze = True + + def unfreeze(self): + self.freeze = False + + def delay(self, n): + for i in self.queue: + i.wait += n + + def tick(self): + """Causes time to pass.""" + if self.freeze: + return + + for i in self.queue: + i.wait -= 1 + + def start(self): + queue = self.queue + queue = filter(lambda x: (x.wait > 0) or (not x.daytime == game.daytime), queue) + queue.sort(key=lambda x: x.priority) + + for ev in queue: + ev.start() + + if not ev.requirements_met(): + continue + + if ev.label: + break + + def is_in_queue(self, ev): + if isinstance(ev, str): + return any(i.id == ev for i in self.queue) + return ev in self.queue + + class Event(object): + """ + Queue is universal for all instanced objects. + """ + + def __init__(self, id, wait=0, priority=5, daytime=True, req=None, label=None, func=None): + self.queued = False + self.id = id + self.wait = wait + self.priority = priority + self.daytime = daytime + self.req = req + self.label = label + self.func = func + self.queue = eventqueue.queue + + if not renpy.has_label(self.label): + raise Exception("Supplied label does not exist.") + + def enlist(self): + self.queued = True + + if not self in self.queue: + self.queue.append(self) + + def requirements_met(self): + if self.req: + return eval(self.req) + return True + + def start(self): + if self in self.queue: + self.queue.remove(self) + + if self.func: + self.func() + + if self.label: + renpy.jump(self.label) + +default eventqueue = EventQueue()