コード例 #1
0
ファイル: event_json.py プロジェクト: EliasFarhan/GBJam
def parse_event_type_json(event_dict, parent_event=None, object=None):
    event = None
    event_type = get_element(event_dict, "type")
    if event_type and isinstance(event_type, CONST.string_type):
        for c in event_type:
            if c != "." and c != "_" and not c.isalpha():
                log("Error: Invalid character type for event type (only alphanumeric '.' and '_'): " + event_type, 1)
                return None
    elif event_type is None:
        event_type = ""

    if event_type:
        dir_list = event_type.split(".")

        module_name = ".".join(dir_list[0 : len(dir_list) - 1])
        class_name = dir_list[len(dir_list) - 1]
        log("Loading: " + module_name + "." + class_name)
        try:
            exec("""from %s import %s""" % (module_name, class_name))
        except ImportError as e:
            log("Error while importing " + event_type + " " + str(e), 1)
            return None

        try:
            d = locals()
            exec("""event = %s.parse_event(event_dict)""" % class_name, globals(), d)
            event = d["event"]
        except Exception as e:
            log("Error initializing event: " + str(e), 1)
            return None
    next_event_dict = get_element(event_dict, "next_event")
    if next_event_dict:
        event.next_event = parse_event_json(next_event_dict)
    return event
コード例 #2
0
ファイル: image.py プロジェクト: EliasFarhan/GBJam
    def parse_image(image_data, pos, size, angle):
        if pos is None:
            log("Invalid arg pos not defined for Image", 1)
            return None
        path = get_element(image_data, "path")
        if path is not None:
            path = CONST.path_prefix + path

        parallax_factor = get_element(image_data, "parallax_factor")
        if parallax_factor is None:
            parallax_factor = 1.0

        image = Image(pos, size=size, angle=angle, path=path, parallax_factor=parallax_factor)

        tmp = get_element(image_data, "tmp")
        if tmp is not None:
            image.tmp = tmp
        else:
            image.tmp = False

        anim_data = get_element(image_data, "anim")
        if anim_data:
            image.anim = Animation.parse_animation(anim_data, image)
        image.init_image(size)
        return image
コード例 #3
0
ファイル: stat_event.py プロジェクト: EliasFarhan/Kudu
 def parse_event(event_dict):
     value_name = get_element(event_dict, "name")
     new_value = get_element(event_dict, "value")
     if value_name and new_value:
         return SetValueEvent(value_name, new_value)
     else:
         log("Invalid arg for IncreaseValueEvent", 1)
         return None
コード例 #4
0
ファイル: animation_main.py プロジェクト: EliasFarhan/GBJam
    def parse_animation(anim_data,obj=None):
        anim_type = get_element(anim_data,"anim_type")
        root_path = get_element(anim_data, "root_path")
        path_list = get_element(anim_data,"path_list")
        state_range = get_element(anim_data, "state_range")
        anim_freq = get_element(anim_data, "anim_freq")
        if not anim_freq:
            anim_freq = CONST.animation_step
        anim = None
        
        '''Check type entry is a string with '.' or alpha'''
        if anim_type and isinstance(anim_type, CONST.string_type):
            for c in anim_type:
                if c != '.' and c != '_' and not c.isalpha():
                    log("Error: Invalid character type for animation type: "+anim_type,1)
                    return None
        elif anim_type is None:
            anim_type = ''
        else:
            log("Error: Invalid type of anim_type, given: "+type(anim_type),1)
            return None
        if anim_type is not '':
            dir_list = anim_type.split(".")

            module_name = ".".join(dir_list[0:len(dir_list)-1])
            class_name = dir_list[len(dir_list)-1]
            log(module_name+" "+class_name)
            try:
                exec('''from %s import %s'''%(module_name, class_name ))
            except ImportError as e:
                log("Error while importing "+anim_type+" "+str(e), 1)
                return None
            
            try:
                d = locals()
                exec('''anim = %s(obj)'''% class_name, globals(), d)
                anim = d['anim']
            except Exception as e:
                log("Error initializing animation: "+str(e), 1)
                return None
        else:
            log("Use default animation")
            anim = Animation(obj)

        if anim and root_path:
            anim.root_path = root_path
        else:
            log("Error: UNDEFINED anim or root_path is None",1)
            return anim
        if path_list and isinstance(path_list,list):
            anim.path_list = path_list
        if state_range and isinstance(state_range,dict):
            anim.state_range = state_range
        anim.anim_freq = anim_freq
        return anim
