- Timestamp:
- Feb 11, 2014, 11:53:04 PM (11 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UFormMain.lfm
r6 r7 72 72 end 73 73 end 74 object Timer1: TTimer 75 Interval = 20 76 OnTimer = Timer1Timer 77 left = 154 78 top = 263 79 end 74 80 end -
trunk/UFormMain.pas
r6 r7 32 32 MenuItem7: TMenuItem; 33 33 PaintBox1: TPaintBox; 34 Timer1: TTimer; 34 35 procedure AExitExecute(Sender: TObject); 35 36 procedure AGameNewExecute(Sender: TObject); … … 50 51 procedure PaintBox1Paint(Sender: TObject); 51 52 procedure EraseBackground(DC: HDC); override; 53 procedure Timer1Timer(Sender: TObject); 52 54 private 53 55 StartMousePoint: TPoint; 54 56 StartViewPoint: TPoint; 55 57 MoveActive: Boolean; 58 RedrawPending: Boolean; 56 59 public 57 60 Game: TGame; 61 procedure Redraw; 58 62 end; 59 63 … … 72 76 procedure TFormMain.PaintBox1Paint(Sender: TObject); 73 77 begin 74 TPlayer(Game.Players[0]).Paint(PaintBox1); 78 with TPlayer(Game.Players[0]) do begin 79 View := Bounds(View.Left, View.Top, PaintBox1.Width, 80 PaintBox1.Height); 81 Paint(PaintBox1); 82 end; 75 83 end; 76 84 77 85 procedure TFormMain.EraseBackground(DC: HDC); 78 86 begin 87 end; 88 89 procedure TFormMain.Timer1Timer(Sender: TObject); 90 begin 91 if RedrawPending then begin 92 RedrawPending := False; 93 PaintBox1.Repaint; 94 end; 95 end; 96 97 procedure TFormMain.Redraw; 98 begin 99 RedrawPending := True; 79 100 end; 80 101 … … 95 116 FormNew.Save(Game); 96 117 Game.New; 97 PaintBox1.Repaint;118 Redraw; 98 119 end; 99 120 end; … … 116 137 StartViewPoint := Point(TPlayer(Game.Players[0]).View.Left, TPlayer(Game.Players[0]).View.Top); 117 138 MoveActive := True; 139 TPlayer(Game.Players[0]).SelectCell(Point(X, Y)); 140 Redraw; 118 141 end; 119 142 end; … … 133 156 TPlayer(Game.Players[0]).View.Bottom - TPlayer(Game.Players[0]).View.Top); 134 157 TPlayer(Game.Players[0]).SelectCell(Point(X, Y)); 135 PaintBox1.Repaint;158 Redraw; 136 159 end; 137 160 end; … … 148 171 with TPlayer(Game.Players[0]) do 149 172 ViewZoom := ViewZoom / ZoomFactor; 150 PaintBox1.Repaint;173 Redraw; 151 174 end; 152 175 … … 156 179 with TPlayer(Game.Players[0]) do 157 180 ViewZoom := ViewZoom * ZoomFactor; 158 PaintBox1.Repaint;181 Redraw; 159 182 end; 160 183 -
trunk/UGame.pas
r6 r7 15 15 X, Y: Double; 16 16 end; 17 18 TPointArray = array of TPoint; 17 19 18 20 TTerrainType = (ttVoid, ttNormal); … … 30 32 31 33 TMap = class 34 private 35 public 32 36 DefaultCellSize: TPoint; 33 37 Cells: array of array of TCell; 34 procedure Paint(Canvas: TCanvas; Rect: TRect; Zoom: Double); 38 function PosToCell(Pos: TPoint; Rect: TRect; Zoom: Double 39 ): TPoint; 40 function GetHexagonPolygon(Pos: TPoint; HexSize: TPoint): TPointArray; 41 procedure Paint(Canvas: TCanvas; Rect: TRect; Zoom: Double; SelectedCell: TPoint); 35 42 constructor Create; 36 43 destructor Destroy; override; … … 72 79 end; 73 80 81 function PtInPoly(const Points: array of TPoint; Pos: TPoint): Boolean; 82 var 83 Count, K, J : Integer; 84 begin 85 Result := False; 86 Count := Length(Points) ; 87 J := Count - 1; 88 for K := 0 to Count-1 do begin 89 if ((Points[K].Y <= Pos.Y) and (Pos.Y < Points[J].Y)) or 90 ((Points[J].Y <= Pos.Y) and (Pos.Y < Points[K].Y)) then 91 begin 92 if (Pos.X < (Points[j].X - Points[K].X) * 93 (Pos.Y - Points[K].Y) / 94 (Points[j].Y - Points[K].Y) + Points[K].X) then 95 Result := not Result; 96 end; 97 J := K; 98 end; 99 end; 100 74 101 { TCell } 75 102 … … 84 111 procedure TPlayer.SelectCell(Pos: TPoint); 85 112 begin 86 113 SelectedCell := Game.Map.PosToCell(Pos, View, ViewZoom); 87 114 end; 88 115 89 116 procedure TPlayer.Paint(PaintBox: TPaintBox); 90 117 begin 91 View := Bounds(View.Left, View.Top, PaintBox.Width, 92 PaintBox.Height); 93 Game.Map.Paint(PaintBox.Canvas, View, ViewZoom); 118 Game.Map.Paint(PaintBox.Canvas, View, ViewZoom, SelectedCell); 94 119 end; 95 120 … … 150 175 { TMap } 151 176 152 procedure TMap.Paint(Canvas: TCanvas; Rect: TRect; Zoom: Double); 177 function TMap.GetHexagonPolygon(Pos: TPoint; HexSize: TPoint): TPointArray; 178 var 179 HexShift: TFloatPoint; 180 begin 181 HexShift := FloatPoint(0.5 * cos(30 / 180 * Pi), 0.5 * sin(30 / 180 * Pi)); 182 SetLength(Result, 6); 183 Result[0] := Point(Round(Pos.X + 0 * HexSize.X), Round(Pos.Y - 0.5 * HexSize.Y)); 184 Result[1] := Point(Round(Pos.X + HexShift.X * HexSize.X), Round(Pos.Y - HexShift.Y * HexSize.Y)); 185 Result[2] := Point(Round(Pos.X + HexShift.X * HexSize.X), Round(Pos.Y + HexShift.Y * HexSize.Y)); 186 Result[3] := Point(Round(Pos.X + 0 * HexSize.X), Round(Pos.Y + 0.5 * HexSize.Y)); 187 Result[4] := Point(Round(Pos.X - HexShift.X * HexSize.X), Round(Pos.Y + HexShift.Y * HexSize.Y)); 188 Result[5] := Point(Round(Pos.X - HexShift.X * HexSize.X), Round(Pos.Y - HexShift.Y * HexSize.Y)); 189 end; 190 191 function TMap.PosToCell(Pos: TPoint; Rect: TRect; Zoom: Double): TPoint; 153 192 var 154 193 CX, CY: Integer; 155 194 X, Y: Double; 156 HexShift: TFloatPoint;157 195 HexSize: TFloatPoint; 158 196 CellSize: TFloatPoint; 197 Points: array of TPoint; 198 begin 199 CellSize := FloatPoint(DefaultCellSize.X / 1.15 * Zoom, DefaultCellSize.Y / 1.35 * Zoom); 200 HexSize := FloatPoint(DefaultCellSize.X * Zoom, DefaultCellSize.Y * Zoom); 201 for CY := Trunc(Rect.Top / CellSize.Y) to Trunc(Rect.Bottom / CellSize.Y) + 1 do 202 for CX := Trunc(Rect.Left / CellSize.X) to Trunc(Rect.Right / CellSize.X) + 1 do begin 203 X := CX - Trunc(Rect.Left / CellSize.X); 204 Y := CY - Trunc(Rect.Top / CellSize.Y); 205 if (CY and 1) = 1 then begin 206 X := X + 0.5; 207 //Y := Y + 0.5; 208 end; 209 if (CX >= 0) and (CY >= 0) and (CY < Length(Cells)) and (CX < Length(Cells[0])) then 210 if Cells[CY, CX].Terrain = ttNormal then begin 211 Points := GetHexagonPolygon(Point(Trunc(X * CellSize.X - Frac(Rect.Left / CellSize.X) * CellSize.X), 212 Trunc(Y * CellSize.Y - Frac(Rect.Top / CellSize.Y) * CellSize.Y)), Point(Trunc(HexSize.X), Trunc(HexSize.Y))); 213 if PtInPoly(Points, Pos) then begin 214 Result := Point(CX, CY); 215 Exit; 216 end; 217 end; 218 end; 219 end; 220 221 222 procedure TMap.Paint(Canvas: TCanvas; Rect: TRect; Zoom: Double; SelectedCell: TPoint); 223 var 224 CX, CY: Integer; 225 X, Y: Double; 226 HexSize: TFloatPoint; 227 CellSize: TFloatPoint; 159 228 160 229 procedure PaintHexagon(X, Y: Double; Text: string); … … 163 232 begin 164 233 with Canvas do begin 165 SetLength(Points, 6); 166 Points[0] := Point(Round(X + 0 * HexSize.X), Round(Y - 0.5 * HexSize.Y)); 167 Points[1] := Point(Round(X + HexShift.X * HexSize.X), Round(Y - HexShift.Y * HexSize.Y)); 168 Points[2] := Point(Round(X + HexShift.X * HexSize.X), Round(Y + HexShift.Y * HexSize.Y)); 169 Points[3] := Point(Round(X + 0 * HexSize.X), Round(Y + 0.5 * HexSize.Y)); 170 Points[4] := Point(Round(X - HexShift.X * HexSize.X), Round(Y + HexShift.Y * HexSize.Y)); 171 Points[5] := Point(Round(X - HexShift.X * HexSize.X), Round(Y - HexShift.Y * HexSize.Y)); 234 Points := GetHexagonPolygon(Point(Trunc(X), Trunc(Y)), Point(Trunc(HexSize.X), Trunc(HexSize.Y))); 172 235 Polygon(Points); 173 174 (*175 MoveTo(Round(X + 0 * HexSize.X), Round(Y - 0.5 * HexSize.Y));176 LineTo(Round(X + HexShift.X * HexSize.X), Round(Y - HexShift.Y * HexSize.Y));177 LineTo(Round(X + HexShift.X * HexSize.X), Round(Y + HexShift.Y * HexSize.Y));178 LineTo(Round(X + 0 * HexSize.X), Round(Y + 0.5 * HexSize.Y));179 LineTo(Round(X - HexShift.X * HexSize.X), Round(Y + HexShift.Y * HexSize.Y));180 LineTo(Round(X - HexShift.X * HexSize.X), Round(Y - HexShift.Y * HexSize.Y));181 LineTo(Round(X + 0 * HexSize.X), Round(Y - 0.5 * HexSize.Y));182 *)183 236 Font.Color := clWhite; 184 237 Font.Size := Trunc(12 * Zoom); … … 189 242 begin 190 243 CellSize := FloatPoint(DefaultCellSize.X / 1.15 * Zoom, DefaultCellSize.Y / 1.35 * Zoom); 191 HexShift := FloatPoint(0.5 * cos(30 / 180 * Pi),192 0.5 * sin(30 / 180 * Pi));193 244 HexSize := FloatPoint(DefaultCellSize.X * Zoom, DefaultCellSize.Y * Zoom); 194 245 with Canvas do try … … 204 255 if (CX >= 0) and (CY >= 0) and (CY < Length(Cells)) and (CX < Length(Cells[0])) then 205 256 if Cells[CY, CX].Terrain = ttNormal then begin 206 Brush.Color := Cells[CY, CX].GetColor; 257 if (SelectedCell.X = CX) and (SelectedCell.Y = CY) then Brush.Color := clGreen 258 else Brush.Color := Cells[CY, CX].GetColor; 207 259 PaintHexagon(X * CellSize.X - Frac(Rect.Left / CellSize.X) * CellSize.X, 208 260 Y * CellSize.Y - Frac(Rect.Top / CellSize.Y) * CellSize.Y, IntToStr(Cells[CY, CX].Power));
Note:
See TracChangeset
for help on using the changeset viewer.