示例#1
0
    def _Logic_Update(self, timeElapsed):
        """This method is simple, but important. It signals the Entities within our dictionary of entities to update themselves with only the timeElapsed as new data.
        NOTE: This may be able to be put into the Entity_Manager class instead of here."""

        #Before updating any of the entities, we need to call all of the active system functions (providing the associated entities as arguments.)

        for (sSystemFuncName, lEntities) in System_Manager._Get_Active_Systems():
            #print sSystemFuncName + " system is being exectuted."
            
            sNextState = self._Call_System_Func(sSystemFuncName, lEntities)

            if sNextState != "NULL,NULL":

                return sNextState.split(',')
            

        #This block iterates through all of the entities in our dictionary of dictionaries and signals each to update itself.
        for (sEntityType, dEntities) in self._dEntities.items():

            for (sEntityName, entity) in dEntities.items():

                #Check to see if the Entity is expired
                if entity._Is_Expired():
                    entity._On_Expire()
                    self._Remove_Entity(entity._Get_Type(), entity._Get_Name())

                entity._Update(timeElapsed)

        return ["NULL","NULL"]
示例#2
0
    def _Logic_Update(self, timeElapsed):
        """This method is simple, but important. It signals the Entities within our dictionary of entities to update themselves with only the timeElapsed as new data.
        NOTE: This may be able to be put into the Entity_Manager class instead of here."""

        #Before updating any of the entities, we need to call all of the active system functions (providing the associated entities as arguments.)

        for (sSystemFuncName,
             lEntities) in System_Manager._Get_Active_Systems():
            #print sSystemFuncName + " system is being exectuted."

            sNextState = self._Call_System_Func(sSystemFuncName, lEntities)

            if sNextState != "NULL,NULL":

                return sNextState.split(',')

        #This block iterates through all of the entities in our dictionary of dictionaries and signals each to update itself.
        for (sEntityType, dEntities) in self._dEntities.items():

            for (sEntityName, entity) in dEntities.items():

                #Check to see if the Entity is expired
                if entity._Is_Expired():
                    entity._On_Expire()
                    self._Remove_Entity(entity._Get_Type(), entity._Get_Name())

                entity._Update(timeElapsed)

        return ["NULL", "NULL"]
