IRESTE/Map.py

41 lines
1.2 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 faciliterat la transition
import sumolib
import pygame as pg
class Map:
def __init__(self,path : str,surface : pg.Surface):
self.fromPath(path)
self.surf=surface
def fromPath(self,path : str):
self.net = sumolib.net.readNet(path,withInternal=True)
def draw(self,screen):
self.minVal=0
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))
screen.blit(self.surf,(0,0))
def convertPos(self,pos):
bounds=self.net.getBoundary()
bounds[0]-=20
bounds[1]-=10
bounds[2]+=10
bounds[3]+=10
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,y)