def start_facing_target(self, target=None): "start_facing_target(target=None): if target is None, use target field" self.stop_facing_target() if target: self.target = target self.tick_action = RealTickFunc( self.check_facing).do() self.check_facing() # prevents jerky start
def set_forward_motion(self, speed=None): """set_forward_motion(self, speed=None) speed: set object's forward velocity or acceleration to this value. Defaults to self.speed """ if speed == None: speed = self.speed else: self.speed = speed if self.speed < 0: self.actual_offset = self.offset + 180 else: self.actual_offset = self.offset self.speed_vector = Vector(0, -speed) if speed: if not self.tick_action: self.tick_action = RealTickFunc(self.update_forward_motion) self.tick_action.do() else: if self.tick_action: self.tick_action.abort() self.tick_action = None self.last_rotation = None self.last_speed = None self.set_motion()
class Mouse_Follow(SpriteComponent): """Object follows the mouse pointer""" # component_info _set = 'pig' _type = 'controls' _class_list = [Node] # attributes: _field_list = [ ['face_movement','Rotate object to direction of motion'] ] # attribute defaults face_movement = False # other defaults tick_action = None @component_method def on_added_to_scene(self): """Start following when object is added to scene""" self.start_following_mouse() @component_method def start_following_mouse(self): "Start following the mouse pointer" if not self.tick_action: self.tick_action = RealTickFunc( self.follow).do() @component_method def stop_following_mouse(self): "Stop following the mouse pointer" if self.tick_action: self.tick_action.abort() def follow(self): if not self.enabled: return owner = self.owner try: mousepos = owner.layer.convert_pos(Mouse.position[0], Mouse.position[1]) # convert from Opioid2D.Vector to simple co-ordinates my_pos = (owner.position[0], owner.position[1]) mousepos = (mousepos[0], mousepos[1]) if my_pos != mousepos: if self.face_movement: owner.rotation = angle_to(my_pos, mousepos) owner.position = mousepos except: # stopping the scene in the middle of this can cause problems return
def on_start(self): "Initialize midi input" self.midi_state = midistate() try: pygame.midi.quit() pygame.midi.init() if self.input_id == -1: self.input_id = pygame.midi.get_default_input_id() if self.input_id == -1: print "Midi_Input.on_start: No midi device available" self.enabled = False return if self.test_mode: print "Midi_Input Device List:" self.print_device_info() print "Midi_Input using device",self.input_id,"\n ", self.print_device_info(self.input_id) except: print "Midi_Input.on_start: Midi input initialization error" self.enabled = False return try: self.input = pygame.midi.Input( self.input_id) except: print "Midi_Input.on_start: Midi port error!" self.enabled = False return self.tick_action = RealTickFunc(self.get_midi).do()
class Mouse_Follow(SpriteComponent): """Object follows the mouse pointer""" # component_info _set = 'pig' _type = 'controls' _class_list = [Node] # attributes: _field_list = [['face_movement', 'Rotate object to direction of motion']] # attribute defaults face_movement = False # other defaults tick_action = None @component_method def on_added_to_scene(self): """Start following when object is added to scene""" self.start_following_mouse() @component_method def start_following_mouse(self): "Start following the mouse pointer" if not self.tick_action: self.tick_action = RealTickFunc(self.follow).do() @component_method def stop_following_mouse(self): "Stop following the mouse pointer" if self.tick_action: self.tick_action.abort() def follow(self): if not self.enabled: return owner = self.owner try: mousepos = owner.layer.convert_pos(Mouse.position[0], Mouse.position[1]) # convert from Opioid2D.Vector to simple co-ordinates my_pos = (owner.position[0], owner.position[1]) mousepos = (mousepos[0], mousepos[1]) if my_pos != mousepos: if self.face_movement: owner.rotation = angle_to(my_pos, mousepos) owner.position = mousepos except: # stopping the scene in the middle of this can cause problems return
class Face_Motion(SpriteComponent): """Force owner to always face the direction that it's moving. Warning: This component uses a tick_action, so it may be slow. """ #component_info _set = 'pig' _type = 'behavior' _class_list = [Node] # attributes: ['name','desc'] or ['name', agui, {'doc':'desc', extra info}] _field_list = [ ['offset','Forward direction is offset by this much'], ] #defaults offset = 0 #other defaults last_position = None tick_action = None @component_method def on_added_to_scene(self): """Start facing motion when object is added to scene""" self.set_face_motion() @component_method def set_face_motion(self): """set_face_motion(): Set object to face its motion.""" self.tick_action = RealTickFunc( self.face_motion).do() self.face_motion() @component_method def on_delete(self): "Abort the tick action on delete" if self.tick_action: self.tick_action.abort() def face_motion(self): if not self.enabled: self.last_position=None return if self.last_position and tuple(self.owner.position)\ != self.last_position: self.owner.rotation = angle_to( self.last_position, self.owner.position) self.last_position = tuple(self.owner.position)
class Face_Motion(SpriteComponent): """Force owner to always face the direction that it's moving. Warning: This component uses a tick_action, so it may be slow. """ #component_info _set = 'pig' _type = 'behavior' _class_list = [Node] # attributes: ['name','desc'] or ['name', agui, {'doc':'desc', extra info}] _field_list = [ ['offset', 'Forward direction is offset by this much'], ] #defaults offset = 0 #other defaults last_position = None tick_action = None @component_method def on_added_to_scene(self): """Start facing motion when object is added to scene""" self.set_face_motion() @component_method def set_face_motion(self): """set_face_motion(): Set object to face its motion.""" self.tick_action = RealTickFunc(self.face_motion).do() self.face_motion() @component_method def on_delete(self): "Abort the tick action on delete" if self.tick_action: self.tick_action.abort() def face_motion(self): if not self.enabled: self.last_position = None return if self.last_position and tuple(self.owner.position)\ != self.last_position: self.owner.rotation = angle_to(self.last_position, self.owner.position) self.last_position = tuple(self.owner.position)
class Forward_Motion(SpriteComponent): """Apply velocity or acceleration in the direction the object is facing. Warning: This component uses a tick_action, so it may be slow.""" #component_info _set = 'pig' _type = 'behavior' _class_list = [Node] # attributes: ['name','desc'] or ['name', agui, {'doc':'desc', extra info}] _field_list = [ ['speed','Forward velocity or acceleration'], ['accelerate',"If True, 'speed' indicates acceleration.\n"+\ "If False, 'speed' indicates velocity"], ] #defaults speed = 200 accelerate = True offset = 0 # decided to hide this from the component gui, but it still works actual_offset = 0 tick_action = None last_rotation = None last_speed = None @component_method def on_added_to_scene(self): """Start facing target when object is added to scene""" self.set_forward_motion() @component_method def set_forward_motion(self, speed=None): """set_forward_motion(self, speed=None) speed: set object's forward velocity or acceleration to this value. Defaults to self.speed """ if speed == None: speed = self.speed else: self.speed = speed if self.speed < 0: self.actual_offset = self.offset + 180 else: self.actual_offset = self.offset self.speed_vector = Vector(0, -speed) if speed: if not self.tick_action: self.tick_action = RealTickFunc(self.update_forward_motion) self.tick_action.do() else: if self.tick_action: self.tick_action.abort() self.tick_action = None self.last_rotation = None self.last_speed = None self.set_motion() @component_method def on_destroy(self): "Abort the tick action on destroy" try: if self.tick_action: self.tick_action.abort() except: pass def set_motion(self): self.speed_vector.direction = self.owner.rotation + self.actual_offset if self.accelerate: self.owner.acceleration = self.speed_vector else: self.owner.velocity = self.speed_vector def update_forward_motion(self): if not self.enabled: return if self.owner.rotation == self.last_rotation and\ self.speed == self.last_speed: return self.set_motion() self.last_rotation = self.owner.rotation self.last_speed = self.speed
class Face_Object( SpriteComponent): """Object turns to face another object""" #component_info _set = 'pig' _type = 'behavior' _class_list = [Node] # attributes: ['name','desc'] or ['name', agui, {'doc':'desc', extra info}] _field_list = [ ['target', GnameDropdown,{'doc':'Object to face towards', 'class_list':[Node]}], ['rotation_speed', 'Speed to turn. Negative = always face object exactly.'], ['offset', 'Offset the rotation by this much'] ] #defaults target = '' rotation_speed = -1 offset = 0 target_angle = None action = None tick_action = None @component_method def on_first_display(self): """Start facing target when object is added to scene""" self.end_action = CallFunc(self.target_reached) self.start_facing_target() @component_method def start_facing_target(self, target=None): "start_facing_target(target=None): if target is None, use target field" self.stop_facing_target() if target: self.target = target self.tick_action = RealTickFunc( self.check_facing).do() self.check_facing() # prevents jerky start @component_method def stop_facing_target(self): "Stop facing current target" if self.tick_action: self.tick_action.abort() @component_method def on_delete(self): "Abort the tick action on delete" if self.tick_action: self.tick_action.abort() def check_facing(self, position=None): """check_facing(position=None) position: an Opioid vector Turn the object toward position. If None, use obj.position""" if not self.enabled: self.action.abort() return if position is None: if not self.target: return obj = get_gnamed_object(self.target) if not obj: return position = obj.position target_angle = angle_to(self.owner.position, position) + self.offset if self.rotation_speed < 0: # set owner to proper rotation instantly self.owner.rotation = target_angle else: if self.target_angle != target_angle: #create an action that will rotate owner to proper angle if self.action: self.action.abort() self.action = (RotateTo(target_angle, speed=self.rotation_speed) + \ self.end_action).do(self.owner) self.target_angle = target_angle if self.owner.rotation == self.target_angle: self.target_reached() def target_reached(self): # opioid has a problem aborting the action if it's complete if self.action: self.action = None
def start_following_mouse(self): "Start following the mouse pointer" if not self.tick_action: self.tick_action = RealTickFunc(self.follow).do()
def set_face_motion(self): """set_face_motion(): Set object to face its motion.""" self.tick_action = RealTickFunc( self.face_motion).do() self.face_motion()
def set_face_motion(self): """set_face_motion(): Set object to face its motion.""" self.tick_action = RealTickFunc(self.face_motion).do() self.face_motion()
def start_following_mouse(self): "Start following the mouse pointer" if not self.tick_action: self.tick_action = RealTickFunc( self.follow).do()