コード例 #5
0
ファイル: level_json.py プロジェクト: EliasFarhan/GBJam
def save_level(level):
    """Save the level from editor only
    it loads the level JSON file and write the modification"""
    '''Load written level data'''
    level_data = load_json(level.filename)
    objects_list = get_element(level_data, "objects")
    for layer_index, layer in enumerate(level.objects):
        for image in layer:
            if isinstance(image,GameObject):
                object_id = image.id
                object_data = next((x for x in objects_list if x['id'] == object_id), None)
                if not object_data:
                    '''Add GameObject or Image in Level JSON dict'''
                    object_id = get_last_object_id()
                    set_last_object_id(object_id+1)
                    object_data = {"id": object_id}
                    objects_list.append(object_data)
                    if isinstance(image,GameObject):
                        object_data["type"] = "GameObject"
                    elif isinstance(image, Image):
                        object_data["type"] = "Image"
                        object_data["path"] = image.path
                object_data["pos"] = image.pos.get_list()
                object_data["angle"] = image.angle
                object_data["size"] = image.size.get_list()
                object_data["layer"] = layer_index + 1
                if image.body and not get_element(object_data, "physic_objects"):
                    """Add physic description

                    TODO:
                    -circle shape
                    -none middle pos for fixtures"""
                    physic_objects = {}
                    if CONST.render == 'sfml':
                        import Box2D as b2
                        if image.body.type == b2.b2_staticBody:
                            physic_objects["type"] = "static"
                        elif image.body.type == b2.b2_kinematicBody:
                            physic_objects["type"] = "kinematic"
                        elif image.body.type == b2.b2_dynamicBody:
                            physic_objects["type"] = "dynamic"
                        body_fixtures = []
                        for fixture in image.body.fixtures:
                            fixture_data = {}
                            if isinstance(fixture.userData, Number):
                                fixture_data["user_data"] = fixture.userData
                            if isinstance(fixture.shape,b2.b2PolygonShape):
                                fixture_data["type"] = "box"

                            body_fixtures.append(fixture_data)
                        physic_objects["fixtures"] = body_fixtures
                    object_data["physic_objects"] = physic_objects

    log(objects_list)
    write_json(level.filename,level_data)
コード例 #6
0
ファイル: stat_event.py プロジェクト: EliasFarhan/Kudu
 def parse_event(event_dict):
     name = get_element(event_dict, "name")
     value = get_element(event_dict, "value")
     if not (name and value):
         log("Invalid arg name and value for ConditionnalEvent", 1)
         return None
     if_event = None
     if_event_dict = get_element(event_dict, "if_event")
     if if_event_dict:
         if_event = parse_event_json(if_event_dict)
     else_event = None
     else_event_dict = get_element(event_dict, "else_event")
     if else_event_dict:
         else_event = parse_event_json(else_event_dict)
     return ConditionnalEvent(name, value, if_event, else_event)
コード例 #7
0
ファイル: audio_event.py プロジェクト: EliasFarhan/Kudu
 def parse_event(event_dict):
     path = get_element(event_dict, "path")
     if path:
         return SoundEvent(path)
     else:
         log("Invalid arg path for SoundEvent",1)
         return None
コード例 #8
0
ファイル: image.py プロジェクト: EliasFarhan/Kudu
    def parse_image(image_data, pos, size, angle):
        if pos is None:
            log("Invalid arg pos not defined for Image",1)
            return None
        path = get_element(image_data, "path")
        if path is not None:
            path = CONST.path_prefix+path

        image = Image(pos, size=size, angle=angle)


        anim_data = get_element(image_data, "anim")
        if anim_data:
            image.anim = Animation.parse_animation(anim_data, image)
        image.init_image(size)
        return image
コード例 #9
0
ファイル: stat_event.py プロジェクト: EliasFarhan/Kudu
 def parse_event(event_dict):
     value_name = get_element(event_dict, "name")
     if value_name:
         return IncreaseValueEvent(value_name)
     else:
         log("Invalid arg for IncreaseValueEvent", 1)
         return None
コード例 #10
0
ファイル: audio_event.py プロジェクト: EliasFarhan/Kudu
 def parse_event(event_dict):
     new_playlist = get_element(event_dict, "playlist")
     if new_playlist:
         return MusicEvent(new_playlist)
     else:
         log("Invalid arg playlist for MusicEvent",1)
         return None
コード例 #11
0
ファイル: gui_event.py プロジェクト: EliasFarhan/GBJam
    def parse_event(event_dict):
        """Dynamic parsing of DialogEvent"""
        text = get_element(event_dict, "text")
        if text is None:
            return None
        text2 = get_element(event_dict, "text2")
        if text2 is None:
            text2 = ''

        dialog_event = DialogEvent(get_level(), text, text2)
        answers = get_element(event_dict, "answers")
        if answers is not None:
            for answer_key in answers:
                if isinstance(answers[answer_key],dict):
                    answers[answer_key] = parse_event_json(answers[answer_key], dialog_event)
                elif isinstance(answers[answer_key], CONST.string_type):
                    answers[answer_key] = load_event(answers[answer_key], dialog_event)

            dialog_event.set_answers(answers)
        return dialog_event
