public Point GetEndPosition(Tile tile, Vehicle v) { int randomLane = 0; if (tile.name == "Spawner" || tile.name == "Road") { randomLane = v.Lane; //v.Lane = randomLane; } else { Tile endTile = simControl.simulationMap.GetConnectingTiles(tile.position)[v.NextDirection - 1]; int tileLanes = endTile.GetLanesIn((v.NextDirection + 1) % 4 + 1); randomLane = Math.Abs(Guid.NewGuid().GetHashCode()) % tileLanes; v.Lane = randomLane; } switch(v.NextDirection) { case 1: return new Point(tile.position.X + 53 + (randomLane * 16), tile.position.Y ); case 2: if (v.LastDirection == 1 && v.Direction != 2) return new Point(tile.position.X - v.Bitmap.Height + 100 - 5, tile.position.Y + 53 + (16 * randomLane) - v.Bitmap.Height); else return new Point(tile.position.X - v.Bitmap.Height + 100 - 5, tile.position.Y + 53 + (16 * randomLane)); case 3: if(v.LastDirection == 4) return new Point(tile.position.X + 37 - (16 * randomLane), tile.position.Y + 100 - v.Bitmap.Height - 5); else return new Point(tile.position.X + 37 - (16 * randomLane), tile.position.Y + 100 - v.Bitmap.Width - 5); case 4 : return new Point(tile.position.X+5,tile.position.Y+ 37 -(16*randomLane)); default : return new Point(0,0); } }
private void GetRandomOutDirection(Tile tile, Vehicle v) { int newDirection = 0; switch (tile.name) { case "Spawner": newDirection = v.Direction; break; case "Road": foreach (int i in tile.Directions) { if (i != ((v.Direction + 1) % 4)+1) { newDirection = i; } } break; case "Crossroad" : switch (tile.GetLanesIn((v.Direction+1)%4+1)) { case 1: while (newDirection == 0 || newDirection == (v.Direction + 1) % 4 + 1) newDirection = RandomNumber(4); break; case 2: switch(v.Lane) { case 1: int number = RandomNumber(2); if (number == 1) newDirection = (v.Direction) % 4 + 1; else newDirection = v.Direction; break; case 0: newDirection = (v.Direction+2) % 4 + 1; break; } break; case 3: switch (v.Lane) { case 0: newDirection = (v.Direction + 2) % 4 + 1; break; case 1: newDirection = v.Direction; break; case 2: newDirection = (v.Direction) % 4 + 1; break; } break; } break; case "Fork" : switch (tile.GetLanesIn((v.Direction + 1) % 4 + 1)) { case 1: while (newDirection == 0 || newDirection == (v.Direction + 1) % 4 + 1 || newDirection == tile.NotDirection) newDirection = RandomNumber(4); break; case 2: if (v.Direction == (tile.NotDirection + 2) % 4 + 1) { switch (v.Lane) { case 1: newDirection = v.Direction; break; case 0: newDirection = (v.Direction + 2) % 4 + 1; break; } } else if(v.Direction == (tile.NotDirection) % 4 + 1) { switch (v.Lane) { case 1: newDirection = (v.Direction) % 4 + 1; break; case 0: newDirection = v.Direction; break; } } else { switch (v.Lane) { case 1: newDirection = (v.Direction) % 4 + 1; break; case 0: newDirection = (v.Direction + 2) % 4 + 1; break; } } break; case 3: if ((v.Direction + 1) % 4 + 1 == (tile.NotDirection + 2) % 4 + 1) { switch (v.Lane) { case 2: newDirection = (v.Direction) % 4 + 1; break; case 1: newDirection = v.Direction; break; case 0: newDirection = v.Direction; break; } } else if ((v.Direction + 1) % 4 + 1 == (tile.NotDirection) % 4 + 1) { switch (v.Lane) { case 2: newDirection = v.Direction; break; case 1: newDirection = v.Direction; break; case 0: newDirection = (v.Direction + 2) % 4 + 1; break; } } else { switch (v.Lane) { case 2: newDirection = (v.Direction) % 4 + 1; break; case 1: newDirection = (v.Direction) % 4 + 1; break; case 0: newDirection = (v.Direction + 2) % 4 + 1; break; } } break; } break; } //if (v.UpdatePoint == 0) { if (v.Direction - newDirection < 0) { v.Bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone); } v.NextDirection = newDirection; } }
//calculates the places and returns if the vehicle is allowed to drive private bool CorrectDistance(Tile t, Vehicle v, int CarSpace) { switch (v.NextDirection) { case 1: if (v.position.Y - v.Speed>= t.position.Y+ CarSpace) return true; break; case 2: if (v.position.X + v.Speed + v.Bitmap.Width + 5 <= t.position.X + t.size.Width - CarSpace) return true; break; case 3: if (v.position.Y + v.Speed + v.Bitmap.Width + 5 <= t.position.Y + t.size.Height - CarSpace) return true; break; case 4: if (v.position.X - v.Speed - 5 >= t.position.X + CarSpace) return true; break; } return false; }
private bool VehicleIsOnEndSpawner(Vehicle v, Tile t) { if (t.name == "Spawner" && t.Directions.Contains((v.Direction + 1) % 4 + 1)) { switch (v.Direction) { case 1: if (v.position.Y - v.Speed <= t.position.Y + 15) return true; break; case 2: if (v.position.X + v.Speed + 15 >= t.position.X + 85) return true; break; case 3: if (v.position.Y + v.Speed + 15 >= t.position.Y + 85) return true; break; case 4: if (v.position.X - v.Speed <= t.position.X + 15) return true; break; } } return false; }
//returns if there are no other cars standing in front private bool DistanceFromCars(Tile t, Vehicle v) { int distance = 0;//distance between the end of the tile and the last car standing still. List<List<Vehicle>> vehicleList = simControl.simulationMap.GetTileMea(t.position.X, t.position.Y).vehicles[v.Direction - 1]; distance = vehicleList[v.Lane].IndexOf(v) * 16; if (t.name == "Fork" || t.name == "Crossroad") return true; return CorrectDistance(t, v, distance); }
//returns if an object is still on the tile private bool StaysOnTile(Tile t, Vehicle v) { return CorrectDistance(t, v, 0); }
private void UpdateVehicle(Tile t, Vehicle v) { if (v.Speed == 0) waitingCars--; v.Speed = t.MaxSpeed + extraSpeed; //if vehicle has to dissapear ----- moet worden vervangen door zwart vlak over de spawner----- if (VehicleIsOnEndSpawner(v, t)) { simControl.simulationMap.GetTileMea(t.position.X, t.position.Y).RemoveVehicle(simControl, v, v.Direction, v.LastDirection, v.Lane); simControl.totalCars--; } if (StaysOnTile(t, v))//if vehicle is still on the tile { if (DistanceFromCars(t, v)) { //if there are other cars standing in front v.Update(t); } else { v.Speed = 0; waitingCars++; } } else { if (t.Access[v.NextDirection - 1, v.Lane])//if the next tile is accessible { //remove vehicle from old tile and add vehicle to new tile Tile[] test = simControl.simulationMap.GetSurroundingTilesSim(t.position); //simControl.simulationMap.GetTile(t.position).RemoveVehicle(simControl, v, v.LastDirection, v.Lane); Tile nextTile = simControl.simulationMap.GetSurroundingTilesSim(t.position)[v.NextDirection - 1]; simControl.simulationMap.GetTile(t.position).RemoveVehicle(simControl, v, v.Direction, v.Direction, v.oldLane); v.reset(); int oldLane = v.Lane; if (nextTile != null) { v.Speed = nextTile.maxSpeed; v.LastDirection = v.Direction; v.Direction = v.NextDirection; v.oldLane = v.Lane; nextTile.AddVehicle(simControl, v, v.Direction, v.Lane); GetRandomOutDirection(nextTile, v); v.endPosition = GetEndPosition(nextTile, v); v.Update(nextTile); } } else { v.Speed = 0; waitingCars++; } } }
/// <summary> /// Adds a vehicle to the tile. /// </summary> /// <param name="sim"></param> /// <param name="v"></param> /// <param name="Side"></param> /// <param name="lane"></param> public void AddVehicle(SimControl sim, Vehicle v, int Side, int lane) { List<List<Vehicle>> sideVehicles = vehicles[Side - 1]; List<Vehicle> laneVehicles = sideVehicles[lane]; laneVehicles.Add(v); numberOfVehicles++; //returns false if the tile is full if (laneVehicles.Count > 5 && this.name != "Spawner" && this.name != "Crossroad" && this.name != "Fork") { Tile lastTile = sim.simulationMap.GetSurroundingTiles(this.position)[(Side + 1) % 4]; lastTile.Access[Side - 1, lane] = false; } }
/// <summary> /// Removes a vehicle on a tile, this is used when a vehicle leaves the map on a spawner /// also removes a vehicle when a vehicle leaves a tile to enter another. /// </summary> /// <param name="sim"></param> /// <param name="v"></param> /// <param name="Side"></param> /// <param name="lane"></param> public void RemoveVehicle(SimControl sim, Vehicle v, int lastSide, int Side, int lane) { List<List<Vehicle>> sideVehicles = vehicles[lastSide - 1]; List<Vehicle> laneVehicles = sideVehicles[lane]; laneVehicles.Remove(v); numberOfVehicles--; //looks if there is space for other cars to come on the tile if (laneVehicles.Count < 5 && this.name != "Spawner" && this.name != "Crossroad" && this.name != "Fork") { Tile lastTile = sim.simulationMap.GetSurroundingTilesSim(this.position)[(lastSide + 1) % 4]; if (lastTile != null) lastTile.Access[lastSide - 1, lane] = true; } }