def test_eq(self): def r(): return random.triangular(0.0, 100.0) equal = [(sf.FloatRect(l, t, w, h), sf.FloatRect(l, t, w, h)) for l, t, w, h in [(r(), r(), r(), r()) for i in range(100)]] for r1, r2 in equal: self.assertEqual(r1, r2)
def test_neq(self): not_equal = [(sf.FloatRect(0, 0, 0, 0), sf.FloatRect(0, 0, 0, 10)), (sf.FloatRect(0, 0, 0, 0), sf.FloatRect(0, 0, 10, 0)), (sf.FloatRect(0, 0, 0, 0), sf.FloatRect(0, 10, 0, 0)), (sf.FloatRect(0, 0, 0, 0), sf.FloatRect(10, 0, 0, 0))] for r1, r2 in not_equal: self.assertNotEqual
def Init(): """This will setup the window and whatever needs setup (just once) at the start of the program.""" config.window = sf.RenderWindow( sf.VideoMode(config.WINDOW_WIDTH, config.WINDOW_HEIGHT), "TileGame") #This makes the background of the screen Black. config.window.clear(sf.Color.BLACK) config.windowView = sf.View() config.windowView.reset( sf.FloatRect(0, 0, config.WINDOW_WIDTH, config.WINDOW_HEIGHT)) #This is required for allowing importlib to import modules within these directories. sys.path.append(os.getcwd() + '\\Components\\') sys.path.append(os.getcwd() + '\\Systems\\') sys.path.append(os.getcwd() + '\\Entities\\')
def random_rect(self): return sf.FloatRect(random.triangular(0.0, 100.0), random.triangular(0.0, 100.0), random.triangular(0.0, 100.0), random.triangular(0.0, 100.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"
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"