From bed36d4226da8a8b42f3ae1b3a58236a09e19ab3 Mon Sep 17 00:00:00 2001 From: Yan Wittmann Date: Tue, 5 Nov 2024 10:44:20 +0100 Subject: [PATCH] Added pathfinding project --- pathfinding-algorithms/.gitattributes | 2 ++ pathfinding-algorithms/.gitignore | 3 ++ pathfinding-algorithms/character.gd | 21 ++++++++++++ pathfinding-algorithms/icon.svg | 1 + pathfinding-algorithms/icon.svg.import | 37 ++++++++++++++++++++ pathfinding-algorithms/main.tscn | 45 +++++++++++++++++++++++++ pathfinding-algorithms/project.godot | 16 +++++++++ pathfinding-algorithms/scenes/wall.tscn | 8 +++++ 8 files changed, 133 insertions(+) create mode 100644 pathfinding-algorithms/.gitattributes create mode 100644 pathfinding-algorithms/.gitignore create mode 100644 pathfinding-algorithms/character.gd create mode 100644 pathfinding-algorithms/icon.svg create mode 100644 pathfinding-algorithms/icon.svg.import create mode 100644 pathfinding-algorithms/main.tscn create mode 100644 pathfinding-algorithms/project.godot create mode 100644 pathfinding-algorithms/scenes/wall.tscn diff --git a/pathfinding-algorithms/.gitattributes b/pathfinding-algorithms/.gitattributes new file mode 100644 index 0000000..8ad74f7 --- /dev/null +++ b/pathfinding-algorithms/.gitattributes @@ -0,0 +1,2 @@ +# Normalize EOL for all files that Git considers text files. +* text=auto eol=lf diff --git a/pathfinding-algorithms/.gitignore b/pathfinding-algorithms/.gitignore new file mode 100644 index 0000000..0af181c --- /dev/null +++ b/pathfinding-algorithms/.gitignore @@ -0,0 +1,3 @@ +# Godot 4+ specific ignores +.godot/ +/android/ diff --git a/pathfinding-algorithms/character.gd b/pathfinding-algorithms/character.gd new file mode 100644 index 0000000..92fb707 --- /dev/null +++ b/pathfinding-algorithms/character.gd @@ -0,0 +1,21 @@ +extends CharacterBody2D + + +const MAX_SPEED = 300.0 +const ACCELERATION = 30 + +@export var nav: NavigationAgent2D + +func _physics_process(delta: float) -> void: + var movement: Vector2 = Vector2() + + nav.target_position = get_global_mouse_position() + + movement = nav.get_next_path_position() - global_position + movement = movement.normalized() * ACCELERATION + + velocity += movement + if velocity.length() > MAX_SPEED: + velocity = velocity.normalized() * MAX_SPEED + + move_and_slide() diff --git a/pathfinding-algorithms/icon.svg b/pathfinding-algorithms/icon.svg new file mode 100644 index 0000000..9d8b7fa --- /dev/null +++ b/pathfinding-algorithms/icon.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/pathfinding-algorithms/icon.svg.import b/pathfinding-algorithms/icon.svg.import new file mode 100644 index 0000000..06b5cbb --- /dev/null +++ b/pathfinding-algorithms/icon.svg.import @@ -0,0 +1,37 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://c53ftc05so8rx" +path="res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://icon.svg" +dest_files=["res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.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 +svg/scale=1.0 +editor/scale_with_editor_scale=false +editor/convert_colors_with_editor_theme=false diff --git a/pathfinding-algorithms/main.tscn b/pathfinding-algorithms/main.tscn new file mode 100644 index 0000000..9ff85ce --- /dev/null +++ b/pathfinding-algorithms/main.tscn @@ -0,0 +1,45 @@ +[gd_scene load_steps=6 format=3 uid="uid://dau1xpov0ke0n"] + +[ext_resource type="PackedScene" uid="uid://dy22higrwdqs6" path="res://scenes/wall.tscn" id="1_me3nh"] +[ext_resource type="Texture2D" uid="uid://c53ftc05so8rx" path="res://icon.svg" id="2_n113n"] +[ext_resource type="Script" path="res://character.gd" id="2_yf8gw"] + +[sub_resource type="NavigationPolygon" id="NavigationPolygon_6menk"] +vertices = PackedVector2Array(771.227, 352.719, 859.359, 629.172, 10.9531, 639.867, 8.04688, 12.8594, 716.352, 232.773, 1102.45, 103.406, 1144.2, 472.531, 892.82, 355.148, 942.891, 552.5, 667.352, 67.3984) +polygons = Array[PackedInt32Array]([PackedInt32Array(0, 1, 2, 3, 4), PackedInt32Array(4, 5, 6, 7), PackedInt32Array(6, 8, 7), PackedInt32Array(4, 7, 0), PackedInt32Array(3, 9, 4)]) +outlines = Array[PackedVector2Array]([PackedVector2Array(-2, 2, 675, 58, 723, 220, 1111, 90, 1155, 479, 936, 566, 885, 365, 785, 363, 873, 639, 1, 650)]) + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_axamc"] +size = Vector2(50, 50) + +[node name="Main" type="Node2D"] + +[node name="NavigationRegion2D" type="NavigationRegion2D" parent="."] +navigation_polygon = SubResource("NavigationPolygon_6menk") + +[node name="walls" type="Node2D" parent="."] + +[node name="Wall1" parent="walls" instance=ExtResource("1_me3nh")] +position = Vector2(348, 498) +scale = Vector2(20, 1) + +[node name="Wall2" parent="walls" instance=ExtResource("1_me3nh")] +position = Vector2(174, 253) +scale = Vector2(10, 1) + +[node name="CharacterBody2D" type="CharacterBody2D" parent="." node_paths=PackedStringArray("nav")] +position = Vector2(486, 349) +collision_mask = 3 +script = ExtResource("2_yf8gw") +nav = NodePath("NavigationAgent2D") +metadata/_edit_group_ = true + +[node name="NavigationAgent2D" type="NavigationAgent2D" parent="CharacterBody2D"] +debug_enabled = true + +[node name="Icon" type="Sprite2D" parent="CharacterBody2D"] +scale = Vector2(0.4, 0.4) +texture = ExtResource("2_n113n") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"] +shape = SubResource("RectangleShape2D_axamc") diff --git a/pathfinding-algorithms/project.godot b/pathfinding-algorithms/project.godot new file mode 100644 index 0000000..7c9e65f --- /dev/null +++ b/pathfinding-algorithms/project.godot @@ -0,0 +1,16 @@ +; Engine configuration file. +; It's best edited using the editor UI and not directly, +; since the parameters that go here are not all obvious. +; +; Format: +; [section] ; section goes between [] +; param=value ; assign values to parameters + +config_version=5 + +[application] + +config/name="pathfinding-algorithms" +run/main_scene="res://main.tscn" +config/features=PackedStringArray("4.3", "Forward Plus") +config/icon="res://icon.svg" diff --git a/pathfinding-algorithms/scenes/wall.tscn b/pathfinding-algorithms/scenes/wall.tscn new file mode 100644 index 0000000..bf3cdd2 --- /dev/null +++ b/pathfinding-algorithms/scenes/wall.tscn @@ -0,0 +1,8 @@ +[gd_scene load_steps=2 format=3 uid="uid://dy22higrwdqs6"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_l8b6c"] + +[node name="Wall" type="StaticBody2D"] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_l8b6c")