Changeset 11 for trunk/UGame.pas


Ignore:
Timestamp:
Oct 4, 2019, 11:57:05 PM (5 years ago)
Author:
chronos
Message:
  • Added: Configurable speed of animations.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r10 r11  
    66
    77uses
    8   Classes, SysUtils, Dialogs, fgl, Graphics, Types, Forms, Math;
     8  Classes, SysUtils, Dialogs, fgl, Graphics, Types, Forms, Math, DateUtils;
    99
    1010type
     
    3030  TGame = class
    3131  private
     32    FMoving: Boolean;
    3233    FOnChange: TNotifyEvent;
    3334    FRunning: Boolean;
     
    4344    Cells: array of array of TCell;
    4445    TopScore: Integer;
     46    AnimationDuration: Integer;
    4547    procedure GameOver;
    4648    function FillRandomCell: Integer;
     
    6062    property Running: Boolean read FRunning write FRunning;
    6163    property OnChange: TNotifyEvent read FOnChange write FOnChange;
     64    property Moving: Boolean read FMoving;
    6265  end;
    6366
     
    318321  MovedCount: Integer;
    319322  X, Y: Integer;
    320   Step: Integer;
    321323  I: Integer;
    322 const
    323   StepCount = 10;
    324   AnimationDuration = 30;
    325 begin
     324  StartTime: TDateTime;
     325  EndTime: TDateTime;
     326  Time: TDateTime;
     327  Part: Double;
     328begin
     329  FMoving := True;
    326330  //Diff := DirectionDiff[Direction];
    327331  case Direction of
     
    397401      Inc(PI.Y);
    398402    end;
    399     for Step := 0 to StepCount - 2 do begin
     403
     404    // Animate cell move
     405    StartTime := Now;
     406    EndTime := StartTime + AnimationDuration / 300 * OneSecond / Size.X;
     407    if AnimationDuration > 0 then
     408    repeat
     409      Time := Now;
     410      Part := (Time - StartTime) / (EndTime - StartTime);
    400411      for Y := 0 to Size.Y - 1 do
    401412        for X := 0 to Size.X - 1 do begin
    402413          if Cells[Y, X].Moving then
    403             Cells[Y, X].Shift := Point(Trunc(Step / StepCount * MoveDirection.X * 100),
    404               Trunc(Step / StepCount * MoveDirection.Y * 100));
     414            Cells[Y, X].Shift := Point(Trunc(Part * MoveDirection.X * 100),
     415              Trunc(Part * MoveDirection.Y * 100));
    405416        end;
    406417      DoChange;
    407418      Application.ProcessMessages;
    408       Sleep(AnimationDuration div StepCount);
    409     end;
     419      Sleep(10);
     420    until Time > EndTime;
     421
     422    // Set final cell values
    410423    for Y := 0 to Size.Y - 1 do
    411424      for X := 0 to Size.X - 1 do begin
     
    416429  end;
    417430  Result := MovedCount;
     431  FMoving := False;
    418432end;
    419433
     
    434448constructor TGame.Create;
    435449begin
     450  AnimationDuration := 30;
    436451end;
    437452
Note: See TracChangeset for help on using the changeset viewer.