project_teddy/scripts/AI.gd

63 lines
2.2 KiB
GDScript

extends CharacterBody3D
const SPEED = 3.0
const JUMP_VELOCITY = 4.5
var health = 100
var bullettimer = 50
var bulletshot = false
var bulletrandom
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
var player
@export var Bullet = preload("res://objects/Bullet/Bullet.tscn")
func _ready():
self.visible = false
$CollisionShape3D.disabled = true
func _physics_process(delta):
if Global.selfTeddy:
player = get_node(Global.selfTeddy)
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta
if Global.roundInSession:
self.visible = true
$CollisionShape3D.disabled = false
if $CollisionShape3D/Neck/Teddy/AnimationPlayer.is_playing() == false and not Global.tutorialComplete:
$CollisionShape3D/Neck/Teddy/AnimationPlayer.play("idle")
if player and Global.playerAlive and Global.tutorialComplete:
if Global.AIHit == true:
Global.AIHit = false
var damage = RandomNumberGenerator.new().randi_range(7, 16)
health -= damage
print(health)
if health <= 0:
self.queue_free()
if $CollisionShape3D/Neck/Teddy/AnimationPlayer.is_playing():
var animation = $CollisionShape3D/Neck/Teddy/AnimationPlayer.get_current_animation()
if animation == "idle":
$CollisionShape3D/Neck/Teddy/AnimationPlayer.play("walk")
if $CollisionShape3D/Neck/Teddy/AnimationPlayer.is_playing() == false:
$CollisionShape3D/Neck/Teddy/AnimationPlayer.play("walk")
bullettimer = bullettimer - 10 * delta
if bullettimer < 0 and bulletshot == false:
bulletshot = true
var b = Bullet.instantiate()
owner.add_child(b)
b.transform = $CollisionShape3D/Neck/BulletGenerator.global_transform
b.velocity = -b.global_transform.basis.z * b.muzzle_velocity
$sound.stream = load("res://sounds/pistol_shoot.wav")
$sound.play()
elif bulletshot == true:
bulletshot = false
bulletrandom = randf_range(1.0, 50.0)
bullettimer = bulletrandom
velocity = (player.transform.origin - transform.origin).normalized() * SPEED
$CollisionShape3D.look_at(Vector3(player.global_transform.origin.x, global_transform.origin.y, player.global_transform.origin.z), Vector3(0,1,0))
move_and_slide()