Changeset 4


Ignore:
Timestamp:
Mar 26, 2015, 12:09:02 PM (9 years ago)
Author:
chronos
Message:
  • Modified: Connect and disconnect stations using mouse move rather then clicking to each station.
  • Added: Show available train count.
Location:
trunk
Files:
3 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/UEngine.pas

    r3 r4  
    3939
    4040  TMetroLine = class
     41    Engine: TEngine;
    4142    Color: TColor;
    4243    Stations: TMetroStations;
    4344    Trains: TMetroTrains;
    4445    TrackPoints: array of TPoint;
     46    procedure ConnectStation(Station: TMetroStation);
     47    procedure DisconnectStation(Station: TMetroStation);
    4548    procedure AddTrack(P1, P2: TPoint);
    4649    function GetTrackLength: Integer;
     
    5356
    5457  TMetroLines = class(TObjectList)
     58    Engine: TEngine;
     59    function AddNew: TMetroLine;
    5560    function SearchByColor(Color: TColor): TMetroLine;
    5661  end;
     
    7883  TMetroTrains = class(TObjectList)
    7984    function GetUnusedTrain: TMetroTrain;
     85    function GetUnusedCount: Integer;
    8086  end;
    8187
     
    109115  private
    110116    LastMousePos: TPoint;
     117    LastSelectedStation: TMetroStation;
     118    MouseHold: Boolean;
    111119    LastNewStationTime: TDateTime;
    112120    LastNewPassengerTime: TDateTime;
    113121    ImagePassenger: TImage;
     122    ImageLocomotive: TImage;
    114123    function GetExistStationShapes: TStationShapeSet;
    115124    function GetStationOnPos(Pos: TPoint): TMetroStation;
     
    150159  TrainSpeed = 3;
    151160  ImagePassengerName = 'Images/Passenger.png';
     161  ImageLocomotiveName = 'Images/Locomotive.png';
    152162  TrainPassengerCount = 6;
    153163  StationMinDistance = 50;
     
    179189  if I < Count then Result := TMetroTrain(Items[I])
    180190    else Result := nil;
     191end;
     192
     193function TMetroTrains.GetUnusedCount: Integer;
     194var
     195  I: Integer;
     196begin
     197  Result := 0;
     198  for I := 0 to Count - 1 do
     199    if not Assigned(TMetroTrain(Items[I]).Line) then Inc(Result);
    181200end;
    182201
     
    210229{ TMetroLines }
    211230
     231function TMetroLines.AddNew: TMetroLine;
     232begin
     233  Result := TMetroLine.Create;
     234  Result.Color := LineColors[Count];
     235  Result.Engine := Engine;
     236  Add(Result);
     237end;
     238
    212239function TMetroLines.SearchByColor(Color: TColor): TMetroLine;
    213240var
     
    221248
    222249{ TMetroLine }
     250
     251procedure TMetroLine.ConnectStation(Station: TMetroStation);
     252var
     253  Train: TMetroTrain;
     254begin
     255  Stations.Add(Station);
     256  Station.Lines.Add(Self);
     257  if Stations.Count = 1 then begin
     258    SetLength(TrackPoints, Length(TrackPoints) + 1);
     259    TrackPoints[High(TrackPoints)] := Station.Position;
     260  end else begin
     261     if Stations.Count > 1 then
     262       AddTrack(TMetroStation(Stations[Stations.Count - 2]).Position, Station.Position);
     263       // Place one train if at least two stations present
     264       if (Stations.Count = 2) then begin
     265         Train := Engine.Trains.GetUnusedTrain;
     266         if Assigned(Train) then begin
     267           Train.Line := Self;
     268           Train.TargetStation := TMetroStation(Stations.First);
     269           Trains.Add(Train);
     270         end;
     271       end;
     272  end;
     273end;
     274
     275procedure TMetroLine.DisconnectStation(Station: TMetroStation);
     276var
     277  I: Integer;
     278begin
     279  if (Stations.Count > 0) and (Stations.Last = Station) then begin
     280    Stations.Delete(Stations.Count - 1);
     281    Station.Lines.Remove(Self);
     282    if Stations.Count > 0 then begin
     283      while (Length(TrackPoints) > 0) and (not ComparePoint(TrackPoints[High(TrackPoints)], TMetroStation(Stations.Last).Position)) do
     284        SetLength(TrackPoints, Length(TrackPoints) - 1);
     285    end else SetLength(TrackPoints, 0);
     286    // Remove trains if less then two stations
     287    if Stations.Count < 2 then
     288      for I := Trains.Count - 1 downto 0 do begin
     289        TMetroTrain(Trains[I]).Line := nil;
     290        Trains.Delete(I);
     291      end;
     292  end;
     293end;
    223294
    224295procedure TMetroLine.AddTrack(P1, P2: TPoint);
     
    306377  Passengers.OwnsObjects := False;
    307378  Direction := 1;
     379  Line := nil;
    308380end;
    309381
     
    528600
    529601procedure TEngine.MouseMove(Position: TPoint);
     602var
     603  Station: TMetroStation;
    530604begin
    531605  LastMousePos := Position;
     606  if MouseHold then begin
     607    if Assigned(SelectedLine) then begin
     608      Station := GetStationOnPos(Position);
     609      if not Assigned(LastSelectedStation) and Assigned(Station) then begin
     610        if (SelectedLine.Stations.IndexOf(Station) = -1) then begin
     611          SelectedLine.ConnectStation(Station);
     612          SelectedStation := Station;
     613        end else
     614        if (SelectedLine.Stations.Count > 0) and (SelectedLine.Stations.Last = Station) then begin
     615          SelectedLine.DisconnectStation(Station);
     616          SelectedStation := TMetroStation(SelectedLine.Stations.Last);
     617        end else if (SelectedLine.Stations.Count > 0) and (SelectedLine.Stations.First = Station) then begin
     618          SelectedLine.ConnectStation(Station);
     619          SelectedStation := Station;
     620        end;
     621      end;
     622      LastSelectedStation := Station;
     623    end;
     624  end;
    532625end;
    533626
     
    537630  Line: TMetroLine;
    538631  I: Integer;
    539   Train: TMetroTrain;
    540 begin
     632begin
     633  MouseHold := False;
    541634  if Button = mbLeft then begin
     635    SelectedStation := nil;
     636
    542637    // Line color selection
    543638    for I := 0 to Lines.Count - 1 do
     
    547642          Exit;
    548643        end;
     644  end else
     645  if Button = mbRight then begin
     646    SelectedLine := nil;
     647  end;
     648end;
     649
     650procedure TEngine.MouseDown(Button: TMouseButton; Position: TPoint);
     651var
     652  Station: TMetroStation;
     653  Line: TMetroLine;
     654  I: Integer;
     655begin
     656  if Button = mbLeft then begin
     657    MouseHold := True;
     658    LastSelectedStation := nil;
    549659
    550660    // Station selection
    551661    Station := GetStationOnPos(Position);
    552     if Assigned(Station) then
    553       if Assigned(SelectedLine) and ((SelectedLine.Stations.IndexOf(Station) = -1) or
    554       (((SelectedLine.Stations.First = Station) and (SelectedLine.Stations.Last <> Station)))) then begin
    555         SelectedLine.Stations.Add(Station);
    556         Station.Lines.Add(SelectedLine);
    557         if SelectedLine.Stations.Count = 1 then begin
    558           SetLength(SelectedLine.TrackPoints, Length(SelectedLine.TrackPoints) + 1);
    559           SelectedLine.TrackPoints[High(SelectedLine.TrackPoints)] := Station.Position;
    560         end else
    561         if SelectedLine.Stations.Count > 1 then
    562           SelectedLine.AddTrack(TMetroStation(SelectedLine.Stations[SelectedLine.Stations.Count - 2]).Position, Station.Position);
    563         if (SelectedLine.Stations.Count = 2) then begin
    564           Train := Trains.GetUnusedTrain;
    565           if Assigned(Train) then begin
    566             Train.Line := SelectedLine;
    567             Train.TargetStation := TMetroStation(SelectedLine.Stations.First);
    568             SelectedLine.Trains.Add(Train);
    569           end;
    570         end;
    571         Exit
    572       end;
     662    if Assigned(Station) then begin
     663      SelectedStation := Station;
     664      for I := 0 to Lines.Count - 1 do
     665      if TMetroLine(Lines[I]).Stations.Count = 0 then
     666        SelectedLine := TMetroLine(Lines[I]);
     667    end;
    573668
    574669    // Line selection
     
    579674    end;
    580675  end;
    581   if Button = mbRight then begin
    582     SelectedLine := nil;
    583   end;
    584 end;
    585 
    586 procedure TEngine.MouseDown(Button: TMouseButton; Position: TPoint);
    587 begin
    588 
    589676end;
    590677
     
    611698
    612699  for I := 0 to 2 do begin
    613     NewLine := TMetroLine.Create;
    614     NewLine.Color := LineColors[Lines.Count];
    615     Lines.Add(NewLine);
     700    NewLine := Lines.AddNew;
    616701    NewTrain := TMetroTrain.Create;
    617702    Trains.Add(NewTrain);
     
    630715  Stations.Engine := Self;
    631716  Lines := TMetroLines.Create;
     717  Lines.Engine := Self;
    632718  Passengers := TMetroPassengers.Create;
    633719  Passengers.Engine := Self;
     
    636722  Trains := TMetroTrains.Create;
    637723  ImagePassenger := TImage.Create(nil);
     724  ImageLocomotive := TImage.Create(nil);
    638725  if FileExists(ImagePassengerName) then
    639726    ImagePassenger.Picture.LoadFromFile(ImagePassengerName);
     727  if FileExists(ImageLocomotiveName) then
     728    ImageLocomotive.Picture.LoadFromFile(ImageLocomotiveName);
    640729end;
    641730
     
    643732begin
    644733  Trains.Free;
     734  ImageLocomotive.Free;
    645735  ImagePassenger.Free;
    646736  View.Free;
     
    679769      Canvas.LineTo(TrackPoints[S]);
    680770    end;
    681     if (SelectedLine = TMetroLine(Lines[I])) and (Stations.Count > 0) then begin
    682       Canvas.MoveTo(TMetroStation(SelectedLine.Stations.Last).Position);
     771    if (SelectedLine = TMetroLine(Lines[I])) and Assigned(SelectedStation) then begin
     772      Canvas.MoveTo(SelectedStation.Position);
    683773      DrawLine(Canvas, LastMousePos);
    684774    end;
     
    744834
    745835  // Interface
     836  Text := IntToStr(ServedPassengerCount);
    746837  Canvas.Draw(Canvas.Width - 100, 20, ImagePassenger.Picture.Bitmap);
    747838  Canvas.Brush.Style := bsClear;
    748   Canvas.TextOut(Canvas.Width - 106 - Canvas.TextWidth(IntToStr(ServedPassengerCount)),
    749     25, IntToStr(ServedPassengerCount));
     839  Canvas.TextOut(Canvas.Width - 106 - Canvas.TextWidth(Text), 25, Text);
     840
     841  Text := IntToStr(Trains.GetUnusedCount);
     842  Canvas.Draw(Canvas.Width - 160, 20, ImageLocomotive.Picture.Bitmap);
     843  Canvas.Brush.Style := bsClear;
     844  Canvas.TextOut(Canvas.Width - 166 - Canvas.TextWidth(Text), 25, Text);
    750845
    751846  // Game over
  • trunk/UGeometric.pas

    r3 r4  
    1212function SubPoint(const P1, P2: TPoint): TPoint;
    1313function PointToLineDistance(const P, V, W: TPoint): Integer;
     14function ComparePoint(P1, P2: TPoint): Boolean;
    1415
    1516implementation
     
    6162end;
    6263
     64function ComparePoint(P1, P2: TPoint): Boolean;
     65begin
     66  Result := (P1.X = P2.X) and (P1.Y = P2.Y);
     67end;
     68
    6369
    6470end.
Note: See TracChangeset for help on using the changeset viewer.