You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.8 KiB

Unit heroEnemy;
Interface
Uses dataUnit, Math;
Procedure moveMan(Var theGS:GameState;rowStep,colStep:integer);
Procedure moveEnemies(Var theGS:GameState);
Implementation
Procedure moveMan(Var theGS:GameState;rowStep,colStep:integer);
Var
move: boolean = false;
nextTile: char;
Begin
nextTile := theGS.map[theGS.manRow+rowStep, theGS.manCol+colStep];
(* Push the boulder *)
If (nextTile In pushable) And
(theGS.map[theGS.manRow+2*rowStep, theGS.manCol+2*colStep] = empty) Then
Begin
theGS.map[theGS.manRow+2*rowStep, theGS.manCol+2*colStep] := nextTile;
move := true;
End;
(* Move man *)
If move Or (nextTile In walkable) Then
Begin
(* Colect gems while where at it *)
If nextTile = gem Then
dec(theGS.goal);
(* The moving *)
theGS.map[theGS.manRow, theGS.manCol] := empty;
theGS.manRow := theGS.manRow + rowStep;
theGS.manCol := theGS.manCol + colStep;
theGS.map[theGS.manRow, theGS.manCol] := man;
End;
End;
Procedure moveEnemies(Var theGS:GameState);
Var
i, row, col, rowstep, colstep, tries: integer;
Begin
For i := 1 To theGS.numEnemies Do
Begin
col := theGS.enemy[i].col;
row := theGS.enemy[i].row;
rowstep := theGS.manRow - row;
colstep := theGS.manCol - col;
(* Figures out general direction. *)
If abs(rowstep) > abs(colstep) Then
colstep := 0
Else If abs(colstep) > abs(rowstep) Then
rowstep := 0
Else If random(2) = 0 Then
rowstep := 0
Else
colstep := 0;
(* Restrict movement to vecinity. *)
rowstep := min(max(-1,rowstep),1);
colstep := min(max(-1,colstep),1);
(* Try to move there. *)
For tries := 4 Downto 1 Do
Begin
(* If there's an obstacle. *)
If theGS.map[row+rowstep, col+colstep] <> empty Then
Begin
If random(1) = 0 Then
colstep := -colstep
Else
rowstep := -rowstep;
If tries Mod 2 = 0 Then
Begin
(* Ol' swap trick *)
colstep := colstep + rowstep;
rowstep := colstep - rowstep;
colstep := colstep - rowstep;
End
End
Else
Begin
(* Move the enemy. *)
theGS.map[row+rowstep, col+colstep] := theGS.map[row,col];
theGS.map[row,col] := empty;
col := col+colstep;
row := row+rowstep;
theGS.enemy[i].col := col;
theGS.enemy[i].row := row;
(* Attack man *)
If abs(col-theGS.manCol) + abs(row-theGS.manRow) <= 1 Then
Begin
theGS.goal := -1;
End;
break;
End;
End;
End;
End;
End.