Skip to content

Commit

Permalink
Ninja: Tweak AI.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bozar committed Jul 16, 2022
1 parent 6dc1a5f commit c1d3248
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
21 changes: 13 additions & 8 deletions library/npc_ai/NinjaAI.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const HP_BAR := [
[Game_DungeonSize.CENTER_X + 1, Game_NinjaData.MAX_X],
[Game_NinjaData.MIN_X, Game_DungeonSize.CENTER_X],
]
const COMMON_HORIZONTAL_GAP := 2
const SHADOW_HORIZONTAL_GAP := 0

var _end_game := false

Expand Down Expand Up @@ -47,13 +49,13 @@ func _ninja_act() -> void:
Game_NinjaData.ATTACK_RANGE):
hit_pc = true
elif _self_pos.y == Game_NinjaData.GROUND_Y:
# Try to jump and hit PC when standing on ground.
move_result = _try_move_vertically(Game_CoordCalculator.UP)
has_moved = move_result[0]
hit_pc = move_result[1]
# Otherwise try to move horizontally.
# Try to move horizontally when standing on ground.
has_moved = _try_move_horizontally(COMMON_HORIZONTAL_GAP)
# Otherwise try to jump and hit PC.
if not has_moved:
has_moved = _try_move_horizontally()
move_result = _try_move_vertically(Game_CoordCalculator.UP)
has_moved = move_result[0]
hit_pc = move_result[1]
else:
# UPDATE: Do not move horizontally in mid-air. Ninjas are close to each
# other in this way and PC can kill them easily.
Expand Down Expand Up @@ -90,7 +92,7 @@ func _shadow_ninja_act() -> void:
var is_vertical := true

if _self_pos.y == Game_NinjaData.GROUND_Y:
has_moved = _try_move_horizontally()
has_moved = _try_move_horizontally(SHADOW_HORIZONTAL_GAP)
is_vertical = false
else:
move_result = _try_move_vertically(Game_CoordCalculator.DOWN)
Expand Down Expand Up @@ -146,10 +148,13 @@ func _try_move_vertically(direction: int) -> Array:
return [true, is_pc]


func _try_move_horizontally() -> bool:
func _try_move_horizontally(min_range: int) -> bool:
var pos := Game_ConvertCoord.sprite_to_coord(_self)
var move_to: Game_IntCoord

if abs(pos.x - _pc_pos.x) <= min_range:
return false

if pos.x < _pc_pos.x:
move_to = Game_IntCoord.new(pos.x + 1, pos.y)
elif pos.x > _pc_pos.x:
Expand Down
2 changes: 1 addition & 1 deletion user/doc/ninja.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ A common ninja may perform one of three actions at the start of his turn.

Action 1. Hit PC if he is adjacent to PC. His symbol changes from n to x for one turn.

Action 2. When on ground, jump up if there is at least one unoccupied grid above. Otherwise, if he is not in the same column with PC, try to move 1 step horizontally towards PC. A ninja can jump at most 2 grids high. PC is hit if he blocks the jumping path. If another NPC blocks the vertical or horizontal path, the ninja cannot move to his destination.
Action 2. When on ground, if he is more than 2 steps away from PC horizontally, try to move 1 step towards PC. Otherwise, jump up if there is at least one unoccupied grid above. A ninja can jump at most 2 grids high. PC is hit if he blocks the jumping path. If another NPC blocks the vertical or horizontal path, the ninja cannot move to his destination.

Action 3. When in mid-air, try to fall down 2 grids. Same as action 2, PC is hit if he blocks the way, and the ninja can be blocked by his allies.

Expand Down

0 comments on commit c1d3248

Please sign in to comment.