コード例 #12
0
ファイル: physic_json.py プロジェクト: EliasFarhan/Kudu
def load_physic_objects(physics_data,image):
    if image is None:
        return
    body_type = get_element(physics_data, "type")
    if body_type:
        pos = Vector2()
        if image.pos:
            pos = image.pos
        if image.screen_relative_pos:
            pos = pos+image.screen_relative_pos*engine.get_screen_size()
        if image.size:
            pos = pos+image.size/2
        if body_type == "dynamic":
            image.body = physics_manager.add_body(pos, BodyType.dynamic)
        elif body_type == "static":
            image.body = physics_manager.add_body(pos, BodyType.static)
        elif body_type == 'kinematic':
            image.body = physics_manager.add_body(pos, BodyType.kinematic)
    pos = (0,0)
    if image.pos:
        pos = image.pos
    if image.screen_relative_pos:
        pos = pos+image.screen_relative_pos*engine.get_screen_size()+image.size/2
    angle = get_element(physics_data, "angle")
    if angle:
        image.body.angle = angle*math.pi/180
    else:
        if image.body and image.angle != 0:
            if CONST.physics == 'b2':
                image.body.angle = image.angle*math.pi/180
            elif CONST.physics == 'pookoo':
                #TODO: set angle for body
                pass
            '''Set new pos for body'''
            v = image.size/2
            v.rotate(image.angle)
            pos = image.pos+v
            pos = pixel2meter(pos)
            if CONST.physics == 'b2':
                image.body.position = pos.get_tuple()
            elif CONST.physics == 'pookoo':
                #TODO: set pos for body
                pass
            
    fixtures_data = get_element(physics_data,"fixtures")
    if fixtures_data:
        for physic_object in fixtures_data:
            sensor = get_element(physic_object, "sensor")
            if sensor is None:
                sensor = False
            user_data = get_element(physic_object,"user_data")
            if user_data is None:
                user_data = image
            obj_type = get_element(physic_object, "type")
            if obj_type == "box":
                pos = get_element(physic_object,"pos")
                if not pos:
                    pos = Vector2()

                size = get_element(physic_object,"size")
                if not size:
                    size = image.size/2

                angle = get_element(physic_object,"angle")
                if angle is None:
                    
                    angle = 0
                image.fixtures.append(physics_manager.add_box(image.body, Vector2(pos), Vector2(size), angle, user_data, sensor))
            elif obj_type == "circle":
                pos = get_element(physic_object,"pos")
                if not pos:
                    pos = (0,0)
                radius = get_element(physic_object,"radius")
                if not radius:
                    radius = 1

                image.fixtures.append(physics_manager.add_circle(image.body,pos,radius,sensor,user_data))
コード例 #13
0
ファイル: level_json.py プロジェクト: EliasFarhan/GBJam
def load_level(level):
    """
    Import a level with:

    -Physics static object
    -Images with or without animation
    -IA (if any)
    -Player position, size, etc... but not recreate the player!!!
    """
    level_data = load_json(CONST.path_prefix+level.filename)
    if level_data is not None:
        log("LOAD PLAYER")
        player_data = get_element(level_data, 'player')
        if player_data is not None:
            '''load the json containing the player
            and treat it as an AnimImage'''
            player = None
            if isinstance(player_data, CONST.string_type):
                log("Loading player "+player_data)
                player_json = load_json(CONST.path_prefix + player_data)
                player = load_image_from_json(player_json, level)

            elif isinstance(player_data,dict):
                player = load_image_from_json(player_data, level)
            else:
                log("Warning: Invalid format for player JSON",1)
            if player:
                level.player = player
        log("LOAD GUI")
        gui_data = get_element(level_data, 'gui')
        if gui_data is not None:
            '''load the json containing the gui
            parameters'''
            if isinstance(gui_data, CONST.string_type):
                GUI.load_gui_json(load_json(gui_data),level)
            elif isinstance(gui_data,dict):
                GUI.load_gui_json(gui_data,level)
            else:
                log("Warning: invalid format for GUI JSON",1)
        log("LOAD_BG COLOR")
        bg_color = get_element(level_data,'bg_color')
        if bg_color is not None:
            level.bg_color = bg_color
        show_mouse = get_element(level_data,'show_mouse')
        if show_mouse is not None:
            level.show_mouse = show_mouse
        
        use_physics = get_element(level_data,'use_physics')
        if use_physics is not None:
            level.use_physics = use_physics
        network = get_element(level_data, 'network')
        if network is not None:
            level.use_network = network
        log("LOAD EVENTS")
        event_data = get_element(level_data, "events")
        if event_data:
            for e in event_data.keys():
                level.event[e] = load_event(event_data[e])

        log("LOAD ImAGE OBJECTS")
        objects_dict = get_element(level_data, 'objects')
        reset_object_id()
        if objects_dict is not None:
            for object_data in objects_dict:
                if isinstance(object_data, CONST.string_type):
                    load_image_from_json(load_json(object_data), level, None)
                
                elif isinstance(object_data,dict):
                    image_type = get_element(object_data,"type")
                    load_image_from_json(object_data,level,image_type)

        checkpoints = get_element(level_data, 'checkpoints')
        if checkpoints:
            log("CHECKPOINTS LIST: "+str(checkpoints))
            v_checkpoints = []
            for c in checkpoints:
                v_checkpoints.append(Vector2(c))
            level.checkpoints = v_checkpoints

        log("END OF LOADING")
        return True
    return False
