CODE
unit MapPicUnit;
interface
uses classes, SysUtils, Graphics, ExtCtrls;
type
TListMapRecord = record
MapID : Word;
Positon : Longword;
end;
TOnShowMap = procedure (Sender: TObject; Pic: TBitMap)of Object;
TMapPic = Class(TComponent)
private
FMapList : Array of TListMapRecord;
FMapFile: String;
FShowMap: Word;
FOnShowMap: TOnShowMap;
procedure SetShowMap(const Value: Word);
procedure AddMapToList(const Value: String);
procedure SetMapFile(const Value: String);
public
Constructor Create(AOwner : TComponent); Override;
Destructor Destroy; Override;
function MapID(P: Integer):Word;
procedure DoOnShowMap(P: TBitMap);
published
property MapFile: String read FMapFile write SetMapFile;
property ShowMap: Word read FShowMap write SetShowMap;
//Event
property OnShowMap: TOnShowMap read FOnShowMap write FOnShowMap;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('WLBot',[TMapPic]);
end;
{ TMapPic }
procedure TMapPic.AddMapToList(const Value: String);
var F : File of Byte;
Len : Word;
D : Array of Byte;
I, II : Integer;
Fsize : Longint;
Name : String;
begin
assignfile(F,Value);
reset(F);
try
Fsize:=Filesize(f);
seek(F,Fsize-2);
setlength(D,2);
read(F,D[0]); read(F,D[1]);
move(d[0],Len,2);
setlength(FMapList,Len);
setlength(D,29);
Seek(F,Fsize-2-(Len*29));
setlength(Name,5);
for I := 0 to Len - 1 do
begin
for II := 0 to 28 do
read(F,D[iI]);
move(D[1],Name[1],5);
FMapList[I].MapID:=strtoint(Name);
move(D[21],FMapList[I].Positon,4);
end;
finally
closefile(F);
end;
end;
constructor TMapPic.Create(AOwner: TComponent);
begin
inherited;
FMapFile:='C:\Program Files\Onnud20\WLOnline\data\Ground.MMG';
FShowMap:=0;
end;
destructor TMapPic.Destroy;
begin
inherited;
end;
procedure TMapPic.DoOnShowMap(P: TBitMap);
begin
if assigned(FOnShowMap) then
FOnShowMap(self,P);
end;
function TMapPic.MapID(P: Integer): Word;
begin
Result := 0;
if Length(FMapFile)>0 then
Result := FMapList[P].MapID;
end;
procedure TMapPic.SetMapFile(const Value: String);
begin
FMapFile := Value;
if Fileexists(Value) then
begin
AddMapToList(Value)
end;
end;
procedure TMapPic.SetShowMap(const Value: Word);
var I, II,Ix, Iy:integer;
F:file of byte;
B:byte;
A:array[1..8]of byte;
X,Y:word;
Plus:byte;
P:tbitmap;
AX, AY: LongWord;
begin
if fileexists(FMapFile) then
begin
if length(FMapList)=0 then
addmaptolist(FMapFile);
FShowMap := Value;
P := Tbitmap.Create;
assignfile(f,FMapFile);
reset(f);
try
for I := 0 to length(FMapList)-1 do
if FMapList[I].MapID = Value then
begin
seek(f,FMapList[I].Positon);
for II:=1 to 8 do
read(F,A[i]);
move(A[1],AX,4); move(a[5],AY,4);
read(F,Plus);
Plus:=(Plus * 6)+1;
seek(F,FMapList[I].Positon+8+plus);
for II:= 1 to 4 do
read(f,a[II]);
move(A[1],X,2); move(A[3],Y,2);
P.Width:=X;
P.Height:=Y;
for Ix:=0 to X-1 do
for Iy:=0 to Y-1 do
begin
read(f,B);
with P do
case B of
0 : canvas.Pixels[Ix,Iy]:= clWhite;
1 : canvas.Pixels[Ix,Iy]:= clBlack;
2 : canvas.Pixels[Ix,Iy]:= clAqua;
end;
end;
DoOnShowMap(P);
end;
finally
P.Free;
CloseFile(F);
end;
end;
end;
end.
เป็น Component ของ Delphi ที่ผมทำเองครับ เอาไว้โชว์ map
เวลาใช้ก็ใส่ ชื่อ file Ground.MMG เข้าไปตรงช่อง MapFile
แล้วเวลาจะให้โชว์นะครับ ก็ใส่ เลข map ลงไป ที่ ShowMap (ตอน runtime นะครับ)
แล้วก็กำหนด event OnShowMap ให้เอา Pic ซึ่งเป็น TbitMap ไปใช้นะครับ
นอกนั้นจะทำอะไรก็ต่อยอดกันเองนะครับ
ปล. ขอบคุณ คุณ CodeGear ที่สอนผมทำ Component