Changeset 77 for trunk/UGame.pas
- Timestamp:
- Oct 5, 2014, 7:13:38 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UGame.pas
r76 r77 25 25 TUnitMoves = class; 26 26 TCells = class; 27 TMap = class; 27 28 28 29 TFloatPoint = record … … 38 39 TCell = class 39 40 private 41 FMap: TMap; 40 42 FPower: Integer; 41 43 procedure SetPower(AValue: Integer); … … 45 47 Polygon: TPointArray; 46 48 Terrain: TTerrainType; 49 PlayerId: Integer; 47 50 Player: TPlayer; 48 51 MovesFrom: TUnitMoves; 49 52 MovesTo: TUnitMoves; 53 NeighborsId: array of Integer; 50 54 Neighbors: TCells; 51 55 Mark: Boolean; 56 procedure FixRefId; 52 57 procedure LoadFromNode(Node: TDOMNode); 53 58 procedure SaveToNode(Node: TDOMNode); … … 60 65 destructor Destroy; override; 61 66 property Power: Integer read FPower write SetPower; 67 property Map: TMap read FMap write FMap; 62 68 end; 63 69 … … 67 73 68 74 TCells = class(TObjectList) 75 Map: TMap; 76 procedure FixRefId; 69 77 function FindById(Id: Integer): TCell; 70 78 procedure LoadFromNode(Node: TDOMNode); … … 235 243 Game: TGame; 236 244 NewPlayerId: Integer; 245 function FindById(Id: Integer): TPlayer; 237 246 procedure New(Name: string; Color: TColor; Mode: TPlayerMode); 238 247 function GetNewPlayerId: Integer; … … 285 294 FMapType: TMapType; 286 295 FOnMove: TMoveEvent; 296 FOnNewTurn: TNotifyEvent; 287 297 FOnWin: TWinEvent; 288 298 FRunning: Boolean; … … 334 344 property OnMove: TMoveEvent read FOnMove write FOnMove; 335 345 property OnWin: TWinEvent read FOnWin write FOnWin; 346 property OnNewTurn: TNotifyEvent read FOnNewTurn write FOnNewTurn; 336 347 end; 337 348 … … 427 438 { TCells } 428 439 440 procedure TCells.FixRefId; 441 var 442 I: Integer; 443 begin 444 for I := 0 to Count - 1 do 445 TCell(Items[I]).FixRefId; 446 end; 447 429 448 function TCells.FindById(Id: Integer): TCell; 430 449 var … … 432 451 begin 433 452 I := 0; 434 while (I < 0) and (TCell(Items[I]).Id <> Id) do Inc(I);453 while (I < Count) and (TCell(Items[I]).Id <> Id) do Inc(I); 435 454 if I < Count then Result := TCell(Items[I]) 436 455 else Result := nil; … … 446 465 while Assigned(Node2) and (Node2.NodeName = 'Cell') do begin 447 466 NewCell := TCell.Create; 467 NewCell.Map := Map; 448 468 NewCell.LoadFromNode(Node2); 449 469 Add(NewCell); … … 466 486 467 487 { TPlayers } 488 489 function TPlayers.FindById(Id: Integer): TPlayer; 490 var 491 I: Integer; 492 begin 493 I := 0; 494 while (I < Count) and (TPlayer(Items[I]).Id <> Id) do Inc(I); 495 if I < Count then Result := TPlayer(Items[I]) 496 else Result := nil; 497 end; 468 498 469 499 procedure TPlayers.New(Name: string; Color: TColor; Mode: TPlayerMode); … … 574 604 for X := 0 to FSize.X - 1 do begin 575 605 NewCell := TCell.Create; 606 NewCell.Map := Self; 576 607 NewCell.PosPx := Point(Trunc(Random * FSize.X * DefaultCellSize.X), Trunc(Random * FSize.Y * DefaultCellSize.Y)); 577 608 SetLength(NewCell.Polygon, 1); … … 618 649 for X := 0 to FSize.X - 1 do begin 619 650 NewCell := TCell.Create; 651 NewCell.Map := Self; 620 652 Reverse := Boolean(X mod 2) xor Boolean(Y mod 2); 621 653 if Reverse then Rev := -1 … … 658 690 for X := 0 to FSize.X - 1 do begin 659 691 NewCell := TCell.Create; 692 NewCell.Map := Self; 660 693 NewCell.PosPx := Point(Trunc(X * DefaultCellSize.X * SquareCellMulX), 661 694 Trunc(Y * DefaultCellSize.Y * SquareCellMulY)); … … 960 993 for X := 0 to FSize.X - 1 do begin 961 994 NewCell := TCell.Create; 995 NewCell.Map := Self; 962 996 NewCell.PosPx := Point(X, Y); 963 997 NewCell.Id := GetNewCellId; … … 971 1005 DefaultCellSize := Point(220, 220); 972 1006 Cells := TCells.create; 1007 Cells.Map := Self; 973 1008 Size := Point(0, 0); 974 1009 Image := TImage.Create(nil); … … 1145 1180 end; 1146 1181 1182 procedure TCell.FixRefId; 1183 var 1184 I: Integer; 1185 begin 1186 Player := Map.Game.Players.FindById(PlayerId); 1187 1188 Neighbors.Count := Length(NeighborsId); 1189 for I := 0 to Length(NeighborsId) - 1 do begin 1190 Neighbors[I] := Map.Cells.FindById(NeighborsId[I]); 1191 end; 1192 end; 1193 1147 1194 procedure TCell.LoadFromNode(Node: TDOMNode); 1148 1195 var … … 1156 1203 PosPx.X := ReadInteger(Node, 'PosX', 0); 1157 1204 PosPx.Y := ReadInteger(Node, 'PosY', 0); 1158 //Player := Map.Game.Players.FindById(ReadInteger(Node, 'Player', 0)); 1205 PlayerId := ReadInteger(Node, 'Player', 0); 1206 1207 Node3 := Node.FindNode('Neighbours'); 1208 if Assigned(Node3) then begin 1209 SetLength(NeighborsId, 0); 1210 Node2 := Node3.FirstChild; 1211 while Assigned(Node2) and (Node2.NodeName = 'Neighbour') do begin 1212 SetLength(NeighborsId, Length(NeighborsId) + 1); 1213 NeighborsId[High(NeighborsId)] := ReadInteger(Node2, 'Id', 0); 1214 Node2 := Node2.NextSibling; 1215 end; 1216 end; 1159 1217 1160 1218 Node3 := Node.FindNode('Polygon'); … … 1977 2035 if Assigned(NewNode) then 1978 2036 Moves.LoadFromNode(NewNode); 2037 2038 Map.Cells.FixRefId; 1979 2039 end; 1980 2040 finally … … 2078 2138 CurrentPlayer := TPlayer(Players[(Players.IndexOf(CurrentPlayer) + 1) mod Players.Count]); 2079 2139 until CurrentPlayer.TotalCells > 0; 2080 if Players.IndexOf(CurrentPlayer) < Players.IndexOf(PrevPlayer) then Inc(TurnCounter); 2140 if Players.IndexOf(CurrentPlayer) < Players.IndexOf(PrevPlayer) then begin 2141 Inc(TurnCounter); 2142 if Assigned(FOnNewTurn) then 2143 FOnNewTurn(Self); 2144 end; 2081 2145 CheckWinObjective; 2082 2146 // For computers take view from previous human … … 2364 2428 for X := 0 to FSize.X - 1 do begin 2365 2429 NewCell := TCell.Create; 2430 NewCell.Map := Self; 2366 2431 PX := X; 2367 2432 PY := Y;
Note:
See TracChangeset
for help on using the changeset viewer.