- Timestamp:
- Mar 26, 2015, 12:09:02 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 3 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UEngine.pas
r3 r4 39 39 40 40 TMetroLine = class 41 Engine: TEngine; 41 42 Color: TColor; 42 43 Stations: TMetroStations; 43 44 Trains: TMetroTrains; 44 45 TrackPoints: array of TPoint; 46 procedure ConnectStation(Station: TMetroStation); 47 procedure DisconnectStation(Station: TMetroStation); 45 48 procedure AddTrack(P1, P2: TPoint); 46 49 function GetTrackLength: Integer; … … 53 56 54 57 TMetroLines = class(TObjectList) 58 Engine: TEngine; 59 function AddNew: TMetroLine; 55 60 function SearchByColor(Color: TColor): TMetroLine; 56 61 end; … … 78 83 TMetroTrains = class(TObjectList) 79 84 function GetUnusedTrain: TMetroTrain; 85 function GetUnusedCount: Integer; 80 86 end; 81 87 … … 109 115 private 110 116 LastMousePos: TPoint; 117 LastSelectedStation: TMetroStation; 118 MouseHold: Boolean; 111 119 LastNewStationTime: TDateTime; 112 120 LastNewPassengerTime: TDateTime; 113 121 ImagePassenger: TImage; 122 ImageLocomotive: TImage; 114 123 function GetExistStationShapes: TStationShapeSet; 115 124 function GetStationOnPos(Pos: TPoint): TMetroStation; … … 150 159 TrainSpeed = 3; 151 160 ImagePassengerName = 'Images/Passenger.png'; 161 ImageLocomotiveName = 'Images/Locomotive.png'; 152 162 TrainPassengerCount = 6; 153 163 StationMinDistance = 50; … … 179 189 if I < Count then Result := TMetroTrain(Items[I]) 180 190 else Result := nil; 191 end; 192 193 function TMetroTrains.GetUnusedCount: Integer; 194 var 195 I: Integer; 196 begin 197 Result := 0; 198 for I := 0 to Count - 1 do 199 if not Assigned(TMetroTrain(Items[I]).Line) then Inc(Result); 181 200 end; 182 201 … … 210 229 { TMetroLines } 211 230 231 function TMetroLines.AddNew: TMetroLine; 232 begin 233 Result := TMetroLine.Create; 234 Result.Color := LineColors[Count]; 235 Result.Engine := Engine; 236 Add(Result); 237 end; 238 212 239 function TMetroLines.SearchByColor(Color: TColor): TMetroLine; 213 240 var … … 221 248 222 249 { TMetroLine } 250 251 procedure TMetroLine.ConnectStation(Station: TMetroStation); 252 var 253 Train: TMetroTrain; 254 begin 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; 273 end; 274 275 procedure TMetroLine.DisconnectStation(Station: TMetroStation); 276 var 277 I: Integer; 278 begin 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; 293 end; 223 294 224 295 procedure TMetroLine.AddTrack(P1, P2: TPoint); … … 306 377 Passengers.OwnsObjects := False; 307 378 Direction := 1; 379 Line := nil; 308 380 end; 309 381 … … 528 600 529 601 procedure TEngine.MouseMove(Position: TPoint); 602 var 603 Station: TMetroStation; 530 604 begin 531 605 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; 532 625 end; 533 626 … … 537 630 Line: TMetroLine; 538 631 I: Integer; 539 Train: TMetroTrain; 540 begin 632 begin 633 MouseHold := False; 541 634 if Button = mbLeft then begin 635 SelectedStation := nil; 636 542 637 // Line color selection 543 638 for I := 0 to Lines.Count - 1 do … … 547 642 Exit; 548 643 end; 644 end else 645 if Button = mbRight then begin 646 SelectedLine := nil; 647 end; 648 end; 649 650 procedure TEngine.MouseDown(Button: TMouseButton; Position: TPoint); 651 var 652 Station: TMetroStation; 653 Line: TMetroLine; 654 I: Integer; 655 begin 656 if Button = mbLeft then begin 657 MouseHold := True; 658 LastSelectedStation := nil; 549 659 550 660 // Station selection 551 661 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; 573 668 574 669 // Line selection … … 579 674 end; 580 675 end; 581 if Button = mbRight then begin582 SelectedLine := nil;583 end;584 end;585 586 procedure TEngine.MouseDown(Button: TMouseButton; Position: TPoint);587 begin588 589 676 end; 590 677 … … 611 698 612 699 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; 616 701 NewTrain := TMetroTrain.Create; 617 702 Trains.Add(NewTrain); … … 630 715 Stations.Engine := Self; 631 716 Lines := TMetroLines.Create; 717 Lines.Engine := Self; 632 718 Passengers := TMetroPassengers.Create; 633 719 Passengers.Engine := Self; … … 636 722 Trains := TMetroTrains.Create; 637 723 ImagePassenger := TImage.Create(nil); 724 ImageLocomotive := TImage.Create(nil); 638 725 if FileExists(ImagePassengerName) then 639 726 ImagePassenger.Picture.LoadFromFile(ImagePassengerName); 727 if FileExists(ImageLocomotiveName) then 728 ImageLocomotive.Picture.LoadFromFile(ImageLocomotiveName); 640 729 end; 641 730 … … 643 732 begin 644 733 Trains.Free; 734 ImageLocomotive.Free; 645 735 ImagePassenger.Free; 646 736 View.Free; … … 679 769 Canvas.LineTo(TrackPoints[S]); 680 770 end; 681 if (SelectedLine = TMetroLine(Lines[I])) and (Stations.Count > 0) then begin682 Canvas.MoveTo( TMetroStation(SelectedLine.Stations.Last).Position);771 if (SelectedLine = TMetroLine(Lines[I])) and Assigned(SelectedStation) then begin 772 Canvas.MoveTo(SelectedStation.Position); 683 773 DrawLine(Canvas, LastMousePos); 684 774 end; … … 744 834 745 835 // Interface 836 Text := IntToStr(ServedPassengerCount); 746 837 Canvas.Draw(Canvas.Width - 100, 20, ImagePassenger.Picture.Bitmap); 747 838 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); 750 845 751 846 // Game over -
trunk/UGeometric.pas
r3 r4 12 12 function SubPoint(const P1, P2: TPoint): TPoint; 13 13 function PointToLineDistance(const P, V, W: TPoint): Integer; 14 function ComparePoint(P1, P2: TPoint): Boolean; 14 15 15 16 implementation … … 61 62 end; 62 63 64 function ComparePoint(P1, P2: TPoint): Boolean; 65 begin 66 Result := (P1.X = P2.X) and (P1.Y = P2.Y); 67 end; 68 63 69 64 70 end.
Note:
See TracChangeset
for help on using the changeset viewer.