public void updateSpeed(CarAgent[] listCars, Peage[] listPeages) { proximity = Road.MAX_SPEED_ROAD_3; // Obstacle detection : For cars proximity = detectProximityCars(proximity, listCars); // Obstacle detection : For Counters, if the car didn't pay already. if (flagPaid == false && (Road.getRoadZone(PosX) == 3 || Road.getRoadZone(PosX) == 4)) { int peageID = -1; // Let's find which counter we need to keep track of. switch ((int)targetY) { case (233): peageID = 0; break; case (283): peageID = 1; break; case (333): peageID = 2; break; case (383): peageID = 3; break; case (433): peageID = 4; break; case (483): peageID = 5; break; } Peage counter = listPeages[peageID]; double distance = DistanceTo(counter); if (distance < proximity) { proximity = distance; if (PosX > Road.ZONE_GUICHET_START) { flagCloseToCounter = true; } } if (Math.Abs(counter.PosX - PosX) > CAR_WIDTH + 10) { flagHorizontalProx = true; } } // Approaching Counter if (proximity < MIN_DISTANCE + 20 && flagCloseToCounter == true) { speedX = 0; } else if (proximity < speedX + MIN_DISTANCE) { // Using Brakes! if (proximity < MIN_DISTANCE + 20) { speedX += (-Road.MAX_SPEED_ROAD_3 * BRAKES_EFFICIENCY) / ACCELERATION; // We take the speed of road 3 as its the fastest. if (speedX < 0) { speedX = 0; } } // Deceleration with obstacle : else if (proximity < (MIN_DISTANCE + 20) * 1.5) { speedX += ((Road.MAX_SPEED_ROAD_3 / (proximity / MIN_DISTANCE)) - Road.MAX_SPEED_ROAD_3) / ACCELERATION; // A VERIFIER if (speedX < (Road.MAX_SPEED_ROAD_3 * 0.2) && speedX > 0) { speedX = (Road.MAX_SPEED_ROAD_3 * 0.2); } else if (speedX < (Road.MAX_SPEED_ROAD_3 * 0.2) && speedX == 0) { speedX = (Road.MAX_SPEED_ROAD_3 * 0.1); } } // Acceleration with obstacle : else { switch (road.Id) { case 3: speedX += (Road.MAX_SPEED_ROAD_1 - (Road.MAX_SPEED_ROAD_1 * ((MIN_DISTANCE + 20) / proximity))) / ACCELERATION; // A VERIFIER if (speedX > Road.MAX_SPEED_ROAD_1) { speedX = Road.MAX_SPEED_ROAD_1; } break; case 2: speedX += (Road.MAX_SPEED_ROAD_2 - (Road.MAX_SPEED_ROAD_2 * ((MIN_DISTANCE + 20) / proximity))) / ACCELERATION; // A VERIFIER if (speedX > Road.MAX_SPEED_ROAD_2) { speedX = Road.MAX_SPEED_ROAD_2; } break; case 1: speedX += (Road.MAX_SPEED_ROAD_3 - (Road.MAX_SPEED_ROAD_3 * ((MIN_DISTANCE + 20) / proximity))) / ACCELERATION; // A VERIFIER if (speedX > Road.MAX_SPEED_ROAD_3) { speedX = Road.MAX_SPEED_ROAD_3; } break; } } } else { // Acceleration without obstacle : switch (road.Id) { case 3: speedX += Road.MAX_SPEED_ROAD_1 / ACCELERATION; if (speedX > Road.MAX_SPEED_ROAD_1) { speedX = Road.MAX_SPEED_ROAD_1; } break; case 2: speedX += Road.MAX_SPEED_ROAD_2 / ACCELERATION; if (speedX > Road.MAX_SPEED_ROAD_2) { speedX = Road.MAX_SPEED_ROAD_2; } break; case 1: speedX += Road.MAX_SPEED_ROAD_3 / ACCELERATION; if (speedX > Road.MAX_SPEED_ROAD_3) { speedX = Road.MAX_SPEED_ROAD_3; } break; } } if (targetY != PosY && flagVerticalProx == false) { // UP or DOWN? int direction = (targetY > PosY ? 1 : -1); int directionMov = (speedY >= 0 ? 1 : -1); double differenceAbs = Math.Abs(targetY - PosY); if (differenceAbs > 30) { // Lots of Y distance yet speedY += (speedX * direction) / ACCELERATION; } else { // Not a lot of Y distance double targetYSpeed = (targetY - PosY) * 1.5; bool flagDeceleration = false; if (Math.Abs(speedY) > Math.Abs(targetYSpeed)) { speedY += ((-Road.MAX_SPEED_ROAD_3 * directionMov) / ACCELERATION); flagDeceleration = true; } if (Math.Abs(speedY) < Math.Abs(targetYSpeed) && flagDeceleration == true) { speedY = targetYSpeed; } else if (Math.Abs(speedY) < Math.Abs(targetYSpeed) && flagDeceleration == false) { speedY += ((Road.MAX_SPEED_ROAD_3 * direction) / ACCELERATION); } } if (Math.Abs(speedY) > (speedX * 0.5)) { speedY = (speedX * 0.5 * direction); } if (Math.Abs(targetY - PosY) < 3) { speedY = 0; } } else { speedY = speedY / 2; } }
public void updateTargetY() { int currentZone = road.getRoadZone(PosX); switch (currentZone) { case (1): // Before transition zone // Nothing to do. break; case (2): // Entering transition zone if (targetCounter.Id == road.Id * 2 || targetCounter.Id == ((road.Id * 2) - 1)) { // Nothing to do. We are on the right road already. } else { int targetRoad = 0; switch (targetCounter.Id) { case 0: targetRoad = 0; break; case 1: targetRoad = 0; break; case 2: targetRoad = 1; break; case 3: targetRoad = 1; break; case 4: targetRoad = 2; break; case 5: targetRoad = 2; break; } road = theWorld.roadsArray[targetRoad]; TargetY = road.PosY; } break; case (3): // Entering "Peage" zone targetY = 183 + 50 * (TargetCounter.Id + 1); break; case (4): // Leaving counter zone targetY = road.PosY; if (speedX > (Road.MAX_SPEED_ROAD_3 * 0.75) && TargetCounter.Id == 0) { speedX = Road.MAX_SPEED_ROAD_3 * 0.75; } break; case (5): // Leaving "Peage" zone // No Switch to do break; } }