SHARE
TWEET

Untitled

a guest Sep 13th, 2017 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. unit GameEnity;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils,  Graphics, GameUtilityUnit, TimerUnit, typinfo;
  9.  
  10.  
  11. type
  12. TEntity = class
  13.    Speed: Real;
  14.    Image: TBitMap;
  15.    Location: TLocation;
  16.    Direction: TDirection;
  17.    Request: TDirection;
  18.    Spawn: TLocation;
  19.    Exclamation: TBitmap;
  20.    constructor Create(X, Y: Integer ; _Game:TObject); Virtual;
  21.    procedure Move(_Direction: TDirection); Virtual;
  22.    procedure Update; Virtual;
  23.    procedure Approach(_Direction: TDirection);
  24.    function DistanceTo(_Location: TLocation):Real;
  25.    function IsMoving: Boolean;
  26.    function GetLocation: TLocation;
  27.    procedure Paint(Canvas: TCanvas); Virtual;
  28.    procedure Respawn; Virtual;
  29.    end;
  30.  
  31.    TEnemyNature = (Rusher, Ambusher, Randomer, Camper);
  32.  
  33. TEnemy = class(TEntity)
  34.    Last: TDirection;
  35.    Nature: TEnemyNature;
  36.    LogicTimer: TTimer;
  37.    Ways: Integer;
  38.    constructor Create(X, Y: Integer; _Nature: TEnemyNature;_Game:TObject);
  39.    procedure Update; Override;
  40.    procedure Move(_Direction: TDirection); Override;
  41.    function OnXRoad: Boolean;
  42.    procedure NewPath;
  43.    end;
  44.  
  45. TPlayer = class(TEntity)
  46.   Images: TPlayerImages;
  47.   constructor Create(X, Y: Integer;_Game:TObject); Override;
  48.   procedure Paint(Canvas: TCanvas); Override;
  49.   procedure Update; Override;
  50.   end;
  51.   TEnemies = array of TEnemy;
  52.  
  53.  
  54. implementation
  55.  
  56. uses GameMain;
  57.  
  58. var
  59.   Game: TGame;
  60.  
  61. procedure TEntity.Paint(Canvas: TCanvas);
  62. var
  63.   X, Y: Integer;
  64. begin
  65.   X := Round(Location.X);
  66.   Y := Round(Location.Y);
  67.   if Image <> nil then
  68.   begin
  69.     Canvas.Draw(X, Y, Image);
  70.     if Game.Killing and (Self is TEnemy) then
  71.     begin
  72.       Canvas.Draw(X + 8, Y - 8, Exclamation);
  73.     end;
  74.   end
  75.   else
  76.   begin
  77.     Canvas.Rectangle(X , Y, X + SQUARE, Y + SQUARE);
  78.   end;
  79. end;
  80.  
  81. constructor TEntity.Create(X, Y: Integer;_Game:TObject);
  82. begin
  83.   Location := ConvertToLocation(X*SQUARE, Y*SQUARE);
  84.   Spawn := Location;
  85.   Direction := NONE;
  86.   Request := NONE;
  87.   Speed := 0.7;
  88.   Image := nil;
  89.   Game := _Game as TGame;
  90. end;
  91.  
  92. procedure TEntity.Move(_Direction: TDirection);
  93. begin
  94.   Request := _Direction;
  95. end;
  96.  
  97. function TEntity.IsMoving: Boolean;
  98. begin
  99.   Result := Direction <> NONE;
  100. end;
  101.  
  102. function TEntity.GetLocation: TLocation;
  103. begin
  104.   Result := Location;
  105. end;
  106.  
  107. procedure TEntity.Update;
  108. begin
  109.   if Request <> None then
  110.   begin
  111.     if ((Round(Location.X) mod SQUARE) < 1) and ((Round(Location.Y) mod SQUARE) < 1) then
  112.     begin
  113.       Location.X := Round(Location.X);
  114.       Location.Y := Round(Location.Y);
  115.       if Game.CanMove(Self, Request) then
  116.       begin
  117.         Direction := Request;
  118.         Request := None;
  119.       end;
  120.     end;
  121.   end;
  122.   if IsMoving and Game.CanMove(Self, Direction) then Approach(Direction)
  123.   else Direction := None;
  124. end;
  125.  
  126. procedure TEntity.Approach(_Direction: TDirection);
  127. begin
  128. case _Direction of
  129.   LEFT: Location.X := Location.X - Speed;
  130.   RIGHT: Location.X := Location.X + Speed;
  131.   UP: Location.Y := Location.Y - Speed;
  132.   DOWN: Location.Y := Location.Y + Speed;
  133.   end;
  134. end;
  135.  
  136. procedure TEntity.Respawn;
  137. begin
  138.   Location := Spawn;
  139. end;
  140.  
  141. function TEntity.DistanceTo(_Location: TLocation): Real;
  142. begin
  143.   Result := Sqrt(Sqr(_Location.X - Location.X) + Sqr(_Location.Y - Location.Y));
  144. end;
  145.  
  146. constructor TPlayer.Create(X, Y: Integer; _Game:TObject);
  147. var
  148.   Dir: TDirection;
  149. begin
  150.   Inherited;
  151.   for Dir := Low(Dir) to High(Dir) do Images[Dir] := TBitmap.Create;
  152.   Images[None].LoadFromFile(IMAGE_DIR + 'pacman.bmp');
  153.   Images[UP].LoadFromFile(IMAGE_DIR + 'pacmanU.bmp');
  154.   Images[DOWN].LoadFromFile(IMAGE_DIR + 'pacmanD.bmp');
  155.   Images[LEFT].LoadFromFile(IMAGE_DIR + 'pacmanL.bmp');
  156.   Images[RIGHT].LoadFromFile(IMAGE_DIR + 'pacmanR.bmp');
  157. end;
  158.  
  159. procedure TPlayer.Paint(Canvas: TCanvas);
  160. begin
  161.   if Direction <> None then Image := Images[Direction];
  162.   Inherited;
  163. end;
  164.  
  165. procedure TPlayer.Update;
  166. begin
  167.   Inherited;
  168.   with Game.GameMap do
  169.     if (Network[Round(Location.Y) div SQUARE][Round(Location.X) div SQUARE] = Food) then
  170.     begin
  171.       Network[Round(Location.Y) div SQUARE][Round(Location.X) div SQUARE] := Nothing;
  172.       Dec(FoodRemaining);
  173.     end
  174.     else
  175.       if Network[Round(Location.Y) div SQUARE][Round(Location.X) div SQUARE] = Ball then
  176.       begin
  177.         Network[Round(Location.Y) div SQUARE][Round(Location.X) div SQUARE] := Nothing;
  178.         Game.StartKilling;
  179.       end;
  180. end;
  181.  
  182. constructor TEnemy.Create(X, Y: Integer; _Nature: TEnemyNature; _Game:TObject);
  183. begin
  184.   Nature := _Nature;
  185.   Location := ConvertToLocation(X * SQUARE, Y * SQUARE);
  186.   Spawn := Location;
  187.   Last := None;
  188.   LogicTimer := TTimer.Create;
  189.   LogicTimer.Reset;
  190.   Game := _Game as TGame;
  191.   Exclamation := TBitmap.Create;
  192.   Exclamation.Transparent := True;
  193.   Exclamation.LoadFromFile(IMAGE_DIR + 'ex.bmp');
  194. end;
  195.  
  196. function TEnemy.OnXRoad:Boolean;
  197. var
  198.   X0, Y0 ,I :Integer;
  199.  
  200. begin
  201.   if ((Round(Location.X) mod SQUARE) <= 1) and ((Round(Location.Y) mod SQUARE) <= 1)then
  202.   begin
  203.     X0 := Round(Location.X) div Square;
  204.     Y0 := Round(Location.Y) div Square;
  205.     I := 0;
  206.     if Game.GameMap.IsFree(X0 + 1, Y0) then Inc(I);
  207.     if Game.GameMap.IsFree(X0 - 1 , Y0) then Inc(I);
  208.     if Game.GameMap.IsFree(X0, Y0 + 1) then Inc(I);
  209.     if Game.GameMap.IsFree(X0, Y0 - 1) then Inc(I);
  210.     if I >= 3 then Result := True else Result := False;
  211.   end
  212.   else
  213.     Result := False;
  214. end;
  215.  
  216. procedure TEnemy.NewPath;
  217. var
  218.   PossibleMoves: TMoves;
  219.   Dir: TDirection;
  220.   R: Integer;
  221.   Debug: TLocation;
  222. begin
  223.   SetLength(PossibleMoves, 0);
  224.   for Dir := Low(TDirection) to RIGHT do
  225.   begin
  226.     if  not (Dir = Invert(Last)) and Game.CanMove(Self, Dir) then
  227.     begin
  228.       SetLength(PossibleMoves, Length(PossibleMoves) + 1);
  229.       PossibleMoves[Length(PossibleMoves) - 1].Destination := PseudoMove(Location, Dir);
  230.       PossibleMoves[Length(PossibleMoves) - 1].Direction := Dir;
  231.     end;
  232.   end;
  233.   if Length(PossibleMoves) = 0 then
  234.   begin
  235.     Move(Invert(Last));
  236.     Exit;
  237.   end;
  238.   if Length(PossibleMoves) = 1 then
  239.   begin
  240.     Move(PossibleMoves[0].Direction);
  241.     Exit;
  242.   end;
  243.   R := Random(Sqr(Length(PossibleMoves))) + 1;
  244.   case Nature of
  245.     Camper:
  246.     begin
  247.       PossibleMoves := Sort(PossibleMoves,Spawn);
  248.       if Length(PossibleMoves) = 2 then
  249.       begin
  250.         if R<=2 then Move(PossibleMoves[0].Direction)
  251.         else Move(PossibleMoves[1].Direction)
  252.       end
  253.       else
  254.       if Length(PossibleMoves) = 3 then
  255.       begin
  256.         if R <= 4 then Move(PossibleMoves[0].Direction)
  257.         else if R <= 7 then Move(PossibleMoves[1].Direction)
  258.         else Move(PossibleMoves[2].Direction)
  259.       end
  260.       else
  261.       begin
  262.         if R <= 6 then Move(PossibleMoves[0].Direction)
  263.         else if R <= 10 then Move(PossibleMoves[1].Direction)
  264.         else if R <= 14 then Move(PossibleMoves[2].Direction)
  265.         else Move(PossibleMoves[3].Direction)
  266.       end;
  267.     end;
  268.     Rusher:
  269.       begin
  270.         PossibleMoves := Sort(PossibleMoves, Game.Player.Location);
  271.         Move(PossibleMoves[0].Direction);
  272.       end;
  273.     Randomer:
  274.       begin
  275.         Move(PossibleMoves[Random(Length(PossibleMoves))].Direction);
  276.       end;
  277.     Ambusher:
  278.       begin
  279.         Debug := GetPointForAmbusher(Game.GameMap.Network);
  280.         PossibleMoves := Sort(PossibleMoves, Debug);
  281.         Move(PossibleMoves[Length(PossibleMoves)-1].Direction);
  282.       end;
  283.   end;
  284. end;
  285.  
  286. procedure TEnemy.Update;
  287. begin
  288.   if ((LogicTimer.GetTime > 100) and OnXRoad()) or (Direction = None) then begin
  289.   LogicTimer.Reset;
  290.   NewPath;
  291.   Direction := Request;
  292.   Request := None;
  293.   end;
  294.   if IsMoving and Game.CanMove(Self, Direction) then Approach(Direction) else Direction := None;
  295. end;
  296.  
  297. procedure TEnemy.Move(_Direction :TDirection);
  298. begin
  299.   Last := _Direction;
  300.   Inherited;
  301. end;
  302.  
  303. end.
RAW Paste Data
Top