unit ObjectDamageRegion;

interface

uses
  CustomObject, Vec2f, IniFiles, Collision, System.SysUtils;

type
  TObjectDamageRegion = class(TCustomObject)
  private
    FRegionPosition: TVec2i;
    FRegionSize: TVec2i;
    FDamage: Integer;
  public

  published
    procedure Load(Aini: TIniFile); override;
    procedure SetActive(AAction: Boolean); override;

  end;

implementation

uses
  Resources, Level.Map, GameMode.Planet;

procedure TObjectDamageRegion.Load(Aini: TIniFile);
var
  Str: String;
begin
  inherited;
  Str := 'Obj_' + IntToStr(ID);
  FRegionPosition := TVec2i.Create(Aini.ReadInteger(Str, 'Left', -1), Aini.ReadInteger(Str, 'Top', -1));
  FRegionSize := TVec2i.Create(Aini.ReadInteger(Str, 'Width', 3), Aini.ReadInteger(Str, 'Height', 3));
  FDamage := Aini.ReadInteger(Str, 'Damage', 10);
end;

procedure TObjectDamageRegion.SetActive(AAction: Boolean);
var
  X, Y: Integer;
  pos: TVec2i;
begin
  if Action <> AAction then
  begin
    inherited;
    pos := Field.Position + FRegionPosition;
    with TGlobals.GameModes.Level.LevelMap do
      for Y := pos.Y to pos.Y + FRegionSize.Y do
        for X := pos.X to pos.X + FRegionSize.X do
        begin
          if Action then
            Fields[X, Y].Damage := Fields[X, Y].Damage + FDamage
          else
            Fields[X, Y].Damage := Fields[X, Y].Damage - FDamage;
        end;
  end;
end;

end.
