IRESTE/Map.py

57 lines
1.7 KiB
Python

# Code pour gerer le reseau (la carte)
# Pour l'instant c'est juste un wrapper autour de sumolib
# mais si on se decide à utiliser notre propre format dans le futur ça facilitera la transition
import sumolib
import pygame as pg
class Map:
def __init__(self,path,surface):
self.fromPath(path)
self.surf=surface
def fromPath(self,path):
self.net = sumolib.net.readNet(path,withInternal=True,withConnections=True)
def text(self,text,pos,font):
img = font.render(text,True,(255,255,255))
self.surf.blit(img,pos)
def draw(self,screen,debug=False,font=None):
for edge in self.net.getEdges():
color=(255,255,255)
if(edge.getFunction()=="internal"):
color=(255,0,0)
for lane in edge.getLanes():
pts=lane.getShape()
pts=map(self.convertPos,pts)
pg.draw.lines(self.surf,color,False,list(pts))
if(debug):
for node in self.net.getNodes():
self.text(node.getID(),self.convertPos(node.getCoord()),font)
screen.blit(self.surf,(0,0))
def getEdge(self,edgeID):
return self.net.getEdge(edgeID)
def getNode(self,nodeID):
return self.net.getNode(nodeID)
def getLane(self,laneID):
return self.net.getLane(laneID)
def convertPos(self,pos):
bounds=self.net.getBoundary()
bounds[0]-=200
bounds[1]-=0
bounds[2]+=10
bounds[3]+=0
scale=min(self.surf.get_width()/(bounds[2]-bounds[0]),self.surf.get_height()/(bounds[3]-bounds[1]))
x=pos[0]*scale-bounds[0]
y=pos[1]*scale-bounds[1]
return (x,self.surf.get_height()-y)