0% found this document useful (0 votes)
14 views4 pages

Ai

The document outlines a series of scripts for AI behavior in Godot 4.3, focusing on enemy movement, chasing the player, and pathfinding. It includes functionalities for health management, animations, and collision detection. The scripts also implement gravity effects and timer-based actions for various enemy states such as roaming, attacking, and dying.

Uploaded by

zxcdanmnbad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views4 pages

Ai

The document outlines a series of scripts for AI behavior in Godot 4.3, focusing on enemy movement, chasing the player, and pathfinding. It includes functionalities for health management, animations, and collision detection. The scripts also implement gravity effects and timer-based actions for various enemy states such as roaming, attacking, and dying.

Uploaded by

zxcdanmnbad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

godot4.

3 ai

1.範圍內行走與追蹤

extends CharacterBody2D

class_name gearteethenemy

const speed = 100


var is_chase:bool = true
var health = 80

var player : CharacterBody2D


var player_in_area = false

var health_max = 80
var health_min = 0

var dead: bool = false


var taking_damage : bool = false
var attacking_damage = 20
var is_attacking :bool = false

var dir: Vector2


const gravity = 900
var knockback_force = 200
var is_roaming:bool = true

func _ready() -> void:


player = Autoloadplayer.player

func _process(delta):
if !is_on_floor():
velocity.y += gravity * delta
velocity.x = 0
move(delta)
handle_animation()
move_and_slide()
func move(delta):
if !dead:
if !is_chase:
velocity += dir * speed * delta
elif is_chase and !taking_damage:
var dir_to_player = position.direction_to(player.global_position)
* speed
velocity.x = dir_to_player.x
dir.x = abs(velocity.x) / velocity.x
is_roaming = true

elif dead:
velocity.x = 0
func handle_animation():
var anim_sprite = $AnimatedSprite2D
if !dead and !taking_damage and !is_attacking:
anim_sprite.play("walk")
if dir.x ==-1:
anim_sprite.flip_h = true
elif dir.x == 1:
anim_sprite.flip_h = false
elif !dead and taking_damage and!is_attacking:
anim_sprite.play("hurt")
await get_tree().create_timer(0.8).timeout
taking_damage = false
elif dead and is_roaming:
is_roaming = false
anim_sprite.play("death")
await get_tree().create_timer(1.0).timeout
handle_death()
func handle_death():
self.queue_free()
func _on_timer_timeout():
$Timer.wait_time = choose([1.5, 2.0, 2.5])
if !is_chase:
dir = choose([Vector2.RIGHT, Vector2.LEFT])
velocity.x = 0
func choose(array):
array.shuffle()
return array.front()

2.pathfinding 無法偵測上方玩家

extends CharacterBody2D

const gravity = 900


const speed = 100.0
const JUMP_VELOCITY = -400.0
var player : CharacterBody2D
@onready var nav_agent := $NavigationAgent2D as NavigationAgent2D

func _ready() -> void:


player = Autoloadplayer.player
func _physics_process(delta: float) -> void:
var dir = to_local(nav_agent.get_next_path_position()).normalized()
velocity.x = dir.x * speed

if !is_on_floor():
velocity.y += gravity * delta
velocity.x = 0
move_and_slide()

# Handle jump.
func makepath() -> void:
nav_agent.target_position = player.global_position

func _on_timer_timeout() -> void:


makepath()

3.大地圖追蹤法,玩家上方 area_2d+pathfinding
4.y 值 x 值 檢定法

extends CharacterBody2D

class_name snaker

@onready var nav_agent := $NavigationAgent2D as NavigationAgent2D

const speed = 200


var is_chase:bool = false
var health = 80

var player : CharacterBody2D


var player_in_area = false

var health_max = 80
var health_min = 0

var dead: bool = false


var taking_damage : bool = false
var attacking_damage = 20
var is_attacking :bool = false

var dir: Vector2


const gravity = 900
var knockback_force = 200
var is_roaming:bool = true

func _ready() -> void:


player=Autoloadplayer.player
if self.global_position.x - player.global_position.x <1:
dir.x = 1
elif self.global_position.x - player.global_position.x >1:
dir.x = -1

func _physics_process(delta: float) -> void:


# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
if self.global_position.y - player.global_position.y >1:
move(delta)
elif self.global_position.y - player.global_position.y <1:
path_finding_chase()

handle_animation()
move_and_slide()
func move(delta):
velocity.x = dir.x * speed
if $RayCast2D.is_colliding():
var collider = $RayCast2D.get_collider()
if collider.is_in_group("wall"):
print("waall")
dir.x = 1
elif $RayCast2D2.is_colliding():
var collider = $RayCast2D2.get_collider()
if collider.is_in_group("wall"):
print("waall2")
dir.x = -1
func handle_animation():
var anim_sprite = $AnimatedSprite2D
if !dead and !taking_damage and !is_attacking:
anim_sprite.play("walk")
if dir.x == 1:
anim_sprite.flip_h = true
elif dir.x == -1:
anim_sprite.flip_h = false
elif !dead and taking_damage and!is_attacking:
anim_sprite.play("hurt")
await get_tree().create_timer(0.8).timeout
taking_damage = false
elif dead and is_roaming:
is_roaming = false
anim_sprite.play("death")
await get_tree().create_timer(1.0).timeout
func path_finding_chase():
var dir = to_local(nav_agent.get_next_path_position()).normalized()
velocity.x = dir.x * speed

func _on_timer_timeout() -> void:


makepath()
func makepath() -> void:
nav_agent.target_position = player.global_position

You might also like