| 1 |
{ |
| 2 |
Copyright 2018-2023 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 |
{ Unit (soldier) on a map. } |
| 17 |
unit GameUnit; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, Contnrs, |
| 22 |
CastleTransform, CastleComponentSerialize, CastleVectors, CastleScene, |
| 23 |
CastleTiledMap; |
| 24 |
|
| 25 |
type |
| 26 |
TUnitKind = (ukAlienLight, ukAlienHeavy, ukHumanLight, ukHumanHeavy); |
| 27 |
|
| 28 |
TUnit = class; |
| 29 |
|
| 30 |
TUnitsOnMap = class(TComponent) |
| 31 |
private |
| 32 |
FItems: array of array of TUnit; |
| 33 |
FUnits: TComponentList; |
| 34 |
strict private |
| 35 |
FMap: TCastleTiledMap; |
| 36 |
FUnitsCount: Integer; |
| 37 |
function GetUnitOnMap(const TilePosition: TVector2Integer): TUnit; |
| 38 |
function GetUnits(const Index: Integer): TUnit; |
| 39 |
procedure SetUnitsCount(const AValue: Integer); |
| 40 |
public |
| 41 |
constructor Create(const AOwner: TComponent; |
| 42 |
const AMap: TCastleTiledMap); reintroduce; |
| 43 |
destructor Destroy; override; |
| 44 |
|
| 45 |
{ What unit is present at each map tile (@nil if none). |
| 46 |
|
| 47 |
You cannot change it directly, instead |
| 48 |
- add new TUnit, |
| 49 |
- destroy a TUnit, |
| 50 |
- change TUnit.TilePosition. |
| 51 |
|
| 52 |
In other words, changes to TUnit are automatically reflected here. } |
| 53 |
property Items[const TilePosition: TVector2Integer]: TUnit read GetUnitOnMap; default; |
| 54 |
|
| 55 |
function UnitsCount: Integer; |
| 56 |
property Units[const Index: Integer]: TUnit read GetUnits; |
| 57 |
|
| 58 |
property Map: TCastleTiledMap read FMap; |
| 59 |
|
| 60 |
function IsWater(const TilePosition: TVector2Integer): Boolean; |
| 61 |
end; |
| 62 |
|
| 63 |
TUnit = class(TComponent) |
| 64 |
private |
| 65 |
class var |
| 66 |
TransformTemplate: TSerializedComponent; |
| 67 |
strict private |
| 68 |
var |
| 69 |
FKind: TUnitKind; |
| 70 |
FAttack, FLife, FMovement: Integer; |
| 71 |
FInitialMovement: Integer; |
| 72 |
ImageIcon: TCastleImageTransform; |
| 73 |
TextAttack: TCastleText; |
| 74 |
TextLife: TCastleText; |
| 75 |
TextMovement: TCastleText; |
| 76 |
Background: TCastleImageTransform; |
| 77 |
FTilePosition: TVector2Integer; |
| 78 |
FUnitsOnMap: TUnitsOnMap; |
| 79 |
procedure SetTilePosition(const Value: TVector2Integer); |
| 80 |
procedure SetUnitsOnMap(const Value: TUnitsOnMap); |
| 81 |
procedure PlaceOnMap; |
| 82 |
procedure RemoveFromMap; |
| 83 |
property UnitsOnMap: TUnitsOnMap read FUnitsOnMap write SetUnitsOnMap; |
| 84 |
procedure SetLife(const Value: Integer); |
| 85 |
procedure SetMovement(const Value: Integer); |
| 86 |
protected |
| 87 |
procedure Notification(AComponent: TComponent; Operation: TOperation); override; |
| 88 |
public |
| 89 |
Transform: TCastleTransform; |
| 90 |
constructor Create(AOwner: TComponent); override; |
| 91 |
destructor Destroy; override; |
| 92 |
procedure Initialize(const AUnitsOnMap: TUnitsOnMap; |
| 93 |
const AKind: TUnitKind; const AnAttack, ALife, AMovement: Integer); overload; |
| 94 |
{ Initialize, choosing default statistics for this TUnitKind. } |
| 95 |
procedure Initialize(const AUnitsOnMap: TUnitsOnMap; |
| 96 |
const AKind: TUnitKind); overload; |
| 97 |
function ToString: String; override; |
| 98 |
function Human: Boolean; |
| 99 |
|
| 100 |
{ Can this unit move to given new position. |
| 101 |
Returns @true if the tile is empty and possible to walk into (not water), |
| 102 |
or occupied by the enemy (in which case we do attack).} |
| 103 |
function CanMove(const NewTilePosition: TVector2Integer): Boolean; |
| 104 |
|
| 105 |
property Kind: TUnitKind read FKind; |
| 106 |
property Attack: Integer read FAttack; |
| 107 |
property InitialMovement: Integer read FInitialMovement; |
| 108 |
{ You can change unit life. Setting life to <= 0 frees the unit instance, |
| 109 |
removing it also from the map. } |
| 110 |
property Life: Integer read FLife write SetLife; |
| 111 |
property Movement: Integer read FMovement write SetMovement; |
| 112 |
property TilePosition: TVector2Integer read FTilePosition write SetTilePosition; |
| 113 |
end; |
| 114 |
|
| 115 |
const |
| 116 |
ZUnit = 100.0; |
| 117 |
ZHover = 200.0; |
| 118 |
|
| 119 |
implementation |
| 120 |
|
| 121 |
uses SysUtils, TypInfo, Math, |
| 122 |
CastleRectangles, CastleStringUtils, CastleColors, CastleViewport; |
| 123 |
|
| 124 |
{ TUnitsOnMap ---------------------------------------------------------------- } |
| 125 |
|
| 126 |
constructor TUnitsOnMap.Create(const AOwner: TComponent; |
| 127 |
const AMap: TCastleTiledMap); |
| 128 |
begin |
| 129 |
inherited Create(AOwner); |
| 130 |
FMap := AMap; |
| 131 |
SetLength(FItems, Map.Data.Width, Map.Data.Height); |
| 132 |
FUnits := TComponentList.Create(false); |
| 133 |
end; |
| 134 |
|
| 135 |
destructor TUnitsOnMap.Destroy; |
| 136 |
begin |
| 137 |
FreeAndNil(FUnits); |
| 138 |
inherited; |
| 139 |
end; |
| 140 |
|
| 141 |
function TUnitsOnMap.GetUnitOnMap(const TilePosition: TVector2Integer): TUnit; |
| 142 |
begin |
| 143 |
Result := FItems[TilePosition.X, TilePosition.Y]; |
| 144 |
end; |
| 145 |
|
| 146 |
function TUnitsOnMap.UnitsCount: Integer; |
| 147 |
begin |
| 148 |
Result := FUnits.Count; |
| 149 |
end; |
| 150 |
|
| 151 |
function TUnitsOnMap.GetUnits(const Index: Integer): TUnit; |
| 152 |
begin |
| 153 |
Result := FUnits[Index] as TUnit; |
| 154 |
end; |
| 155 |
|
| 156 |
procedure TUnitsOnMap.SetUnitsCount(const AValue: Integer); |
| 157 |
begin |
| 158 |
if FUnitsCount = AValue then Exit; |
| 159 |
FUnitsCount := AValue; |
| 160 |
end; |
| 161 |
|
| 162 |
function TUnitsOnMap.IsWater(const TilePosition: TVector2Integer): Boolean; |
| 163 |
var |
| 164 |
Tileset: TCastleTiledMapData.TTileset; |
| 165 |
Frame: Integer; |
| 166 |
HorizontalFlip, VerticalFlip, DiagonalFlip: Boolean; |
| 167 |
begin |
| 168 |
Result := Map.Data.TileRenderData(TilePosition, |
| 169 |
Map.Data.Layers[0], |
| 170 |
Tileset, Frame, HorizontalFlip, VerticalFlip, DiagonalFlip) and |
| 171 |
{ Water is on 1, 5, 9 tiles (counting from 0) in data/maps/tileset-terrain.png . } |
| 172 |
((Frame mod 4) = 1); |
| 173 |
end; |
| 174 |
|
| 175 |
{ TUnit ----------------------------------------------------------------------- } |
| 176 |
|
| 177 |
constructor TUnit.Create(AOwner: TComponent); |
| 178 |
begin |
| 179 |
inherited; |
| 180 |
if TransformTemplate = nil then |
| 181 |
TransformTemplate := TSerializedComponent.Create('castle-data:/unit.castle-transform'); |
| 182 |
Transform := TransformTemplate.TransformLoad(Self); |
| 183 |
|
| 184 |
Background := FindRequiredComponent('Background') as TCastleImageTransform; |
| 185 |
ImageIcon := FindRequiredComponent('ImageIcon') as TCastleImageTransform; |
| 186 |
TextAttack := FindRequiredComponent('TextAttack') as TCastleText; |
| 187 |
TextLife := FindRequiredComponent('TextLife') as TCastleText; |
| 188 |
TextMovement := FindRequiredComponent('TextMovement') as TCastleText; |
| 189 |
end; |
| 190 |
|
| 191 |
procedure TUnit.Initialize(const AUnitsOnMap: TUnitsOnMap; |
| 192 |
const AKind: TUnitKind; |
| 193 |
const AnAttack, ALife, AMovement: Integer); |
| 194 |
const |
| 195 |
UnitIconUrls: array [TUnitKind] of string = |
| 196 |
( 'castle-data:/units/alien1.png', |
| 197 |
'castle-data:/units/alien2.png', |
| 198 |
'castle-data:/units/human1.png', |
| 199 |
'castle-data:/units/human2.png' |
| 200 |
); |
| 201 |
begin |
| 202 |
FKind := AKind; |
| 203 |
FAttack := AnAttack; |
| 204 |
FLife := ALife; |
| 205 |
FInitialMovement := AMovement; |
| 206 |
FMovement := AMovement; |
| 207 |
|
| 208 |
// adjust components in Transform |
| 209 |
ImageIcon.URL := UnitIconUrls[AKind]; |
| 210 |
// change Background.Color (RGB, leave alpha as it was) |
| 211 |
if Human then |
| 212 |
Background.Color := ColorOpacity(HexToColor('4CFF6B'), Background.Color.W) |
| 213 |
else |
| 214 |
Background.Color := ColorOpacity(HexToColor('FF664C'), Background.Color.W); |
| 215 |
TextAttack.Caption := IntToStr(Attack); |
| 216 |
TextLife.Caption := IntToStr(Life); |
| 217 |
TextMovement.Caption := IntToStr(Movement); |
| 218 |
|
| 219 |
UnitsOnMap := AUnitsOnMap; |
| 220 |
UnitsOnMap.FUnits.Add(Self); |
| 221 |
|
| 222 |
PlaceOnMap; |
| 223 |
end; |
| 224 |
|
| 225 |
procedure TUnit.Initialize(const AUnitsOnMap: TUnitsOnMap; const AKind: TUnitKind); |
| 226 |
var |
| 227 |
Heavy: Boolean; |
| 228 |
AnAttack, ALife, AMovement: Integer; |
| 229 |
begin |
| 230 |
Heavy := AKind in [ukAlienHeavy, ukHumanHeavy]; |
| 231 |
AnAttack := IfThen(Heavy, 7, 3); |
| 232 |
ALife := IfThen(Heavy, 20, 10); |
| 233 |
AMovement := IfThen(Heavy, 2, 5); |
| 234 |
Initialize(AUnitsOnMap, AKind, AnAttack, ALife, AMovement); |
| 235 |
end; |
| 236 |
|
| 237 |
destructor TUnit.Destroy; |
| 238 |
begin |
| 239 |
if UnitsOnMap <> nil then |
| 240 |
begin |
| 241 |
UnitsOnMap.FUnits.Remove(Self); |
| 242 |
RemoveFromMap; |
| 243 |
UnitsOnMap := nil; |
| 244 |
end; |
| 245 |
inherited; |
| 246 |
end; |
| 247 |
|
| 248 |
procedure TUnit.PlaceOnMap; |
| 249 |
var |
| 250 |
R: TFloatRectangle; |
| 251 |
Scale: Single; |
| 252 |
const |
| 253 |
{ Must correspond to the unit.castle-transform original size (see in editor at design-time). } |
| 254 |
UnitDesignedSize = 256.0; |
| 255 |
{ Additional unit scale to look better. } |
| 256 |
UnitScale = 0.8; |
| 257 |
{ Additional unit scale to look better on Isometric maps. } |
| 258 |
UnitScaleIsometric = 0.75; |
| 259 |
begin |
| 260 |
if UnitsOnMap <> nil then |
| 261 |
begin |
| 262 |
UnitsOnMap.FItems[TilePosition.X, TilePosition.Y] := Self; |
| 263 |
|
| 264 |
R := UnitsOnMap.Map.TileRectangle(TilePosition); |
| 265 |
Transform.Translation := Vector3(R.Center, ZUnit); |
| 266 |
|
| 267 |
{ The unit.castle-transform is designed for tile size = 256. |
| 268 |
Adjust it to match the actual tile size. } |
| 269 |
Scale := UnitScale * R.Height / UnitDesignedSize; |
| 270 |
{ In case of isometric map, the displayed units should be smaller, |
| 271 |
otherwise it's too crowdy on the map. } |
| 272 |
if UnitsOnMap.Map.Data.Orientation in [moIsometric, moIsometricStaggered] then |
| 273 |
Scale := Scale * UnitScaleIsometric; |
| 274 |
Transform.Scale := Vector3(Scale, Scale, 1.0); |
| 275 |
|
| 276 |
Transform.Exists := true; |
| 277 |
end; |
| 278 |
end; |
| 279 |
|
| 280 |
procedure TUnit.RemoveFromMap; |
| 281 |
begin |
| 282 |
if (UnitsOnMap <> nil) and |
| 283 |
(UnitsOnMap.FItems[TilePosition.X, TilePosition.Y] = Self) then |
| 284 |
UnitsOnMap.FItems[TilePosition.X, TilePosition.Y] := nil; |
| 285 |
|
| 286 |
Transform.Exists := false; |
| 287 |
end; |
| 288 |
|
| 289 |
procedure TUnit.SetTilePosition(const Value: TVector2Integer); |
| 290 |
begin |
| 291 |
RemoveFromMap; |
| 292 |
FTilePosition := Value; |
| 293 |
PlaceOnMap; |
| 294 |
end; |
| 295 |
|
| 296 |
function TUnit.ToString: String; |
| 297 |
begin |
| 298 |
Result := Format('%s (Attack:%d, Life:%d, Movement:%d)', [ |
| 299 |
SEnding(GetEnumName(TypeInfo(TUnitKind), Ord(Kind)), 3), |
| 300 |
Attack, |
| 301 |
Life, |
| 302 |
Movement |
| 303 |
]); |
| 304 |
end; |
| 305 |
|
| 306 |
function TUnit.Human: Boolean; |
| 307 |
begin |
| 308 |
Result := FKind in [ukHumanLight, ukHumanHeavy]; |
| 309 |
end; |
| 310 |
|
| 311 |
function TUnit.CanMove(const NewTilePosition: TVector2Integer): Boolean; |
| 312 |
const |
| 313 |
{ Both true and false work OK, change to determine |
| 314 |
whether you can move/attack along diagonals. } |
| 315 |
CornersAreNeighbors = true; |
| 316 |
var |
| 317 |
UnitOnNewPosition: TUnit; |
| 318 |
begin |
| 319 |
Result := |
| 320 |
(UnitsOnMap <> nil) and |
| 321 |
(Movement > 0) and |
| 322 |
// can only move over neighbor tile, that is not water |
| 323 |
(UnitsOnMap.Map.Data.TileNeighbor( |
| 324 |
TilePosition, NewTilePosition, CornersAreNeighbors)) and |
| 325 |
not UnitsOnMap.IsWater(NewTilePosition); |
| 326 |
|
| 327 |
// cannot move over a unit of the same side |
| 328 |
if Result then |
| 329 |
begin |
| 330 |
UnitOnNewPosition := UnitsOnMap[NewTilePosition]; |
| 331 |
if (UnitOnNewPosition <> nil) and (UnitOnNewPosition.Human = Human) then |
| 332 |
Exit(false); |
| 333 |
end; |
| 334 |
end; |
| 335 |
|
| 336 |
procedure TUnit.Notification(AComponent: TComponent; Operation: TOperation); |
| 337 |
begin |
| 338 |
inherited; |
| 339 |
if (Operation = opRemove) and (AComponent = FUnitsOnMap) then |
| 340 |
{ set to nil by SetUnitsOnMap to clean nicely } |
| 341 |
UnitsOnMap := nil; |
| 342 |
end; |
| 343 |
|
| 344 |
procedure TUnit.SetUnitsOnMap(const Value: TUnitsOnMap); |
| 345 |
begin |
| 346 |
if FUnitsOnMap <> Value then |
| 347 |
begin |
| 348 |
if FUnitsOnMap <> nil then |
| 349 |
FUnitsOnMap.RemoveFreeNotification(Self); |
| 350 |
FUnitsOnMap := Value; |
| 351 |
if FUnitsOnMap <> nil then |
| 352 |
FUnitsOnMap.FreeNotification(Self); |
| 353 |
end; |
| 354 |
end; |
| 355 |
|
| 356 |
procedure TUnit.SetLife(const Value: Integer); |
| 357 |
begin |
| 358 |
if FLife <> Value then |
| 359 |
begin |
| 360 |
FLife := Value; |
| 361 |
if Value > 0 then |
| 362 |
TextLife.Caption := IntToStr(Life) |
| 363 |
else |
| 364 |
Self.Destroy; |
| 365 |
end; |
| 366 |
end; |
| 367 |
|
| 368 |
procedure TUnit.SetMovement(const Value: Integer); |
| 369 |
begin |
| 370 |
if FMovement <> Value then |
| 371 |
begin |
| 372 |
FMovement := Value; |
| 373 |
TextMovement.Caption := IntToStr(Movement); |
| 374 |
end; |
| 375 |
end; |
| 376 |
|
| 377 |
initialization |
| 378 |
|
| 379 |
finalization |
| 380 |
FreeAndNil(TUnit.TransformTemplate); |
| 381 |
end. |