|
| 1 | +class Event: |
| 2 | + def __init__(self, trigger, action, name): |
| 3 | + """Initializes an Event object. |
| 4 | +
|
| 5 | + Parameters |
| 6 | + ---------- |
| 7 | + trigger : function |
| 8 | + A function that takes the current state of the simulation as input and returns a boolean value. The event will be triggered when this function returns True. |
| 9 | + action : function |
| 10 | + A function that takes the current state of the simulation as input and performs some action when the event is triggered. |
| 11 | + name : str |
| 12 | + A name for the event, used for identification purposes. |
| 13 | +
|
| 14 | + """ |
| 15 | + self.trigger = trigger |
| 16 | + self.action = action |
| 17 | + self.name = name |
| 18 | + |
| 19 | + # TODO: check_trigger does note receive enough arguments to substitute parachute events |
| 20 | + def check_trigger(self, state): |
| 21 | + """Checks if the event should be triggered based on the current state of the simulation. |
| 22 | +
|
| 23 | + Parameters |
| 24 | + ---------- |
| 25 | + state : dict |
| 26 | + The current state of the simulation, containing information such as altitude, velocity, and time. |
| 27 | +
|
| 28 | + Returns |
| 29 | + ------- |
| 30 | + bool |
| 31 | + True if the event should be triggered, False otherwise. |
| 32 | +
|
| 33 | + """ |
| 34 | + return self.trigger(state) |
| 35 | + |
| 36 | + def execute_action(self, state): |
| 37 | + """Executes the action associated with the event. |
| 38 | +
|
| 39 | + Parameters |
| 40 | + ---------- |
| 41 | + state : dict |
| 42 | + The current state of the simulation, containing information such as altitude, velocity, and time. |
| 43 | +
|
| 44 | + """ |
| 45 | + return self.action(state) |
| 46 | + |
| 47 | + def __repr__(self): |
| 48 | + # TODO: Implement a more informative string representation of the Event object. |
| 49 | + pass |
| 50 | + |
| 51 | + def __str__(self): |
| 52 | + # TODO: Implement a more informative string representation of the Event object. |
| 53 | + pass |
| 54 | + |
| 55 | + def __call__(self, *args, **kwds): |
| 56 | + # TODO: This should call the action (or the trigger?) |
| 57 | + pass |
| 58 | + |
| 59 | + |
| 60 | +# TODO: Implement functions which are standard types of events, such as motor burnout events, landing events, etc. |
| 61 | +# def motor_burnout_trigger(state): |
| 62 | +# """A trigger function that returns |
| 63 | + |
| 64 | +# # TODO: think about this |
| 65 | +# class ParachuteEvent(Event): |
| 66 | + |
| 67 | + |
| 68 | +# def __init__(self, trigger, action, name, parachute): |
| 69 | +# """Initializes a ParachuteEvent object. |
| 70 | + |
| 71 | +# Parameters |
| 72 | +# ---------- |
| 73 | +# trigger : function |
| 74 | +# A function that takes the current state of the simulation as input and returns a boolean value. The event will be triggered when this function returns True. |
| 75 | +# action : function |
| 76 | +# A function that takes the current state of the simulation as input and performs some action when the event is triggered. |
| 77 | +# name : str |
| 78 | +# A name for the event, used for identification purposes. |
| 79 | +# parachute : Parachute |
| 80 | +# The parachute associated with this event. |
| 81 | + |
| 82 | +# """ |
| 83 | +# super().__init__(trigger, action, name) |
| 84 | +# self.parachute = parachute |
| 85 | + |
| 86 | +# def apogee_trigger(state): |
| 87 | +# """A trigger function that returns True when the rocket reaches apogee. |
| 88 | + |
| 89 | +# Parameters |
| 90 | +# ---------- |
| 91 | +# state : dict |
| 92 | +# The current state of the simulation, containing information such as altitude, velocity, and time. |
| 93 | + |
| 94 | +# Returns |
| 95 | +# ------- |
| 96 | +# bool |
| 97 | +# True if the rocket has reached apogee, False otherwise. |
| 98 | + |
| 99 | +# """ |
| 100 | +# return state['velocity'] <= 0 |
0 commit comments