diff --git a/.godot/editor/editor_layout.cfg b/.godot/editor/editor_layout.cfg index 9f7d193..42d6434 100644 --- a/.godot/editor/editor_layout.cfg +++ b/.godot/editor/editor_layout.cfg @@ -20,7 +20,7 @@ open_scenes=["res://scenes/WorldGeneration.tscn", "res://scenes/3DWorld.tscn", " [ScriptEditor] -open_scripts=["res://scripts/Camera3D.gd", "res://scripts/Chunks.gd", "res://scripts/Entity.gd", "res://scripts/Global.gd", "res://scripts/Instance3D.gd", "res://scripts/Instance.gd", "res://scripts/Map.gd", "res://scripts/Selection.gd", "res://scripts/World.gd", "res://scripts/WorldGeneration.gd"] +open_scripts=["res://scripts/Camera3D.gd", "res://scripts/Chunks.gd", "res://scripts/Entity.gd", "res://scripts/Global.gd", "res://scripts/Instance3D.gd", "res://scripts/Map.gd", "res://scripts/Selection.gd", "res://scripts/World.gd", "res://scripts/WorldGeneration.gd"] open_help=["Callable", "GridMap", "Node3D", "Object", "PhysicsDirectBodyState3D", "PhysicsDirectSpaceState2D", "PhysicsDirectSpaceState3D", "RayCast3D", "RID", "Vector2"] script_split_offset=70 list_split_offset=0 diff --git a/.godot/editor/filesystem_cache8 b/.godot/editor/filesystem_cache8 index b9c20f3..76fa637 100644 --- a/.godot/editor/filesystem_cache8 +++ b/.godot/editor/filesystem_cache8 @@ -1,12 +1,12 @@ 2a60927148abd1d33b818b535e306557 -::res://::1677783027 +::res://::1678475097 export_presets.cfg::TextFile::-1::1677194603::0::1::::<><>:: icon.png::CompressedTexture2D::4672001524465425285::1676578483::1676582905::1::::<><>:: README.md::TextFile::-1::1676578483::0::1::::<><>:: -::res://assets/::1677704033 -environment.tres::Environment::8600195688495287911::1677704033::0::1::::<><>:: +::res://assets/::1678217906 +environment.tres::Environment::8600195688495287911::1678217906::0::1::::<><>:: water.gdshader::Shader::-1::1677174532::0::1::::<><>:: -::res://assets/blocs/::1677704516 +::res://assets/blocs/::1678233640 0_sides.glb::PackedScene::7036506600244808940::1677174532::1677174532::1::::<><>:: 0_sides_grass.png::CompressedTexture2D::4551055068461159203::1677174532::1677174532::1::::<><>:: 1_side.glb::PackedScene::5601417408597394875::1677174532::1677617210::1::::<><>:: @@ -20,7 +20,7 @@ water.gdshader::Shader::-1::1677174532::0::1::::<><>:: 4_sides.glb::PackedScene::139420628202576697::1677174532::1677174532::1::::<><>:: 4_sides_grass.png::CompressedTexture2D::7961782411803042998::1677174532::1677174532::1::::<><>:: blocs.meshlib::MeshLibrary::3161916306234183153::1677617602::0::1::::<><>::uid://b53thhe2sa6x0<>uid://cfr1s2lqng8eo<>uid://c0xxcgdm7s2ak<>uid://bffdt5if5pnda<>uid://kc4nqve8lx14<>uid://dlo0nmochxy4u -blocs.tscn::PackedScene::3779811605783367088::1677704516::0::1::::<><>::uid://b53thhe2sa6x0<>uid://cfr1s2lqng8eo<>uid://c0xxcgdm7s2ak<>uid://bffdt5if5pnda<>uid://kc4nqve8lx14<>uid://dlo0nmochxy4u +blocs.tscn::PackedScene::3779811605783367088::1678233640::0::1::::<><>::uid://b53thhe2sa6x0<>uid://cfr1s2lqng8eo<>uid://c0xxcgdm7s2ak<>uid://bffdt5if5pnda<>uid://kc4nqve8lx14<>uid://dlo0nmochxy4u ::res://assets/images/::1677174532 colors.png::CompressedTexture2D::2558953172502239658::1677174532::1677174532::1::::<><>:: grass_top.png::CompressedTexture2D::3263707750932711970::1677174532::1677174532::1::::<><>:: @@ -55,19 +55,19 @@ sdqs.meshlib::MeshLibrary::2440886030015449773::1677616939::0::1::::<><>::uid:// blocs.meshlib::MeshLibrary::5001218139483850589::1677702801::0::1::::<><>::uid://ffn1ia7c3x60<>uid://birlu82nfgbsx<>uid://t03cipsdfuim<>uid://68inkwq5fe2v<>uid://blv0o0tit1gob<>uid://dg3l2dsc7n6s<>uid://dpewuerjdlgms<>uid://d3a4ur67k13wa<>uid://d1ioc5uj71iud<>uid://2s6bm46h6kfw<>uid://bttpxki4m82q1<>uid://bh7awq00jlfkn ::res://assets/ui/::1677619095 bloc_select.png::CompressedTexture2D::1141775147858261351::1677618805::1677619095::1::::<><>:: -::res://scenes/::1677704516 -3DWorld.tscn::PackedScene::3599411568061770725::1677704516::0::1::::<><>::uid://blbwkgls3oafx<>res://scripts/Chunks.gd<>res://scripts/Camera3D.gd<>res://assets/water.gdshader<>uid://dur874iy77jjr<>uid://qjc4ttmkxbch +::res://scenes/::1678233640 +3DWorld.tscn::PackedScene::3599411568061770725::1678233640::0::1::::<><>::uid://blbwkgls3oafx<>res://scripts/Chunks.gd<>res://scripts/Camera3D.gd<>res://assets/water.gdshader<>uid://dur874iy77jjr<>uid://qjc4ttmkxbch<>res://scripts/Selection.gd Game.tscn::PackedScene::9075056520838281688::1677174532::0::1::::<><>::uid://brjt1u0dgbmi4<>uid://d1sd7pxywgunu -HUD.tscn::PackedScene::9021570906125339886::1677704516::0::1::::<><>::res://scripts/Map.gd -Instance.tscn::PackedScene::4220804716574195494::1677704516::0::1::::<><>::res://scripts/Instance3D.gd -WorldGeneration.tscn::PackedScene::1108513713840477990::1677704515::0::1::::<><>::res://scripts/WorldGeneration.gd -::res://scripts/::1677704514 +HUD.tscn::PackedScene::9021570906125339886::1678233640::0::1::::<><>::res://scripts/Map.gd +Instance.tscn::PackedScene::4220804716574195494::1678233640::0::1::::<><>::res://scripts/Instance3D.gd +WorldGeneration.tscn::PackedScene::1108513713840477990::1678233639::0::1::::<><>::res://scripts/WorldGeneration.gd +::res://scripts/::1678233622 Camera3D.gd::GDScript::-1::1677174532::0::1::::<>Camera3D<>:: -Chunks.gd::GDScript::-1::1677704514::0::1::::<>GridMap<>:: -Entity.gd::GDScript::-1::1677703359::0::1::::Entity<>Node<>:: +Chunks.gd::GDScript::-1::1678219623::0::1::::<>GridMap<>:: +Entity.gd::GDScript::-1::1678233622::0::1::::Entity<>Node<>:: Global.gd::GDScript::-1::1677174532::0::1::::<>Node<>:: -Instance3D.gd::GDScript::-1::1677613303::0::1::::<>CharacterBody3D<>:: -Instance.gd::GDScript::-1::1677531382::0::1::::<>CharacterBody3D<>:: -Map.gd::GDScript::-1::1677190875::0::1::::<>TextureRect<>:: -World.gd::GDScript::-1::1677530828::0::1::::World<>Node<>:: -WorldGeneration.gd::GDScript::-1::1677536001::0::1::::<>Control<>:: +Instance3D.gd::GDScript::-1::1677873181::0::1::::<>CharacterBody3D<>:: +Map.gd::GDScript::-1::1678219623::0::1::::<>TextureRect<>:: +Selection.gd::GDScript::-1::1678222510::0::1::::<>Node3D<>:: +World.gd::GDScript::-1::1678232290::0::1::::World<>Node<>:: +WorldGeneration.gd::GDScript::-1::1678232290::0::1::::<>Control<>:: diff --git a/.godot/editor/filesystem_update4 b/.godot/editor/filesystem_update4 index eab4041..84bffe7 100644 --- a/.godot/editor/filesystem_update4 +++ b/.godot/editor/filesystem_update4 @@ -4,7 +4,5 @@ res://scenes/3DWorld.tscn res://scenes/Instance.tscn res://assets/blocs/blocs.tscn res://scenes/HUD.tscn -res://scripts/Selection.gd -res://scripts/Chunks.gd -res://scripts/World.gd res://scripts/Entity.gd +res://scripts/World.gd diff --git a/.godot/editor/project_metadata.cfg b/.godot/editor/project_metadata.cfg index f50e88a..f770323 100644 --- a/.godot/editor/project_metadata.cfg +++ b/.godot/editor/project_metadata.cfg @@ -1,6 +1,6 @@ [editor_metadata] -executable_path="/home/valentin/Téléchargements/Godot_v4.0-stable_linux.x86_64" +executable_path="/app/bin/godot-bin" tile_source_sort=0 use_advanced_connections=false @@ -12,7 +12,7 @@ run_reload_scripts=true [recent_files] scenes=["res://scenes/HUD.tscn", "res://assets/blocs/blocs.tscn", "res://scenes/Instance.tscn", "res://scenes/3DWorld.tscn", "res://scenes/WorldGeneration.tscn", "res://assets/blocs/4_sides.glb", "res://assets/blocs/3_side_angle.glb", "res://assets/blocs/2_side_opposite.glb", "res://assets/blocs/2_side_angle.glb", "res://assets/blocs/1_side.glb"] -scripts=["PhysicsDirectSpaceState2D", "res://scripts/Selection.gd", "Vector2", "RID", "RayCast3D", "PhysicsDirectSpaceState3D", "PhysicsDirectBodyState3D", "Object", "Node3D", "GridMap"] +scripts=["Vector2", "RID", "RayCast3D", "PhysicsDirectSpaceState3D", "PhysicsDirectSpaceState2D", "PhysicsDirectBodyState3D", "Object", "Node3D", "GridMap", "Callable"] [color_picker] @@ -23,6 +23,7 @@ recent_presets=PackedColorArray(1, 1, 1, 1, 0.72549, 0.74902, 0.0156863, 1, 0.74 project_settings=Rect2(575, 408, 1531, 671) search_help=Rect2(1192, 168, 1368, 912) export=Rect2(468, 69, 1800, 1011) +editor_settings=Rect2(830, 227, 900, 700) [project_settings] diff --git a/.godot/editor/script_editor_cache.cfg b/.godot/editor/script_editor_cache.cfg index d057dc1..9466bb0 100644 --- a/.godot/editor/script_editor_cache.cfg +++ b/.godot/editor/script_editor_cache.cfg @@ -3,16 +3,12 @@ state={ "bookmarks": PackedInt32Array(), "breakpoints": PackedInt32Array(), -"column": 1, +"column": 21, "folded_lines": Array[int]([]), "h_scroll_position": 0, -"row": 11, -"scroll_position": 0.0, -"selection": true, -"selection_from_column": 1, -"selection_from_line": 11, -"selection_to_column": 17, -"selection_to_line": 11, +"row": 15, +"scroll_position": 32.0, +"selection": false, "syntax_highlighter": "GDScript" } @@ -24,8 +20,8 @@ state={ "column": 0, "folded_lines": Array[int]([]), "h_scroll_position": 0, -"row": 10, -"scroll_position": 39.0, +"row": 90, +"scroll_position": 65.0, "selection": false, "syntax_highlighter": "GDScript" } @@ -35,11 +31,11 @@ state={ state={ "bookmarks": PackedInt32Array(), "breakpoints": PackedInt32Array(), -"column": 47, +"column": 28, "folded_lines": Array[int]([]), "h_scroll_position": 0, -"row": 90, -"scroll_position": 67.0, +"row": 78, +"scroll_position": 54.0, "selection": false, "syntax_highlighter": "GDScript" } @@ -49,11 +45,11 @@ state={ state={ "bookmarks": PackedInt32Array(), "breakpoints": PackedInt32Array(), -"column": 26, +"column": 2, "folded_lines": Array[int]([]), "h_scroll_position": 0, -"row": 32, -"scroll_position": 12.0, +"row": 82, +"scroll_position": 63.0, "selection": false, "syntax_highlighter": "GDScript" } @@ -96,24 +92,6 @@ state={ "h_scroll_position": 0, "row": 2, "scroll_position": 0.0, -"selection": true, -"selection_from_column": 5, -"selection_from_line": 2, -"selection_to_column": 17, -"selection_to_line": 2, -"syntax_highlighter": "GDScript" -} - -[res://scripts/Instance.gd] - -state={ -"bookmarks": PackedInt32Array(), -"breakpoints": PackedInt32Array(), -"column": 0, -"folded_lines": Array[int]([]), -"h_scroll_position": 0, -"row": 0, -"scroll_position": 1.0, "selection": false, "syntax_highlighter": "GDScript" } @@ -123,10 +101,10 @@ state={ state={ "bookmarks": PackedInt32Array(), "breakpoints": PackedInt32Array(), -"column": 0, +"column": 17, "folded_lines": Array[int]([]), "h_scroll_position": 0, -"row": 15, +"row": 3, "scroll_position": 0.0, "selection": false, "syntax_highlighter": "GDScript" @@ -137,11 +115,11 @@ state={ state={ "bookmarks": PackedInt32Array(), "breakpoints": PackedInt32Array(), -"column": 22, +"column": 3, "folded_lines": Array[int]([]), "h_scroll_position": 0, -"row": 21, -"scroll_position": 10.0, +"row": 22, +"scroll_position": 0.0, "selection": false, "syntax_highlighter": "GDScript" } diff --git a/scripts/Chunks.gd b/scripts/Chunks.gd index 9942f6e..21ff660 100644 --- a/scripts/Chunks.gd +++ b/scripts/Chunks.gd @@ -12,12 +12,12 @@ func setFloor(): clear() for mz in range(0, Global.world.width - 1): for mx in range(0, Global.world.height - 1): - var bloc = Global.world.get_bloc(Vector2(mx, mz)) + var bloc = Global.world.get_bloc(Vector2i(mx, mz)) var my: float = bloc.position.y var meshID var mesh_rotation if bloc.type != 0: - var neighbours = Global.world.get_neighbours_4_at_same_height(Vector2(mx, mz)) + var neighbours = Global.world.get_neighbours_4_at_same_height(Vector2i(mx, mz)) if neighbours == Global.directions_4.RIGHT: meshID = Global.bloc_sides_id.SIDE_3 mesh_rotation = Global.GRID_ROTATION[3] @@ -77,15 +77,3 @@ func setEntities(): entity_instance.connect_to_world(entity_instance.id) entities.append(entity_instance) add_child(entity_instance) - -# TEST À ENLEVER -var inputs = {"ui_right": Vector2.RIGHT, - "ui_left": Vector2.LEFT, - "ui_up": Vector2.UP, - "ui_down": Vector2.DOWN} - -func _unhandled_input(event): - for dir in inputs.keys(): - if event.is_action_pressed(dir): - var new_position = Global.world.entities[23].position2D + inputs[dir] - Global.world.entities[23].move(new_position) diff --git a/scripts/Entity.gd b/scripts/Entity.gd index 90904a0..6e173c4 100644 --- a/scripts/Entity.gd +++ b/scripts/Entity.gd @@ -3,17 +3,17 @@ extends Node class_name Entity var position: Vector3 -var position2D: Vector2 +var position2D: Vector2i var movement: int = -1 var movement_step: float = 0.0 var id = -1 signal moving -func _init(id: int, position: Vector2): +func _init(id: int, position: Vector2i): self.id = id self.position.x = position.x - self.position.y = Global.world.get_height(Vector2(position.x, position.y)) + self.position.y = Global.world.get_height(Vector2i(position.x, position.y)) self.position.z = position.y position2D = position @@ -24,14 +24,67 @@ func get_data(): } return data - -func move(new_position: Vector2): + +func move(new_position: Vector2i): Global.world.blocs[position.x][position.y].entity = -1 self.position.x = new_position.x - self.position.y = Global.world.get_height(Vector2(new_position.x, new_position.y)) + self.position.y = Global.world.get_height(Vector2i(new_position.x, new_position.y)) self.position.z = new_position.y position2D = new_position Global.world.blocs[new_position.x][new_position.y].entity = id emit_signal("moving", self.position) +### Mettre un speed +func follow_path(path: Array): + for next in path: + var t = Timer.new() + t.wait_time = 0.3 + t.set_one_shot(true) + t.autostart = true + Engine.get_main_loop().get_root().add_child(t) + await t.timeout + move(next) + t.queue_free() + + +func heuristic(a: Vector2i, b: Vector2i) -> float: + return abs(a.x - b.x) + abs(a.y - b.y) + +func pathfinding(goal: Vector2i): + var frontier = [] + var priorities = {} + var came_from = {} + var cost_so_far = {} + came_from[position2D] = null + cost_so_far[position2D] = 0 + frontier.append(position2D) + + while frontier.size(): + var current = frontier.pop_front() + + if current == goal: + break + + for next in Global.world.neighbours(current): + var new_cost = cost_so_far[current] + Global.world.cost(current, next) + if next not in cost_so_far or new_cost < cost_so_far[next]: + cost_so_far[next] = new_cost + var priority = new_cost + heuristic(goal, next) + frontier.append(next) + priorities[next] = priority + came_from[next] = current + frontier.sort_custom( + func(a, b): + if priorities[a] < priorities[b]: + return true + return false + ) + + var current = goal + var path = [] + while current != position2D: + path.append(current) + current = came_from[current] + path.reverse() + return path diff --git a/scripts/Instance.gd b/scripts/Instance.gd deleted file mode 100644 index 06a4c08..0000000 --- a/scripts/Instance.gd +++ /dev/null @@ -1,31 +0,0 @@ -extends CharacterBody3D - - -const SPEED = 5.0 -const JUMP_VELOCITY = 4.5 - -# Get the gravity from the project settings to be synced with RigidBody nodes. -var gravity = ProjectSettings.get_setting("physics/3d/default_gravity") - - -func _physics_process(delta): - # Add the gravity. - if not is_on_floor(): - velocity.y -= gravity * delta - - # Handle Jump. - if Input.is_action_just_pressed("ui_accept") and is_on_floor(): - velocity.y = JUMP_VELOCITY - - # Get the input direction and handle the movement/deceleration. - # As good practice, you should replace UI actions with custom gameplay actions. - var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down") - var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() - if direction: - velocity.x = direction.x * SPEED - velocity.z = direction.z * SPEED - else: - velocity.x = move_toward(velocity.x, 0, SPEED) - velocity.z = move_toward(velocity.z, 0, SPEED) - - move_and_slide() diff --git a/scripts/Instance3D.gd b/scripts/Instance3D.gd index 978ed76..77e08f4 100644 --- a/scripts/Instance3D.gd +++ b/scripts/Instance3D.gd @@ -1,7 +1,7 @@ extends CharacterBody3D -const SPEED = 10.0 +const SPEED = 3.0 var id = -1 func connect_to_world(id): @@ -11,5 +11,5 @@ func connect_to_world(id): func _on_entity_moving(new_position): var tween = get_tree().create_tween() - tween.tween_property(self, "position", new_position, 1/SPEED).set_trans(Tween.TRANS_SINE) + tween.tween_property(self, "position", new_position, 1/SPEED) pass diff --git a/scripts/Map.gd b/scripts/Map.gd index 870038b..15409ff 100644 --- a/scripts/Map.gd +++ b/scripts/Map.gd @@ -6,7 +6,7 @@ func _ready(): for x in Global.world.width: for z in Global.world.height: var pixel_color = Color("#00F") - var bloc = Global.world.get_bloc(Vector2(x, z)) + var bloc = Global.world.get_bloc(Vector2i(x, z)) if bloc.type != 0: pixel_color = Color("#0F0") if bloc.entity != -1: diff --git a/scripts/Selection.gd b/scripts/Selection.gd index d477a5d..ebc13f8 100644 --- a/scripts/Selection.gd +++ b/scripts/Selection.gd @@ -19,8 +19,8 @@ func _process(delta): selection = reset_highlight() if Input.is_action_just_pressed("main_command"): if selection.type == 1: - Global.world.entities[selection.data.id].move(Vector2(highlight.data.position.x, highlight.data.position.z)) - + var path = Global.world.entities[selection.data.id].pathfinding(Vector2i(highlight.data.position.x, highlight.data.position.z)) + Global.world.entities[selection.data.id].follow_path(path) func _unhandled_input(event): if event is InputEventMouseMotion: highlight = get_highlight() diff --git a/scripts/World.gd b/scripts/World.gd index 28991dd..87c4e2e 100644 --- a/scripts/World.gd +++ b/scripts/World.gd @@ -12,7 +12,7 @@ var entities: Array var width: int var height: int -func get_neighbours_8_at_same_height(point: Vector2): +func get_neighbours_8_at_same_height(point: Vector2i): var neighbours = 0 var current_bloc = heightMap[point.x][point.y] if heightMap[point.x-1][point.y-1] == current_bloc: @@ -34,7 +34,7 @@ func get_neighbours_8_at_same_height(point: Vector2): return neighbours -func get_neighbours_4_at_same_height(point: Vector2): +func get_neighbours_4_at_same_height(point: Vector2i): var neighbours = 0 var current_bloc = heightMap[point.x][point.y] if heightMap[point.x][point.y-1] == current_bloc: @@ -48,7 +48,21 @@ func get_neighbours_4_at_same_height(point: Vector2): return neighbours -func get_bloc(point: Vector2): +func neighbours(point: Vector2i): + var neighbours = [] + + return [ + Vector2i(point.x, point.y - 1), + Vector2i(point.x + 1, point.y), + Vector2i(point.x, point.y + 1), + Vector2i(point.x - 1, point.y), + Vector2i(point.x + 1, point.y - 1), + Vector2i(point.x + 1, point.y + 1), + Vector2i(point.x - 1, point.y + 1), + Vector2i(point.x - 1, point.y - 1) + ] + +func get_bloc(point: Vector2i): var bloc = {} bloc["position"] = Vector3i(point.x, heightMap[point.x][point.y], point.y) for key in blocs[point.x][point.y].keys(): @@ -56,10 +70,22 @@ func get_bloc(point: Vector2): return bloc -func get_height(point: Vector2): +func get_height(point: Vector2i): return heightMap[point.x][point.y] -func add_entity(position: Vector2): +func add_entity(position: Vector2i): entities.append(Entity.new(entities.size(), position)) blocs[position.x][position.y]["entity"] = entities.size() - 1 pass + +func cost(point1: Vector2i, point2: Vector2i): + var cost = 1 + if ( + (point1.x == point2.x + 1 and point1.y == point2.y + 1) or + (point1.x == point2.x + 1 and point1.y == point2.y - 1) or + (point1.x == point2.x - 1 and point1.y == point2.y + 1) or + (point1.x == point2.x - 1 and point1.y == point2.y - 1) + ): + cost = 2 + return cost + diff --git a/scripts/WorldGeneration.gd b/scripts/WorldGeneration.gd index fdd9779..49ebef3 100644 --- a/scripts/WorldGeneration.gd +++ b/scripts/WorldGeneration.gd @@ -12,7 +12,7 @@ func _ready(): # TEST À ENLEVER for x in range(0, 8): for y in range(0, 8): - Global.world.add_entity(Vector2(x, y)) + Global.world.add_entity(Vector2i(x, y)) # FIN TEST À ENLEVER