def build_entity(cls, i, ex, args): ex.set_tag = hive.plugin(cls.set_tag, identifier="entity.tag.set", export_to_parent=True) ex.get_tag = hive.plugin(cls.get_tag, identifier="entity.tag.get", export_to_parent=True) ex.set_visibility = hive.plugin(cls.set_visibility, identifier="entity.visibility.set", export_to_parent=True) ex.get_visibility = hive.plugin(cls.get_visibility, identifier="entity.visibility.get", export_to_parent=True) ex.spawn_entity = hive.plugin(cls.spawn_entity, identifier="entity.spawn", export_to_parent=True) ex.destroy_entity = hive.plugin(cls.destroy_entity, identifier="entity.destroy", export_to_parent=True) ex.register_entity_factory = hive.plugin(cls.register_entity_factory, "entity.register_factory", export_to_parent=True) ex.register_hive_destructor = hive.plugin(cls.register_hive_destructor, "entity.register_destructor", export_to_parent=True) # Push out entity destroyed ex.on_entity_destroyed = hive.socket(cls.on_entity_destroyed, "entity.on_destroyed", policy=hive.MultipleOptional, export_to_parent=True) ex.on_entity_created = hive.socket(cls.on_entity_created, "entity.on_created", policy=hive.MultipleOptional, export_to_parent=True)
def build_instantiator(cls, i, ex, args, meta_args): """Instantiates a Hive class at runtime""" # If this is built now, then it won't perform matchmaking, so use meta hive bind_meta_class = hive.meta_hive("BindEnvironment", build_bind_environment, declare_build_environment, builder_cls=BindEnvironmentClass) #assert bind_meta_class._hive_object_class i.bind_meta_class = hive.property(cls, "bind_meta_class", "class", bind_meta_class) i.trig_instantiate = hive.triggerfunc(cls.instantiate) i.do_instantiate = hive.triggerable(i.trig_instantiate) i.hive_class = hive.property(cls, "hive_class", "class") i.pull_hive_class = hive.pull_in(i.hive_class) ex.hive_class = hive.antenna(i.pull_hive_class) ex.create = hive.entry(i.do_instantiate) hive.trigger(i.trig_instantiate, i.pull_hive_class, pretrigger=True) ex.process_id = hive.property(cls, "last_created_process_id", "int.process_id") i.pull_process_id = hive.pull_out(ex.process_id) ex.last_process_id = hive.output(i.pull_process_id) i.push_stop_process = hive.push_in(cls.stop_hive) ex.stop_process = hive.antenna(i.push_stop_process) # Bind class plugin ex.bind_on_created = hive.socket(cls.add_on_created, identifier="bind.on_created", policy=hive.MultipleOptional) ex.add_get_plugins = hive.socket(cls.add_get_plugins, identifier="bind.get_plugins", policy=hive.MultipleOptional) ex.add_get_config = hive.socket(cls.add_get_config, identifier="bind.get_config", policy=hive.MultipleOptional) # Bind instantiator if meta_args.bind_process == 'child': # Add startup and stop callbacks ex.on_stopped = hive.plugin(cls.stop_all_processes, identifier="on_stopped")
def build_mainloop(cls, i, ex, args): i.event_manager = EventManager(export_namespace=True) i.input_manager = InputHandler(export_namespace=True) i.entity_manager = EntityManager(export_namespace=True) i.transform_manager = TransformManager(export_namespace=True) i.physics_manager = PhysicsManager(export_namespace=True) # Connect input manager hive.connect(i.tick, i.input_manager.update) hive.connect(i.input_manager.event, i.event_manager.event_in) # Connect physics hive.connect(i.tick, i.physics_manager.tick) hive.connect(i.pull_tick_rate, i.physics_manager.tick_rate) # Send tick event and step Panda i.on_tick = hive.triggerable(cls.on_tick) hive.trigger(i.tick, i.on_tick) # Get read event ex.get_dispatcher = hive.socket(cls.set_event_dispatcher, "event.process") ex.get_add_handler = hive.socket(cls.set_add_handler, "event.add_handler") # Add startup and stop callbacks ex.main_on_started = hive.plugin(cls.on_started, identifier="on_started")
def build_mainloop(cls, i, ex, args): """Blocking fixed-timestep trigger generator""" i.tick = hive.triggerfunc() i.stop = hive.triggerable(cls.stop) i.run = hive.triggerable(cls.run) ex.tick = hive.hook(i.tick) ex.run = hive.entry(i.run) ex.stop = hive.entry(i.stop) i.tick_rate = hive.property(cls, "tick_rate", 'int') i.pull_tick_rate = hive.pull_out(i.tick_rate) ex.tick_rate = hive.output(i.pull_tick_rate) ex.get_tick_rate = hive.plugin(cls.get_tick_rate, identifier="app.get_tick_rate") ex.quit = hive.plugin(cls.stop, identifier="app.quit")
def build_scene(cls, i, ex, args): i.bge_scene = hive.property(cls, "scene") ex.get_entity_id = hive.plugin(cls.get_entity_id, identifier="entity.get") ex.get_position_absolute = hive.plugin( cls.get_position_absolute, identifier="entity.position.absolute.get") ex.get_position_relative = hive.plugin( cls.get_position_relative, identifier="entity.position.relative.get") ex.get_orientation_absolute = hive.plugin( cls.get_orientation_absolute, identifier="entity.orientation.absolute.get") ex.get_orientation_relative = hive.plugin( cls.get_orientation_relative, identifier="entity.orientation.relative.get") ex.spawn_entity = hive.plugin(cls.spawn_entity, identifier="entity.spawn") ex.get_scene = hive.plugin(cls.get_scene, identifier="entity.get_current") import dragonfly ex.on_tick = dragonfly.event.Tick() def f(self): print("I") if not hasattr(self, 'a'): self.a = 1 self.spawn_entity.plugin()("Cube", "c1") i.mod_tick = hive.modifier(f) hive.trigger(ex.on_tick, i.mod_tick)
def external_builder(self, cls, i, ex, args, meta_args): """Adds bind-plugin sockets to ex wrapper if conditions allow.""" meta_args_dict = meta_args.to_ordered_dict() for attr_name, plugin_entry in self._plugins.items(): for condition in plugin_entry.condition_stack: if not condition(meta_args_dict): break else: method = getattr(cls, attr_name) socket = hive.socket(method, plugin_entry.identifier, policy=plugin_entry.socket_policy) setattr(ex, attr_name, socket) get_plugins_plugin = hive.plugin(cls.get_plugins, identifier="bind.get_plugins") get_config_plugin = hive.plugin(cls.get_config, identifier="bind.get_config") setattr(ex, '{}_get_plugins'.format(self._name), get_plugins_plugin) setattr(ex, '{}_get_config'.format(self._name), get_config_plugin)
def build_some_instance(i, ex, args): i.some_var = hive.attribute("str") ex.on_tick = dragonfly.event.OnTick() i.mod = hive.modifier(lambda self: print(self, self._some_var)) hive.connect(ex.on_tick, i.mod) def on_stopped(): print("I am closed!") ex.on_closed = hive.plugin(on_stopped, "on_stopped")
def build_spawn(cls, i, ex, args, meta_args): """Spawn an entity into the scene""" ex.get_spawn_entity = hive.socket(cls.set_spawn_entity, "entity.spawn") # Associate entity with this hive so it is safely destroyed ex.get_register_destructor = hive.socket(cls.set_register_destructor, "entity.register_destructor") ex.on_entity_process_instantiated = hive.plugin( cls.on_entity_process_created, "bind.on_created") i.entity_class_id = hive.property(cls, "entity_class_id", "str.entity_class_id") i.pull_class_id = hive.pull_in(i.entity_class_id) ex.entity_class_id = hive.antenna(i.pull_class_id) i.entity_last_created_id = hive.property(cls, "entity_last_created_id", "int.entity_id") if meta_args.spawn_hive: i.pull_entity_id = hive.pull_out(i.entity_last_created_id) ex.entity_last_created_id = hive.output(i.pull_entity_id) else: i.push_entity_id = hive.push_out(i.entity_last_created_id) ex.created_entity_id = hive.output(i.push_entity_id) i.do_spawn = hive.triggerable(cls.do_spawn_entity) i.trigger = hive.triggerfunc(i.do_spawn) i.on_triggered = hive.triggerable(i.trigger) hive.trigger(i.trigger, i.pull_class_id, pretrigger=True) # Process instantiator if meta_args.spawn_hive: i.instantiator = Instantiator(forward_events='all', bind_process='child') # Pull entity to instantiator hive.connect(i.pull_entity_id, i.instantiator.entity_id) # Get last created ex.hive_class = hive.antenna(i.instantiator.hive_class) ex.last_process_id = hive.output(i.instantiator.last_process_id) ex.stop_process = hive.antenna(i.instantiator.stop_process) ex.pause_events = hive.antenna(i.instantiator.pause_events) ex.resume_events = hive.antenna(i.instantiator.resume_events) # Instantiate hive.trigger(i.trigger, i.instantiator.create) else: # Finally push out entity hive.trigger(i.trigger, i.push_entity_id) ex.spawn = hive.entry(i.on_triggered)
def build_event_environment(cls, i, ex, args, meta_args): """Runtime event environment for instantiated hive. Provides appropriate sockets and plugins for event interface """ ex.add_handler = hive.plugin(cls.add_handler, identifier="event.add_handler") ex.remove_handler = hive.plugin(cls.remove_handler, identifier="event.remove_handler") ex.read_event = hive.plugin(cls.handle_event, identifier="event.process") ex.event_on_stopped = hive.plugin(cls.on_closed, identifier="on_stopped", policy=hive.SingleOptional) i.pause_events = hive.triggerable(cls.pause) ex.pause_events = hive.entry(i.pause_events) i.resume_events = hive.triggerable(cls.resume) ex.resume_events = hive.entry(i.resume_events)
def build_house(cls, i, ex, args): ex.some_plugin = hive.plugin(cls.get_current_hive, identifier="get.house", data_type="float") # Auto connect i.filler = FillerHive() ex.filler = hive.hook(i.filler) # Manual connect i.fido = DogHive(name="Main", import_namespace=False) ex.fido = hive.hook(i.fido) hive.connect(ex.some_plugin, i.fido.some_socket)
def build_chessboard(cls, i, ex, args): prop_move = h.property(cls, "prop_move", "str") # TODO: make buffer i.make_move = h.push_in(prop_move) i.trig_make_move = h.triggerable(cls.trig_make_move) # TODO: make modifier h.trigger(i.make_move, i.trig_make_move) ex.make_move = h.antenna(i.make_move) ex.do_make_move = cls.make_move ex.p_make_move = h.plugin(cls.make_move) ex.prop_move = prop_move ex.set_turn = h.socket(cls.set_turn) ex.add_moveprocessor = h.socket(cls.add_moveprocessor)
def time_builder(cls, i, ex, args): """Access to Python time module""" i.elapsed = hive.property(cls, 'elapsed', 'float') i.elapsed_out = hive.pull_out(i.elapsed) ex.elapsed_out = hive.output(i.elapsed_out) ex.get_add_handler = hive.socket(cls.set_add_handler, "event.add_handler") ex.on_started = hive.plugin(cls.on_started, "on_started", policy=hive.SingleRequired) ex.get_get_tick_rate = hive.socket(cls.set_get_tick_rate, "app.get_tick_rate")
def event_builder(cls, i, ex, args): ex.add_handler = hive.plugin(cls.add_handler, identifier="event.add_handler", export_to_parent=True) ex.remove_handler = hive.plugin(cls.remove_handler, identifier="event.remove_handler", export_to_parent=True) ex.read_event = hive.plugin(cls.handle_event, identifier="event.process", export_to_parent=True) # Send startup and stop events ex.on_stopped = hive.plugin(cls.on_stopped, identifier="on_stopped") ex.on_started = hive.plugin(cls.on_started, identifier="on_started") # Allow events to be pushed in i.event_in = hive.property(cls, 'pushed_event', 'tuple') i.push_event = hive.push_in(i.event_in) ex.event_in = hive.antenna(i.push_event) i.on_event_in = hive.triggerable(cls.on_event_in) hive.trigger(i.push_event, i.on_event_in)
def build_h(cls, i, ex, args, meta_args): print("Build hive", meta_args.i) is_root = meta_args.root if is_root: print("IS ROOT") ex.plug = hive.plugin(cls.print_name, identifier="some_api.func") if meta_args.i: setattr(ex, "h_{}".format(meta_args.i - 1), SomeHive(i=meta_args.i - 1, root=False, name="<internal>")) else: ex.sock = hive.socket(cls.get_plug, identifier="some_api.func")
def build_bind(cls, i, ex, args, meta_args): if meta_args.forward_events == "none": return if meta_args.forward_events == "by_leader": i.event_leader = hive.property(cls, "leader", "tuple") i.pull_event_leader = hive.pull_in(i.event_leader) ex.event_leader = hive.antenna(i.pull_event_leader) i.push_pause_in = hive.push_in(cls.pause) ex.pause_events = hive.antenna(i.push_pause_in) i.push_resume_in = hive.push_in(cls.resume) ex.resume_events = hive.antenna(i.push_resume_in) ex.on_created = hive.plugin(cls.on_created, "bind.on_created")
def environment_builder(self, cls, i, ex, args, meta_args): """Adds bind-plugin plugins to ex wrapper if conditions allow.""" meta_args_dict = meta_args.bind_meta_args.to_ordered_dict() for attr_name, plugin_entry in self._plugins.items(): if not plugin_entry.declare_for_environment: continue for condition in plugin_entry.condition_stack: if not condition(meta_args_dict): break else: method = getattr(cls, attr_name) plugin = hive.plugin(method, plugin_entry.identifier, plugin_entry.plugin_policy) setattr(ex, attr_name, plugin)
def build_physics_manager(cls, i, ex, args): i.tick_rate = hive.property(cls, "tick_rate", 'int') i.pull_tick_rate = hive.pull_in(i.tick_rate) ex.tick_rate = hive.antenna(i.pull_tick_rate) i.do_update = hive.triggerfunc(cls.update) hive.trigger(i.do_update, i.pull_tick_rate, pretrigger=True) i.on_tick = hive.triggerable(i.do_update) ex.tick = hive.entry(i.on_tick) ex.on_entity_destroyed = hive.plugin(cls.on_entity_destroyed, "entity.on_destroyed", policy=hive.SingleRequired) ex.on_entity_created = hive.plugin(cls.on_entity_created, "entity.on_created", policy=hive.SingleRequired) ex.get_angular_velocity = hive.plugin(cls.get_angular_velocity, "entity.angular_velocity.get", export_to_parent=True) ex.set_angular_velocity = hive.plugin(cls.set_angular_velocity, "entity.angular_velocity.set", export_to_parent=True) ex.get_linear_velocity = hive.plugin(cls.get_linear_velocity, "entity.angular_velocity.get", export_to_parent=True) ex.set_linear_velocity = hive.plugin(cls.set_linear_velocity, "entity.angular_velocity.set", export_to_parent=True)
def build_entity_environment(cls, i, ex, args, meta_args): """Runtime event environment for instantiated hive. Provides appropriate sockets and plugins for event interface """ if meta_args.bind_meta_args.bind_entity != "bound": return ex.get_bound_entity = hive.plugin(cls.get_bound_entity, "entity.get_bound") ex.get_bound_position_absolute = hive.plugin( cls.get_bound_pos_abs, "entity.bound.position.get.absolute") ex.set_bound_position_absolute = hive.plugin( cls.set_bound_pos_abs, "entity.bound.position.set.absolute") ex.get_bound_position_relative = hive.plugin( cls.get_bound_pos_rel, "entity.bound.position.get.relative") ex.set_bound_position_relative = hive.plugin( cls.set_bound_pos_rel, "entity.bound.position.set.relative") ex.get_bound_orientation_absolute = hive.plugin( cls.get_bound_ori_abs, "entity.bound.orientation.get.absolute") ex.set_bound_orientation_absolute = hive.plugin( cls.set_bound_ori_abs, "entity.bound.orientation.set.absolute") ex.get_bound_orientation_relative = hive.plugin( cls.get_bound_ori_rel, "entity.bound.orientation.get.relative") ex.set_bound_orientation_relative = hive.plugin( cls.set_bound_ori_rel, "entity.bound.orientation.set.relative") ex.get_bound_parent = hive.plugin(cls.get_bound_parent, "entity.bound.parent.get") ex.set_bound_parent = hive.plugin(cls.set_bound_parent, "entity.bound.parent.set") ex.get_bound_tag = hive.plugin(cls.get_bound_tag, "entity.bound.tag.get") ex.set_bound_tag = hive.plugin(cls.set_bound_tag, "entity.bound.tag.set") ex.get_bound_visibility = hive.plugin(cls.get_bound_visibility, "entity.bound.visibility.get") ex.set_bound_visibility = hive.plugin(cls.set_bound_visibility, "entity.bound.visibility.set") ex.bound_destroy = hive.plugin(cls.bound_destroy, "entity.bound.destroy")
def build_drone(cls, i, ex, args): ex.plug = hive.plugin(cls.print_name, identifier="some_api.func", export_to_parent=True)
def build_entity_transform(cls, i, ex, args): ex.set_abs_position = hive.plugin( cls.set_absolute_position, identifier="entity.position.set.absolute", export_to_parent=True) ex.get_abs_position = hive.plugin( cls.get_absolute_position, identifier="entity.position.get.absolute", export_to_parent=True) ex.set_rel_position = hive.plugin( cls.set_relative_position, identifier="entity.position.set.relative", export_to_parent=True) ex.get_rel_position = hive.plugin( cls.get_relative_position, identifier="entity.position.get.relative", export_to_parent=True) ex.set_abs_orientation = hive.plugin( cls.set_absolute_orientation, identifier="entity.orientation.set.absolute", export_to_parent=True) ex.get_abs_orientation = hive.plugin( cls.get_absolute_orientation, identifier="entity.orientation.get.absolute", export_to_parent=True) ex.set_rel_orientation = hive.plugin( cls.set_relative_orientation, identifier="entity.orientation.set.relative", export_to_parent=True) ex.get_rel_orientation = hive.plugin( cls.get_relative_orientation, identifier="entity.orientation.get.relative", export_to_parent=True) ex.set_parent = hive.plugin(cls.set_parent, identifier="entity.parent.set", export_to_parent=True) ex.get_parent = hive.plugin(cls.get_parent, identifier="entity.parent.get", export_to_parent=True) ex.on_entity_destroyed = hive.plugin(cls.on_entity_destroyed, "entity.on_destroyed", policy=hive.SingleRequired) ex.on_entity_created = hive.plugin(cls.on_entity_created, "entity.on_created", policy=hive.SingleRequired)