Firstly i would like to tell that I'm not sure that this is the forum where i should post so if this isn't it i would like my thread to be moved to the appropriate forum.
Here is the problem I've got, I'm using a 2d array to store my x,y coordinates where the character should move and so on where the 1st coordinates are the ones where my character currently is and the last are the place where the mouse has clicked and he should go to. I have no problem with going to the right direction but sometimes some of the "ifs" cover each other and the procedure gets bugged.
Here is the source where I divide the land into 8 splits (the 8th is commented because if i implement it one of the others gets bugged)
A := (Y - DotY) / (X - DotX);
B := (X*DotY - DotX*Y) / (X - DotX);
MoveX := Abs(X - DotX);
MoveY := Abs(Y - DotY);
if ((X - DotX) > 0) and ((Y - DotY) < (X - DotX)) then
begin
DirX := -1;
DirY := -1;
end;
if ((X - DotX) < -(Y - DotY)) and ((Y - DotY) < 0) then
begin
DirX := 1;
DirY := 1;
end;
if ((X - DotX) > 0) and ((Y - DotY) < -(X - DotX)) then
begin
DirX := 1;
DirY := 1;
end;
if ((X - DotX) < (Y - DotY)) and ((Y - DotY) > 0) then
begin
DirX := -1;
DirY := -1;
end;
if ((X - DotX) < -(Y - DotY)) and ((Y - DotY) > 0) then
begin
DirX := 1;
DirY := 1;
end;
if ((X - DotX) < 0) and ((Y - DotY) < -(X - DotX)) then
begin
DirX := 1;
DirY := 1;
end;
if ((X - DotX) < 0) and ((Y - DotY) < (X - DotX)) then
begin
DirX := 1;
DirY := 1;
end;
{ if ((X - DotX) > (Y - DotY)) and ((Y - DotY) < 0) then
begin
DirX := 1;
DirY := 1;
end; }
if (MoveX <> 0) or (MoveY <> 0) then
begin
if MoveX >= MoveY then
begin
Pos[1][1] := DotX;
Pos[1][2] := DotY;
J := 1;
for I := 2 to MoveX do
begin
Pos[1] := DotX + I;
Pos[2] := Round(A*Pos[1] + B);
J := I + 1;
end;
Pos[J][1] := X;
Pos[J][2] := Y;
N := 2;
Nmax := MoveX;
end
else
begin
Pos[1][1] := DotX;
Pos[1][2] := DotY;
J := 1;
for I := 1 to MoveY do
begin
Pos[2] := DotY + I;
Pos[1] := Round((Pos[2] - B) / A);
J := I + 1;
end;
Pos[J][1] := X;
Pos[J][2] := Y;
N := 2;
Nmax := MoveY;
end
end
else
begin
PacmanMoving := False;
end;
procedure TMainForm.MoveOneStep;
begin
if N < Nmax then
begin
StepX := DirX * (Pos[N][1] - Pos[N-1][1]);
StepY := DirY * (Pos[N][2] - Pos[N-1][2]);
MoveMap(StepX, StepY);
MoveAnimation(StepX, StepY);
N := N + 1;
end
else
PacmanMoving := False;
end;
procedure TMainForm.MoveMap(mx, my : Integer);
begin
StartX := StartX + mx;
StartY := StartY + my;
end;
StartX and StartY are integers where the map i use begins to be drawed (I'm scrolling the map not the character when he is supposed to move)
Here is where i change the animation but i will make it more accurate so that it doesn't change the frames of the character very often.
procedure TMainForm.MoveAnimation(mx, my : Integer);
begin
if (mx = 0) and (my = 0) then
Pacman.AnimCount := 0
else
Pacman.AnimCount := 16;
if my < 0 then
begin
if mx < 0 then Pacman.AnimStart := 60;
if mx = 0 then Pacman.AnimStart := 76;
if mx > 0 then Pacman.AnimStart := 92;
end;
if my = 0 then
begin
if mx < 0 then Pacman.AnimStart := 44;
if mx > 0 then Pacman.AnimStart := 108;
end;
if my > 0 then
begin
if mx < 0 then Pacman.AnimStart := 28;
if mx = 0 then Pacman.AnimStart := 12;
if mx > 0 then Pacman.AnimStart := 124;
end;
end;