コード例 #14
0
ファイル: ground.py プロジェクト: EliasFarhan/GBJam
 def parse_image(json_data, pos, size, angle):
     nmb_size = get_element(json_data,"nmb_size")
     show_bottom = get_element(json_data, "show_bottom")
     if show_bottom is None:
         show_bottom = True
     return Ground(Vector2(pos), Vector2(nmb_size), show_bottom=show_bottom)
コード例 #15
0
ファイル: stump.py プロジェクト: EliasFarhan/GBJam
 def parse_image(json_data, pos, size, angle):
     nmb_size = get_element(json_data,"nmb_size")
     return Stump(Vector2(pos), nmb_size)
コード例 #16
0
ファイル: image_json.py プロジェクト: EliasFarhan/GBJam
def load_image_from_json(image_data, level, image_type=None):
    global global_object_id
    image = None
    if image_type is None:
        try:
            image_type = image_data["type"]
        except KeyError:
            log("Error: No image_type for:" + str(image_data),1)
            return 
    pos = get_element(image_data, "pos")
    size = get_element(image_data, "size")
    layer = get_element(image_data, "layer")
    angle = get_element(image_data, "angle")
    object_id = get_element(image_data, "id")
    if object_id is None:
        object_id = global_object_id
    if angle is None:
        angle = 0
    if image_type == "GameObject":
        image = GameObject()
        image.pos = Vector2(pos)
        image.size = Vector2(size)
        image.update_rect()
        image.angle = angle
        image.id = object_id
    elif image_type == "Image":
        image = Image.parse_image(image_data, pos, size, angle)
        if image is not None:
            image.id = object_id
    elif image_type == "Text":
        font = get_element(image_data, "font")
        text = get_element(image_data, "text")
        color = get_element(image_data, "color")
        if font and text:
            font = CONST.path_prefix+font
        else:
            log("Invalid arg font and text not defined for Text",1)
            return
        if not color:
            color = [0,0,0]
        image = Text(pos, size, font, text, angle,color)
        image.id = object_id
    else:
        if not isinstance(image_type,CONST.string_type):
            log("image_type not a string",1)
            return
        for c in image_type:
            if c != '.' and c != '_' and not c.isalpha():
                return
        dir_list = image_type.split(".")
        try:
            exec('''from %s import %s'''%(".".join(dir_list[0:len(dir_list)-1]), dir_list[len(dir_list)-1]))
        except ImportError:
            log("Error: ImportError with: "+str(image_type),1)
            return
        try:
            d = locals()
            exec('''image = %s.parse_image(image_data, pos, size, angle)'''%(dir_list[len(dir_list)-1]),globals(),d)
            image = d['image']
        except Exception as e:
            import traceback
            log('Error with loading image_type: %s'%(image_type)+" "+str(e),1)
            traceback.print_exc()
            return
    physic_objects = get_element(image_data, "physic_objects")
    if physic_objects:
        load_physic_objects(physic_objects, image)
    
    event_path = get_element(image_data, "event")
    if image and event_path:
        image.event = load_event(event_path)
    if not layer or layer < 0:
        layer = 1
    elif layer > len(level.objects)-1:
        layer = len(level.objects)-1
    if image:
        level.objects[layer-1].append(image)
    global_object_id += 1
    return image
コード例 #17
0
ファイル: level_event.py プロジェクト: EliasFarhan/GBJam
 def parse_event(event_dict):
     new_level_name = get_element(event_dict,"name")
     if new_level_name:
         return SwitchEvent(get_level(),new_level_name)
     else:
         log("Invalid arg name for SwitchEvent")