示例#3
0
def ChangeState(lCurState, lNxtState, window, windView, EntManager):
    """This function is passed a couple lists representing the info on the different levels of this game's
    hierarchical finite state machine. This function essentially generically sets up the Entity and Asset Managers
    based off of data that can be retreived from xml files.
    @param lCurState This list contains the information on which state the program is in and it takes acount into
        sub-states. So each element of the list is a sub-state of the previous element.
    @param lNxtState This list contains the information on which state the program is to be switched to and it takes acount into
        sub-states. So each element of the list is a sub-state of the previous element.
    @param windView This is SFML's View object and allows us to zoom in on the what would be shown in the window. This
        essentially just gives us the option to zoom in on the stuff visible for a certain state (can be specified in xml data.)
    @param EntManager This is the entity manager and is for loading entities into the game based on the state being switched to.
        The xml data tells which entities need to be loaded for what state."""

    print "NEW STATE!", lNxtState
    #The data will lie within the nextState[0]+".txt" file and the nextState[1] element within that elemthe ent.
    tree = parse("StateData/%s/%s.xml"%(lNxtState[0],lNxtState[1]))
    
    #The root element and the element containing the entities we need will be using this variable.
    root = tree.getroot()


    #This will reset the windowView's dimensions within the actual window with respect to the new state
    #windView.reset(sf.FloatRect((window.width - int(root.find('viewWidth').text))/2, \
    #            (window.height - int(root.find('viewHeight').text))/2,   \
    #            int(root.find('viewWidth').text),    \
    #            int(root.find('viewHeight').text)))

    #print float(root.find('viewWidthRatio').text)

    print "The new view's stats:\nx:%f\ny:%f\nwidth:%f\nheight:%f"%(int(window.width - int(window.width*float(root.find('viewWidthRatio').text)))/2,     \
                                int(window.height - int(window.height*float(root.find('viewHeightRatio').text)))/2,  \
                                int(window.width*float(root.find('viewWidthRatio').text)),                           \
                                int(window.height*float(root.find('viewHeightRatio').text)))
    
    windView.reset(sf.FloatRect((window.width - int(window.width*float(root.find('viewWidthRatio').text)))/2,     \
                                (window.height - int(window.height*float(root.find('viewHeightRatio').text)))/2,  \
                                window.width*float(root.find('viewWidthRatio').text),                           \
                                window.height*float(root.find('viewHeightRatio').text)))
    
    config.Tile_Width = window.width / (config.CHUNK_TILES_WIDE*2.)
    config.Tile_Height = window.height / (config.CHUNK_TILES_HIGH*2.)

    print "TileWidth is %f and TileHeight is %f"%(config.Tile_Width, config.Tile_Height)
    print "Window dimensions are %d x %d"%(window.width, window.height)
    
    #windView.reset(sf.FloatRect(int(window.width - window.width*float(root.find('viewWidthRatio').text)/2), \
    #                int(window.height - window.height*float(root.find('viewHeightRatio').text)/2),            \
    #                int(window.width*float(root.find('viewWidthRatio').text)),               \
    #                int(window.height*float(root.find('viewHeightRatio').text))))

    #This clears all of the things that in the game since the last state
    EntManager._Empty_Entity_Containers()
    AstManager._Empty_Assets()
    Input_Manager._Empty_Inputs()
    System_Manager._Empty_Systems()

    for entity in root.findall('Entity'):

        entityInstance = GetEntityBlueprints(entity)

        EntManager._Add_Entity(entityInstance)

            
    #Each one of these nodes will be an input that will be initialized for the state that is being loaded (and a multitude of kinds.)
    for inpoot in root.findall("Input"):

        #print inpoot.attrib

        #Check to see if this input's type is a hotspot.
        if inpoot.attrib["type"] == "hotspot":
            Input_Manager._Add_Hotspot(inpoot.find("x").text, inpoot.find("y").text, inpoot.find("width").text, inpoot.find("height").text, \
                                       inpoot.find("OnPressed").find("type").text if inpoot.find("OnPressed") != None else None,    \
                                       inpoot.find("OnSelected").find("system").text if inpoot.find("OnSelected") != None else None,    \
                                       AssembleEntityInfo(inpoot, "OnSelected"), \
                                       inpoot.find("OnDeselected").find("system").text if inpoot.find("OnDeselected") != None else None,    \
                                       AssembleEntityInfo(inpoot, "OnDeselected"), \
                                       inpoot.find("OnPressed").find("system").text if inpoot.find("OnPressed") != None else None,    \
                                       AssembleEntityInfo(inpoot, "OnPressed"), \
                                       inpoot.find("OnReleased").find("system").text if inpoot.find("OnReleased") != None else None,   \
                                       AssembleEntityInfo(inpoot, "OnReleased"))

        #Check to see if thisinput's type is a action.
        elif inpoot.attrib["type"] == "key":
            #This will add a key_Listener to our Input_Manager given the attribute data from the inpoot elemenet from the xml file.
            Input_Manager._Add_Key_Listener(inpoot.find("key").text,    \
                                            inpoot.find("OnPressed").find("type").text if inpoot.find("OnPressed") != None else None,  \
                                            inpoot.find("OnPressed").find("system").text if inpoot.find("OnPressed") != None else None,   \
                                            AssembleEntityInfo(inpoot, "OnPressed"),    \
                                            inpoot.find("OnReleased").find("system").text if inpoot.find("OnReleased") != None else None,   \
                                            AssembleEntityInfo(inpoot, "OnReleased"))

        elif inpoot.attrib["type"] == "mouse":
            Input_Manager._Add_Mouse_Listener(inpoot.find("button").text,             \
                                              inpoot.find("OnPressed").find("type").text if inpoot.find("OnPressed") != None else None,  \
                                              inpoot.find("OnPressed").find("system").text if inpoot.find("OnPressed") != None else None,   \
                                              AssembleEntityInfo(inpoot, "OnPressed"),    \
                                              inpoot.find("OnReleased").find("system").text if inpoot.find("OnReleased") != None else None,   \
                                              AssembleEntityInfo(inpoot, "OnReleased"))

    #These are the systems that are relevant to this state and they will be added into the System_Queue class.
    for system in root.findall("System"):
        
        #This will load a system into the System_Queue and then it will be activated next update.
        System_Manager._Add_System(system.find("type").text, system.find("systemFunc").text,  AssembleEntityInfo(system))
        
            
    #Now we gotta update the state variables so that we aren't signaling to change states anymore
    for i in xrange(len(lCurState)):
        lCurState[i] = lNxtState[i]
        lNxtState[i] = "NULL"
