Fix enemy return ball two times each time

This commit is contained in:
Mathilde Grapin 2023-07-01 17:12:02 +02:00
parent a9a9f05296
commit 2b11ec8638
10 changed files with 29 additions and 42 deletions

View file

@ -4,7 +4,7 @@ extends CharacterBody2D
const y_offset = -10 const y_offset = -10
const player_path = "/root/Main/TileMap/Player" const player_path = "/root/Main/TileMap/Player"
const enemy_path = "/root/Main/TileMap/Enemy" const enemy_path = "/root/Main/TileMap/Enemy"
signal notify_enemy(new_target) signal notify_enemy(ball_target: Vector2i)
var speed = 100 var speed = 100
var target = Vector2.ZERO var target = Vector2.ZERO
@onready var tile_map: TileMap = get_parent() @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) tile_map.reset_and_set_target_cell(new_target)
func _on_player_hit_ball(): 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) update_target(new_target)
notify_enemy.emit(new_target) notify_enemy.emit(new_target)
func _on_enemy_hit_ball(): 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(): func connect_player():
var player = get_node(player_path) var player = get_node(player_path)

View file

@ -3,8 +3,8 @@ extends ConditionLeaf
func tick(actor, _blackboard): func tick(actor, _blackboard):
if ( if (
actor.next_target != null &&
actor.ball_in_game() && actor.ball_in_game() &&
actor.next_destination != null &&
!actor.collide_with_ball !actor.collide_with_ball
): ):
return SUCCESS return SUCCESS

View file

@ -4,8 +4,8 @@ extends ConditionLeaf
func tick(actor, _blackboard): func tick(actor, _blackboard):
if ( if (
actor.ball_in_game() && actor.ball_in_game() &&
actor.next_target != null && actor.collide_with_ball &&
actor.collide_with_ball !actor.ball_aims_to_bottom
): ):
return SUCCESS return SUCCESS
return FAILURE return FAILURE

View file

@ -1,10 +0,0 @@
class_name CanWaitCodition
extends ConditionLeaf
func tick(actor, _blackboard):
if (
actor.next_target == null
):
return SUCCESS
return FAILURE

View file

@ -2,5 +2,5 @@ class_name GetBallDestinationAction
extends ActionLeaf extends ActionLeaf
func tick(actor: Node, blackboard: Blackboard): func tick(actor: Node, blackboard: Blackboard):
blackboard.set_value("destination", actor.next_target) blackboard.set_value("destination", actor.next_destination)
return SUCCESS return SUCCESS

View file

@ -11,6 +11,7 @@ func tick(actor: Node, blackboard: Blackboard):
actor.move_to(destination) actor.move_to(destination)
if actor.position == destination: if actor.position == destination:
actor.next_destination == null
return SUCCESS return SUCCESS
return RUNNING return RUNNING

View file

@ -4,7 +4,7 @@ extends ActionLeaf
func before_run(actor, _blackboard): func before_run(actor, _blackboard):
actor.play_hit_ball_animation() actor.play_hit_ball_animation()
func tick(actor, blackboard): func tick(actor, _blackboard):
if !actor.hit_ball_animation_finished: if !actor.hit_ball_animation_finished:
return RUNNING return RUNNING
actor.throw_ball() actor.throw_ball()

View file

@ -1,8 +0,0 @@
class_name WaitAction
extends ActionLeaf
func before_run(actor, _blackboard):
actor.play_idle_animation()
func tick(_actor, _blackboard):
return RUNNING

View file

@ -6,9 +6,10 @@ const ball_name = "Ball"
const y_spawn_offset = -8 const y_spawn_offset = -8
signal hit_ball signal hit_ball
var speed = 80 var speed = 80
var next_target var next_destination
var collide_with_ball = false var collide_with_ball = false
var hit_ball_animation_finished = false var hit_ball_animation_finished = false
var ball_aims_to_bottom = false
@onready var tile_map: TileMap = get_parent() @onready var tile_map: TileMap = get_parent()
@onready var animation_player = $AnimationPlayer @onready var animation_player = $AnimationPlayer
@onready var sprite = $Sprite2D @onready var sprite = $Sprite2D
@ -32,7 +33,6 @@ func throw_ball():
func return_ball(): func return_ball():
hit_ball.emit() hit_ball.emit()
next_target = null
func flip_sprite(destination): func flip_sprite(destination):
sprite.flip_h = position.x > destination.x sprite.flip_h = position.x > destination.x
@ -52,9 +52,13 @@ func play_walk_animation():
func _on_hit_ball_animation_finished(_anim_name): func _on_hit_ball_animation_finished(_anim_name):
hit_ball_animation_finished = true hit_ball_animation_finished = true
func _on_notify_enemy(new_target): func _on_notify_enemy(ball_target: Vector2i):
# TODO: add to new_target an offset depending from where the enemy come from if tile_map.is_in_bottom_area(ball_target):
next_target = tile_map.map_to_local(new_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): func _on_area_2d_body_entered(_body):
collide_with_ball = true collide_with_ball = true

View file

@ -31,38 +31,36 @@ func draw_wall():
for x in range(map_width): 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? 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) 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) 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 w = get_area_width()
var h = get_top_area_height() var h = get_top_area_height()
return Vector2i(randi_range(w.x, w.y), randi_range(h.x, h.y)) 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 w = get_area_width()
var h = get_bottom_area_height() var h = get_bottom_area_height()
return Vector2i(randi_range(w.x, w.y), randi_range(h.x, h.y)) return Vector2i(randi_range(w.x, w.y), randi_range(h.x, h.y))
func is_in_bottom_area(local_position: Vector2): func is_in_bottom_area(cell: Vector2i) -> bool:
var map_position = local_to_map(local_position)
var height = get_bottom_area_height() var height = get_bottom_area_height()
return cell.y >= height.x && cell.y <= height.y
return map_position.y >= height.x && map_position.y <= height.y func get_area_width() -> Vector2i:
func get_area_width():
return Vector2i(0, map_width - 1) 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) var middle_height = floor(map_height / 2.0)
return Vector2i(0, middle_height - 1) 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) var middle_height = floor(map_height / 2.0)
return Vector2i(middle_height + 1, map_height - 1) return Vector2i(middle_height + 1, map_height - 1)