Adds a minimalist AI for NPCs

main
Ervan Lefevre 2 years ago
parent f2648f6c3c
commit 90500c2ee1
  1. 35
      nodes/npc/npc.gd
  2. BIN
      nodes/npc/npc.png
  3. 34
      nodes/npc/npc.png.import
  4. 24
      nodes/npc/npc.tscn
  5. 2
      project.godot
  6. 25
      scenes/poc.tscn

@ -0,0 +1,35 @@
extends CharacterBody2D
class_name Npc
const SPEED = 100
const MINIMUM_STATE_DURATION = 3
const STATES = ['panic']
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var direction = 0
func _ready():
pick_new_direction()
$fsm.set_states(STATES)
func panic_state(delta):
if is_on_wall():
direction *= -1
var should_update = (
$fsm.state_duration >= MINIMUM_STATE_DURATION
&& ceil($fsm.state_duration) % 2 == (randi() %2)
)
if should_update:
pick_new_direction()
$fsm.state_duration = 0
velocity.x = direction
velocity.y += gravity * delta
move_and_slide()
func pick_new_direction():
direction = [1,-1][randi_range(0,1)] * SPEED

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://bm258xfujps7e"
path="res://.godot/imported/npc.png-6ab5db0d3e525d0fae0b471428195d99.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://nodes/npc/npc.png"
dest_files=["res://.godot/imported/npc.png-6ab5db0d3e525d0fae0b471428195d99.ctex"]
[params]
compress/mode=0
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=false
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1

@ -0,0 +1,24 @@
[gd_scene load_steps=5 format=3 uid="uid://dvx48q5ecyxjs"]
[ext_resource type="Script" path="res://nodes/npc/npc.gd" id="1_38jh5"]
[ext_resource type="Texture2D" uid="uid://bm258xfujps7e" path="res://nodes/npc/npc.png" id="1_hgsqd"]
[ext_resource type="Script" path="res://scripts/ia/fsm.gd" id="3_vhp64"]
[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_f223n"]
radius = 4.0
height = 14.0
[node name="npc" type="CharacterBody2D"]
script = ExtResource("1_38jh5")
[node name="shape" type="CollisionShape2D" parent="."]
rotation = 1.5708
shape = SubResource("CapsuleShape2D_f223n")
[node name="sprite" type="Sprite2D" parent="."]
scale = Vector2(0.5, 0.5)
texture = ExtResource("1_hgsqd")
[node name="fsm" type="Node2D" parent="." node_paths=PackedStringArray("root")]
script = ExtResource("3_vhp64")
root = NodePath("..")

@ -10,7 +10,7 @@ config_version=5
[application]
config/name="LD54"
config/name="Ant Underwater Odyssey"
run/main_scene="res://scenes/main_menu.tscn"
config/features=PackedStringArray("4.1", "GL Compatibility")
config/icon="res://icon.svg"

@ -1,9 +1,10 @@
[gd_scene load_steps=16 format=3 uid="uid://ce8ov6npvl27w"]
[gd_scene load_steps=17 format=3 uid="uid://ce8ov6npvl27w"]
[ext_resource type="Texture2D" uid="uid://cfq8pma4bpgq" path="res://assets/tilesets/cave_tileset.png" id="1_vofrt"]
[ext_resource type="PackedScene" uid="uid://c5rmt3jeffjx7" path="res://nodes/player/player.tscn" id="2_gxv3v"]
[ext_resource type="Texture2D" uid="uid://ccqiabs4p3ryl" path="res://assets/placeholders/map.png" id="3_aeud1"]
[ext_resource type="Script" path="res://scripts/gameplay/rising_level.gd" id="4_ts3u4"]
[ext_resource type="PackedScene" uid="uid://dvx48q5ecyxjs" path="res://nodes/npc/npc.tscn" id="5_0u2ly"]
[sub_resource type="NavigationPolygon" id="NavigationPolygon_3t6yd"]
vertices = PackedVector2Array(0, -16, 16, -16, 16, 16, 0, 16)
@ -240,8 +241,28 @@ position = Vector2(1136, 1968)
position = Vector2(1736, 3056)
monitorable = false
script = ExtResource("4_ts3u4")
RISING_SPEED = 0
RISING_SPEED = 5
[node name="shape" type="CollisionShape2D" parent="water_level"]
position = Vector2(2.08165e-12, 2.08165e-12)
shape = SubResource("RectangleShape2D_qoxeb")
[node name="npcs" type="Node2D" parent="."]
[node name="npc" parent="npcs" instance=ExtResource("5_0u2ly")]
position = Vector2(1320, 1976)
[node name="npc2" parent="npcs" instance=ExtResource("5_0u2ly")]
position = Vector2(1360, 1976)
[node name="npc3" parent="npcs" instance=ExtResource("5_0u2ly")]
position = Vector2(1432, 1992)
[node name="npc4" parent="npcs" instance=ExtResource("5_0u2ly")]
position = Vector2(1496, 1992)
[node name="npc5" parent="npcs" instance=ExtResource("5_0u2ly")]
position = Vector2(1552, 1992)
[node name="npc6" parent="npcs" instance=ExtResource("5_0u2ly")]
position = Vector2(1256, 1968)

Loading…
Cancel
Save