Changeset 13
- Timestamp:
- Feb 19, 2014, 10:42:16 PM (11 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UFormMain.pas
r12 r13 186 186 Shift: TShiftState; X, Y: Integer); 187 187 begin 188 if ( StartMousePoint.X = X) and (StartMousePoint.Y = Y) then begin188 if (Abs(StartMousePoint.X - X) < 5) and (Abs(StartMousePoint.Y - Y) < 5) then begin 189 189 if Game.CurrentPlayer.Mode = pmHuman then begin 190 190 Game.CurrentPlayer.SelectCell(Point(X, Y)); -
trunk/UGame.pas
r12 r13 23 23 24 24 TCell = class 25 Pos: TPoint; 25 26 Terrain: TTerrainType; 26 27 Power: Integer; 27 28 Player: TPlayer; 29 MovesFrom: TObjectList; 30 MovesTo: TObjectList; 28 31 function GetColor: TColor; 32 constructor Create; 33 destructor Destroy; override; 29 34 end; 30 35 … … 32 37 33 38 TMap = class 39 40 end; 41 42 { THexMap } 43 44 THexMap = class(TMap) 34 45 private 46 FSize: TPoint; 47 procedure SetSize(AValue: TPoint); 35 48 public 49 Game: TGame; 36 50 MaxPower: Integer; 37 51 DefaultCellSize: TPoint; … … 39 53 function PosToCell(Pos: TPoint; Rect: TRect; Zoom: Double 40 54 ): TPoint; 55 function CellToPos(Cell: TCell; Rect: TRect; Zoom: Double 56 ): TPoint; 41 57 function GetHexagonPolygon(Pos: TPoint; HexSize: TPoint): TPointArray; 42 58 procedure Paint(Canvas: TCanvas; Rect: TRect; Zoom: Double; SelectedCell: TPoint); 43 59 constructor Create; 44 60 destructor Destroy; override; 45 procedure Init(Size: TPoint);46 61 procedure Grow(APlayer: TPlayer); 62 procedure ClearCellMoves; 63 property Size: TPoint read FSize write SetSize; 47 64 end; 48 65 … … 83 100 public 84 101 Players: TObjectList; // TList<TPlayer> 85 Map: T Map;102 Map: THexMap; 86 103 VoidEnabled: Boolean; 87 104 CurrentPlayer: TPlayer; … … 151 168 end; 152 169 170 constructor TCell.Create; 171 begin 172 MovesFrom := TObjectList.Create; 173 MovesFrom.OwnsObjects := False; 174 MovesTo := TObjectList.Create; 175 MovesTo.OwnsObjects := False; 176 end; 177 178 destructor TCell.Destroy; 179 begin 180 FreeAndNil(MovesFrom); 181 FreeAndNil(MovesTo); 182 inherited Destroy; 183 end; 184 153 185 { TPlayer } 154 186 … … 163 195 begin 164 196 NewSelectedCell := Game.Map.PosToCell(Pos, View, ViewZoom); 165 if (NewSelectedCell.X >= 0) and (NewSelectedCell.X < Length(Game.Map.Cells[0])) and166 (NewSelectedCell.Y >= 0) and (NewSelectedCell.Y < Length(Game.Map.Cells)) then begin197 if (NewSelectedCell.X >= 0) and (NewSelectedCell.X < Game.Map.Size.X) and 198 (NewSelectedCell.Y >= 0) and (NewSelectedCell.Y < Game.Map.Size.Y) then begin 167 199 if IsCellsNeighbor(NewSelectedCell, SelectedCell) then begin 168 200 Game.SetMove(TCell(Game.Map.Cells[SelectedCell.Y, SelectedCell.X]), … … 287 319 //VoidEnabled := True; 288 320 289 Map := TMap.Create; 321 Map := THexMap.Create; 322 Map.Game := Self; 290 323 Moves := TObjectList.Create; 291 324 end; … … 306 339 StartCell: TCell; 307 340 begin 308 Map. Init(Point(20, 20));309 for Y := 0 to Length(Map.Cells)- 1 do310 for X := 0 to Length(Map.Cells[0])- 1 do341 Map.Size := Point(20, 20); 342 for Y := 0 to Map.Size.Y - 1 do 343 for X := 0 to Map.Size.X - 1 do 311 344 with Map.Cells[Y, X] do begin 312 345 if VoidEnabled and (Random(2) = 0) then Terrain := ttVoid … … 316 349 for I := 0 to Players.Count - 1 do 317 350 with TPlayer(Players[I]) do begin 318 StartCell := Map.Cells[Random( Length(Map.Cells)), Random(Length(Map.Cells[0]))];351 StartCell := Map.Cells[Random(Map.Size.Y), Random(Map.Size.X)]; 319 352 StartCell.Terrain := ttNormal; 320 353 StartCell.Player := TPlayer(Players[I]); … … 323 356 end; 324 357 325 { T Map }326 327 function T Map.GetHexagonPolygon(Pos: TPoint; HexSize: TPoint): TPointArray;358 { THexMap } 359 360 function THexMap.GetHexagonPolygon(Pos: TPoint; HexSize: TPoint): TPointArray; 328 361 var 329 362 HexShift: TFloatPoint; … … 339 372 end; 340 373 341 function TMap.PosToCell(Pos: TPoint; Rect: TRect; Zoom: Double): TPoint; 374 procedure THexMap.SetSize(AValue: TPoint); 375 var 376 X, Y: Integer; 377 NewCell: TCell; 378 C: Integer; 379 begin 380 // Free previous 381 for Y := 0 to FSize.Y - 1 do 382 for X := 0 to FSize.X - 1 do begin 383 Cells[Y, X].Destroy; 384 end; 385 FSize := AValue; 386 // Allocate and init new 387 SetLength(Cells, FSize.X, FSize.Y); 388 for Y := 0 to FSize.Y - 1 do 389 for X := 0 to FSize.X - 1 do begin 390 NewCell := TCell.Create; 391 NewCell.Pos := Point(X, Y); 392 Cells[Y, X] := NewCell; 393 end; 394 end; 395 396 function THexMap.PosToCell(Pos: TPoint; Rect: TRect; Zoom: Double): TPoint; 342 397 var 343 398 CX, CY: Integer; … … 357 412 //Y := Y + 0.5; 358 413 end; 359 if (CX >= 0) and (CY >= 0) and (CY < Length(Cells)) and (CX < Length(Cells[0])) then414 if (CX >= 0) and (CY >= 0) and (CY < Size.Y) and (CX < Size.X) then 360 415 if Cells[CY, CX].Terrain = ttNormal then begin 361 416 Points := GetHexagonPolygon(Point(Trunc(X * CellSize.X - Frac(Rect.Left / CellSize.X) * CellSize.X), … … 369 424 end; 370 425 371 372 procedure TMap.Paint(Canvas: TCanvas; Rect: TRect; Zoom: Double; SelectedCell: TPoint); 426 function THexMap.CellToPos(Cell: TCell; Rect: TRect; Zoom: Double): TPoint; 373 427 var 374 428 CX, CY: Integer; … … 376 430 HexSize: TFloatPoint; 377 431 CellSize: TFloatPoint; 432 Points: array of TPoint; 433 begin 434 CellSize := FloatPoint(DefaultCellSize.X / 1.15 * Zoom, DefaultCellSize.Y / 1.35 * Zoom); 435 HexSize := FloatPoint(DefaultCellSize.X * Zoom, DefaultCellSize.Y * Zoom); 436 X := Cell.Pos.X - Trunc(Rect.Left / CellSize.X); 437 Y := Cell.Pos.Y - Trunc(Rect.Top / CellSize.Y); 438 if (Cell.Pos.Y and 1) = 1 then begin 439 X := X + 0.5; 440 //Y := Y + 0.5; 441 end; 442 443 Result.X := Trunc(X * CellSize.X - Frac(Rect.Left / CellSize.X) * CellSize.X); 444 Result.Y := Trunc(Y * CellSize.Y - Frac(Rect.Top / CellSize.Y) * CellSize.Y); 445 end; 446 447 448 procedure THexMap.Paint(Canvas: TCanvas; Rect: TRect; Zoom: Double; SelectedCell: TPoint); 449 var 450 CX, CY: Integer; 451 X, Y: Double; 452 HexSize: TFloatPoint; 453 CellSize: TFloatPoint; 454 I: Integer; 455 Points: array of TPoint; 456 Cell: TCell; 457 PosFrom, PosTo: TPoint; 378 458 379 459 procedure PaintHexagon(X, Y: Double; Text: string); … … 395 475 with Canvas do try 396 476 Lock; 477 ClearCellMoves; 478 // Update moves in cells 479 for I := 0 to Game.Moves.Count - 1 do 480 with TMove(Game.Moves[I]) do begin 481 CellFrom.MovesFrom.Add(TMove(Game.Moves[I])); 482 CellTo.MovesTo.Add(TMove(Game.Moves[I])); 483 end; 484 397 485 for CY := Trunc(Rect.Top / CellSize.Y) to Trunc(Rect.Bottom / CellSize.Y) + 1 do 398 486 for CX := Trunc(Rect.Left / CellSize.X) to Trunc(Rect.Right / CellSize.X) + 1 do begin … … 403 491 //Y := Y + 0.5; 404 492 end; 405 if (CX >= 0) and (CY >= 0) and (CY < Length(Cells)) and (CX < Length(Cells[0])) then 406 if Cells[CY, CX].Terrain = ttNormal then begin 407 if (SelectedCell.X = CX) and (SelectedCell.Y = CY) then Brush.Color := clGreen 408 else if IsCellsNeighbor(SelectedCell, Point(CX, CY)) then Brush.Color := clPurple 409 else Brush.Color := Cells[CY, CX].GetColor; 410 PaintHexagon(X * CellSize.X - Frac(Rect.Left / CellSize.X) * CellSize.X, 411 Y * CellSize.Y - Frac(Rect.Top / CellSize.Y) * CellSize.Y, IntToStr(Cells[CY, CX].Power)); 493 if (CX >= 0) and (CY >= 0) and (CY < Size.Y) and (CX < Size.X) then begin 494 Cell := Cells[CY, CX]; 495 if Cell.Terrain = ttNormal then begin 496 if (SelectedCell.X = CX) and (SelectedCell.Y = CY) then Brush.Color := clGreen 497 else if IsCellsNeighbor(SelectedCell, Point(CX, CY)) then Brush.Color := clPurple 498 else Brush.Color := Cell.GetColor; 499 Pen.Color := clBlack; 500 PaintHexagon(X * CellSize.X - Frac(Rect.Left / CellSize.X) * CellSize.X, 501 Y * CellSize.Y - Frac(Rect.Top / CellSize.Y) * CellSize.Y, IntToStr(Cell.Power)); 502 // Draw arrows 503 Pen.Color := clCream; 504 for I := 0 to Cell.MovesFrom.Count - 1 do begin 505 PosFrom := CellToPos(Cell, Rect, Zoom); 506 PosTo := CellToPos(TMove(Cell.MovesFrom[I]).CellTo, Rect, Zoom); 507 Line(Trunc(PosFrom.X + (PosTo.X - PosFrom.X) / 4), 508 Trunc(PosFrom.Y + (PosTo.Y - PosFrom.Y) / 4), 509 Trunc(PosFrom.X + (PosTo.X - PosFrom.X) / 2), 510 Trunc(PosFrom.Y + (PosTo.Y - PosFrom.Y) / 2)); 511 end; 512 end; 412 513 end; 413 514 end; … … 417 518 end; 418 519 419 constructor T Map.Create;520 constructor THexMap.Create; 420 521 begin 421 522 DefaultCellSize := Point(62, 62); … … 423 524 end; 424 525 425 destructor T Map.Destroy;426 begin 427 Init(Point(0, 0));526 destructor THexMap.Destroy; 527 begin 528 Size := Point(0, 0); 428 529 inherited Destroy; 429 530 end; 430 531 431 procedure T Map.Init(Size: TPoint);532 procedure THexMap.Grow(APlayer: TPlayer); 432 533 var 433 534 X, Y: Integer; 434 NewCell: TCell; 435 C: Integer; 436 begin 437 // Free previous 438 for Y := 0 to Length(Cells) - 1 do 439 for X := 0 to Length(Cells[0]) - 1 do begin 440 Cells[Y, X].Destroy; 441 end; 442 // Allocate and init new 443 SetLength(Cells, Size.X, Size.Y); 444 for Y := 0 to Length(Cells) - 1 do 445 for X := 0 to Length(Cells[0]) - 1 do begin 446 NewCell := TCell.Create; 447 Cells[Y, X] := NewCell; 448 end; 449 end; 450 451 procedure TMap.Grow(APlayer: TPlayer); 452 var 453 X, Y: Integer; 454 begin 455 for Y := 0 to Length(Cells) - 1 do 456 for X := 0 to Length(Cells[0]) - 1 do 535 begin 536 for Y := 0 to Size.Y - 1 do 537 for X := 0 to Size.X - 1 do 457 538 with TCell(Cells[Y, X]) do begin 458 539 if Player = APlayer then begin … … 463 544 end; 464 545 546 procedure THexMap.ClearCellMoves; 547 var 548 X, Y: Integer; 549 begin 550 for Y := 0 to Size.Y - 1 do 551 for X := 0 to Size.X - 1 do begin 552 Cells[Y, X].MovesFrom.Clear; 553 Cells[Y, X].MovesTo.Clear; 554 end; 555 end; 556 465 557 end. 466 558 -
trunk/xtactics.lpi
r12 r13 63 63 <IsPartOfProject Value="True"/> 64 64 <ComponentName Value="FormMove"/> 65 <HasResources Value="True"/> 65 66 <ResourceBaseClass Value="Form"/> 66 67 <UnitName Value="UFormMove"/>
Note:
See TracChangeset
for help on using the changeset viewer.