Fix enemy return ball two times each time
This commit is contained in:
parent
a9a9f05296
commit
2b11ec8638
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
class_name CanWaitCodition
|
||||
extends ConditionLeaf
|
||||
|
||||
func tick(actor, _blackboard):
|
||||
if (
|
||||
actor.next_target == null
|
||||
):
|
||||
|
||||
return SUCCESS
|
||||
return FAILURE
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
class_name WaitAction
|
||||
extends ActionLeaf
|
||||
|
||||
func before_run(actor, _blackboard):
|
||||
actor.play_idle_animation()
|
||||
|
||||
func tick(_actor, _blackboard):
|
||||
return RUNNING
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue