diff --git a/scripts/ball/ball.gd b/scripts/ball/ball.gd index b0640f8..8d12b7a 100644 --- a/scripts/ball/ball.gd +++ b/scripts/ball/ball.gd @@ -4,7 +4,7 @@ extends CharacterBody2D const y_offset = -10 const player_path = "/root/Main/TileMap/Player" const enemy_path = "/root/Main/TileMap/Enemy" -signal notify_enemy(new_target) +signal notify_enemy(ball_target: Vector2i) var speed = 100 var target = Vector2.ZERO @onready var tile_map: TileMap = get_parent() @@ -23,12 +23,14 @@ func update_target(new_target: Vector2i): tile_map.reset_and_set_target_cell(new_target) func _on_player_hit_ball(): - var new_target = tile_map.get_random_top_cell() + var new_target: Vector2i = tile_map.get_random_top_cell() update_target(new_target) notify_enemy.emit(new_target) func _on_enemy_hit_ball(): - update_target(tile_map.get_random_bottom_cell()) + var new_target: Vector2i = tile_map.get_random_bottom_cell() + update_target(new_target) + notify_enemy.emit(new_target) func connect_player(): var player = get_node(player_path) diff --git a/scripts/enemy/behavior_tree/can_go_to_ball_condition.gd b/scripts/enemy/behavior_tree/can_go_to_ball_condition.gd index 98bd2cc..0567af4 100644 --- a/scripts/enemy/behavior_tree/can_go_to_ball_condition.gd +++ b/scripts/enemy/behavior_tree/can_go_to_ball_condition.gd @@ -3,8 +3,8 @@ extends ConditionLeaf func tick(actor, _blackboard): if ( - actor.next_target != null && actor.ball_in_game() && + actor.next_destination != null && !actor.collide_with_ball ): return SUCCESS diff --git a/scripts/enemy/behavior_tree/can_return_ball_condition.gd b/scripts/enemy/behavior_tree/can_return_ball_condition.gd index f1fde18..f44d023 100644 --- a/scripts/enemy/behavior_tree/can_return_ball_condition.gd +++ b/scripts/enemy/behavior_tree/can_return_ball_condition.gd @@ -4,8 +4,8 @@ extends ConditionLeaf func tick(actor, _blackboard): if ( actor.ball_in_game() && - actor.next_target != null && - actor.collide_with_ball + actor.collide_with_ball && + !actor.ball_aims_to_bottom ): return SUCCESS return FAILURE diff --git a/scripts/enemy/behavior_tree/can_wait_condition.gd b/scripts/enemy/behavior_tree/can_wait_condition.gd deleted file mode 100644 index 4b27811..0000000 --- a/scripts/enemy/behavior_tree/can_wait_condition.gd +++ /dev/null @@ -1,10 +0,0 @@ -class_name CanWaitCodition -extends ConditionLeaf - -func tick(actor, _blackboard): - if ( - actor.next_target == null - ): - - return SUCCESS - return FAILURE diff --git a/scripts/enemy/behavior_tree/get_ball_destination_action.gd b/scripts/enemy/behavior_tree/get_ball_destination_action.gd index 6dc0170..25bf5e3 100644 --- a/scripts/enemy/behavior_tree/get_ball_destination_action.gd +++ b/scripts/enemy/behavior_tree/get_ball_destination_action.gd @@ -2,5 +2,5 @@ class_name GetBallDestinationAction extends ActionLeaf func tick(actor: Node, blackboard: Blackboard): - blackboard.set_value("destination", actor.next_target) + blackboard.set_value("destination", actor.next_destination) return SUCCESS diff --git a/scripts/enemy/behavior_tree/move_to_destination_action.gd b/scripts/enemy/behavior_tree/move_to_destination_action.gd index 8926496..2fa1911 100644 --- a/scripts/enemy/behavior_tree/move_to_destination_action.gd +++ b/scripts/enemy/behavior_tree/move_to_destination_action.gd @@ -11,6 +11,7 @@ func tick(actor: Node, blackboard: Blackboard): actor.move_to(destination) if actor.position == destination: + actor.next_destination == null return SUCCESS return RUNNING diff --git a/scripts/enemy/behavior_tree/throw_ball_action.gd b/scripts/enemy/behavior_tree/throw_ball_action.gd index 1b0c05c..9ca768c 100644 --- a/scripts/enemy/behavior_tree/throw_ball_action.gd +++ b/scripts/enemy/behavior_tree/throw_ball_action.gd @@ -4,7 +4,7 @@ extends ActionLeaf func before_run(actor, _blackboard): actor.play_hit_ball_animation() -func tick(actor, blackboard): +func tick(actor, _blackboard): if !actor.hit_ball_animation_finished: return RUNNING actor.throw_ball() diff --git a/scripts/enemy/behavior_tree/wait_action.gd b/scripts/enemy/behavior_tree/wait_action.gd deleted file mode 100644 index 56c82ce..0000000 --- a/scripts/enemy/behavior_tree/wait_action.gd +++ /dev/null @@ -1,8 +0,0 @@ -class_name WaitAction -extends ActionLeaf - -func before_run(actor, _blackboard): - actor.play_idle_animation() - -func tick(_actor, _blackboard): - return RUNNING diff --git a/scripts/enemy/enemy.gd b/scripts/enemy/enemy.gd index 057597d..27c4ef7 100644 --- a/scripts/enemy/enemy.gd +++ b/scripts/enemy/enemy.gd @@ -6,9 +6,10 @@ const ball_name = "Ball" const y_spawn_offset = -8 signal hit_ball var speed = 80 -var next_target +var next_destination var collide_with_ball = false var hit_ball_animation_finished = false +var ball_aims_to_bottom = false @onready var tile_map: TileMap = get_parent() @onready var animation_player = $AnimationPlayer @onready var sprite = $Sprite2D @@ -32,7 +33,6 @@ func throw_ball(): func return_ball(): hit_ball.emit() - next_target = null func flip_sprite(destination): sprite.flip_h = position.x > destination.x @@ -52,9 +52,13 @@ func play_walk_animation(): func _on_hit_ball_animation_finished(_anim_name): hit_ball_animation_finished = true -func _on_notify_enemy(new_target): - # TODO: add to new_target an offset depending from where the enemy come from - next_target = tile_map.map_to_local(new_target) +func _on_notify_enemy(ball_target: Vector2i): + if tile_map.is_in_bottom_area(ball_target): + ball_aims_to_bottom = true + else: + ball_aims_to_bottom = false + # TODO: add to ball_target an offset depending from where the enemy come from + next_destination = tile_map.map_to_local(ball_target) func _on_area_2d_body_entered(_body): collide_with_ball = true diff --git a/scripts/tile_map/tile_map.gd b/scripts/tile_map/tile_map.gd index 275886a..2da0c9d 100644 --- a/scripts/tile_map/tile_map.gd +++ b/scripts/tile_map/tile_map.gd @@ -31,38 +31,36 @@ func draw_wall(): for x in range(map_width): set_cell(1, Vector2i(x - 1, middle_height - 1), wall_tile_source_id, Vector2i(0, 0), 0) # why have we to add -1? -func get_top_spawn_cell(): +func get_top_spawn_cell() -> Vector2i: return Vector2i(floor(map_width / 2.0), 1) -func get_bottom_spawn_cell(): +func get_bottom_spawn_cell() -> Vector2i: return Vector2i(floor(map_width / 2.0), map_height - 2) -func get_random_top_cell(): +func get_random_top_cell() -> Vector2i: var w = get_area_width() var h = get_top_area_height() return Vector2i(randi_range(w.x, w.y), randi_range(h.x, h.y)) -func get_random_bottom_cell(): +func get_random_bottom_cell() -> Vector2i: var w = get_area_width() var h = get_bottom_area_height() return Vector2i(randi_range(w.x, w.y), randi_range(h.x, h.y)) -func is_in_bottom_area(local_position: Vector2): - var map_position = local_to_map(local_position) +func is_in_bottom_area(cell: Vector2i) -> bool: var height = get_bottom_area_height() - - return map_position.y >= height.x && map_position.y <= height.y + return cell.y >= height.x && cell.y <= height.y -func get_area_width(): +func get_area_width() -> Vector2i: return Vector2i(0, map_width - 1) -func get_top_area_height(): +func get_top_area_height() -> Vector2i: var middle_height = floor(map_height / 2.0) return Vector2i(0, middle_height - 1) -func get_bottom_area_height(): +func get_bottom_area_height() -> Vector2i: var middle_height = floor(map_height / 2.0) return Vector2i(middle_height + 1, map_height - 1)