2009-05-15 23:37:10 +02:00
|
|
|
(*
|
|
|
|
* CDDL HEADER START
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the terms of the
|
|
|
|
* Common Development and Distribution License, Version 1.0 only
|
|
|
|
* (the "License"). You may not use this file except in compliance
|
|
|
|
* with the License.
|
|
|
|
*
|
|
|
|
* You can obtain a copy of the license at
|
|
|
|
* http://www.opensource.org/licenses/cddl1.php.
|
|
|
|
* See the License for the specific language governing permissions
|
|
|
|
* and limitations under the License.
|
|
|
|
*
|
|
|
|
* When distributing Covered Code, include this CDDL HEADER in each
|
|
|
|
* file and include the License file at
|
|
|
|
* http://www.opensource.org/licenses/cddl1.php. If applicable,
|
|
|
|
* add the following below this CDDL HEADER, with the fields enclosed
|
|
|
|
* by brackets "[]" replaced with your own identifying * information:
|
|
|
|
* Portions Copyright [yyyy] [name of copyright owner]
|
|
|
|
*
|
|
|
|
* CDDL HEADER END
|
|
|
|
*
|
|
|
|
*
|
2009-12-02 19:14:00 +01:00
|
|
|
* Portions Copyright 2009 Andreas Schneider
|
2009-05-15 23:37:10 +02:00
|
|
|
*)
|
|
|
|
unit UWorldItem;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2009-12-02 19:14:00 +01:00
|
|
|
Classes, fgl, UMulBlock;
|
2009-05-15 23:37:10 +02:00
|
|
|
|
|
|
|
type
|
|
|
|
TWorldBlock = class;
|
2009-08-02 13:45:26 +02:00
|
|
|
|
|
|
|
{ TWorldItem }
|
|
|
|
|
2009-05-15 23:37:10 +02:00
|
|
|
TWorldItem = class(TMulBlock)
|
|
|
|
constructor Create(AOwner: TWorldBlock);
|
|
|
|
protected
|
2009-12-23 16:39:24 +01:00
|
|
|
FOwner: TWorldBlock;
|
2015-05-01 12:23:03 +02:00
|
|
|
FTileID: LongWord;
|
2009-12-23 16:39:24 +01:00
|
|
|
FX: Word;
|
|
|
|
FY: Word;
|
|
|
|
FZ: ShortInt;
|
2009-05-15 23:37:10 +02:00
|
|
|
FSelected: Boolean;
|
|
|
|
FCanBeEdited: Boolean;
|
|
|
|
FLocked: Boolean;
|
|
|
|
FPriority: Integer;
|
|
|
|
FPriorityBonus: ShortInt;
|
|
|
|
FPrioritySolver: Integer;
|
|
|
|
procedure DoChanged;
|
2015-05-01 12:23:03 +02:00
|
|
|
function GetTileID: LongWord; virtual;
|
2009-12-23 16:39:24 +01:00
|
|
|
function GetZ: ShortInt; virtual;
|
|
|
|
procedure SetLocked(ALocked: Boolean);
|
|
|
|
procedure SetOwner(AOwner: TWorldBlock);
|
|
|
|
procedure SetSelected(ASelected: Boolean);
|
2015-05-01 12:23:03 +02:00
|
|
|
procedure SetTileID(AValue: LongWord);
|
2009-12-23 16:39:24 +01:00
|
|
|
procedure SetX(AValue: Word);
|
|
|
|
procedure SetY(AValue: Word);
|
|
|
|
procedure SetZ(AValue: ShortInt);
|
2009-05-15 23:37:10 +02:00
|
|
|
public
|
|
|
|
procedure UpdatePos(AX, AY: Word; AZ: ShortInt);
|
|
|
|
procedure Delete;
|
2009-12-04 00:58:59 +01:00
|
|
|
|
2009-05-15 23:37:10 +02:00
|
|
|
property Owner: TWorldBlock read FOwner write SetOwner;
|
2015-05-01 12:23:03 +02:00
|
|
|
property TileID: LongWord read GetTileID write SetTileID;
|
2009-05-15 23:37:10 +02:00
|
|
|
property X: Word read FX write SetX;
|
|
|
|
property Y: Word read FY write SetY;
|
2009-12-04 00:58:59 +01:00
|
|
|
property Z: ShortInt read GetZ write SetZ;
|
2009-05-15 23:37:10 +02:00
|
|
|
property Selected: Boolean read FSelected write SetSelected;
|
|
|
|
property CanBeEdited: Boolean read FCanBeEdited write FCanBeEdited;
|
|
|
|
property Locked: Boolean read FLocked write SetLocked;
|
|
|
|
property Priority: Integer read FPriority write FPriority;
|
|
|
|
property PriorityBonus: ShortInt read FPriorityBonus write FPriorityBonus;
|
|
|
|
property PrioritySolver: Integer read FPrioritySolver write FPrioritySolver;
|
2009-12-04 00:58:59 +01:00
|
|
|
|
2015-05-01 12:23:03 +02:00
|
|
|
property RawTileID: LongWord read FTileID;
|
2009-12-04 00:58:59 +01:00
|
|
|
property RawZ: ShortInt read FZ;
|
2009-05-15 23:37:10 +02:00
|
|
|
end;
|
2009-08-02 13:45:26 +02:00
|
|
|
|
2009-12-02 19:14:00 +01:00
|
|
|
TWorldItemList = specialize TFPGObjectList<TWorldItem>;
|
|
|
|
|
2009-08-02 13:45:26 +02:00
|
|
|
{ TWorldBlock }
|
|
|
|
|
2009-05-15 23:37:10 +02:00
|
|
|
TWorldBlock = class(TMulBlock)
|
|
|
|
constructor Create;
|
|
|
|
protected
|
|
|
|
FX: Word;
|
|
|
|
FY: Word;
|
|
|
|
FRefCount: Integer;
|
2009-12-23 16:39:24 +01:00
|
|
|
FChanged: Boolean;
|
2009-05-15 23:37:10 +02:00
|
|
|
public
|
|
|
|
property X: Word read FX write FX;
|
|
|
|
property Y: Word read FY write FY;
|
|
|
|
property RefCount: Integer read FRefCount;
|
2009-12-23 16:39:24 +01:00
|
|
|
property Changed: Boolean read FChanged write FChanged;
|
2009-05-15 23:37:10 +02:00
|
|
|
procedure AddRef;
|
|
|
|
procedure RemoveRef;
|
|
|
|
end;
|
|
|
|
|
2009-12-02 18:40:03 +01:00
|
|
|
TVirtualTile = class(TWorldItem);
|
|
|
|
|
2009-12-02 19:14:00 +01:00
|
|
|
function CompareWorldItems(const AItem1, AItem2: TWorldItem): Integer;
|
2009-08-02 13:45:26 +02:00
|
|
|
|
2009-05-15 23:37:10 +02:00
|
|
|
implementation
|
|
|
|
|
2009-08-02 13:45:26 +02:00
|
|
|
uses
|
|
|
|
UMap, UStatics;
|
|
|
|
|
2009-12-02 19:14:00 +01:00
|
|
|
function CompareWorldItems(const AItem1, AItem2: TWorldItem): Integer;
|
2009-08-02 13:45:26 +02:00
|
|
|
begin
|
2009-12-02 19:14:00 +01:00
|
|
|
if AItem1.X <> AItem2.X then
|
|
|
|
Exit(AItem1.X - AItem2.X);
|
2009-08-02 13:45:26 +02:00
|
|
|
|
2009-12-02 19:14:00 +01:00
|
|
|
if AItem1.Y <> AItem2.Y then
|
|
|
|
Exit(AItem1.Y - AItem2.Y);
|
2009-08-02 13:45:26 +02:00
|
|
|
|
2009-12-02 19:14:00 +01:00
|
|
|
Result := AItem1.Priority - AItem2.Priority;
|
2009-08-02 13:45:26 +02:00
|
|
|
if Result = 0 then
|
|
|
|
begin
|
2009-12-02 19:14:00 +01:00
|
|
|
if (AItem1 is TMapCell) and (AItem2 is TStaticItem) then
|
2009-08-02 13:45:26 +02:00
|
|
|
Result := -1
|
2009-12-02 19:14:00 +01:00
|
|
|
else if (AItem1 is TStaticItem) and (AItem2 is TMapCell) then
|
2009-12-02 18:40:03 +01:00
|
|
|
Result := 1
|
2009-12-02 19:14:00 +01:00
|
|
|
else if (AItem1 is TMapCell) and (AItem2 is TVirtualTile) then
|
2009-12-02 18:40:03 +01:00
|
|
|
Result := -1
|
2009-12-02 19:14:00 +01:00
|
|
|
else if (AItem1 is TVirtualTile) and (AItem2 is TMapCell) then
|
2009-08-02 13:45:26 +02:00
|
|
|
Result := 1;
|
|
|
|
end;
|
|
|
|
|
|
|
|
if Result = 0 then
|
2009-12-02 19:14:00 +01:00
|
|
|
Result := AItem1.PrioritySolver - AItem2.PrioritySolver;
|
2009-08-02 13:45:26 +02:00
|
|
|
end;
|
|
|
|
|
2009-05-15 23:37:10 +02:00
|
|
|
{ TWorldItem }
|
|
|
|
|
|
|
|
constructor TWorldItem.Create(AOwner: TWorldBlock);
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
FSelected := False;
|
|
|
|
FLocked := False;
|
|
|
|
FOwner := AOwner;
|
|
|
|
end;
|
|
|
|
|
2009-12-23 16:39:24 +01:00
|
|
|
procedure TWorldItem.DoChanged;
|
|
|
|
begin
|
|
|
|
if FOwner <> nil then
|
|
|
|
FOwner.Changed := True;
|
|
|
|
end;
|
|
|
|
|
2015-05-01 12:23:03 +02:00
|
|
|
function TWorldItem.GetTileID: LongWord;
|
2009-12-04 00:58:59 +01:00
|
|
|
begin
|
|
|
|
Result := FTileID;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TWorldItem.GetZ: ShortInt;
|
|
|
|
begin
|
|
|
|
Result := FZ;
|
|
|
|
end;
|
|
|
|
|
2009-05-15 23:37:10 +02:00
|
|
|
procedure TWorldItem.Delete;
|
|
|
|
begin
|
|
|
|
SetSelected(False);
|
|
|
|
SetLocked(False);
|
|
|
|
DoChanged;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TWorldItem.SetLocked(ALocked: Boolean);
|
|
|
|
begin
|
|
|
|
if FLocked <> ALocked then
|
|
|
|
begin
|
|
|
|
FLocked := ALocked;
|
2009-12-23 16:39:24 +01:00
|
|
|
if FOwner <> nil then
|
2009-05-15 23:37:10 +02:00
|
|
|
if FLocked then
|
|
|
|
FOwner.AddRef
|
|
|
|
else
|
|
|
|
FOwner.RemoveRef;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TWorldItem.SetOwner(AOwner: TWorldBlock);
|
|
|
|
begin
|
|
|
|
if FOwner <> AOwner then
|
|
|
|
begin
|
2009-12-23 16:39:24 +01:00
|
|
|
if FOwner <> nil then
|
2009-05-15 23:37:10 +02:00
|
|
|
begin
|
2009-12-23 16:39:24 +01:00
|
|
|
FOwner.Changed := True;
|
2009-05-15 23:37:10 +02:00
|
|
|
if FLocked then FOwner.RemoveRef;
|
|
|
|
if FSelected then FOwner.RemoveRef;
|
|
|
|
end;
|
|
|
|
FOwner := AOwner;
|
2009-12-23 16:39:24 +01:00
|
|
|
if FOwner <> nil then
|
2009-05-15 23:37:10 +02:00
|
|
|
begin
|
2009-12-23 16:39:24 +01:00
|
|
|
FOwner.Changed := True;
|
2009-05-15 23:37:10 +02:00
|
|
|
if FLocked then FOwner.AddRef;
|
|
|
|
if FSelected then FOwner.AddRef;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TWorldItem.SetSelected(ASelected: Boolean);
|
|
|
|
begin
|
|
|
|
if (FOwner <> nil) and (ASelected <> FSelected) then
|
|
|
|
if ASelected then
|
|
|
|
FOwner.AddRef
|
|
|
|
else
|
|
|
|
FOwner.RemoveRef;
|
|
|
|
FSelected := ASelected;
|
|
|
|
end;
|
|
|
|
|
2015-05-01 12:23:03 +02:00
|
|
|
procedure TWorldItem.SetTileID(AValue: LongWord);
|
2009-05-15 23:37:10 +02:00
|
|
|
begin
|
2009-12-23 16:39:24 +01:00
|
|
|
if FTileID = AValue then
|
|
|
|
Exit;
|
|
|
|
|
|
|
|
FTileID := AValue;
|
2009-05-15 23:37:10 +02:00
|
|
|
DoChanged;
|
|
|
|
end;
|
|
|
|
|
2009-12-23 16:39:24 +01:00
|
|
|
procedure TWorldItem.SetX(AValue: Word);
|
2009-05-15 23:37:10 +02:00
|
|
|
begin
|
2009-12-23 16:39:24 +01:00
|
|
|
if FX = AValue then
|
|
|
|
Exit;
|
|
|
|
|
|
|
|
FX := AValue;
|
2009-05-15 23:37:10 +02:00
|
|
|
DoChanged;
|
|
|
|
end;
|
|
|
|
|
2009-12-23 16:39:24 +01:00
|
|
|
procedure TWorldItem.SetY(AValue: Word);
|
2009-05-15 23:37:10 +02:00
|
|
|
begin
|
2009-12-23 16:39:24 +01:00
|
|
|
if FY = AValue then
|
|
|
|
Exit;
|
|
|
|
|
|
|
|
FY := AValue;
|
|
|
|
DoChanged;
|
2009-05-15 23:37:10 +02:00
|
|
|
end;
|
|
|
|
|
2009-12-23 16:39:24 +01:00
|
|
|
procedure TWorldItem.SetZ(AValue: ShortInt);
|
2009-05-15 23:37:10 +02:00
|
|
|
begin
|
2009-12-23 16:39:24 +01:00
|
|
|
if FZ = AValue then
|
|
|
|
Exit;
|
|
|
|
|
|
|
|
FZ := AValue;
|
2009-05-15 23:37:10 +02:00
|
|
|
DoChanged;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TWorldItem.UpdatePos(AX, AY: Word; AZ: ShortInt);
|
|
|
|
begin
|
|
|
|
FX := AX;
|
|
|
|
FY := AY;
|
|
|
|
FZ := AZ;
|
|
|
|
DoChanged;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{ TWorldBlock }
|
|
|
|
|
|
|
|
procedure TWorldBlock.AddRef;
|
|
|
|
begin
|
|
|
|
Inc(FRefCount);
|
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TWorldBlock.Create;
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
FRefCount := 0;
|
2009-12-23 16:39:24 +01:00
|
|
|
FChanged := False;
|
2009-05-15 23:37:10 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TWorldBlock.RemoveRef;
|
|
|
|
begin
|
|
|
|
if FRefCount > 0 then
|
|
|
|
Dec(FRefCount);
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|
|
|
|
|