Bunny AI Done using Nav Agent

This commit is contained in:
Snoweuph 2023-04-09 16:39:45 +02:00
parent 97a49b122b
commit fd552541a7
10 changed files with 95 additions and 12 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

View file

@ -1,7 +1,12 @@
[gd_resource type="TileSet" load_steps=3 format=3 uid="uid://bj7uu2180mie3"]
[gd_resource type="TileSet" load_steps=4 format=3 uid="uid://bj7uu2180mie3"]
[ext_resource type="Texture2D" uid="uid://bqi3ddjrcj2bh" path="res://Assets/Sprites/TileMap.png" id="1_cl3se"]
[sub_resource type="NavigationPolygon" id="NavigationPolygon_fhapu"]
vertices = PackedVector2Array(-16, -16, 16, -16, 16, 16, -16, 16)
polygons = Array[PackedInt32Array]([PackedInt32Array(0, 1, 2, 3)])
outlines = Array[PackedVector2Array]([PackedVector2Array(-16, -16, 16, -16, 16, 16, -16, 16)])
[sub_resource type="TileSetAtlasSource" id="TileSetAtlasSource_mgef5"]
texture = ExtResource("1_cl3se")
texture_region_size = Vector2i(32, 32)
@ -485,6 +490,12 @@ texture_region_size = Vector2i(32, 32)
11:3/0/terrains_peering_bit/left_side = 0
11:3/0/terrains_peering_bit/top_left_corner = 0
11:3/0/terrains_peering_bit/top_side = 0
10:1/0 = 0
10:1/0/terrain_set = 0
10:1/0/terrain = 1
10:1/0/physics_layer_0/linear_velocity = Vector2(0, 0)
10:1/0/physics_layer_0/angular_velocity = 0.0
10:1/0/navigation_layer_0/polygon = SubResource("NavigationPolygon_fhapu")
[resource]
tile_size = Vector2i(32, 32)
@ -493,4 +504,7 @@ physics_layer_0/collision_mask = 0
terrain_set_0/mode = 0
terrain_set_0/terrain_0/name = "Walls"
terrain_set_0/terrain_0/color = Color(1, 0.301961, 0.329412, 1)
terrain_set_0/terrain_1/name = "Ground"
terrain_set_0/terrain_1/color = Color(0.5, 0.4375, 0.25, 1)
navigation_layer_0/layers = 1
sources/1 = SubResource("TileSetAtlasSource_mgef5")

View file

