Example objects:
Code:
TShipType = class
private
public
ShipName: String;
Picture: TDirectDrawSurface;
PictureFile: String;
MaxHP, MaxShield, MaxSpeed: Double;
HitSoundIndex: Integer;
HitBoxes: Array of TRect;
Turrets: Array of TPoint;
end;
TShip = class
private
ShieldAnimIndex: Integer;
ShieldHits: Integer;
Procedure Shoot;
Procedure HitCheck;
Procedure Explode;
Function AI: ShortInt;
public
ShipType: ^TShipType;
Position: TPoint;
HP, Shield, Speed: Double;
NoShield, Invulnerable: Boolean;
LaserType: ^TLaserType;
Lasers: Array of TPoint;
LaserCoolDown: Word;
IsWarping, IsExploding: Boolean;
WarpCounter, ExplosionCounter: Word;
Procedure WarpIn;
Procedure Update;
Procedure Draw;
Procedure DrawLasers;
end;
Picture loader for the ship's pictures:
Code:
...
If Length(ShipTypes) > 0 Then
For I := 0 To Length(ShipTypes) - 1 Do
Try
ShipTypes[I].Picture := TDirectDrawSurface.Create(Window.DXDraw.DDraw);
ShipTypes[I].Picture.LoadFromFile(Misc.GetAppDir + ShipTypes[I].PictureFile);
ShipTypes[I].Picture.TransparentColor := $FF00FF;
Except
Misc.Error('Cannot load the picture: ' + ShipTypes[I].PictureFile + ' of ship: ' + ShipTypes[I].ShipName);
end;
...
Picture loader for the shield picture:
Code:
If ShieldType.PictureFile <> '' Then
begin
ShieldType.Picture := TDirectDrawSurface.Create(Window.DXDraw.DDraw);
ShieldType.Picture.LoadFromFile(Misc.GetAppDir + ShieldType.PictureFile);
ShieldType.Picture.TransparentColor := $FF00FF;
end;
Drawing code:
Code:
Procedure TShip.Draw;
var ShipRect: TRect;
// Function to get the shield effect's alpha
Function RetShieldAlpha: Integer;
var I: Integer;
begin
Result := 100;
For I := 1 To ShieldHits Do
Result := Result + 15;
If Result > 200 Then Result := 200;
If ShieldAnimIndex < 15 Then Result := Result + Round((ShieldAnimIndex - 15) * 6.7)
end;
begin
If NOT(IsWarping AND (WarpCounter <= 30)) Then
begin
ShipRect := PicRect(Speed, ShipType.MaxSpeed, ShipType.Picture.Width, ShipType.Picture.Height);
// If Warping: (30-110. warpframe)
If IsWarping AND (WarpCounter > 30) AND (WarpCounter < 110) Then
ShipRect.Top := Round(ShipRect.Bottom - ((WarpCounter - 30) / (80 / ShipRect.Bottom)));
// Draw
Window.DXDraw.Surface.Draw(Position.X, Position.Y, ShipRect, ShipType.Picture, True);
// Shield
If ShieldAnimIndex > 0 Then Window.DXDraw.Surface.DrawAlpha(Bounds(Position.X, Position.Y + ShipType.Picture.Height + 5 - ShipRect.Top - ShieldType.Picture.Height, ShipType.Picture.Width DIV 5, ShieldType.Picture.Height), ShieldType.Picture.ClientRect, ShieldType.Picture, True, RetShieldAlpha);
// Spark
If ((HP < ShipType.MaxHP / 6) AND (Random(64) = 0)) OR ((HP < ShipType.MaxHP / 3) AND (Random(64) = 0)) Then
begin
Misc.Sounds.Play(SparkSounds[Random(2)], Round((Position.X / (800 - ShipType.Picture.Width DIV 5) * 200) - 100));
With Window.DXDraw.Surface.Canvas Do
begin
Pen.Width := 1;
Pen.Color := clAqua;
MoveTo(Random(ShipType.Picture.Width DIV 10) + Position.X + ShipType.Picture.Width DIV 20, Random(ShipType.Picture.Height) + Position.Y);
LineTo(Random(ShipType.Picture.Width DIV 10) + Position.X + ShipType.Picture.Width DIV 20, Random(ShipType.Picture.Height) + Position.Y);
Release;
end;
end;
end;
end;
P.S.: I know my code is shit at places and that spark thing is very lame, but I'm a lazy person, so don't tell me about these...
Bookmarks