public World() { //left roads[0] = new Road(0, 280, 1); //top roads[1] = new Road(238, 0, 2); //bottom roads[2] = new Road(300, 503, 3); //right roads[3] = new Road(540, 220, 4); }
// Constructor public CarAgent(int paramId, CarAgent[] cars, World paramWorld) { // unique ID attribution // Well... unique for as long as you only look at existing cars. id = paramId; flagAbort = false; world = paramWorld; // Roll of the speed mult. int rollSpeedMult = RondPointWindow.rnd.Next(0, 7); SpeedMult = 1 - ((rollSpeedMult - 3) / 100); // = 0.97 0.98 0.99 1 1.01 1.02 1.03 int nbRoad = 0; nbRoad = RondPointWindow.rnd.Next(0, 4); road = world.roads[nbRoad]; PosX = road.PosX; PosY = road.PosY; targetY = PosY; targetX = PosX; // First Range Calculation for spawn-eligibility on the selected road. bool spawnable = false; bool pass; if (RondPointWindow.carCount > 0) { while (spawnable == false) { pass = true; foreach (CarAgent car in cars) { if (car != null) { if (car.Road == road) { if (DistanceTo(car) < 70) { pass = false; break; } } } } if (pass == true) { spawnable = true; } else { break; } } if (spawnable == false) { flagAbort = true; } } switch (road.Id) { case 1: //left speedX = Road.MaxSpeedRoad; speedY = 0; break; case 2: //top speedX = 0; speedY = Road.MaxSpeedRoad; break; case 3: //bottom speedX = 0; speedY = -Road.MaxSpeedRoad; break; default: //right speedX = -Road.MaxSpeedRoad; speedY = 0; break; } updateSpeed(paramWorld.roads); }
internal void Update(CarAgent car, CarAgent[] cars, Road[] roads) { updateTarget(car); updateAngle(); updateSpeed(roads); updatePosition(); }
public void updateSpeed(Road[] roads) { if (road.getRoadZone(PosX, PosY) == 2) { int direction; int directionMov; if (targetX != PosX) { if (targetX == roads[0].PosX) { //origin left target bottom direction = 1; directionMov = (speedX >= 0 ? 1 : -1); } else if (targetX == roads[1].PosX) { //origin top target left direction = -1; directionMov = (speedX >= 0 ? 1 : -1); } else if (targetX == roads[2].PosX) { //origin bottom target right direction = 1; directionMov = (speedX >= 0 ? 1 : -1); } else { //origin right target top direction = -1; directionMov = (speedX >= 0 ? 1 : -1); } double differenceAbs = Math.Abs(targetX - PosX); /*if (differenceAbs > 30) { // Lots of Y distance yet speedY += (speedX * direction) / 50;*/ speedX = (road.MaxSpeedRoad * direction); /*} else { // Not a lot of Y distance double targetYSpeed = (targetY - PosY) * 1.5; if (Math.Abs(speedY) > Math.Abs(targetYSpeed)) { speedY += ((-Road.MaxSpeedRoad * directionMov) / 50); } if (Math.Abs(speedY) < Math.Abs(targetYSpeed)) { speedY = targetYSpeed; } else if (Math.Abs(speedY) < Math.Abs(targetYSpeed)) { speedY += ((Road.MaxSpeedRoad * direction) / 50); } }*/ /*if (Math.Abs(speedY) > (speedX * 0.5)) { speedY = (speedX * 0.5 * direction); }*/ /*if (Math.Abs(targetY - PosY) < 3) { speedY = 0; }*/ } if (targetY != PosY) { if (targetY == roads[0].PosX) { //origin left target bottom direction = 1; directionMov = (speedY >= 0 ? 1 : -1); } else if (targetY == roads[1].PosX) { //origin top target left direction = 1; directionMov = (speedY >= 0 ? 1 : -1); } else if (targetY == roads[2].PosX) { //origin bottom target right direction = -1; directionMov = (speedY >= 0 ? 1 : -1); } else { //origin right target top direction = -1; directionMov = (speedY >= 0 ? 1 : -1); } double differenceAbs = Math.Abs(targetX - PosY); /*if (differenceAbs > 30) { // Lots of Y distance yet speedY += (speedX * direction) / 50;*/ speedY = (road.MaxSpeedRoad * direction); } } }