示例#4
0
def ChangeState(lCurState, lNxtState, windView, EntManager, AstManager):
    """This function is passed a couple lists representing the info on the different levels of this game's
    hierarchical finite state machine. This function essentially generically sets up the Entity and Asset Managers
    based off of data that can be retreived from xml files.
    @param lCurState This list contains the information on which state the program is in and it takes acount into
        sub-states. So each element of the list is a sub-state of the previous element.
    @param lNxtState This list contains the information on which state the program is to be switched to and it takes acount into
        sub-states. So each element of the list is a sub-state of the previous element.
    @param windView This is SFML's View object and allows us to zoom in on the what would be shown in the window. This
        essentially just gives us the option to zoom in on the stuff visible for a certain state (can be specified in xml data.)
    @param EntManager This is the entity manager and is for loading entities into the game based on the state being switched to.
        The xml data tells which entities need to be loaded for what state.
    @param AstManager This is the asset manager and it is for loading in assets that are needed by entities. The xml data
        specifies what assets are to be fetched and used for the assembling of each entity. Once an asset is fetched once,
        it won't need to be loaded the second time because it's stored in the AstManager."""

    print "NEW STATE!", lNxtState
    #The data will lie within the nextState[0]+".txt" file and the nextState[1] element within that elemthe ent.
    tree = parse("StateData/StateInit.xml")
    
    #The root element and the element containing the entities we need will be using this variable.
    root = tree.getroot()

    if root.find(lNxtState[0]) != None:
        #This changes the node to the one that represents the state we're switching to.
        root = root.find(lNxtState[0]).find(lNxtState[1])
        
        if root == None:
            print "There was a problem finding %s in the StateInit.xml file."%(lNxtState[1])
    else:
        print "There was a problem finding %s in the StateInit.xml file."%(lNxtState[0])

    #This will reset the windowView's dimensions within the actual window with respect to the new state
    windView.reset(sf.FloatRect(int(int(root.find('viewWidth').text)//4), \
                int(int(root.find('viewHeight').text)//4),   \
                int(int(root.find('viewWidth').text)//2),    \
                int(int(root.find('viewHeight').text)//2)))

    #This clears all of the things that in the game since the last state
    EntManager._Empty_Entity_Containers()
    AstManager._Empty_Assets()
    Input_Manager._Empty_Inputs()
    System_Manager._Empty_Systems()

    for entity in root.findall('Entity'):

        entityInstance = None

        #This checks to see if there is a function that exists that will assemble this entity.
        if entity.find('assembleFunc') != None:

            #This will hold all of the attributes needed to assemble the entity (using the xml files to get the data later on.)
            dEntityAttribs = {}

            #This will loop through all of the attribute names for each entity.
            for attrib in entity.find('Attributes').getiterator():
                
                #Check to see if this is a sound for the entity!
                if attrib.tag == 'Sound':
                    #THis will start a new list of Sounds if we haven't already loaded a sound into this entity's attributes.
                    if dEntityAttribs.get(attrib.tag, None) == None:
                        dEntityAttribs[attrib.tag] = []

                    #Query the AssetManager for a sound that is associated with this entity, then throw that into the dictionary of attributes!
                    dEntityAttribs[attrib.tag].append(AstManager._Get_Sound(attrib.attrib['name']))

                elif attrib.tag == 'Music':

                    #THis will start a new list of Musics if we haven't already loaded a sound into this entity's attributes.
                    if dEntityAttribs.get(attrib.tag, None) == None:
                        dEntityAttribs[attrib.tag] = []

                    dEntityAttribs[attrib.tag].append(AstManager._Get_Music(attrib.attrib['name']))

                #Check to see if this is a texture for the entitititity.
                elif attrib.tag == 'Texture':

                    #THis will start a new list of Textures if we haven't already loaded a sound into this entity's attributes.
                    if dEntityAttribs.get(attrib.tag, None) == None:
                        dEntityAttribs[attrib.tag] = []

                    #Query the AssetManager for a texture that is associated with this entity, then throw that into the dictionary of attributes!
                    dEntityAttribs[attrib.tag].append(AstManager._Get_Texture(attrib.attrib['name']))

                #Check to see if this is a font for the entitititity.
                elif attrib.tag == 'Font':

                    #THis will start a new list of Fonts if we haven't already loaded a sound into this entity's attributes.
                    if dEntityAttribs.get(attrib.tag, None) == None:
                        dEntityAttribs[attrib.tag] = []

                    #Query the AssetManager for a font that is associated with this entity, then throw that into the dictionary of attributes!
                    dEntityAttribs[attrib.tag].append(AstManager._Get_Font(attrib.attrib['name']))

                else:
                    #Anything else will just be put in the dictionary as an attribute
                    dEntityAttribs[attrib.tag] = attrib.text

            module = importlib.import_module('entities')

            assembleFunc = getattr(module, entity.find('assembleFunc').text)
               
            #Here we're using the Assemble*() function associated with the name of this entity to assemble the entity so that
            #we can add it to the EntityManager.
            #And all Assemble*() functions will use the same arguments(using a dictionary to allow dynamic arguments.)
            entityInstance = assembleFunc(entity.attrib['name'], entity.attrib['type'], dEntityAttribs)

        else:
            #Here we will add in a default entity instance.
            entityInstance = entities.Entity(entity.attrib['name'], entity.attrib['type'],{})
        
        #THis adds in the components that exist in the xml file for this entity (it allows custom/variations of entities to exist.)
        for component in entity.findall('Component'):

            #These are for getting the actual 
            module = importlib.import_module('components')

            componentClass = getattr(module, component.attrib['name'])

            #This will add in a component into the entity we just created.
            #And note that it is giving the component a dictionary of the data in the xml files.
            entityInstance._Add_Component(componentClass({DataTag.tag: DataTag.text for DataTag in component.getiterator()}))

        EntManager._Add_Entity(entityInstance)

            
    #Each one of these nodes will be an input that will be initialized for the state that is being loaded (and a multitude of kinds.)
    for inpoot in root.findall("Input"):

        #Check to see if this input's type is a hotspot.
        if inpoot.attrib["type"] == "hotspot":
            Input_Manager._Add_Hotspot(inpoot.find("x").text, inpoot.find("y").text, inpoot.find("width").text, inpoot.find("height").text, \
                                       inpoot.find("OnPressed").find("type").text if inpoot.find("OnPressed") != None else None,    \
                                       inpoot.find("OnSelected").find("system").text if inpoot.find("OnSelected") != None else None,    \
                                       AssembleEntityInfo(inpoot, "OnSelected"), \
                                       inpoot.find("OnDeselected").find("system").text if inpoot.find("OnDeselected") != None else None,    \
                                       AssembleEntityInfo(inpoot, "OnDeselected"), \
                                       inpoot.find("OnPressed").find("system").text if inpoot.find("OnPressed") != None else None,    \
                                       AssembleEntityInfo(inpoot, "OnPressed"), \
                                       inpoot.find("OnReleased").find("system").text if inpoot.find("OnReleased") != None else None,   \
                                       AssembleEntityInfo(inpoot, "OnReleased"))

        #Check to see if thisinput's type is a action.
        elif inpoot.attrib["type"] == "key":
            #This will add a key_Listener to our Input_Manager given the attribute data from the inpoot elemenet from the xml file.
            Input_Manager._Add_Key_Listener(inpoot.find("key").text,    \
                                            inpoot.find("OnPressed").find("type").text if inpoot.find("OnPressed") != None else None,  \
                                            inpoot.find("OnPressed").find("system").text if inpoot.find("OnPressed") != None else None,   \
                                            AssembleEntityInfo(inpoot, "OnPressed"),    \
                                            inpoot.find("OnReleased").find("system").text if inpoot.find("OnReleased") != None else None,   \
                                            AssembleEntityInfo(inpoot, "OnReleased"))

        elif inpoot.attrib["type"] == "mouse":
            pass

    #These are the systems that are relevant to this state and they will be added into the System_Queue class.
    for system in root.findall("System"):

        #This will load a system into the System_Queue and then it will be activated next update.
        systems.System_Queue._Add_System(system.find("type").text, system.find("system").text, AssembleEntityInfo(root))
        
            
    #Now we gotta update the state variables so that we aren't signaling to change states anymore
    for i in xrange(len(lCurState)):
        lCurState[i] = lNxtState[i]
        lNxtState[i] = "NULL"
示例#5
0
def ChangeState(lCurState, lNxtState, windView, EntManager, AstManager):
    """This function is passed a couple lists representing the info on the different levels of this game's
    hierarchical finite state machine. This function essentially generically sets up the Entity and Asset Managers
    based off of data that can be retreived from xml files.
    @param lCurState This list contains the information on which state the program is in and it takes acount into
        sub-states. So each element of the list is a sub-state of the previous element.
    @param lNxtState This list contains the information on which state the program is to be switched to and it takes acount into
        sub-states. So each element of the list is a sub-state of the previous element.
    @param windView This is SFML's View object and allows us to zoom in on the what would be shown in the window. This
        essentially just gives us the option to zoom in on the stuff visible for a certain state (can be specified in xml data.)
    @param EntManager This is the entity manager and is for loading entities into the game based on the state being switched to.
        The xml data tells which entities need to be loaded for what state.
    @param AstManager This is the asset manager and it is for loading in assets that are needed by entities. The xml data
        specifies what assets are to be fetched and used for the assembling of each entity. Once an asset is fetched once,
        it won't need to be loaded the second time because it's stored in the AstManager."""

    print "NEW STATE!", lNxtState
    #The data will lie within the nextState[0]+".txt" file and the nextState[1] element within that elemthe ent.
    tree = parse("StateData/StateInit.xml")

    #The root element and the element containing the entities we need will be using this variable.
    root = tree.getroot()

    if root.find(lNxtState[0]) != None:
        #This changes the node to the one that represents the state we're switching to.
        root = root.find(lNxtState[0]).find(lNxtState[1])

        if root == None:
            print "There was a problem finding %s in the StateInit.xml file." % (
                lNxtState[1])
    else:
        print "There was a problem finding %s in the StateInit.xml file." % (
            lNxtState[0])

    #This will reset the windowView's dimensions within the actual window with respect to the new state
    windView.reset(sf.FloatRect(int(int(root.find('viewWidth').text)//4), \
                int(int(root.find('viewHeight').text)//4),   \
                int(int(root.find('viewWidth').text)//2),    \
                int(int(root.find('viewHeight').text)//2)))

    #This clears all of the things that in the game since the last state
    EntManager._Empty_Entity_Containers()
    AstManager._Empty_Assets()
    Input_Manager._Empty_Inputs()
    System_Manager._Empty_Systems()

    for entity in root.findall('Entity'):

        entityInstance = None

        #This checks to see if there is a function that exists that will assemble this entity.
        if entity.find('assembleFunc') != None:

            #This will hold all of the attributes needed to assemble the entity (using the xml files to get the data later on.)
            dEntityAttribs = {}

            #This will loop through all of the attribute names for each entity.
            for attrib in entity.find('Attributes').getiterator():

                #Check to see if this is a sound for the entity!
                if attrib.tag == 'Sound':
                    #THis will start a new list of Sounds if we haven't already loaded a sound into this entity's attributes.
                    if dEntityAttribs.get(attrib.tag, None) == None:
                        dEntityAttribs[attrib.tag] = []

                    #Query the AssetManager for a sound that is associated with this entity, then throw that into the dictionary of attributes!
                    dEntityAttribs[attrib.tag].append(
                        AstManager._Get_Sound(attrib.attrib['name']))

                elif attrib.tag == 'Music':

                    #THis will start a new list of Musics if we haven't already loaded a sound into this entity's attributes.
                    if dEntityAttribs.get(attrib.tag, None) == None:
                        dEntityAttribs[attrib.tag] = []

                    dEntityAttribs[attrib.tag].append(
                        AstManager._Get_Music(attrib.attrib['name']))

                #Check to see if this is a texture for the entitititity.
                elif attrib.tag == 'Texture':

                    #THis will start a new list of Textures if we haven't already loaded a sound into this entity's attributes.
                    if dEntityAttribs.get(attrib.tag, None) == None:
                        dEntityAttribs[attrib.tag] = []

                    #Query the AssetManager for a texture that is associated with this entity, then throw that into the dictionary of attributes!
                    dEntityAttribs[attrib.tag].append(
                        AstManager._Get_Texture(attrib.attrib['name']))

                #Check to see if this is a font for the entitititity.
                elif attrib.tag == 'Font':

                    #THis will start a new list of Fonts if we haven't already loaded a sound into this entity's attributes.
                    if dEntityAttribs.get(attrib.tag, None) == None:
                        dEntityAttribs[attrib.tag] = []

                    #Query the AssetManager for a font that is associated with this entity, then throw that into the dictionary of attributes!
                    dEntityAttribs[attrib.tag].append(
                        AstManager._Get_Font(attrib.attrib['name']))

                else:
                    #Anything else will just be put in the dictionary as an attribute
                    dEntityAttribs[attrib.tag] = attrib.text

            module = importlib.import_module('entities')

            assembleFunc = getattr(module, entity.find('assembleFunc').text)

            #Here we're using the Assemble*() function associated with the name of this entity to assemble the entity so that
            #we can add it to the EntityManager.
            #And all Assemble*() functions will use the same arguments(using a dictionary to allow dynamic arguments.)
            entityInstance = assembleFunc(entity.attrib['name'],
                                          entity.attrib['type'],
                                          dEntityAttribs)

        else:
            #Here we will add in a default entity instance.
            entityInstance = entities.Entity(entity.attrib['name'],
                                             entity.attrib['type'], {})

        #THis adds in the components that exist in the xml file for this entity (it allows custom/variations of entities to exist.)
        for component in entity.findall('Component'):

            #These are for getting the actual
            module = importlib.import_module('components')

            componentClass = getattr(module, component.attrib['name'])

            #This will add in a component into the entity we just created.
            #And note that it is giving the component a dictionary of the data in the xml files.
            entityInstance._Add_Component(
                componentClass({
                    DataTag.tag: DataTag.text
                    for DataTag in component.getiterator()
                }))

        EntManager._Add_Entity(entityInstance)

    #Each one of these nodes will be an input that will be initialized for the state that is being loaded (and a multitude of kinds.)
    for inpoot in root.findall("Input"):

        #Check to see if this input's type is a hotspot.
        if inpoot.attrib["type"] == "hotspot":
            Input_Manager._Add_Hotspot(inpoot.find("x").text, inpoot.find("y").text, inpoot.find("width").text, inpoot.find("height").text, \
                                       inpoot.find("OnPressed").find("type").text if inpoot.find("OnPressed") != None else None,    \
                                       inpoot.find("OnSelected").find("system").text if inpoot.find("OnSelected") != None else None,    \
                                       AssembleEntityInfo(inpoot, "OnSelected"), \
                                       inpoot.find("OnDeselected").find("system").text if inpoot.find("OnDeselected") != None else None,    \
                                       AssembleEntityInfo(inpoot, "OnDeselected"), \
                                       inpoot.find("OnPressed").find("system").text if inpoot.find("OnPressed") != None else None,    \
                                       AssembleEntityInfo(inpoot, "OnPressed"), \
                                       inpoot.find("OnReleased").find("system").text if inpoot.find("OnReleased") != None else None,   \
                                       AssembleEntityInfo(inpoot, "OnReleased"))

        #Check to see if thisinput's type is a action.
        elif inpoot.attrib["type"] == "key":
            #This will add a key_Listener to our Input_Manager given the attribute data from the inpoot elemenet from the xml file.
            Input_Manager._Add_Key_Listener(inpoot.find("key").text,    \
                                            inpoot.find("OnPressed").find("type").text if inpoot.find("OnPressed") != None else None,  \
                                            inpoot.find("OnPressed").find("system").text if inpoot.find("OnPressed") != None else None,   \
                                            AssembleEntityInfo(inpoot, "OnPressed"),    \
                                            inpoot.find("OnReleased").find("system").text if inpoot.find("OnReleased") != None else None,   \
                                            AssembleEntityInfo(inpoot, "OnReleased"))

        elif inpoot.attrib["type"] == "mouse":
            pass

    #These are the systems that are relevant to this state and they will be added into the System_Queue class.
    for system in root.findall("System"):

        #This will load a system into the System_Queue and then it will be activated next update.
        systems.System_Queue._Add_System(
            system.find("type").text,
            system.find("system").text, AssembleEntityInfo(root))

    #Now we gotta update the state variables so that we aren't signaling to change states anymore
    for i in xrange(len(lCurState)):
        lCurState[i] = lNxtState[i]
        lNxtState[i] = "NULL"
示例#6
0
def ChangeState(lCurState, lNxtState, window, windView, EntManager):
    """This function is passed a couple lists representing the info on the different levels of this game's
    hierarchical finite state machine. This function essentially generically sets up the Entity and Asset Managers
    based off of data that can be retreived from xml files.
    @param lCurState This list contains the information on which state the program is in and it takes acount into
        sub-states. So each element of the list is a sub-state of the previous element.
    @param lNxtState This list contains the information on which state the program is to be switched to and it takes acount into
        sub-states. So each element of the list is a sub-state of the previous element.
    @param windView This is SFML's View object and allows us to zoom in on the what would be shown in the window. This
        essentially just gives us the option to zoom in on the stuff visible for a certain state (can be specified in xml data.)
    @param EntManager This is the entity manager and is for loading entities into the game based on the state being switched to.
        The xml data tells which entities need to be loaded for what state."""

    print "NEW STATE!", lNxtState
    #The data will lie within the nextState[0]+".txt" file and the nextState[1] element within that elemthe ent.
    tree = parse("StateData/%s/%s.xml" % (lNxtState[0], lNxtState[1]))

    #The root element and the element containing the entities we need will be using this variable.
    root = tree.getroot()

    #This will reset the windowView's dimensions within the actual window with respect to the new state
    #windView.reset(sf.FloatRect((window.width - int(root.find('viewWidth').text))/2, \
    #            (window.height - int(root.find('viewHeight').text))/2,   \
    #            int(root.find('viewWidth').text),    \
    #            int(root.find('viewHeight').text)))

    #print float(root.find('viewWidthRatio').text)

    print "The new view's stats:\nx:%f\ny:%f\nwidth:%f\nheight:%f"%(int(window.width - int(window.width*float(root.find('viewWidthRatio').text)))/2,     \
                                int(window.height - int(window.height*float(root.find('viewHeightRatio').text)))/2,  \
                                int(window.width*float(root.find('viewWidthRatio').text)),                           \
                                int(window.height*float(root.find('viewHeightRatio').text)))

    windView.reset(sf.FloatRect((window.width - int(window.width*float(root.find('viewWidthRatio').text)))/2,     \
                                (window.height - int(window.height*float(root.find('viewHeightRatio').text)))/2,  \
                                window.width*float(root.find('viewWidthRatio').text),                           \
                                window.height*float(root.find('viewHeightRatio').text)))

    config.Tile_Width = window.width / (config.CHUNK_TILES_WIDE * 2.)
    config.Tile_Height = window.height / (config.CHUNK_TILES_HIGH * 2.)

    print "TileWidth is %f and TileHeight is %f" % (config.Tile_Width,
                                                    config.Tile_Height)
    print "Window dimensions are %d x %d" % (window.width, window.height)

    #windView.reset(sf.FloatRect(int(window.width - window.width*float(root.find('viewWidthRatio').text)/2), \
    #                int(window.height - window.height*float(root.find('viewHeightRatio').text)/2),            \
    #                int(window.width*float(root.find('viewWidthRatio').text)),               \
    #                int(window.height*float(root.find('viewHeightRatio').text))))

    #This clears all of the things that in the game since the last state
    EntManager._Empty_Entity_Containers()
    AstManager._Empty_Assets()
    Input_Manager._Empty_Inputs()
    System_Manager._Empty_Systems()

    for entity in root.findall('Entity'):

        entityInstance = GetEntityBlueprints(entity)

        EntManager._Add_Entity(entityInstance)

    #Each one of these nodes will be an input that will be initialized for the state that is being loaded (and a multitude of kinds.)
    for inpoot in root.findall("Input"):

        #print inpoot.attrib

        #Check to see if this input's type is a hotspot.
        if inpoot.attrib["type"] == "hotspot":
            Input_Manager._Add_Hotspot(inpoot.find("x").text, inpoot.find("y").text, inpoot.find("width").text, inpoot.find("height").text, \
                                       inpoot.find("OnPressed").find("type").text if inpoot.find("OnPressed") != None else None,    \
                                       inpoot.find("OnSelected").find("system").text if inpoot.find("OnSelected") != None else None,    \
                                       AssembleEntityInfo(inpoot, "OnSelected"), \
                                       inpoot.find("OnDeselected").find("system").text if inpoot.find("OnDeselected") != None else None,    \
                                       AssembleEntityInfo(inpoot, "OnDeselected"), \
                                       inpoot.find("OnPressed").find("system").text if inpoot.find("OnPressed") != None else None,    \
                                       AssembleEntityInfo(inpoot, "OnPressed"), \
                                       inpoot.find("OnReleased").find("system").text if inpoot.find("OnReleased") != None else None,   \
                                       AssembleEntityInfo(inpoot, "OnReleased"))

        #Check to see if thisinput's type is a action.
        elif inpoot.attrib["type"] == "key":
            #This will add a key_Listener to our Input_Manager given the attribute data from the inpoot elemenet from the xml file.
            Input_Manager._Add_Key_Listener(inpoot.find("key").text,    \
                                            inpoot.find("OnPressed").find("type").text if inpoot.find("OnPressed") != None else None,  \
                                            inpoot.find("OnPressed").find("system").text if inpoot.find("OnPressed") != None else None,   \
                                            AssembleEntityInfo(inpoot, "OnPressed"),    \
                                            inpoot.find("OnReleased").find("system").text if inpoot.find("OnReleased") != None else None,   \
                                            AssembleEntityInfo(inpoot, "OnReleased"))

        elif inpoot.attrib["type"] == "mouse":
            Input_Manager._Add_Mouse_Listener(inpoot.find("button").text,             \
                                              inpoot.find("OnPressed").find("type").text if inpoot.find("OnPressed") != None else None,  \
                                              inpoot.find("OnPressed").find("system").text if inpoot.find("OnPressed") != None else None,   \
                                              AssembleEntityInfo(inpoot, "OnPressed"),    \
                                              inpoot.find("OnReleased").find("system").text if inpoot.find("OnReleased") != None else None,   \
                                              AssembleEntityInfo(inpoot, "OnReleased"))

    #These are the systems that are relevant to this state and they will be added into the System_Queue class.
    for system in root.findall("System"):

        #This will load a system into the System_Queue and then it will be activated next update.
        System_Manager._Add_System(
            system.find("type").text,
            system.find("systemFunc").text, AssembleEntityInfo(system))

    #Now we gotta update the state variables so that we aren't signaling to change states anymore
    for i in xrange(len(lCurState)):
        lCurState[i] = lNxtState[i]
        lNxtState[i] = "NULL"