@ -1,19 +1,37 @@
[gd_scene load_steps=4 format=3 uid="uid://cpl4tllohhyel"]
[gd_scene load_steps=5 format=3 uid="uid://cpl4tllohhyel"]
[ext_resource type="Script" path="res://Scripts/EntitySystem/Bunny.gd" id="1_1kj2b"]
[ext_resource type="Texture2D" uid="uid://df2fluk0dlqar" path="res://Assets/Sprites/Bunny/bad_bunny_spritesheet.png" id="2_v8p70"]
[sub_resource type="CircleShape2D" id="CircleShape2D_sedmr"]
[node name="Bunny" type="CharacterBody2D"]
[sub_resource type="CircleShape2D" id="CircleShape2D_itvjj"]
radius = 14.0
[node name="Bunny" type="CharacterBody2D" node_paths=PackedStringArray("agent")]
collision_layer = 4
collision_mask = 11
collision_mask = 15
script = ExtResource("1_1kj2b")
agent = NodePath("NavigationAgent")
[node name="Sprite2D" type="Sprite2D" parent="."]
position = Vector2(0.5, 0)
texture = ExtResource("2_v8p70")
hframes = 5
vframes = 4
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
shape = SubResource("CircleShape2D_sedmr")
[node name="NavigationAgent" type="NavigationAgent2D" parent="."]
path_metadata_flags = 0
[node name="Area2D" type="Area2D" parent="."]
collision_layer = 64
collision_mask = 4
[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"]
shape = SubResource("CircleShape2D_itvjj")
[connection signal="body_entered" from="Area2D" to="." method="_on_collision"]
[connection signal="body_exited" from="Area2D" to="." method="_of_collision"]

View file

@ -142,7 +142,6 @@ player_save_distance = 320.0
position = Vector2(1440, 810)
collision_layer = 0
script = ExtResource("5_d2cbj")
speed = 200
animation_player = NodePath("Player Animator")
[node name="Player Sprite" type="Sprite2D" parent="Player"]
@ -156,8 +155,8 @@ position = Vector2(2, -24)
polygon = PackedVector2Array(12, 32, -16, 32, -16, -24, 12, -24)
[node name="Player Camera" type="Camera2D" parent="Player"]
position = Vector2(-1, -25)
zoom = Vector2(3, 3)
position = Vector2(0, -25)
zoom = Vector2(2, 2)
position_smoothing_enabled = true
drag_horizontal_enabled = true
drag_vertical_enabled = true

View file

@ -1,12 +1,49 @@
extends CharacterBody2D
class_name Bunny
@export var health : int
@export var min_distance_to_player := 80.0
@export var max_distance_to_player := 800.0
@export var agent : NavigationAgent2D
@export var speed := 200.0
var health : int
var team : int
var player : Node2D
var on_death_callbacks : Array
var dir : Vector2
var spread_time : float
var current_time : float
var collisions : int
func _ready():
seed(int(str(self)))
spread_time = randf() * 3
current_time = spread_time
func _physics_process(delta):
current_time -= delta
if player != null and self.global_position.distance_to(player.global_position) < max_distance_to_player and collisions <= 3:
if current_time <= 0:
current_time = spread_time
agent.target_position = player.global_position
if min_distance_to_player and agent.is_target_reachable():
var next_location = agent.get_next_path_position() + Vector2(randf() - 0.5 * 10, randf() - 0.5 * 10)
dir = self.global_position.direction_to(next_location).normalized()
if self.global_position.distance_to(player.global_position) > min_distance_to_player:
self.velocity = dir * delta * 60 * speed
move_and_slide()
pass
func _on_collision(body):
if body != self: collisions += 1
pass
func _of_collision(body):
if body != self: collisions-= 1
pass
func damage(damage : int):
health -= damage
if(health <= 0): on_death()
@ -24,3 +61,9 @@ func on_death():
func sub_on_death(callback : Callable):
on_death_callbacks.push_front(callback)
pass

View file

@ -12,6 +12,7 @@ func spawn_bunny(pos : Vector2, team : int, health : int) -> Bunny:
bunny.global_position = pos
bunny.health = health
bunny.team = team
bunny.player = player
return bunny
func spawn_wave(free_tiles : Array, team: int, amount : int, health : int) -> Array:

View file

@ -12,11 +12,10 @@ func _ready():
print("time for generation: " + str(gen_data.time))
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
if(bunnys.size() == 0):
wave += 1
bunnys = bunny_generator.spawn_wave(gen_data.free_tiles.duplicate(), TEAM.EVIL, wave, 3)
bunnys = bunny_generator.spawn_wave(gen_data.free_tiles.duplicate(), TEAM.EVIL, wave * 500, 3)
for bunny in bunnys:
bunny.sub_on_death(func(bunny): bunnys.erase(bunny))
bunny.sub_on_death(func(bunny): bunny.queue_free())

View file

@ -266,5 +266,5 @@ func set_terrain_tile_solid(x : int, y : int):
pass
func set_terrain_tile_non_solid(x : int, y : int):
self.set_cell(0, Vector2i(x,y), -1)
self.set_cells_terrain_connect(0, [Vector2i(x,y)], 0, 1, false)
pass

View file

@ -1,6 +1,6 @@
extends CharacterBody2D
@export var speed = 20
@export var speed = 200
@export_range(0, 1) var damping_factor = 0.6
@export var animation_player : AnimationPlayer

View file

@ -15,6 +15,13 @@ run/main_scene="res://Scenes/Test.tscn"
config/features=PackedStringArray("4.0", "GL Compatibility")
config/icon="res://icon.svg"
[display]
window/size/viewport_width=1280
window/size/viewport_height=720
window/stretch/mode="viewport"
window/stretch/aspect="expand"
[dotnet]
project/assembly_name="HoppyEaster"
@ -59,10 +66,12 @@ attack={
[layer_names]
2d_physics/layer_1="Map"
2d_navigation/layer_1="Map"
2d_physics/layer_2="Player"
2d_physics/layer_3="Bunny"
2d_physics/layer_4="Projectile"
2d_physics/layer_5="Layer 5"
2d_physics/layer_7="bunny_check"
[rendering]