📄 hgespriteengine.pas
字号:
unit HGESpriteEngine;
(*
** HGE Sprite Engine helper class
** Extension to the HGE engine
** Extension added by DraculaLin
** This extension is NOT part of the original HGE engine.
*)
interface
uses
Windows, Types, Classes, SysUtils, Math, HGE, HGEDef, HGEImages, HGECanvas,
HGENURBS;
type
TCollideMode = (cmCircle, cmRect, cmQuadrangle, cmPolygon);
TAnimPlayMode = (pmForward, pmBackward, pmPingPong);
TJumpState = (jsNone, jsJumping, jsFalling);
TImageType = (itSingleImage, itSpriteSheet);
TGUIType = (gtNormal, gtForm, gtButton, gtScrollBar, gtEdit);
{ ESpriteError }
ESpriteError = class(Exception);
TSpriteEngine = class;
TSpriteClass = class of TSprite;
TSprite = class
private
FEngine: TSpriteEngine;
FParent: TSprite;
FList: TList;
FDrawList: TList;
FDeaded: Boolean;
FWidth: Integer;
FHeight: Integer;
FName: string;
FX, FY: Single;
FZ: Integer;
FWorldX, FWorldY: Single;
FVisible: Boolean;
FBlendMode: Integer;
FDoCollision: Boolean;
FCollisioned: Boolean;
FImageName: string;
FPatternIndex: Integer;
FImageIndex: Integer;
FMoved: Boolean;
FTruncMove: Boolean;
FCollidePos: TPoint2;
FCollideRadius: Integer;
FCollideRect: TRect;
FTag: Integer;
FCollideQuadrangle: TPoint4;
FCollidePolygon : TPolygon;
FCollideMode: TCollideMode;
FZSet: Boolean;
procedure Add(Sprite: TSprite);
procedure Remove(Sprite: TSprite);
procedure AddDrawList(Sprite: TSprite);
procedure Draw; virtual;
function GetCount: Integer;
function GetItem(Index: Integer): TSprite;
function GetImageWidth: Integer;
function GetImageHeight: Integer;
function GetPatternWidth: Integer;
function GetPatternHeight: Integer;
function GetPatternCount: Integer;
protected
procedure DoDraw; virtual;
procedure DoMove(const MoveCount: Single); virtual;
procedure DoCollision(const Sprite: TSprite); virtual;
procedure SetName(const Value: string); virtual;
procedure SetImageName(const Value: string); virtual;
procedure SetPatternIndex(const Value: Integer); virtual;
procedure SetX(const Value: Single); virtual;
procedure SetY(const Value: Single); virtual;
procedure SetZ(const Value: Integer); virtual;
public
constructor Create(const AParent: TSprite); virtual;
destructor Destroy; override;
procedure Assign(const Value: TSprite); virtual;
procedure Clear;
procedure Move(const MoveCount: Single);
procedure SetPos(X, Y: Single); overload;
procedure SetPos(X, Y: Single; Z: Integer); overload;
procedure Collision(const Other: TSprite); overload; virtual;
procedure Collision; overload; virtual;
procedure Dead;
property Visible: Boolean read FVisible write FVisible;
property X: Single read FX write SetX;
property Y: Single read FY write SetY;
property Z: Integer read FZ write SetZ;
property ImageName: string read FImageName write SetImageName;
property PatternIndex: Integer read FPatternIndex write SetPatternIndex;
property ImageIndex : Integer read FImageIndex write FImageIndex;
property ImageWidth: Integer read GetImageWidth;
property ImageHeight: Integer read GetImageHeight;
property PatternWidth: Integer read GetPatternWidth;
property PatternHeight: Integer read GetPatternHeight;
property Width: Integer read FWidth write FWidth ;
property Height:Integer read FHeight write FHeight ;
property PatternCount: Integer read GetPatternCount;
property WorldX: Single read FWorldX write FWorldX;
property WorldY: Single read FWorldY write FWorldY;
property BlendMode: Integer read FBlendMode write FBlendMode;
property Name: string read FName write SetName;
property Moved: Boolean read FMoved write FMoved;
property TruncMove: Boolean read FTruncMove write FTruncMove;
property CollidePos: TPoint2 read FCollidePos write FCollidePos;
property CollideRadius: Integer read FCollideRadius write FCollideRadius;
property CollideRect: TRect read FCollideRect write FCollideRect;
property CollideQuadrangle: TPoint4 read FCollideQuadrangle write FCollideQuadrangle;
property CollidePolygon : TPolygon read FCollidePolygon write FCollidePolygon;
property CollideMode: TCollideMode read FCollideMode write FCollideMode;
property Collisioned: Boolean read FCollisioned write FCollisioned;
property Items[Index: Integer]: TSprite read GetItem; default;
property Count: Integer read GetCount;
property Engine: TSpriteEngine read FEngine write FEngine;
property Parent: TSprite read FParent;
property Tag: Integer read FTag write FTag;
end;
TSpriteEx=class(TSprite)
private
FX1, FY1, FX2, FY2, FX3, FY3, FX4, FY4: Single;
FMirrorX, FMirrorY: Boolean;
FCenterX, FCenterY: Single;
FDoCenter: Boolean;
FColor1, FColor2, FColor3, FColor4: Cardinal;
FRed, FGreen, FBlue: Integer;
FAlpha: Integer;
FAngle: Single;
FAngle360: Integer;
FSrcAngle, FDestAngle: Single;
FScaleX, FScaleY: Single;
FOffsetX, FOffsetY: Single;
FDrawMode: Integer;
FImageType: TImageType;
FSelected : Boolean;
FGroupNumber : Integer;
FMouseEnterFlag: Boolean;
FMouseDownFlag: Boolean;
FActiveRect : TRect;
FSpeedX, FSpeedY: Single;
FPositionListX, FPositionListY: TList;
FAttachTo: TSprite;
FCanCollision: Boolean;
protected
procedure SetRed(const Value: Integer); virtual;
procedure SetGreen(const Value: Integer); virtual;
procedure SetBlue(const Value: Integer); virtual;
procedure SetAlpha(const Value: Integer); virtual;
procedure SetDrawMode(const Value: Integer); virtual;
procedure SetAngle360(Value: Integer);
Procedure SetGroupNumber(AGroupNumber: Integer); virtual;
procedure SetSelected(ASelected: Boolean); virtual;
function GetBoundsRect: TRect; virtual;
public
constructor Create(const AParent: TSprite); override;
destructor Destroy; override;
procedure Assign(const Value: TSprite); override;
procedure DoDraw; override;
procedure SetColor(const Color: Cardinal); overload;
procedure SetColor(Red, Green, Blue: Cardinal; Alpha: Cardinal=255); overload;
procedure LookAt(TargetX, TargetY: Integer);
procedure TowardToAngle(Angle: Integer; Speed: Single; DoLookAt: Boolean);
procedure TowardToPos(TargetX, TargetY: Integer; Speed: Single;DoLookAt: Boolean);
procedure RotateToAngle(Angle: Integer; RotateSpeed, MoveSpeed: Single );
procedure RotateToPos(DestX, DestY: Integer; RotateSpeed, MoveSpeed: Single );
procedure CircleToAngle(Angle, LookAtX, LookAtY: Integer; RotateSpeed, MoveSpeed: Single; DoLookAt: Boolean);
procedure CircleToPos(DestX, DestY, LookAtX, LookAtY: Integer; RotateSpeed, MoveSpeed: Single; DoLookAt: Boolean);
procedure Attach(Sprite: TSprite); virtual;
procedure Detach;
function GetSpriteAt(X, Y: Integer): TSprite;
function GetDistance(Sprite1, Sprite2: TSprite): Real;
function MouseInRect: Boolean;
function SpriteInRect1(InArea: TRect): Boolean;
function SpriteInRect2(InArea: TRect): Boolean;
procedure DoMouseEvent;
procedure OnMouseEnter; virtual;
procedure OnMouseLeave; virtual;
procedure OnMouseMove; virtual;
procedure OnLMouseDown; virtual;
procedure OnLMouseUp; virtual;
procedure OnRMouseDown; virtual;
procedure OnRMouseUp; virtual;
procedure OnMouseDbClick; virtual;
procedure OnMouseWheelUp; virtual;
procedure OnMouseWheelDown; virtual;
procedure OnMouseDrag; virtual;
property ActiveRect:TRect read FActiveRect write FActiveRect; //for mouse event
property X1: Single read FX1 write FX1;
property Y1: Single read FY1 write FY1;
property X2: Single read FX2 write FX2;
property Y2: Single read FY2 write FY2;
property X3: Single read FX3 write FX3;
property Y3: Single read FY3 write FY3;
property X4: Single read FX4 write FX4;
property Y4: Single read FY4 write FY4;
property Red: Integer read FRed write SetRed default 255;
property Green: Integer read FGreen write SetGreen default 255;
property Blue: Integer read FBlue write SetBlue default 255;
property Alpha: Integer read FAlpha write SetAlpha default 255;
property Color1: Cardinal read FColor1 write FColor1;
property Color2: Cardinal read FColor2 write FColor2;
property Color3: Cardinal read FColor3 write FColor3;
property Color4: Cardinal read FColor4 write FColor4;
property Angle: Single read FAngle write FAngle;
property Angle360: Integer read FAngle360 write SetAngle360;
property CenterX: Single read FCenterX write FCenterX;
property CenterY: Single read FCenterY write FCenterY;
property ScaleX: Single read FScaleX write FScaleX;
property ScaleY: Single read FScaleY write FScaleY;
property OffsetX: Single read FOffsetX write FOffsetX;
property OffsetY: Single read FOffsetY write FOffsetY;
property DoCenter: Boolean read FDoCenter write FDoCenter;
property MirrorX: Boolean read FMirrorX write FMirrorX;
property MirrorY: Boolean read FMirrorY write FMirrorY;
property SpeedX: Single read FSpeedX write FSpeedX;
property SpeedY: Single read FSpeedY write FSpeedY;
property DrawMode: Integer read FDrawMode write SetDrawMode;
property ImageType: TImageType read FImageType write FImageType;
property BoundsRect: TRect read GetBoundsRect;
property GroupNumber: Integer read FGroupNumber write SetGroupNumber;
property Selected: Boolean read FSelected write SetSelected;
property CanCollision: Boolean read FCanCollision write FCanCollision;
end;
TAnimatedSprite = class(TSpriteEx)
private
FDoAnimate: Boolean;
FAnimLooped: Boolean;
FAnimStart: Integer;
FAnimCount: Integer;
FAnimSpeed: Single;
FAnimPos: Single;
FAnimEnded: Boolean;
FDoFlag1, FDoFlag2: Boolean;
FAnimPlayMode: TAnimPlayMode;
procedure SetAnimStart(Value: Integer);
public
constructor Create(const AParent: TSprite); override;
procedure Assign(const Value: TSprite); override;
procedure DoMove(const MoveCount: Single); override;
procedure SetAnim(AniImageName: string; AniStart, AniCount: Integer; AniSpeed: Single; AniLooped, DoMirror, DoAnimate: Boolean; PlayMode: TAnimPlayMode=pmForward); overload; virtual;
procedure SetAnim(AniImageName: string; AniStart, AniCount: Integer; AniSpeed: Single; AniLooped: Boolean; PlayMode: TAnimPlayMode=pmForward); overload; virtual;
procedure OnAnimStart; virtual;
procedure OnAnimEnd; virtual;
property AnimPos: Single read FAnimPos write FAnimPos;
property AnimStart: Integer read FAnimStart write SetAnimStart;
property AnimCount: Integer read FAnimCount write FAnimCount;
property AnimSpeed: Single read FAnimSpeed write FAnimSpeed;
property AnimLooped: Boolean read FAnimLooped write FAnimLooped;
property DoAnimate: Boolean read FDoAnimate write FDoAnimate;
property AnimEnded: Boolean read FAnimEnded;
property AnimPlayMode: TAnimPlayMode read FAnimPlayMode write FAnimPlayMode;
end;
TParticleSprite = class(TAnimatedSprite)
private
FAccelX: Real;
FAccelY: Real;
FVelocityX: Real;
FVelocityY: Real;
FUpdateSpeed : Single;
FDecay: Real;
FLifeTime: Real;
public
constructor Create(const AParent: TSprite); override;
procedure DoMove(const MoveCount: Single); override;
property AccelX: Real read FAccelX write FAccelX;
property AccelY: Real read FAccelY write FAccelY;
property VelocityX: Real read FVelocityX write FVelocityX;
property VelocityY: Real read FVelocityY write FVelocityY;
property UpdateSpeed : Single read FUpdateSpeed write FUpdateSpeed;
property Decay: Real read FDecay write FDecay;
property LifeTime: Real read FLifeTime write FLifeTime;
end;
TPlayerSprite = class(TAnimatedSprite)
private
FSpeed: Single;
FAcc: Single;
FDcc: Single;
FMinSpeed: Single;
FMaxSpeed: Single;
FVelocityX: Single;
FVelocityY: Single;
FDirection: Integer;
procedure SetSpeed(Value: Single);
procedure SetDirection(Value: Integer);
public
constructor Create(const AParent: TSprite); override;
procedure UpdatePos(const MoveCount: Single);
procedure FlipXDirection;
procedure FlipYDirection;
procedure Accelerate; virtual;
procedure Deccelerate; virtual;
property Speed: Single read FSpeed write SetSpeed;
property MinSpeed: Single read FMinSpeed write FMinSpeed;
property MaxSpeed: Single read FMaxSpeed write FMaxSpeed;
property VelocityX: Single read FVelocityX write FVelocityX;
property VelocityY: Single read FVelocityY write FVelocityY;
property Acceleration: Single read FAcc write FAcc;
property Decceleration: Single read FDcc write FDcc;
property Direction: Integer read FDirection write SetDirection;
end;
TFaderSprite=class(TAnimatedSprite)
private
FMirrorCount, FCurrentColorCount, FNumColors: Integer;
FCurCol, FMultiCols: ^Cardinal;
FMulti: Boolean;
Counter: Single;
FSpeed: Single;
FLooped, FMultiFade, FMirrorFade, FFadeEnded: Boolean;
FSrcR, FSrcG, FSrcB, FSrcA,
FDestR, FDestG, FDestB, FDestA,
FCurR, FCurG, FCurB, FCurA: Byte;
procedure SetFadeSpeed(Speed: Single);
public
constructor Create(const AParent: TSprite); override;
destructor Destroy; override;
procedure DoMove(const MoveCount: Single); override;
procedure MultiFade(Colors: array of Cardinal);
procedure SetSourceColor(Red, Green, Blue, Alpha: Byte); overload;
procedure SetSourceColor(Color: Cardinal); overload;
procedure SetDestinationColor(Red, Green, Blue, Alpha: Byte); overload;
procedure SetDestinationColor(Color: Cardinal); overload;
procedure FadeIn(Red, Green, Blue: Byte; Speed: Single);
procedure FadeOut(Red, Green, Blue: Byte; Speed: Single);
procedure SwapColors;
procedure Reset;
procedure Stop;
property FadeEnded: Boolean read FFadeEnded;
property FadeSpeed: Single read FSpeed write SetFadeSpeed;
property MirrorFade: Boolean read FMirrorFade write FMirrorFade;
property LoopFade: Boolean read FLooped write FLooped;
end;
TJumperSprite = class(TPlayerSprite)
private
FJumpCount: Integer;
FJumpSpeed: Single;
FJumpHeight: Single;
FMaxFallSpeed: Single;
FDoJump: Boolean;
FJumpState: TJumpState;
procedure SetJumpState(Value: TJumpState);
public
constructor Create(const AParent: TSprite); override;
procedure DoMove(const MoveCount: Single); override;
procedure Accelerate; override;
procedure Deccelerate; override;
property JumpCount: Integer read FJumpCount write FJumpCount;
property JumpState: TJumpState read FJumpState write SetJumpState;
property JumpSpeed: Single read FJumpSpeed write FJumpSpeed;
property JumpHeight: Single read FJumpHeight write FJumpHeight;
property MaxFallSpeed: Single read FMaxFallSpeed write FMaxFallSpeed;
property DoJump: Boolean read FDoJump write FDoJump;
end;
TJumperSpriteEx = class(TPlayerSprite)
private
FJumpCount: Integer;
FJumpSpeed: Single;
FJumpStartSpeed: Single;
FJumpHeight: Single;
FLowJumpSpeed: Single;
FLowJumpGravity: Single;
FHighJumpValue: Integer;
FHighJumpSpeed: Single;
FFallingSpeed: Single;
FMaxFallSpeed: Single;
FDoJump: Boolean;
FJumpState: TJumpState;
FHoldKey: Boolean;
FOffset:Single;
procedure SetJumpState(Value: TJumpState);
public
constructor Create(const AParent: TSprite); override;
procedure DoMove(const MoveCount: Single); override;
procedure Accelerate; override;
procedure Deccelerate; override;
property JumpStartSpeed: Single read FJumpStartSpeed write FJumpStartSpeed;
property LowJumpSpeed: Single read FLowJumpSpeed write FLowJumpSpeed;
property LowJumpGravity: Single read FLowJumpGravity write FLowJumpgravity;
property HighJumpValue: Integer read FHighJumpValue write FHighJumpValue;
property HighJumpSpeed: Single read FHighJumpSpeed write FHighJumpSpeed;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -