Changeset 22 for trunk/UCore.pas


Ignore:
Timestamp:
Sep 28, 2011, 3:10:01 PM (13 years ago)
Author:
george
Message:
  • Added: Definition class for surface matter types.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UCore.pas

    r21 r22  
    2424  TPlayer = class;
    2525
    26   TSurfaceMatter = (smNothing, smDirt1, smDirt2, smRock, smCannon,
    27     smBullet1, smBullet2,
    28     smPlayer1H, smPlayer1L, smPlayer2H, smPlayer2L,
    29     smPlayer3H, smPlayer3L, smPlayer4H, smPlayer4L);
    30 
    3126  TRealPoint = record
    3227    X, Y: Real;
     
    6156    destructor Destroy; override;
    6257  end;
     58
     59  TMatterIndex = (miSpace, miDirt1, miDirt2, miRock, miBullet1, miBullet2,
     60    miPlayer1Cannon, miPlayer1Home, miPlayer1TankBody, miPlayer1TankBody2,
     61    miPlayer2Cannon, miPlayer2Home, miPlayer2TankBody, miPlayer2TankBody2,
     62    miPlayer3Cannon, miPlayer3Home, miPlayer3TankBody, miPlayer3TankBody2,
     63    miPlayer4Cannon, miPlayer4Home, miPlayer4TankBody, miPlayer4TankBody2,
     64    miPlayer5Cannon, miPlayer5Home, miPlayer5TankBody, miPlayer5TankBody2,
     65    miPlayer6Cannon, miPlayer6Home, miPlayer6TankBody, miPlayer6TankBody2,
     66    miPlayer7Cannon, miPlayer7Home, miPlayer7TankBody, miPlayer7TankBody2,
     67    miPlayer8Cannon, miPlayer8Home, miPlayer8TankBody, miPlayer8TankBody2);
     68
     69  TMatterKind = (mkSpace, mkDirt, mkRock, mkCannon, mkBullet, mkTankBody,
     70    mkHome);
    6371
    6472  { TPlayer }
     
    96104    procedure Paint;
    97105    procedure PlaceHouse;
    98     function CheckColision: TSurfaceMatter;
     106    function CheckColision: TMatterIndex;
    99107    procedure ShowTank;
    100108    procedure HideTank;
     
    105113  end;
    106114
     115  { TMatter }
     116
     117  TMatter = class
     118    Kind: TMatterKind;
     119    Player: Integer;
     120    Color: TColor;
     121  end;
     122
    107123  { TWorld }
    108124
     
    111127    function GetSize: TMatrixByteIndex;
    112128    procedure SetSize(const AValue: TMatrixByteIndex);
     129    procedure InitMatter;
    113130  public
    114131    Engine: TEngine;
    115132    Surface: TMatrixByte;
     133    Matter: TListObject; // TListObject<TMatter>
    116134    procedure Generate;
    117135    constructor Create;
     
    183201
    184202const
    185   SurfaceMatterColors: array[TSurfaceMatter] of TColor = (clBlack, $0756b0,
    186     $2170c3, TColor($9a9a9a), clYellow, clRed, clRed,
    187     TColor($00ff00), TColor($00a000), TColor($ff2c2c), TColor($b60000),
    188     TColor($0000ff), TColor($0000a0), TColor($ff2cff), TColor($b600b6));
    189203  DirectionToDelta: array[0..7] of TPoint =
    190204    ((X: 0; Y: -1), (X: 1; Y: -1), (X: 1; Y: 0), (X: 1; Y: 1),
     
    273287end;
    274288
     289procedure TWorld.InitMatter;
     290begin
     291  // Space
     292  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     293    Kind := mkSpace;
     294    Color := clBlack;
     295    Player := -1;
     296  end;
     297  // Dirt1
     298  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     299    Kind := mkDirt;
     300    Color := $0756b0;
     301    Player := -1;
     302  end;
     303  // Dirt2
     304  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     305    Kind := mkDirt;
     306    Color := $2170c3;
     307    Player := -1;
     308  end;
     309  // Rock
     310  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     311    Kind := mkRock;
     312    Color := TColor($9a9a9a);
     313    Player := -1;
     314  end;
     315  // Bullet1
     316  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     317    Kind := mkBullet;
     318    Color := clRed;
     319    Player := -1;
     320  end;
     321  // Bullet2
     322  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     323    Kind := mkBullet;
     324    Color := clRed;
     325    Player := -1;
     326  end;
     327
     328  // Player 1 cannon
     329  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     330    Kind := mkCannon;
     331    Color := clYellow;
     332    Player := 0;
     333  end;
     334  // Player 1 home
     335  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     336    Kind := mkHome;
     337    Color := TColor($00ff00);
     338    Player := 0;
     339  end;
     340  // Player 1 body1
     341  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     342    Kind := mkTankBody;
     343    Color := TColor($00ff00);
     344    Player := 0;
     345  end;
     346  // Player 1 body2
     347  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     348    Kind := mkTankBody;
     349    Color := TColor($00a000);
     350    Player := 0;
     351  end;
     352
     353  // Player 2 cannon
     354  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     355    Kind := mkCannon;
     356    Color := clYellow;
     357    Player := 1;
     358  end;
     359  // Player 2 home
     360  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     361    Kind := mkHome;
     362    Color :=  TColor($ff2c2c);
     363    Player := 1;
     364  end;
     365  // Player 2 body1
     366  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     367    Kind := mkTankBody;
     368    Color := TColor($ff2c2c);
     369    Player := 1;
     370  end;
     371  // Player 2 body2
     372  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     373    Kind := mkTankBody;
     374    Color := TColor($b60000);
     375    Player := 1;
     376  end;
     377
     378  // Player 3 cannon
     379  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     380    Kind := mkCannon;
     381    Color := clYellow;
     382    Player := 2;
     383  end;
     384  // Player 3 home
     385  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     386    Kind := mkHome;
     387    Color :=  TColor($0000ff);
     388    Player := 2;
     389  end;
     390  // Player 3 body1
     391  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     392    Kind := mkTankBody;
     393    Color := TColor($0000ff);
     394    Player := 2;
     395  end;
     396  // Player 3 body2
     397  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     398    Kind := mkTankBody;
     399    Color := TColor($0000a0);
     400    Player := 2;
     401  end;
     402
     403  // Player 4 cannon
     404  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     405    Kind := mkCannon;
     406    Color := clYellow;
     407    Player := 3;
     408  end;
     409  // Player 4 home
     410  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     411    Kind := mkHome;
     412    Color :=  TColor($ff2cff);
     413    Player := 3;
     414  end;
     415  // Player 4 body1
     416  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     417    Kind := mkTankBody;
     418    Color := TColor($ff2cff);
     419    Player := 3;
     420  end;
     421  // Player 4 body2
     422  with TMatter(Matter.AddNew(TMatter.Create)) do begin
     423    Kind := mkTankBody;
     424    Color := TColor($b600b6);
     425    Player := 3;
     426  end;
     427end;
     428
    275429procedure TWorld.Generate;
    276430var
     
    282436    for X := 0 to Surface.Count.X - 1 do begin
    283437      if Random < 0.5 then
    284         Surface.ItemsXY[X, Y] := Byte(smDirt1) else
    285         Surface.ItemsXY[X, Y] := Byte(smDirt2);
     438        Surface.ItemsXY[X, Y] := Byte(miDirt1) else
     439        Surface.ItemsXY[X, Y] := Byte(miDirt2);
    286440    end;
    287441
     
    290444  for Y := 0 to Surface.Count.Y - 1 do begin
    291445    for X := 0 to Round(Distance) - 1 do begin
    292       Surface.ItemsXY[X, Y] := Byte(smRock);
     446      Surface.ItemsXY[X, Y] := Byte(miRock);
    293447    end;
    294448    Delta := (Random * 2 - 1) * 3 - (Distance / (0.1 * Surface.Count.X) * 2 - 1);
     
    300454  for Y := 0 to Surface.Count.Y - 1 do begin
    301455    for X := 0 to Round(Distance) - 1 do begin
    302       Surface.ItemsXY[Surface.Count.X - 1 - X, Y] := Byte(smRock);
     456      Surface.ItemsXY[Surface.Count.X - 1 - X, Y] := Byte(miRock);
    303457    end;
    304458    Delta := (Random * 2 - 1) * 3 - (Distance / (0.1 * Surface.Count.X) * 2 - 1);
     
    310464  for X := 0 to Surface.Count.X - 1 do begin
    311465    for Y := 0 to Round(Distance) - 1 do begin
    312       Surface.ItemsXY[X, Y] := Byte(smRock);
     466      Surface.ItemsXY[X, Y] := Byte(miRock);
    313467    end;
    314468    Delta := (Random * 2 - 1) * 3 - (Distance / (0.1 * Surface.Count.Y) * 2 - 1);
     
    320474  for X := 0 to Surface.Count.X - 1 do begin
    321475    for Y := 0 to Round(Distance) - 1 do begin
    322       Surface.ItemsXY[X, Surface.Count.Y - 1 - Y] := Byte(smRock);
     476      Surface.ItemsXY[X, Surface.Count.Y - 1 - Y] := Byte(miRock);
    323477    end;
    324478    Delta := (Random * 2 - 1) * 3 - (Distance / (0.1 * Surface.Count.Y) * 2 - 1);
     
    331485  NewSize: TMatrixByteIndex;
    332486begin
     487  Matter := TListObject.Create;
     488  InitMatter;
    333489  Surface := TMatrixByte.Create;
    334490  NewSize.X := 800;
     
    340496begin
    341497  Surface.Free;
     498  Matter.Free;
    342499  inherited Destroy;
    343500end;
     
    360517      PixelPtr := PixelRowPtr;
    361518      for X := 0 to Bitmap.Width - 1 do begin
    362         P := SurfaceMatterColors[TSurfaceMatter(
    363           Surface.ItemsXY[Trunc(X / Bitmap.Width * Surface.Count.X),
    364             Trunc(Y / Bitmap.Height * Surface.Count.Y)])];
     519        P := TMatter(Matter[Surface.ItemsXY[Trunc(X / Bitmap.Width * Surface.Count.X),
     520            Trunc(Y / Bitmap.Height * Surface.Count.Y)]]).Color;
    365521        PixelPtr^ := SwapBRComponent(P);
    366522        Inc(PByte(PixelPtr), BytePerPixel);
     
    378534var
    379535  Delta: TPoint;
    380   Matter: TSurfaceMatter;
     536  Matter: TMatterIndex;
    381537  NewBullet: TBullet;
    382538  I: Integer;
     
    408564    HideTank;
    409565    Matter := CheckColision;
    410     if (Matter = smDirt1) then Dig := not Dig;
    411     if (Matter = smNothing) or ((Matter = smDirt1) and (not Dig)) then begin
     566    if (Matter = miDirt1) then Dig := not Dig;
     567    if (Matter = miSpace) or ((Matter = miDirt1) and (not Dig)) then begin
    412568      Position := NewPosition;
    413569      Direction := NewDirection;
     
    474630  with TBullet(Bullets[I]), Engine.World.Surface do begin
    475631    Pos := Point(Trunc(Position.X), Trunc(Position.Y));
    476     if (ItemsXY[LastPos.X, LastPos.Y] = Byte(smBullet1)) or
    477       (ItemsXY[LastPos.X, LastPos.Y] = Byte(smBullet2)) then
    478       ItemsXY[LastPos.X, LastPos.Y] := Byte(smNothing);
     632    if (ItemsXY[LastPos.X, LastPos.Y] = Byte(miBullet1)) or
     633      (ItemsXY[LastPos.X, LastPos.Y] = Byte(miBullet2)) then
     634      ItemsXY[LastPos.X, LastPos.Y] := Byte(miSpace);
    479635    LastPos := Pos;
    480636
     
    491647    Pos := Point(Trunc(Position.X), Trunc(Position.Y));
    492648
    493     if (ItemsXY[Pos.X, Pos.Y] <> Byte(smNothing)) and
    494     (ItemsXY[Pos.X, Pos.Y] <> Byte(smBullet1)) and
    495     (ItemsXY[Pos.X, Pos.Y] <> Byte(smBullet2)) then begin
    496       if (ItemsXY[Pos.X, Pos.Y] = Byte(smDirt1)) or
    497         (ItemsXY[Pos.X, Pos.Y] = Byte(smDirt2)) then begin
    498           ItemsXY[Pos.X, Pos.Y] := Byte(smNothing);
     649    if (ItemsXY[Pos.X, Pos.Y] <> Byte(miSpace)) and
     650    (ItemsXY[Pos.X, Pos.Y] <> Byte(miBullet1)) and
     651    (ItemsXY[Pos.X, Pos.Y] <> Byte(miBullet2)) then begin
     652      if (ItemsXY[Pos.X, Pos.Y] = Byte(miDirt1)) or
     653        (ItemsXY[Pos.X, Pos.Y] = Byte(miDirt2)) then begin
     654          ItemsXY[Pos.X, Pos.Y] := Byte(miSpace);
    499655          if StopByDirt then begin
    500656            Explosion(LastPos, BulletExplosionRange);
     
    504660          end;
    505661        end else begin
    506           if (ItemsXY[Pos.X, Pos.Y] = Byte(smPlayer1L)) or (ItemsXY[Pos.X, Pos.Y] = Byte(smPlayer1H)) then
     662          if (ItemsXY[Pos.X, Pos.Y] = Byte(miPlayer1TankBody)) or
     663          (ItemsXY[Pos.X, Pos.Y] = Byte(miPlayer1TankBody2)) then
    507664            with TPlayer(Engine.Players[0]) do begin
    508665              Shield := Shield - 1 / ShieldSteps;
    509666            end;
    510           if (ItemsXY[Pos.X, Pos.Y] = Byte(smPlayer2L)) or (ItemsXY[Pos.X, Pos.Y] = Byte(smPlayer2H)) then
     667          if (ItemsXY[Pos.X, Pos.Y] = Byte(miPlayer2Tankbody)) or
     668          (ItemsXY[Pos.X, Pos.Y] = Byte(miPlayer2TankBody2)) then
    511669            with TPlayer(Engine.Players[1]) do begin
    512670              Shield := Shield - 1 / ShieldSteps;
    513671            end;
    514           if (ItemsXY[Pos.X, Pos.Y] = Byte(smPlayer3L)) or (ItemsXY[Pos.X, Pos.Y] = Byte(smPlayer3H)) then
     672          if (ItemsXY[Pos.X, Pos.Y] = Byte(miPlayer3TankBody)) or
     673          (ItemsXY[Pos.X, Pos.Y] = Byte(miPlayer3TankBody2)) then
    515674            with TPlayer(Engine.Players[2]) do begin
    516675              Shield := Shield - 1 / ShieldSteps;
    517676            end;
    518           if (ItemsXY[Pos.X, Pos.Y] = Byte(smPlayer4L)) or (ItemsXY[Pos.X, Pos.Y] = Byte(smPlayer4H)) then
     677          if (ItemsXY[Pos.X, Pos.Y] = Byte(miPlayer4TankBody)) or
     678          (ItemsXY[Pos.X, Pos.Y] = Byte(miPlayer4TankBody2)) then
    519679            with TPlayer(Engine.Players[3]) do begin
    520680              Shield := Shield - 1 / ShieldSteps;
     
    534694        Continue;
    535695      end;
    536     ItemsXY[Pos.X, Pos.Y] := Byte(smBullet1);
     696    ItemsXY[Pos.X, Pos.Y] := Byte(miBullet1);
    537697    //Engine.World.Surface.ItemsXY[LastPos.X, LastPos.Y] := Byte(smBullet2);
    538698    Engine.Redraw;
     
    552712//    FillRect(ScreenFrame);
    553713    Fill(CreateIndex(ScreenFrame.Left, ScreenFrame.Top),
    554       CreateIndex(ScreenFrame.Right - 1, ScreenFrame.Bottom - 1), SurfaceMatterColors[smRock]);
     714      CreateIndex(ScreenFrame.Right - 1, ScreenFrame.Bottom - 1),
     715      TMatter(Engine.World.Matter[Integer(miRock)]).Color);
    555716
    556717    with Engine.World do
     
    560721        YY := Y - ScreenFrame.Top - ((ScreenFrame.Bottom - ScreenFrame.Top) div 2) + Position.Y;
    561722        if (YY >= 0) and (YY < Surface.Count.Y) and (XX >= 0) and (XX < Surface.Count.X) then
    562           ItemsXY[X, Y] := SurfaceMatterColors[TSurfaceMatter(Surface.ItemsXY[XX, YY])];
     723          ItemsXY[X, Y] := TMatter(Engine.World.Matter[Surface.ItemsXY[XX, YY]]).Color;
    563724      end;
    564725
     
    592753      not (((Y = 0) or (Y = (HouseSize - 1))) and (X > ((HouseSize - DoorSize) div 2)) and
    593754      (X < ((HouseSize - 1 + DoorSize) div 2)))
    594       then Matter := Byte(smPlayer1H) + Id * 2
    595         else Matter := Byte(smNothing);
     755      then Matter := Byte(miPlayer1Cannon) + Id * 4
     756        else Matter := Byte(miSpace);
    596757      Engine.World.Surface.ItemsXY[House.Left + X,
    597758        House.Top + Y] := Matter;
     
    599760end;
    600761
    601 function TPlayer.CheckColision: TSurfaceMatter;
     762function TPlayer.CheckColision: TMatterIndex;
    602763var
    603764  X, Y: Integer;
    604765  XX, YY: Integer;
    605766begin
    606   Result := smNothing;
     767  Result := miSpace;
    607768  with Engine.World, TTank(Tanks[NewDirection]) do
    608769  for Y := 0 to Image.Count.Y - 1 do
     
    611772    YY := Y + NewPosition.Y - Image.Count.Y div 2;
    612773    if (Image.ItemsXY[X, Y] > 0) and
    613     (Surface.ItemsXY[XX, YY] <> Byte(smNothing)) then
     774    (Surface.ItemsXY[XX, YY] <> Byte(miSpace)) then
    614775    begin
    615       Result := smDirt1;
    616       if (Surface.ItemsXY[XX, YY] <> Byte(smDirt1)) and
    617       (Surface.ItemsXY[XX, YY] <> Byte(smDirt2)) then
     776      Result := miDirt1;
     777      if (Surface.ItemsXY[XX, YY] <> Byte(miDirt1)) and
     778      (Surface.ItemsXY[XX, YY] <> Byte(miDirt2)) then
    618779      begin
    619         Result := TSurfaceMatter(Surface.ItemsXY[XX, YY]);
     780        Result := TMatterIndex(Surface.ItemsXY[XX, YY]);
    620781        Exit;
    621782      end;
     
    709870    Image.Count := Image.CreateIndex(7, 7);
    710871    for I := 0 to 3 do
    711       Image[3, I] := Byte(smCannon);
     872      Image[3, I] := Byte(miPlayer1Cannon) + Id * 4;
    712873    for I := 1 to 6 do begin
    713       Image[1, I] := Byte(smPlayer1H) + Id * 2;
    714       Image[5, I] := Byte(smPlayer1H) + Id * 2;
     874      Image[1, I] := Byte(miPlayer1TankBody) + Id * 4;
     875      Image[5, I] := Byte(miPlayer1TankBody) + Id * 4;
    715876    end;
    716877    for I := 2 to 5 do begin
    717       Image[2, I] := Byte(smPlayer1H) + Id * 2 + 1;
    718       Image[4, I] := Byte(smPlayer1H) + Id * 2 + 1;
    719     end;
    720     Image[3, 4] := Byte(smPlayer1H) + Id * 2 + 1;
    721     Image[3, 5] := Byte(smPlayer1H) + Id * 2 + 1;
     878      Image[2, I] := Byte(miPlayer1TankBody2) + Id * 4;
     879      Image[4, I] := Byte(miPlayer1TankBody2) + Id * 4;
     880    end;
     881    Image[3, 4] := Byte(miPlayer1TankBody2) + Id * 4;
     882    Image[3, 5] := Byte(miPlayer1TankBody2) + Id * 4;
    722883  end;
    723884  Tanks.Add(NewTank);
     
    727888    Image.Count := Image.CreateIndex(7, 7);
    728889    for I := 0 to 2 do
    729       Image[3 + I, 3 - I] := Byte(smCannon);
     890      Image[3 + I, 3 - I] := Byte(miPlayer1Cannon) + Id * 4;
    730891    for I := 0 to 3 do begin
    731       Image[I, 3 - I] := Byte(smPlayer1H) + Id * 2;
    732       Image[3 + I, 6 - I] := Byte(smPlayer1H) + Id * 2;
     892      Image[I, 3 - I] := Byte(miPlayer1TankBody) + Id * 4;
     893      Image[3 + I, 6 - I] := Byte(miPlayer1TankBody) + Id * 4;
    733894    end;
    734895    for I := 0 to 2 do begin
    735       Image[1 + I, 3 - I] := Byte(smPlayer1H) + Id * 2 + 1;
    736       Image[3 + I, 5 - I] := Byte(smPlayer1H) + Id * 2 + 1;
    737     end;
    738     Image[3, 2] := Byte(smPlayer1H) + Id * 2 + 1;
    739     Image[2, 3] := Byte(smPlayer1H) + Id * 2 + 1;
    740     Image[2, 4] := Byte(smPlayer1H) + Id * 2 + 1;
    741     Image[3, 4] := Byte(smPlayer1H) + Id * 2 + 1;
    742     Image[4, 3] := Byte(smPlayer1H) + Id * 2 + 1;
     896      Image[1 + I, 3 - I] := Byte(miPlayer1TankBody2) + Id * 4;
     897      Image[3 + I, 5 - I] := Byte(miPlayer1TankBody2) + Id * 4;
     898    end;
     899    Image[3, 2] := Byte(miPlayer1TankBody2) + Id * 4;
     900    Image[2, 3] := Byte(miPlayer1TankBody2) + Id * 4;
     901    Image[2, 4] := Byte(miPlayer1TankBody2) + Id * 4;
     902    Image[3, 4] := Byte(miPlayer1TankBody2) + Id * 4;
     903    Image[4, 3] := Byte(miPlayer1TankBody2) + Id * 4;
    743904  end;
    744905  Tanks.Add(NewTank);
Note: See TracChangeset for help on using the changeset viewer.