Last active
December 9, 2015 01:24
-
-
Save bedekelly/9e20cd2f5ccf8e9e4874 to your computer and use it in GitHub Desktop.
Object-Oriented Events for IF, including an interrupt_before method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A short example of my proposed object-oriented event framework for IF games. | |
Every event should implement a go() method, which calls super().go() before doing | |
anything else. After that, it should check whether its own go() method is | |
applicable by making a call to self.should_fire(<EventType>). | |
""" | |
class Event: | |
"""Generic Event type.""" | |
def __init__(self, obj): | |
self.obj = obj # Event is applied to this object. | |
self._interrupts = set() # Set of things to interrupt for. | |
self._interrupt_before = set() # Set of things to interrupt before.. | |
self._should_continue = True # Has our event (not) been interrupted? | |
def should_fire(self, current_type): | |
"""Determine whether we should apply current_type's handler.""" | |
if not self._should_continue: | |
# We've already interrupted; don't handle the event. | |
return False | |
# Only fire the next event if we're not in the set of interrupts. | |
self._should_continue = current_type not in self._interrupts | |
# Only fire *this* event if we're not interrupted before it. | |
return current_type not in self._interrupt_before | |
def go(self): | |
"""Override this method if you want your event to do something.""" | |
def interrupt_after(self, *args): | |
"""Add an arbitrary list of event types at which we interrupt.""" | |
for a in args: | |
self._interrupts.add(a) | |
def interrupt_before(self, *args): | |
"""Add an arbitrary list of event types before which we interrupt.""" | |
for a in args: | |
self._interrupts.add(a) | |
self._interrupt_before.add(a) | |
class Touch(Event): | |
"""Sent when an object is touched.""" | |
def go(self): | |
super().go() | |
if self.should_fire(Touch): | |
print(self.obj, "was touched!") | |
class Pull(Touch): | |
"""Sent when an object is pulled.""" | |
def go(self): | |
super().go() | |
if self.should_fire(Pull): | |
print(self.obj, "was pulled!") | |
class LeverPull(Pull): | |
"""Sent when a lever is pulled.""" | |
def go(self): | |
super().go() | |
if self.should_fire(LeverPull): | |
print(self.obj, "was lever-pulled!") | |
class ElectrifiedLeverPull(LeverPull): | |
"""Sent when an electrified lever is pulled.""" | |
def __init__(self, obj): | |
super().__init__(obj) | |
self.interrupt_after(Touch) | |
def go(self): | |
super().go() | |
if self.should_fire(ElectrifiedLeverPull): | |
print(self.obj, "was electric-lever-pulled!") | |
else: | |
print("ZAP!") | |
class ForcefieldLeverPull(LeverPull): | |
"""Sent when the user tries to pull a forcefield lever.""" | |
def __init__(self, obj): | |
super().__init__(obj) | |
if self.obj.forcefield_active: | |
self.interrupt_before(Touch) | |
def go(self): | |
super().go() | |
if self.should_fire(ForcefieldLeverPull): | |
print(self.obj, "was forcefield-lever-pulled!") | |
else: # self.obj.forcefield_active is True | |
print("You hit the forcefield around the lever!") | |
class WorkingLever: | |
def __str__(self): | |
return "Working Lever" | |
class ElectrifiedLever: | |
def __str__(self): | |
return "Electrified Lever" | |
class ForcefieldLever: | |
def __init__(self): | |
self.forcefield_active = True | |
def __str__(self): | |
return "Forcefield Lever" | |
LeverPull(WorkingLever()).go() | |
print() | |
ElectrifiedLeverPull(ElectrifiedLever()).go() | |
print() | |
forcefield_lever = ForcefieldLever() | |
ForcefieldLeverPull(forcefield_lever).go() | |
print() | |
forcefield_lever.forcefield_active = False | |
ForcefieldLeverPull(forcefield_lever).go() | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment