The AI now takes little breaks

This commit is contained in:
Paul Black 2022-03-07 21:20:37 -07:00
parent e546abf095
commit 475c640604

View file

@ -1,32 +1,49 @@
extends KinematicBody
export (PackedScene) var Bullet = preload("res://Objects/Bullet/Bullet.tscn")
## Timer vars
#Bullets
var _bullettimer = null
var bullettime = null
var bulletrandomnum = RandomNumberGenerator.new()
#Walking
var _walkingtimer = null
var walkingtime = null
var walkingrandomnum = RandomNumberGenerator.new()
var ablewalk = 0
var speed = 10
var gravity = 100
var velocity = Vector3.ZERO
onready var player = get_node("/root/Spatial/PlayerTank")
var bullettime = null
func _ready(): ##For bullet timer
func _ready():
##For bullet timer
_bullettimer = Timer.new()
add_child(_bullettimer)
bulletrandomnum.randomize()
bullettime = bulletrandomnum.randf_range(0.0, 5.0)
bullettime = bulletrandomnum.randf_range(1.0, 5.0)
_bullettimer.connect("timeout", self, "_bullet_fire")
_bullettimer.set_wait_time(bullettime)
_bullettimer.set_one_shot(false) # Make sure it loops
_bullettimer.start()
##For walking timer
_walkingtimer = Timer.new()
add_child(_walkingtimer)
walkingrandomnum.randomize()
walkingtime = walkingrandomnum.randf_range(1.0, 10.0)
_walkingtimer.connect("timeout", self, "_walk_towards_player_timer")
_walkingtimer.set_wait_time(walkingtime)
_walkingtimer.set_one_shot(false) # Make sure it loops
_walkingtimer.start()
func _physics_process(delta):
velocity = Vector3.ZERO
if player != null: # Makes sure player is not an empty variable (not equal to nill/null)
$CollisionShape.look_at(Vector3(player.global_transform.origin.x, global_transform.origin.y, player.global_transform.origin.z), Vector3(0,1,0)) # Look at player
velocity = (player.transform.origin - transform.origin).normalized() * speed # make velocity direction equal to player direction
velocity = move_and_slide(velocity) # Move AI towards player
if ablewalk == 0:
_walk_towards_player()
_look_at_player()
func _bullet_fire():
if player != null:
@ -38,3 +55,22 @@ func _bullet_fire():
bulletrandomnum.randomize()
bullettime = bulletrandomnum.randf_range(1.0, 5.0)
_bullettimer.set_wait_time(bullettime)
func _walk_towards_player():
if player != null: # Makes sure player is not an empty variable (not equal to nill/null)
velocity = (player.transform.origin - transform.origin).normalized() * speed # make velocity direction equal to player direction
velocity = move_and_slide(velocity) # Move AI towards player
func _look_at_player():
if player != null:
$CollisionShape.look_at(Vector3(player.global_transform.origin.x, global_transform.origin.y, player.global_transform.origin.z), Vector3(0,1,0)) # Look at player
func _walk_towards_player_timer():
walkingrandomnum.randomize()
walkingtime = walkingrandomnum.randf_range(1.0, 10.0)
_walkingtimer.set_wait_time(walkingtime)
if ablewalk == 1:
ablewalk = 0
else:
if ablewalk == 0:
ablewalk = 1