| 1 |
{ |
| 2 |
Copyright 2011-2018 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Map composed from tiles. } |
| 17 |
unit GameMap; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, CastleVectors, CastleImages, CastleGLUtils, CastleGLImages; |
| 22 |
|
| 23 |
type |
| 24 |
TTile = class |
| 25 |
public |
| 26 |
DrawableImage: TDrawableImage; |
| 27 |
{ Relative URL vs tiles directory. |
| 28 |
This is read and written from/to a map file. } |
| 29 |
RelativeUrl: String; |
| 30 |
{ This is an absolute URL constructed from RelativeUrl. } |
| 31 |
function FullUrl: String; |
| 32 |
public |
| 33 |
CharCode: char; |
| 34 |
procedure LoadFromFile; virtual; abstract; |
| 35 |
destructor Destroy; override; |
| 36 |
end; |
| 37 |
|
| 38 |
{ Base ground tile. When loading from file, this will always be resized to |
| 39 |
BaseWidth x BaseHeight. |
| 40 |
|
| 41 |
It must have alpha channel set properly, |
| 42 |
so that corners are not drawn. If the image doesn't have alpha channel, |
| 43 |
alpha channel will be added and colors equal to lower-left corner |
| 44 |
will be set transparent. It's @link(Image) is always TRGBAlphaImage. } |
| 45 |
TBaseTile = class(TTile) |
| 46 |
procedure LoadFromFile; override; |
| 47 |
end; |
| 48 |
|
| 49 |
TBonusTile = class(TTile) |
| 50 |
procedure LoadFromFile; override; |
| 51 |
end; |
| 52 |
|
| 53 |
TMapTile = class |
| 54 |
{ Base ground tile. } |
| 55 |
BaseTile: TBaseTile; |
| 56 |
{ Object drawn on top of base. Usually has alpha such that |
| 57 |
base is visible beside it. May be large, covering other tiles. |
| 58 |
May be nil to indicate no object. } |
| 59 |
BonusTile: TBonusTile; |
| 60 |
end; |
| 61 |
|
| 62 |
TMap = class |
| 63 |
private |
| 64 |
procedure CommonCreate; |
| 65 |
{ Call this after Width and Height are set, this will initialize Items. } |
| 66 |
procedure CreateItems; |
| 67 |
public |
| 68 |
BaseTiles: array[char] of TBaseTile; |
| 69 |
BonusTiles: array[char] of TBonusTile; |
| 70 |
BaseTilesList: TList; |
| 71 |
BonusTilesList: TList; |
| 72 |
Items: array of array of TMapTile; |
| 73 |
{ Must be > 0 always } |
| 74 |
Width, Height: Cardinal; |
| 75 |
PlayerStartX, PlayerStartY: Cardinal; |
| 76 |
constructor Create(AWidth, AHeight: Cardinal); |
| 77 |
constructor CreateFromFile(const AUrl: String); |
| 78 |
destructor Destroy; override; |
| 79 |
procedure SaveToFile(const AUrl: String); |
| 80 |
end; |
| 81 |
|
| 82 |
implementation |
| 83 |
|
| 84 |
uses SysUtils, |
| 85 |
CastleFilesUtils, CastleUtils, CastleStringUtils, |
| 86 |
CastleClassUtils, CastleDownload, |
| 87 |
GameWindow; |
| 88 |
|
| 89 |
{ TTile ---------------------------------------------------------------------- } |
| 90 |
|
| 91 |
destructor TTile.Destroy; |
| 92 |
begin |
| 93 |
FreeAndNil(DrawableImage); |
| 94 |
inherited; |
| 95 |
end; |
| 96 |
|
| 97 |
function TTile.FullUrl: String; |
| 98 |
begin |
| 99 |
Result := 'castle-data:/tiles/' + RelativeUrl; |
| 100 |
end; |
| 101 |
|
| 102 |
{ TBaseTile ------------------------------------------------------------------ } |
| 103 |
|
| 104 |
procedure TBaseTile.LoadFromFile; |
| 105 |
var |
| 106 |
Image: TCastleImage; |
| 107 |
NewImage: TRGBAlphaImage; |
| 108 |
begin |
| 109 |
Image := LoadImage(FullUrl, [TRGBImage, TRGBAlphaImage], BaseWidth, BaseHeight); |
| 110 |
if not (Image is TRGBAlphaImage) then |
| 111 |
begin |
| 112 |
{$ifndef FPC}{$POINTERMATH ON}{$endif} |
| 113 |
NewImage := (Image as TRGBImage).ToRGBAlphaImage; |
| 114 |
NewImage.AlphaDecide( |
| 115 |
Vector3Byte( |
| 116 |
TRGBAlphaImage(Image).Pixels[0][0], |
| 117 |
TRGBAlphaImage(Image).Pixels[1][0], |
| 118 |
TRGBAlphaImage(Image).Pixels[2][0]), |
| 119 |
0, 0, 255); |
| 120 |
Writeln('Alpha added to "', RelativeUrl, '" while loading'); |
| 121 |
FreeAndNil(Image); |
| 122 |
Image := NewImage; |
| 123 |
{ This will automatically fix such images, assuming that URL |
| 124 |
extension is PNG. |
| 125 |
SaveImage(Image, FullUrl); } |
| 126 |
{$ifndef FPC}{$POINTERMATH OFF}{$endif} |
| 127 |
end; |
| 128 |
|
| 129 |
DrawableImage := TDrawableImage.Create(Image, false, true); |
| 130 |
end; |
| 131 |
|
| 132 |
{ TBonusTile ----------------------------------------------------------------- } |
| 133 |
|
| 134 |
procedure TBonusTile.LoadFromFile; |
| 135 |
var |
| 136 |
Image: TCastleImage; |
| 137 |
begin |
| 138 |
Image := LoadImage(FullUrl, PixelsImageClasses); |
| 139 |
DrawableImage := TDrawableImage.Create(Image, false, true); |
| 140 |
end; |
| 141 |
|
| 142 |
{ TMap ----------------------------------------------------------------------- } |
| 143 |
|
| 144 |
procedure TMap.CommonCreate; |
| 145 |
begin |
| 146 |
inherited Create; |
| 147 |
BaseTilesList := TList.Create; |
| 148 |
BonusTilesList := TList.Create; |
| 149 |
end; |
| 150 |
|
| 151 |
constructor TMap.Create(AWidth, AHeight: Cardinal); |
| 152 |
begin |
| 153 |
CommonCreate; |
| 154 |
Width := AWidth; |
| 155 |
Height := AHeight; |
| 156 |
CreateItems; |
| 157 |
end; |
| 158 |
|
| 159 |
constructor TMap.CreateFromFile(const AUrl: String); |
| 160 |
|
| 161 |
procedure ReadlnTileLine(const F: TTextReader; |
| 162 |
var C: char; var RelativeUrl: String); |
| 163 |
var |
| 164 |
CStr: string; |
| 165 |
begin |
| 166 |
CStr := F.Read; |
| 167 |
if Length(CStr) <> 1 then |
| 168 |
raise Exception.Create('Not a single 1st character'); |
| 169 |
C := CStr[1]; |
| 170 |
|
| 171 |
RelativeUrl := F.Read; |
| 172 |
if RelativeUrl = '' then |
| 173 |
raise Exception.CreateFmt('Empty URL after character "%s"', [C]); |
| 174 |
|
| 175 |
F.Readln; |
| 176 |
end; |
| 177 |
|
| 178 |
var |
| 179 |
F: TTextReader; |
| 180 |
BaseTilesCount, BonusTilesCount: Cardinal; |
| 181 |
C: char; |
| 182 |
S: string; |
| 183 |
I: Integer; |
| 184 |
X, Y: Cardinal; |
| 185 |
begin |
| 186 |
CommonCreate; |
| 187 |
|
| 188 |
F := TTextReader.Create(AUrl); |
| 189 |
try |
| 190 |
Width := F.ReadInteger; |
| 191 |
Height := F.ReadInteger; |
| 192 |
PlayerStartX := F.ReadInteger; |
| 193 |
PlayerStartY := F.ReadInteger; |
| 194 |
BaseTilesCount := F.ReadInteger; |
| 195 |
BonusTilesCount := F.ReadInteger; |
| 196 |
|
| 197 |
if (Width = 0) or (Height = 0) then |
| 198 |
raise Exception.Create('Map width and height must be > 0'); |
| 199 |
|
| 200 |
for I := 0 to Integer(BaseTilesCount) - 1 do |
| 201 |
begin |
| 202 |
ReadlnTileLine(F, C, S); |
| 203 |
BaseTiles[C] := TBaseTile.Create; |
| 204 |
BaseTiles[C].CharCode := C; |
| 205 |
BaseTiles[C].RelativeUrl := S; |
| 206 |
BaseTiles[C].LoadFromFile; |
| 207 |
BaseTilesList.Add(BaseTiles[C]); |
| 208 |
end; |
| 209 |
|
| 210 |
for I := 0 to Integer(BonusTilesCount) - 1 do |
| 211 |
begin |
| 212 |
ReadlnTileLine(F, C, S); |
| 213 |
if C = '_' then |
| 214 |
raise Exception.Create('Bonus tile character cannot be "_"'); |
| 215 |
BonusTiles[C] := TBonusTile.Create; |
| 216 |
BonusTiles[C].CharCode := C; |
| 217 |
BonusTiles[C].RelativeUrl := S; |
| 218 |
BonusTiles[C].LoadFromFile; |
| 219 |
BonusTilesList.Add(BonusTiles[C]); |
| 220 |
end; |
| 221 |
|
| 222 |
CreateItems; |
| 223 |
|
| 224 |
for Y := Height - 1 downto 0 do |
| 225 |
begin |
| 226 |
S := F.Readln; |
| 227 |
if Cardinal(Length(S)) <> Width * 2 then |
| 228 |
raise Exception.CreateFmt('Map line %d has wrong length (%d instead of %d)', |
| 229 |
[Y, Cardinal(Length(S)), Width * 2]); |
| 230 |
for X := 0 to Width - 1 do |
| 231 |
begin |
| 232 |
C := S[X*2 + 1]; |
| 233 |
Items[X, Y].BaseTile := BaseTiles[C]; |
| 234 |
if Items[X, Y].BaseTile = nil then |
| 235 |
raise Exception.CreateFmt('Base tile character "%s" not initialized, ' + |
| 236 |
'but used on map position (%d, %d)', [C, X, Y]); |
| 237 |
C := S[X*2 + 2]; |
| 238 |
if C <> '_' then |
| 239 |
begin |
| 240 |
Items[X, Y].BonusTile := BonusTiles[C]; |
| 241 |
if Items[X, Y].BonusTile = nil then |
| 242 |
raise Exception.CreateFmt('Bonus tile character "%s" not initialized, ' + |
| 243 |
'but used on map position (%d, %d)', [C, X, Y]); |
| 244 |
end else |
| 245 |
Items[X, Y].BonusTile := nil; |
| 246 |
end; |
| 247 |
end; |
| 248 |
finally FreeAndNil(F) end; |
| 249 |
end; |
| 250 |
|
| 251 |
procedure TMap.CreateItems; |
| 252 |
var |
| 253 |
X, Y: Cardinal; |
| 254 |
begin |
| 255 |
SetLength(Items, Width, Height); |
| 256 |
for X := 0 to Width - 1 do |
| 257 |
for Y := 0 to Height - 1 do |
| 258 |
Items[X, Y] := TMapTile.Create; |
| 259 |
end; |
| 260 |
|
| 261 |
destructor TMap.Destroy; |
| 262 |
var |
| 263 |
X, Y: Integer; |
| 264 |
C: char; |
| 265 |
begin |
| 266 |
{ We can't assume that Width and Height are > 0 here, |
| 267 |
they are possibly not initialized. That's why X, Y must be Integer, |
| 268 |
not Cardinal. } |
| 269 |
for X := 0 to Length(Items) - 1 do |
| 270 |
for Y := 0 to Length(Items[X]) - 1 do |
| 271 |
FreeAndNil(Items[X, Y]); |
| 272 |
|
| 273 |
for C := Low(C) to High(C) do |
| 274 |
begin |
| 275 |
FreeAndNil(BaseTiles[C]); |
| 276 |
FreeAndNil(BonusTiles[C]); |
| 277 |
end; |
| 278 |
|
| 279 |
FreeAndNil(BaseTilesList); |
| 280 |
FreeAndNil(BonusTilesList); |
| 281 |
|
| 282 |
inherited; |
| 283 |
end; |
| 284 |
|
| 285 |
procedure TMap.SaveToFile(const AUrl: String); |
| 286 |
var |
| 287 |
F: TStream; |
| 288 |
S: string; |
| 289 |
I: Integer; |
| 290 |
X, Y: Cardinal; |
| 291 |
begin |
| 292 |
F := UrlSaveStream(AUrl); |
| 293 |
try |
| 294 |
WritelnStr(F, Format('%d %d', [Width, Height])); |
| 295 |
WritelnStr(F, Format('%d %d', [PlayerStartX, PlayerStartY])); |
| 296 |
WritelnStr(F, Format('%d %d', [BaseTilesList.Count, BonusTilesList.Count])); |
| 297 |
|
| 298 |
for I := 0 to BaseTilesList.Count - 1 do |
| 299 |
WritelnStr(F, Format('%s %s', [ |
| 300 |
TBaseTile(BaseTilesList[I]).CharCode, |
| 301 |
TBaseTile(BaseTilesList[I]).RelativeUrl])); |
| 302 |
|
| 303 |
for I := 0 to BonusTilesList.Count - 1 do |
| 304 |
WritelnStr(F, Format('%s %s', [ |
| 305 |
TBonusTile(BonusTilesList[I]).CharCode, |
| 306 |
TBonusTile(BonusTilesList[I]).RelativeUrl])); |
| 307 |
|
| 308 |
for Y := Height - 1 downto 0 do |
| 309 |
begin |
| 310 |
SetLength(S, Width * 2); |
| 311 |
for X := 0 to Width - 1 do |
| 312 |
begin |
| 313 |
S[X*2 + 1] := Items[X, Y].BaseTile.CharCode; |
| 314 |
if Items[X, Y].BonusTile <> nil then |
| 315 |
S[X*2 + 2] := Items[X, Y].BonusTile.CharCode else |
| 316 |
S[X*2 + 2] := '_'; |
| 317 |
end; |
| 318 |
WritelnStr(F, S); |
| 319 |
end; |
| 320 |
finally FreeAndNil(F) end; |
| 321 |
end; |
| 322 |
|
| 323 |
end. |