public void Move(int entityNum, string id, int zoneNum) //вызываем когда надо запустить движение объекта { Zone zone = (Zone)zoneNum; Entity entity = (Entity)entityNum; switch (entity) { case Entity.PLANE: lock (VisualizerHandler.Planes) { var planeTuple = VisualizerHandler.Planes.Find(x => x.Item1.Id.Equals(id)); // if (planeTuple == null) return; Plane plane = planeTuple.Item1; planeTuple.Item4 = zone; Point newCoords = GetZonePoint(zone); planeTuple.Item3 = newCoords; plane.State = EntityState.MOVING; } break; case Entity.PASSENGER: lock (VisualizerHandler.Passengers) { var passengerTuple = VisualizerHandler.Passengers.Find(x => x.Item1.Id.Equals(id)); // if (passengerTuple == null) return; Passenger passenger = passengerTuple.Item1; passengerTuple.Item4 = zone; Point newCoords = GetZonePoint(zone); passengerTuple.Item3 = newCoords; passenger.State = EntityState.MOVING; } break; case Entity.FUEL_TRUCK: case Entity.CARGO_TRUCK: case Entity.BUS: lock (VisualizerHandler.LandVehicles) { var vehicleTuple = VisualizerHandler.LandVehicles.Find(x => x.Item1.Id.Equals(id)); LandVehicle vehicle = vehicleTuple.Item1; vehicleTuple.Item4 = zone; Point newCoords = GetZonePoint(zone); vehicleTuple.Item3 = newCoords; vehicle.State = EntityState.MOVING; } break; default: throw new ArgumentOutOfRangeException(nameof(entity), entity, null); } }
public void Spawn(int entityNum, string id, int zoneNum, int cargoCount) //спавним объект (все, кроме самолётов) { Zone zone = (Zone)zoneNum; Entity entity = (Entity)entityNum; switch (entity) { case Entity.BUS: case Entity.CARGO_TRUCK: case Entity.FUEL_TRUCK: { LandVehicle vehicle = new LandVehicle() //новая машинка { Cargo = cargoCount, CurrentZone = zone, Id = id, State = EntityState.WAITING_FOR_COMMAND, VehicleType = entity }; Point coords = GetZonePoint(zone); //выбираем рандомную точку в зоне для спавна lock (VisualizerHandler.LandVehicles) { VisualizerHandler.LandVehicles.Add(new Triple <LandVehicle, Point, Point, Zone>(vehicle, coords, coords, zone)); //добавляем в список отрисовки } } break; case Entity.PASSENGER: //новый пассажир { Passenger passenger = new Passenger() { CargoCount = cargoCount, CurrentZone = zone, Id = id, State = EntityState.WAITING_FOR_COMMAND, }; Point coords = GetZonePoint(zone); lock (VisualizerHandler.Passengers) { VisualizerHandler.Passengers.Add(new Triple <Passenger, Point, Point, Zone>(passenger, coords, coords, zone)); //добавляем в список отрисовки } } break; } }
public void Spawn(int entityNum, string id, int zoneNum, int cargoCount) { Zone zone = (Zone)zoneNum; Entity entity = (Entity)entityNum; switch (entity) { case Entity.BUS: case Entity.CARGO_TRUCK: case Entity.FUEL_TRUCK: { LandVehicle vehicle = new LandVehicle() { Cargo = cargoCount, CurrentZone = zone, Id = id, State = EntityState.WAITING_FOR_COMMAND, VehicleType = entity }; Point coords = GetZonePoint(zone); lock (VisualizerHandler.LandVehicles) { VisualizerHandler.LandVehicles.Add(new Triple <LandVehicle, Point, Point, Zone>(vehicle, coords, coords, zone)); } } break; case Entity.PASSENGER: { Passenger passenger = new Passenger() { CargoCount = cargoCount, CurrentZone = zone, Id = id, State = EntityState.WAITING_FOR_COMMAND, }; Point coords = GetZonePoint(zone); lock (VisualizerHandler.Passengers) { VisualizerHandler.Passengers.Add(new Triple <Passenger, Point, Point, Zone>(passenger, coords, coords, zone)); } } break; } }
public void UpdateCargo(string id, int cargoCount) //обновление значений полей с гурзом/топливом у машинок { lock (VisualizerHandler.Planes) { Plane plane = VisualizerHandler.Planes.Select(x => x.Item1).ToList().Find(x => x.Id.Equals(id)); if (plane != null) { plane.CargoCount = cargoCount; } } lock (VisualizerHandler.LandVehicles) { LandVehicle vehicle = VisualizerHandler.LandVehicles.Select(x => x.Item1).ToList().Find(x => x.Id.Equals(id)); if (vehicle != null) { vehicle.Cargo = cargoCount; } } }
public static void HandleVisuals() { while (true) { Thread.Sleep(10); lock (LandVehicles) { foreach (Triple <LandVehicle, Point, Point, Zone> landVehicleTriple in LandVehicles) { LandVehicle vehicle = landVehicleTriple.Item1; if (vehicle.State != EntityState.MOVING) { continue; } Point currentPoint = landVehicleTriple.Item2; Point targetPoint = landVehicleTriple.Item3; int diffX = targetPoint.X - currentPoint.X; diffX = Math.Min(5, Math.Abs(targetPoint.X - currentPoint.X)) * Math.Sign(diffX); int diffY = targetPoint.Y - currentPoint.Y; diffY = Math.Min(5, Math.Abs(targetPoint.Y - currentPoint.Y)) * Math.Sign(diffY); currentPoint.X += diffX; currentPoint.Y += diffY; landVehicleTriple.Item2 = currentPoint; if (currentPoint.X == targetPoint.X && currentPoint.Y == targetPoint.Y) { CompleteMove(vehicle.VehicleType, vehicle.Id, landVehicleTriple.Item4); vehicle.CurrentZone = landVehicleTriple.Item4; vehicle.State = EntityState.WAITING_FOR_COMMAND; } } } lock (Passengers) { foreach (Triple <Passenger, Point, Point, Zone> passengerTriple in Passengers) { Passenger passenger = passengerTriple.Item1; if (passenger.State != EntityState.MOVING) { continue; } Point currentPoint = passengerTriple.Item2; Point targetPoint = passengerTriple.Item3; int diffX = targetPoint.X - currentPoint.X; diffX = Math.Sign(diffX); int diffY = targetPoint.Y - currentPoint.Y; diffY = Math.Sign(diffY); passengerTriple.Item2 = new Point(currentPoint.X + diffX, currentPoint.Y + diffY); if (passengerTriple.Item2.X == targetPoint.X && passengerTriple.Item2.Y == targetPoint.Y) { CompleteMove(Entity.PASSENGER, passenger.Id, passengerTriple.Item4); passenger.CurrentZone = passengerTriple.Item4; passenger.State = EntityState.WAITING_FOR_COMMAND; } } } lock (Planes) { foreach (Triple <Plane, Point, Point, Zone> planeTriple in Planes) { int speed = 5; Plane plane = planeTriple.Item1; if (plane.State != EntityState.MOVING) { continue; } Point currentPoint = planeTriple.Item2; Point targetPoint = planeTriple.Item3; int diffX = targetPoint.X - currentPoint.X; int diffY = targetPoint.Y - currentPoint.Y; double length = Math.Sqrt(Math.Pow(diffX, 2) + Math.Pow(diffY, 2)); if (length > speed) { diffX = (int)(diffX / length * speed); diffY = (int)(diffY / length * speed); } planeTriple.Item2 = new Point(currentPoint.X + diffX, currentPoint.Y + diffY); if (planeTriple.Item2.X == targetPoint.X && planeTriple.Item2.Y == targetPoint.Y) { CompleteMove(Entity.PLANE, plane.Id, planeTriple.Item4); plane.CurrentZone = planeTriple.Item4; plane.State = EntityState.WAITING_FOR_COMMAND; } } } } }