root / UOLib / UWorldItem.pas @ 152:2c10e1ad6647
History | View | Annotate | Download (6 kB)
| 1 | (*
|
|---|---|
| 2 | * CDDL HEADER START |
| 3 | * |
| 4 | * The contents of this file are subject to the terms of the |
| 5 | * Common Development and Distribution License, Version 1.0 only |
| 6 | * (the "License"). You may not use this file except in compliance |
| 7 | * with the License. |
| 8 | * |
| 9 | * You can obtain a copy of the license at |
| 10 | * http://www.opensource.org/licenses/cddl1.php. |
| 11 | * See the License for the specific language governing permissions |
| 12 | * and limitations under the License. |
| 13 | * |
| 14 | * When distributing Covered Code, include this CDDL HEADER in each |
| 15 | * file and include the License file at |
| 16 | * http://www.opensource.org/licenses/cddl1.php. If applicable, |
| 17 | * add the following below this CDDL HEADER, with the fields enclosed |
| 18 | * by brackets "[]" replaced with your own identifying * information: |
| 19 | * Portions Copyright [yyyy] [name of copyright owner] |
| 20 | * |
| 21 | * CDDL HEADER END |
| 22 | * |
| 23 | * |
| 24 | * Portions Copyright 2009 Andreas Schneider |
| 25 | *) |
| 26 | unit UWorldItem;
|
| 27 | |
| 28 | {$mode objfpc}{$H+} |
| 29 | |
| 30 | interface
|
| 31 | |
| 32 | uses
|
| 33 | Classes, fgl, UMulBlock; |
| 34 | |
| 35 | type
|
| 36 | TWorldBlock = class;
|
| 37 | |
| 38 | { TWorldItem }
|
| 39 | |
| 40 | TWorldItem = class(TMulBlock)
|
| 41 | constructor Create(AOwner: TWorldBlock);
|
| 42 | protected
|
| 43 | FOwner: TWorldBlock; |
| 44 | FTileID: Word; |
| 45 | FX: Word; |
| 46 | FY: Word; |
| 47 | FZ: ShortInt; |
| 48 | FSelected: Boolean; |
| 49 | FCanBeEdited: Boolean; |
| 50 | FLocked: Boolean; |
| 51 | FPriority: Integer; |
| 52 | FPriorityBonus: ShortInt; |
| 53 | FPrioritySolver: Integer; |
| 54 | procedure DoChanged;
|
| 55 | function GetTileID: Word; virtual; |
| 56 | function GetZ: ShortInt; virtual; |
| 57 | procedure SetLocked(ALocked: Boolean);
|
| 58 | procedure SetOwner(AOwner: TWorldBlock);
|
| 59 | procedure SetSelected(ASelected: Boolean);
|
| 60 | procedure SetTileID(AValue: Word);
|
| 61 | procedure SetX(AValue: Word);
|
| 62 | procedure SetY(AValue: Word);
|
| 63 | procedure SetZ(AValue: ShortInt);
|
| 64 | public
|
| 65 | procedure UpdatePos(AX, AY: Word; AZ: ShortInt);
|
| 66 | procedure Delete;
|
| 67 | |
| 68 | property Owner: TWorldBlock read FOwner write SetOwner; |
| 69 | property TileID: Word read GetTileID write SetTileID; |
| 70 | property X: Word read FX write SetX; |
| 71 | property Y: Word read FY write SetY; |
| 72 | property Z: ShortInt read GetZ write SetZ; |
| 73 | property Selected: Boolean read FSelected write SetSelected; |
| 74 | property CanBeEdited: Boolean read FCanBeEdited write FCanBeEdited; |
| 75 | property Locked: Boolean read FLocked write SetLocked; |
| 76 | property Priority: Integer read FPriority write FPriority; |
| 77 | property PriorityBonus: ShortInt read FPriorityBonus write FPriorityBonus; |
| 78 | property PrioritySolver: Integer read FPrioritySolver write FPrioritySolver; |
| 79 | |
| 80 | property RawTileID: Word read FTileID; |
| 81 | property RawZ: ShortInt read FZ; |
| 82 | end;
|
| 83 | |
| 84 | TWorldItemList = specialize TFPGObjectList<TWorldItem>; |
| 85 | |
| 86 | { TWorldBlock }
|
| 87 | |
| 88 | TWorldBlock = class(TMulBlock)
|
| 89 | constructor Create;
|
| 90 | protected
|
| 91 | FX: Word; |
| 92 | FY: Word; |
| 93 | FRefCount: Integer; |
| 94 | FChanged: Boolean; |
| 95 | public
|
| 96 | property X: Word read FX write FX; |
| 97 | property Y: Word read FY write FY; |
| 98 | property RefCount: Integer read FRefCount; |
| 99 | property Changed: Boolean read FChanged write FChanged; |
| 100 | procedure AddRef;
|
| 101 | procedure RemoveRef;
|
| 102 | end;
|
| 103 | |
| 104 | TVirtualTile = class(TWorldItem);
|
| 105 | |
| 106 | function CompareWorldItems(const AItem1, AItem2: TWorldItem): Integer; |
| 107 | |
| 108 | implementation
|
| 109 | |
| 110 | uses
|
| 111 | UMap, UStatics; |
| 112 | |
| 113 | function CompareWorldItems(const AItem1, AItem2: TWorldItem): Integer; |
| 114 | begin
|
| 115 | if AItem1.X <> AItem2.X then |
| 116 | Exit(AItem1.X - AItem2.X); |
| 117 | |
| 118 | if AItem1.Y <> AItem2.Y then |
| 119 | Exit(AItem1.Y - AItem2.Y); |
| 120 | |
| 121 | Result := AItem1.Priority - AItem2.Priority; |
| 122 | if Result = 0 then |
| 123 | begin
|
| 124 | if (AItem1 is TMapCell) and (AItem2 is TStaticItem) then |
| 125 | Result := -1
|
| 126 | else if (AItem1 is TStaticItem) and (AItem2 is TMapCell) then |
| 127 | Result := 1
|
| 128 | else if (AItem1 is TMapCell) and (AItem2 is TVirtualTile) then |
| 129 | Result := -1
|
| 130 | else if (AItem1 is TVirtualTile) and (AItem2 is TMapCell) then |
| 131 | Result := 1;
|
| 132 | end;
|
| 133 | |
| 134 | if Result = 0 then |
| 135 | Result := AItem1.PrioritySolver - AItem2.PrioritySolver; |
| 136 | end;
|
| 137 | |
| 138 | { TWorldItem }
|
| 139 | |
| 140 | constructor TWorldItem.Create(AOwner: TWorldBlock);
|
| 141 | begin
|
| 142 | inherited Create;
|
| 143 | FSelected := False; |
| 144 | FLocked := False; |
| 145 | FOwner := AOwner; |
| 146 | end;
|
| 147 | |
| 148 | procedure TWorldItem.DoChanged;
|
| 149 | begin
|
| 150 | if FOwner <> nil then |
| 151 | FOwner.Changed := True; |
| 152 | end;
|
| 153 | |
| 154 | function TWorldItem.GetTileID: Word;
|
| 155 | begin
|
| 156 | Result := FTileID; |
| 157 | end;
|
| 158 | |
| 159 | function TWorldItem.GetZ: ShortInt;
|
| 160 | begin
|
| 161 | Result := FZ; |
| 162 | end;
|
| 163 | |
| 164 | procedure TWorldItem.Delete;
|
| 165 | begin
|
| 166 | SetSelected(False); |
| 167 | SetLocked(False); |
| 168 | DoChanged; |
| 169 | end;
|
| 170 | |
| 171 | procedure TWorldItem.SetLocked(ALocked: Boolean);
|
| 172 | begin
|
| 173 | if FLocked <> ALocked then |
| 174 | begin
|
| 175 | FLocked := ALocked; |
| 176 | if FOwner <> nil then |
| 177 | if FLocked then |
| 178 | FOwner.AddRef |
| 179 | else
|
| 180 | FOwner.RemoveRef; |
| 181 | end;
|
| 182 | end;
|
| 183 | |
| 184 | procedure TWorldItem.SetOwner(AOwner: TWorldBlock);
|
| 185 | begin
|
| 186 | if FOwner <> AOwner then |
| 187 | begin
|
| 188 | if FOwner <> nil then |
| 189 | begin
|
| 190 | FOwner.Changed := True; |
| 191 | if FLocked then FOwner.RemoveRef; |
| 192 | if FSelected then FOwner.RemoveRef; |
| 193 | end;
|
| 194 | FOwner := AOwner; |
| 195 | if FOwner <> nil then |
| 196 | begin
|
| 197 | FOwner.Changed := True; |
| 198 | if FLocked then FOwner.AddRef; |
| 199 | if FSelected then FOwner.AddRef; |
| 200 | end;
|
| 201 | end;
|
| 202 | end;
|
| 203 | |
| 204 | procedure TWorldItem.SetSelected(ASelected: Boolean);
|
| 205 | begin
|
| 206 | if (FOwner <> nil) and (ASelected <> FSelected) then |
| 207 | if ASelected then |
| 208 | FOwner.AddRef |
| 209 | else
|
| 210 | FOwner.RemoveRef; |
| 211 | FSelected := ASelected; |
| 212 | end;
|
| 213 | |
| 214 | procedure TWorldItem.SetTileID(AValue: Word);
|
| 215 | begin
|
| 216 | if FTileID = AValue then |
| 217 | Exit; |
| 218 | |
| 219 | FTileID := AValue; |
| 220 | DoChanged; |
| 221 | end;
|
| 222 | |
| 223 | procedure TWorldItem.SetX(AValue: Word);
|
| 224 | begin
|
| 225 | if FX = AValue then |
| 226 | Exit; |
| 227 | |
| 228 | FX := AValue; |
| 229 | DoChanged; |
| 230 | end;
|
| 231 | |
| 232 | procedure TWorldItem.SetY(AValue: Word);
|
| 233 | begin
|
| 234 | if FY = AValue then |
| 235 | Exit; |
| 236 | |
| 237 | FY := AValue; |
| 238 | DoChanged; |
| 239 | end;
|
| 240 | |
| 241 | procedure TWorldItem.SetZ(AValue: ShortInt);
|
| 242 | begin
|
| 243 | if FZ = AValue then |
| 244 | Exit; |
| 245 | |
| 246 | FZ := AValue; |
| 247 | DoChanged; |
| 248 | end;
|
| 249 | |
| 250 | procedure TWorldItem.UpdatePos(AX, AY: Word; AZ: ShortInt);
|
| 251 | begin
|
| 252 | FX := AX; |
| 253 | FY := AY; |
| 254 | FZ := AZ; |
| 255 | DoChanged; |
| 256 | end;
|
| 257 | |
| 258 | { TWorldBlock }
|
| 259 | |
| 260 | procedure TWorldBlock.AddRef;
|
| 261 | begin
|
| 262 | Inc(FRefCount); |
| 263 | end;
|
| 264 | |
| 265 | constructor TWorldBlock.Create;
|
| 266 | begin
|
| 267 | inherited Create;
|
| 268 | FRefCount := 0;
|
| 269 | FChanged := False; |
| 270 | end;
|
| 271 | |
| 272 | procedure TWorldBlock.RemoveRef;
|
| 273 | begin
|
| 274 | if FRefCount > 0 then |
| 275 | Dec(FRefCount); |
| 276 | end;
|
| 277 | |
| 278 | end.
|
| 279 |