Event Queue #1

* Added EventQueue system for events with requirements, and timers.
This commit is contained in:
LoafyLemon 2022-05-17 22:05:27 +01:00
parent 42756113b8
commit b09157538d
3 changed files with 91 additions and 1 deletions

View File

@ -95,8 +95,9 @@ label day_start:
if not first_random_twins: if not first_random_twins:
$ twins_interest = True $ twins_interest = True
# Deliver mail # Pass time
$ mailbox.tick() $ mailbox.tick()
$ eventqueue.tick()
# Update map locations # Update map locations
call set_her_map_location() call set_her_map_location()

View File

@ -357,6 +357,7 @@ label quests:
# (Optional) Genie gets caught by Snape. (E0 can play either before or after E1) # (Optional) Genie gets caught by Snape. (E0 can play either before or after E1)
jump potions_intro_E0 jump potions_intro_E0
$ eventqueue.start()
# All quest events should somehow end with a jump to the main room day/night cycle # 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 # If no quest event is triggered, resume normally from the main room

View File

@ -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()