project_teddy/scripts/AI.gd

84 lines
2.8 KiB
GDScript3
Raw Normal View History

2023-02-07 13:22:31 -07:00
extends CharacterBody3D
2023-02-09 13:10:25 -07:00
const SPEED = 3.0
2023-02-07 13:22:31 -07:00
const JUMP_VELOCITY = 4.5
2023-03-19 01:56:03 -06:00
var health = 100
2023-02-07 13:22:31 -07:00
var bullettimer = 50
var bulletshot = false
var bulletrandom
2023-03-19 16:28:17 -06:00
var dead = false
2023-02-07 13:22:31 -07:00
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
var player
2023-03-19 16:28:17 -06:00
@export var Bullet = preload("res://objects/AI-Bullet/Bullet.tscn")
2023-02-07 13:22:31 -07:00
2023-03-19 01:35:35 -06:00
func _ready():
self.visible = false
$CollisionShape3D.disabled = true
2023-02-07 13:22:31 -07:00
func _physics_process(delta):
if Global.selfTeddy:
player = get_node(Global.selfTeddy)
2023-02-07 13:22:31 -07:00
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta
2023-03-19 16:37:06 -06:00
if $CollisionShape3D/Neck/Teddy/AnimationPlayer.is_playing() == false:
$CollisionShape3D/Neck/Teddy/AnimationPlayer.play("idle")
2023-02-07 13:22:31 -07:00
2023-03-19 01:35:35 -06:00
if Global.roundInSession:
self.visible = true
$CollisionShape3D.disabled = false
2023-03-19 16:28:17 -06:00
if player and Global.playerAlive and Global.tutorialComplete and not dead:
2023-03-19 01:56:03 -06:00
if Global.AIHit == true:
Global.AIHit = false
var damage = RandomNumberGenerator.new().randi_range(7, 16)
health -= damage
2023-05-19 12:56:34 -06:00
spawn_cotton()
2023-03-19 01:56:03 -06:00
if health <= 0:
2023-03-19 16:28:17 -06:00
dead_AI()
2023-03-19 01:35:35 -06:00
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
2023-03-23 12:48:49 -06:00
var direction = (player.transform.origin - transform.origin).normalized()
var xz_velocity = Vector3(direction.x, 0, direction.z) * SPEED
var y_velocity = velocity.y
velocity = Vector3(xz_velocity.x, y_velocity, xz_velocity.z)
2023-03-19 01:35:35 -06:00
$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()
2023-03-19 16:28:17 -06:00
func dead_AI():
health = 100
dead = true
2023-05-27 16:49:09 -06:00
Global.AIKilled = true
2023-03-19 16:28:17 -06:00
$CollisionShape3D/Neck/Teddy.visible = false
$CollisionShape3D.disabled = true
await get_tree().create_timer(10).timeout
$CollisionShape3D/Neck/Teddy.visible = true
$CollisionShape3D.disabled = false
dead = false
2023-05-19 12:56:34 -06:00
func spawn_cotton():
$CPUParticles3D.emitting = true
await get_tree().create_timer(0.5).timeout
$CPUParticles3D.emitting = false