From 90500c2ee1c6d49264f51aff4f37f67c69877bc7 Mon Sep 17 00:00:00 2001 From: Ervan Lefevre Date: Sat, 30 Sep 2023 14:16:00 +0200 Subject: [PATCH] :sparkles: Adds a minimalist AI for NPCs --- nodes/npc/npc.gd | 35 +++++++++++++++++++++++++++++++++++ nodes/npc/npc.png | Bin 0 -> 1034 bytes nodes/npc/npc.png.import | 34 ++++++++++++++++++++++++++++++++++ nodes/npc/npc.tscn | 24 ++++++++++++++++++++++++ project.godot | 2 +- scenes/poc.tscn | 25 +++++++++++++++++++++++-- 6 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 nodes/npc/npc.gd create mode 100644 nodes/npc/npc.png create mode 100644 nodes/npc/npc.png.import create mode 100644 nodes/npc/npc.tscn diff --git a/nodes/npc/npc.gd b/nodes/npc/npc.gd new file mode 100644 index 0000000..7c0a64a --- /dev/null +++ b/nodes/npc/npc.gd @@ -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 diff --git a/nodes/npc/npc.png b/nodes/npc/npc.png new file mode 100644 index 0000000000000000000000000000000000000000..8b30563ac6572b138616cc51a56c55d93139ea80 GIT binary patch literal 1034 zcmV+l1oiugP)EX>4Tx04R}tkv&MmKpe$iQ?)7;2Rn#}WT;Md@B?wIRVYG*QY*CTU~=gfG-*gu zTpR`0f`cE6RRbkxtDMhls^O8_R9XiiS!&O&nHKjq-(z z%L?Z$&T6^Jn)l={4Cb_zWvAO zrG<}x{%zpmx~0i`z~v4w_@qmQmKj!Ztv~iGtK^f0F`cXwCrR{TL1t624YJ`L;&{y<^a?$hK=n2000SaNLh0L z0GY}F0GY}GYM3o`>7bQmlE00IU{L_t(&-tAXENCHtD{@%;R zh@htE+DbT~`3U475OQjBIuM-PnnWsy%0XNjr6CX;!lfl}7Q>Eq1!uRSy(N4Nd2&AP z@~L-vr|*L|-0yz(c;EN?^L__NNJvOXNUSVKkPl~Q8slwM(ETi-Z=7SxiF`y&mww{W*e8G;+pF7Vw|zbE zIV?WBzUvCzoMi8dqZAS^PmYb!E{2;B#Ao7b0Cuy*x&ffz;zo0CxaV0Mtpyi(GjYdApsDyj!H5$|7*rV58R`uBvGg8xfYNwS1y@Xd1H<_q7D3BrnpBc%l9gW3sV1;&J^?LtJ#KBLiQ?3p2|=M172? zW%qI*Bix#KB=+MPWRnOqATjMZ7y|xBE0W^O$GyY%H#l!d0Rmngp%*}eAX(-N3v>fq zbP2E-{8?Bshxs4^Tc#3N>RTXGky=>+sEoc8Kw_ow3m5L=M7Dlc(EtDd07*qoM6N<$ Ef)XUt0{{R3 literal 0 HcmV?d00001 diff --git a/nodes/npc/npc.png.import b/nodes/npc/npc.png.import new file mode 100644 index 0000000..6937127 --- /dev/null +++ b/nodes/npc/npc.png.import @@ -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 diff --git a/nodes/npc/npc.tscn b/nodes/npc/npc.tscn new file mode 100644 index 0000000..093a1de --- /dev/null +++ b/nodes/npc/npc.tscn @@ -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("..") diff --git a/project.godot b/project.godot index 076b15d..91baaf6 100644 --- a/project.godot +++ b/project.godot @@ -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" diff --git a/scenes/poc.tscn b/scenes/poc.tscn index 8ef95af..9e1cbd5 100644 --- a/scenes/poc.tscn +++ b/scenes/poc.tscn @@ -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)