sep-pm-platformer/project/level/elements/ReceiverLevelElement.py

63 lines
2.0 KiB
Python

from abc import ABC
from level.elements.StaticLevelElement import StaticLevelElement
from physics.TickData import TickData
class ReceiverLevelElement(StaticLevelElement, ABC):
def __init__(self, tile: dict, loaded_level):
super().__init__(tile, loaded_level)
self.requires = {
}
if 'requires_and' in tile:
self.requires['and'] = tile['requires_and']
if 'requires_or' in tile:
self.requires['or'] = tile['requires_or']
if 'requires_xor' in tile:
self.requires['xor'] = tile['requires_xor']
self.active_state = False
if 'active_state' in tile:
self.set_active(tile['active_state'] == 'True')
else:
self.set_active(False)
def set_active(self, active: bool):
self.active_state = active
def tick(self, tick_data: TickData):
super().tick(tick_data)
active = False
if 'and' in self.requires:
for requirement in self.requires['and']:
element = self.loaded_level.get_element_by_id(requirement)
if element and element.emitter_state:
active = True
else:
active = False
break
if 'or' in self.requires:
for requirement in self.requires['or']:
element = self.loaded_level.get_element_by_id(requirement)
if element and element.emitter_state:
active = True
break
if 'xor' in self.requires:
print(self.requires['xor'])
for requirement in self.requires['xor']:
element = self.loaded_level.get_element_by_id(requirement)
if element and element.emitter_state:
if active:
active = False
break
else:
active = True
if active != self.active_state:
self.set_active(active)