Changeset 95 for trunk/UEngine.pas
- Timestamp:
- Sep 28, 2022, 12:23:17 AM (20 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UEngine.pas
r94 r95 7 7 uses 8 8 {$IFDEF DARWIN}MacOSAll, CocoaAll, CocoaUtils,{$ENDIF} 9 Classes, SysUtils, Graphics, Controls, ExtCtrls, Math, DateUtils, URegistry,10 U MetaCanvas, Generics.Collections, Generics.Defaults, UMenu, UControls,11 U MetroPassenger, UColors, UView, URiver, UTrack, UCity, UGeometric;9 Classes, SysUtils, Graphics, Controls, ExtCtrls, Math, DateUtils, Types, 10 URegistry, UMetaCanvas, Generics.Collections, Generics.Defaults, UMenu, 11 UControls, UMetroPassenger, UColors, UView, URiver, UTrack, UCity, UGeometric; 12 12 13 13 type … … 148 148 end; 149 149 150 TGameState = (gsNotStarted, gsRunning, gsPaused, gsGameOver, gsMenu );150 TGameState = (gsNotStarted, gsRunning, gsPaused, gsGameOver, gsMenu, gsNewWeek); 151 151 152 152 { TEngine } … … 187 187 procedure DrawTrains(Canvas: TCanvas); 188 188 procedure DrawGameOver(Canvas: TCanvas; CanvasSize: TPoint); 189 procedure DrawNew Train(Canvas: TCanvas; CanvasSize: TPoint);189 procedure DrawNewWeek(Canvas: TCanvas; CanvasSize: TPoint); 190 190 procedure DrawStationPassengerOverload(Canvas: TCanvas); 191 191 procedure DrawLines(Canvas: TCanvas); … … 210 210 procedure ButtonPause(Sender: TObject); 211 211 procedure ButtonFastForward(Sender: TObject); 212 procedure ButtonNewTrain(Sender: TObject); 212 213 procedure ButtonBackClick(Sender: TObject); 213 214 procedure DarkModeChanged(Sender: TObject); … … 217 218 procedure InitCities; 218 219 public 220 Week: Integer; 219 221 Colors: TColors; 220 222 Passengers: TMetroPassengers; … … 241 243 ImageFastForward: TImage; 242 244 ImageCarriage: TImage; 245 ImageNewTrain: TImage; 243 246 HighestServedPassengerCount: Integer; 244 247 HighestServedDaysCount: Integer; … … 320 323 SNoOldStationToConnectNew = 'No old line station to connect new station'; 321 324 SStationWithoutMapStation = 'Station have to have MapStation'; 325 SNewTrain = 'You get a new train for your metro'; 326 SWeek = 'week'; 327 STrain = 'Train'; 328 SPlay = 'Play'; 329 SOptions = 'Options'; 330 SExit = 'Exit'; 331 SBigMetro = 'Big Metro'; 332 SDarkMode = 'Dark mode'; 333 SLanguage = 'Language'; 334 SCzech = 'Czech'; 335 SEnglish = 'English'; 336 SBack = 'Back'; 337 SAutomatic = 'Automatic'; 338 SFullScreen = 'Full screen'; 339 SContinue = 'Continue'; 340 SRestart = 'Try again'; 322 341 323 342 // Cities … … 1666 1685 end; 1667 1686 1687 procedure TEngine.ButtonNewTrain(Sender: TObject); 1688 begin 1689 Trains.AddNew; 1690 if Random < 0.2 then Carriages.AddNew; 1691 State := gsRunning; 1692 Redraw; 1693 end; 1694 1668 1695 procedure TEngine.MenuItemGameContinue(Sender: TObject); 1669 1696 begin … … 1832 1859 I: Integer; 1833 1860 Y: Integer; 1861 IsDay: Boolean; 1834 1862 const 1835 1863 ClockSize = 20; 1836 1864 begin 1837 if (Time / OneHour > 6) and (Time / OneHour < 18) then begin 1865 IsDay := (((Time / OneHour) mod 24) > 6) and (((Time / OneHour) mod 24) < 18); 1866 if IsDay then begin 1838 1867 Canvas.Brush.Style := bsSolid; 1839 1868 Canvas.Brush.Color := Colors.Background; … … 1850 1879 Canvas.Brush.Style := bsClear; 1851 1880 1852 if (Time / OneHour > 6) and (Time / OneHour < 18)then begin1881 if IsDay then begin 1853 1882 Canvas.Pen.Color := Colors.Text; 1854 1883 end else begin … … 1899 1928 procedure TEngine.DrawTrains(Canvas: TCanvas); 1900 1929 var 1901 I: Integer;1902 1930 P: Integer; 1903 1931 Pos: TPoint; … … 1978 2006 Brush.Color := Self.Colors.Background; 1979 2007 1980 Y := 100;2008 Y := ScaleY(100, 96); 1981 2009 Font.Size := 40; 1982 2010 TextOut((CanvasSize.X - TextWidth(SGameOver)) div 2, Y, SGameOver); … … 1991 2019 Y := Y + Round(TextHeight(SGameOverStatistic) * 1.1); 1992 2020 1993 Y := Y + 16;2021 Y := Y + ScaleY(16, 96); 1994 2022 1995 2023 // Calculate new highest score … … 2010 2038 end; 2011 2039 2012 procedure TEngine.DrawNewTrain(Canvas: TCanvas; CanvasSize: TPoint); 2013 begin 2014 2040 procedure TEngine.DrawNewWeek(Canvas: TCanvas; CanvasSize: TPoint); 2041 var 2042 Text: string; 2043 TextSize: TSize; 2044 Y: Integer; 2045 X: Integer; 2046 const 2047 ImageZoom = 4; 2048 begin 2049 with Canvas do begin 2050 Pen.Color := Self.Colors.Text; 2051 Pen.Style := psSolid; 2052 Pen.Width := ScaleX(2, 96); 2053 Brush.Color := Self.Colors.Background2; 2054 Brush.Style := bsSolid; 2055 Rectangle(Bounds(CanvasSize.X div 4, CanvasSize.Y div 4, CanvasSize.X div 2, CanvasSize.Y div 2)); 2056 Pen.Style := psClear; 2057 X := CanvasSize.X div 4 + ScaleX(10, 96); 2058 Y := CanvasSize.Y div 4 + ScaleX(10, 96); 2059 2060 Text := IntToStr(Week) + '. ' + SWeek; 2061 Font.Size := 30; 2062 Font.Color := Self.Colors.Text; 2063 TextSize := TextExtent(Text); 2064 TextOut(X, Y, Text); 2065 Y := Y + Round(1.1 * TextSize.Height); 2066 2067 Text := SNewTrain; 2068 Font.Size := 14; 2069 Font.Color := Self.Colors.Text; 2070 TextSize := TextExtent(Text); 2071 TextOut(X, Y, Text); 2072 Y := Y + Round(1.1 * TextSize.Height); 2073 2074 ImageNewTrain.Canvas := Canvas; 2075 ImageNewTrain.Bounds := Bounds((CanvasSize.X - ImageLocomotive.Bitmap.Width * ImageZoom) div 2, 2076 (CanvasSize.Y - ImageLocomotive.Bitmap.Height * ImageZoom) div 2, 2077 ImageLocomotive.Bitmap.Width * ImageZoom, 2078 ImageLocomotive.Bitmap.Height * ImageZoom); 2079 ImageNewTrain.Paint; 2080 2081 Y := (CanvasSize.Y - ImageLocomotive.Bitmap.Height * ImageZoom) div 2 + 2082 Round(ImageLocomotive.Bitmap.Height * ImageZoom * 1.1); 2083 2084 Text := STrain; 2085 Font.Size := 20; 2086 Font.Color := Self.Colors.Text; 2087 TextSize := TextExtent(Text); 2088 TextOut((CanvasSize.X - TextSize.Width) div 2, Y, Text); 2089 end; 2015 2090 end; 2016 2091 … … 2280 2355 Redraw; // Redraw on every tick because engine time is changed so clock should be redrawn 2281 2356 2282 // Add new trains 2283 if (Time - LastNewWeekTime) > NewTrainPeriod then begin 2284 LastNewWeekTime := Time; 2285 Trains.AddNew; 2286 if Random < 0.2 then Carriages.AddNew; 2287 // TODO: Show notification screen with confirmation 2288 Redraw; 2289 end; 2290 2291 // Add new shape 2292 if (Time - LastNewShapeTime) > NewShapePeriod then begin 2293 LastNewShapeTime := Time; 2294 if ShapeCount <= Integer(High(TStationShape)) then Inc(ShapeCount); 2295 Redraw; 2296 end; 2297 2298 // Add new stations 2299 if (Time - LastNewStationTime) > NewStationPeriod then begin 2300 LastNewStationTime := Time; 2301 Stations.AddNew; 2302 ResizeView; 2303 Redraw; 2304 end; 2305 2306 // Add new passengers 2307 if (Time - LastNewPassengerTime) > NewPassengerPeriod then begin 2308 LastNewPassengerTime := Time; 2357 // Add new trains 2358 if (Time - LastNewWeekTime) > NewTrainPeriod then begin 2359 LastNewWeekTime := Time; 2360 Inc(Week); 2361 State := gsNewWeek; 2362 Redraw; 2363 end; 2364 2365 // Add new shape 2366 if (Time - LastNewShapeTime) > NewShapePeriod then begin 2367 LastNewShapeTime := Time; 2368 if ShapeCount <= Integer(High(TStationShape)) then Inc(ShapeCount); 2369 Redraw; 2370 end; 2371 2372 // Add new stations 2373 if (Time - LastNewStationTime) > NewStationPeriod then begin 2374 LastNewStationTime := Time; 2375 Stations.AddNew; 2376 ResizeView; 2377 Redraw; 2378 end; 2379 2380 // Add new passengers 2381 if (Time - LastNewPassengerTime) > NewPassengerPeriod then begin 2382 LastNewPassengerTime := Time; 2383 for MapStation in Stations do 2384 with MapStation do 2385 if Random < NewPassengerProbability then begin 2386 Passenger := Self.Passengers.AddNew; 2387 Passenger.Shape := TStationShape(Random(Integer(ShapeCount))); 2388 Passengers.Add(Passenger); 2389 2390 // Passenger is not allowed to have same shape 2391 while (Passenger.Shape = Shape) or 2392 not (Passenger.Shape in GetExistStationShapes) do 2393 Passenger.Shape := TStationShape((Integer(Passenger.Shape) + 1) mod Integer(ShapeCount)); 2394 Redraw; 2395 end; 2396 end; 2397 2398 // Check station passenger overload state 2309 2399 for MapStation in Stations do 2310 with MapStation do 2311 if Random < NewPassengerProbability then begin 2312 Passenger := Self.Passengers.AddNew; 2313 Passenger.Shape := TStationShape(Random(Integer(ShapeCount))); 2314 Passengers.Add(Passenger); 2315 2316 // Passenger is not allowed to have same shape 2317 while (Passenger.Shape = Shape) or 2318 not (Passenger.Shape in GetExistStationShapes) do 2319 Passenger.Shape := TStationShape((Integer(Passenger.Shape) + 1) mod Integer(ShapeCount)); 2320 Redraw; 2321 end; 2322 end; 2323 2324 // Check station passenger overload state 2325 for MapStation in Stations do 2326 with MapStation do begin 2327 if Passengers.Count > MaxWaitingPassengers then begin 2328 OverloadDuration := OverloadDuration + (FTime - FLastTime); 2329 if OverloadDuration > MaxPassengersOveloadTime then 2330 OverloadDuration := MaxPassengersOveloadTime; 2331 if OverloadDuration < MaxPassengersOveloadTime then Redraw; 2332 end; 2333 if Passengers.Count <= MaxWaitingPassengers then begin 2334 if OverloadDuration > 0 then Redraw; 2335 OverloadDuration := OverloadDuration - (FTime - FLastTime); 2336 if OverloadDuration < 0 then begin 2337 OverloadDuration := 0; 2338 end; 2339 end; 2340 end; 2341 2342 TrainMovement; 2343 2344 // Game over 2345 for MapStation in Stations do 2346 with MapStation do begin 2347 if OverloadDuration >= MaxPassengersOveloadTime then begin 2348 State := gsGameOver; 2349 Redraw; 2350 end; 2351 end; 2352 2400 with MapStation do begin 2401 if Passengers.Count > MaxWaitingPassengers then begin 2402 OverloadDuration := OverloadDuration + (FTime - FLastTime); 2403 if OverloadDuration > MaxPassengersOveloadTime then 2404 OverloadDuration := MaxPassengersOveloadTime; 2405 if OverloadDuration < MaxPassengersOveloadTime then Redraw; 2406 end; 2407 if Passengers.Count <= MaxWaitingPassengers then begin 2408 if OverloadDuration > 0 then Redraw; 2409 OverloadDuration := OverloadDuration - (FTime - FLastTime); 2410 if OverloadDuration < 0 then begin 2411 OverloadDuration := 0; 2412 end; 2413 end; 2414 end; 2415 2416 TrainMovement; 2417 2418 // Game over 2419 for MapStation in Stations do 2420 with MapStation do begin 2421 if OverloadDuration >= MaxPassengersOveloadTime then begin 2422 State := gsGameOver; 2423 Redraw; 2424 end; 2425 end; 2353 2426 end; 2354 2427 LastTickTime := Now; … … 2425 2498 Menu.MouseUp(Button, Position); 2426 2499 Redraw; 2427 end else begin 2428 // Back button 2429 if ButtonBack.Bounds.Contains(Position) then begin 2430 if Assigned(ButtonBack.OnClick) then 2431 ButtonBack.OnClick(ButtonBack); 2432 end; 2433 2434 // Pause button 2435 if ImagePause.Bounds.Contains(Position) then begin 2436 if Assigned(ImagePause.OnClick) then 2437 ImagePause.OnClick(ImagePause); 2438 end; 2439 2440 // Play button 2441 if ImagePlay.Bounds.Contains(Position) then begin 2442 if Assigned(ImagePlay.OnClick) then 2443 ImagePlay.OnClick(ImagePlay); 2444 end; 2445 2446 // Fast forward button 2447 if ImageFastForward.Bounds.Contains(Position) then begin 2448 if Assigned(ImageFastForward.OnClick) then 2449 ImageFastForward.OnClick(ImageFastForward); 2450 end; 2500 end else 2501 if State = gsNewWeek then begin 2502 ImageNewTrain.MouseUp(Position); 2503 end 2504 else 2505 if State = gsRunning then begin 2506 ButtonBack.MouseUp(Position); 2507 ImagePause.MouseUp(Position); 2508 ImagePlay.MouseUp(Position); 2509 ImageFastForward.MouseUp(Position); 2451 2510 2452 2511 // Place selected train if focused track … … 2608 2667 KeyF3 = 114; 2609 2668 KeyF4 = 115; 2669 KeyF5 = 116; 2610 2670 begin 2611 2671 if Key = KeyEsc then begin … … 2635 2695 Redraw; 2636 2696 end; 2697 end else 2698 if Key = KeyF5 then begin 2699 if State = gsRunning then begin 2700 State := gsNewWeek; 2701 Redraw; 2702 end; 2637 2703 end; 2638 2704 {$ENDIF} … … 2647 2713 procedure TEngine.Clear; 2648 2714 begin 2715 Week := 0; 2649 2716 Trains.Clear; 2650 2717 Passengers.Clear; … … 2673 2740 end; 2674 2741 2675 for I := 0 to 8do begin2742 for I := 0 to 2 do begin 2676 2743 Lines.AddNew; 2677 2744 NewTrain := TMetroTrain.Create; … … 2754 2821 ImageFastForward := TImage.Create; 2755 2822 ImageFastForward.OnClick := ButtonFastForward; 2823 ImageNewTrain := TImage.Create; 2824 ImageNewTrain.OnClick := ButtonNewTrain; 2756 2825 //if FileExists(ImagePassengerName) then 2757 2826 // ImagePassenger.Picture.LoadFromFile(ImagePassengerName); … … 2770 2839 FreeAndNil(Trains); 2771 2840 FreeAndNil(Carriages); 2841 FreeAndNil(ImageNewTrain); 2772 2842 FreeAndNil(ImagePlay); 2773 2843 FreeAndNil(ImageFastForward); … … 2809 2879 MetaCanvas.DrawTo(Canvas); 2810 2880 2811 if State <> gsMenu then begin2812 DrawGameControls(Canvas, CanvasSize);2813 end;2814 2815 2881 // Game over 2816 2882 if State = gsGameOver then … … 2820 2886 if State = gsMenu then begin 2821 2887 Menu.Paint(Canvas, CanvasSize); 2888 end else 2889 if State = gsNewWeek then begin 2890 DrawNewWeek(Canvas, CanvasSize); 2891 end else 2892 if State in [gsRunning, gsPaused] then begin 2893 DrawGameControls(Canvas, CanvasSize); 2822 2894 end; 2823 2895
Note:
See TracChangeset
for help on using the changeset viewer.