obrazki są pełnymi prostokątami czy też chcesz kolizje per pixel zrobic?
//Component by ziomber
//mail: [email protected]
unit Noxxdxsprite;
interface
uses
ExtCtrls, DXSprite, Windows, Messages, SysUtils, Classes, Graphics, Controls,
Forms, Dialogs, DXDraws, DXClass, DirectX;
type
TAnimation = class(TImageSprite);
type
tanim_wall = object
x,y,frame_width, frame_height : integer;
wall_rect : TRect;
texture, bmp : timage;
frames : array of timage;
frame_count : integer;
anim_speed : single;
procedure bind_texture_memory(filename : string);
procedure free_wall;
end;
type twall_animation_mover = record
animated_walls : array of tanim_wall;
count : array of integer;
end;
type
twall = object
x,y, width, height : integer;
bmp, texture : timage;
wall_rect : TRect;
procedure initialize_wall_rect;
procedure bind_texture_memory(filename : string);
procedure free_wall;
end;
type
TNoxxdxsprite = class(TComponent)
private
ee : TDXImagelist;
procedure setee(value : TDXImagelist);
protected
public
published
function collision(left1, top1, width1, height1, left2, top2, width2, height2 : single) : boolean;
function rect_collision(sprite : TAnimation; rect : TRect) : boolean;
function sprite_collision(sprite1, sprite2 : TAnimation) : boolean;
function get_sprite_x(sprite : TAnimation): integer;
function get_sprite_y(sprite : TAnimation): integer;
function get_sprite_rect(sprite : TAnimation): TRect;
function CursorPos(X, Y, X1, Y1, X2, Y2 : Integer): Boolean; //by eska
function rCursorPos(X, Y: Integer; sprite : tanimation): Boolean;
function img_col_collision(sprite1, sprite2 : TAnimation) : boolean;
property Imagelist : TDXImagelist read ee write setee;
end;
procedure Register;
var
col : boolean = false;
col_image : array of array of array of boolean;
implementation
procedure Register;
begin
RegisterComponents('DelphiX', [TNoxxdxsprite]);
end;
function tnoxxdxsprite.img_col_collision(sprite1, sprite2 : TAnimation) : boolean;
begin
result := false;
end;
procedure tnoxxdxsprite.setee(value : TDXImagelist);
begin
ee := value;
end;
//animacja
procedure tanim_wall.bind_texture_memory(filename : string);
var
i : integer;
wox : integer;
begin
wox := bmp.width div frame_width; //jakby cos to bmp.width /bitmap.width albo canvas.width
bmp:=timage.Create(nil);
bmp.Parent := nil;
bmp.AutoSize := true;
bmp.Picture.LoadFromFile(filename);
setlength(frames,wox);
frame_count := wox-1;
for i:=0 to wox-1 do
begin
frames[i]:=timage.Create(nil);
frames[i].Parent := nil;
frames[i].Height := bmp.height;
frames[i].Width := frame_width;
frames[i].Canvas.CopyRect(rect(0,0,frame_width,bmp.height),bmp.canvas,rect(i*frame_width,0,I*frame_width+frame_width,bmp.height));
end;
end;
procedure tanim_wall.free_wall;
var
i : integer;
begin
for i:=0 to frame_count do
frames[i].free;
end;
//normal rect
procedure twall.initialize_wall_rect;
begin
wall_rect.Left := x;
wall_rect.top := y;
wall_rect.right := x+width;
wall_rect.bottom := y+height;
end;
procedure twall.bind_texture_memory(filename : string);
var
i,i2 : integer;
begin
bmp:=timage.Create(nil);
bmp.Parent := nil;
bmp.AutoSize := true;
bmp.Picture.LoadFromFile(filename);
texture:=timage.Create(nil);
texture.Parent := nil;
texture.height := height;
texture.Width := width;
for i:=0 to Width do
for i2:=0 to height do
texture.Canvas.Draw(i*bmp.width,i2*bmp.height,bmp.picture.graphic);
end;
procedure twall.free_wall;
begin
bmp.free;
texture.free;
end;
function TNoxxdxsprite.Collision(left1, top1, width1, height1, left2, top2, width2, height2 : single) : boolean;
var
i,i2,i3,i4 : single;
begin
i := Left2 - Width1;
i2 := Left2 + Width2;
i3 := Top2 - Height1;
i4 := Top2 + Height2;
If ((Left1 > i) and (Left1 < i2) and (Top1 > i3) and
(Top1 < i4)) then result := true else result := false;
end;
function TNoxxdxsprite.rect_collision(sprite : TAnimation; rect : TRect) : boolean;
var
i,i2,i3,i4 : integer;
left1, top1, width1, height1, left2, top2, width2, height2 : integer;
e : trect;
begin
e := get_sprite_rect(sprite);
left1 := e.Left;
top1 := e.top;
width1 := e.Right - e.left;
height1 := e.bottom - e.top;
left2 := rect.Left;
top2 := rect.top;
width2 := rect.Right - rect.left;
height2 := rect.bottom - rect.top;
i := Left2 - Width1;
i2 := Left2 + Width2;
i3 := Top2 - Height1;
i4 := Top2 + Height2;
If ((Left1 > i) and (Left1 < i2) and (Top1 > i3) and
(Top1 < i4)) then result := true else result := false;
end;
function TNoxxdxsprite.sprite_collision(sprite1, sprite2 : TAnimation) : boolean;
var
i,i2,i3,i4 : integer;
left1, top1, width1, height1, left2, top2, width2, height2 : integer;
e,e2 : trect;
begin
e := get_sprite_rect(sprite1);
e2 := get_sprite_rect(sprite2);
left1 := e.Left;
top1 := e.top;
width1 := e.Right - e.left;
height1 := e.bottom - e.top;
left2 := e2.Left;
top2 := e2.top;
width2 := e2.Right - e2.left;
height2 := e2.bottom - e2.top;
i := Left2 - Width1;
i2 := Left2 + Width2;
i3 := Top2 - Height1;
i4 := Top2 + Height2;
If ((Left1 > i) and (Left1 < i2) and (Top1 > i3) and
(Top1 < i4)) then result := true else result := false;
end;
function TNoxxdxsprite.get_sprite_x(sprite : TAnimation): integer;
begin
result := trunc(sprite.x)-(sprite.image.width div 2);
end;
function TNoxxdxsprite.get_sprite_y(sprite : TAnimation): integer;
begin
result := trunc(sprite.y)-(sprite.image.width div 2);
end;
function TNoxxdxsprite.get_sprite_rect(sprite : TAnimation): TRect;
var
point_x : integer;
point_y : integer;
final : TRect;
begin
point_x := round(sprite.x)-(sprite.image.width div 2);
point_y := round(sprite.y)-(sprite.image.height div 2);
Final := rect(point_x,point_y,point_x+sprite.image.width,point_y+sprite.image.height);
result := final;
end;
function TNoxxdxsprite.CursorPos(X, Y, X1, Y1, X2, Y2 : Integer): Boolean; //by eSka :)
begin
if ((X > X1) and (X < X2) and (Y > Y1) and (Y < Y2))
then
Result := true else result:= False;
end;
function TNoxxdxsprite.rCursorPos(X, Y: Integer; sprite : tanimation): Boolean;
var
X1, Y1, X2, Y2 : Integer;
o : trect;
begin
o := get_sprite_rect(sprite);
X1 := o.left;
Y1 := o.top;
X2 := o.right;
Y2 := o.bottom;
if ((X > X1) and (X < X2) and (Y > Y1) and (Y < Y2)) //by eSka
then
Result := true else result:= False;
end;
end.