Ajout du pathfinding

pull/19/head
Valentin Stark 3 years ago
parent f24e50a6de
commit c84dc9c682
  1. 2
      .godot/editor/editor_layout.cfg
  2. 36
      .godot/editor/filesystem_cache8
  3. 4
      .godot/editor/filesystem_update4
  4. 5
      .godot/editor/project_metadata.cfg
  5. 56
      .godot/editor/script_editor_cache.cfg
  6. 16
      scripts/Chunks.gd
  7. 63
      scripts/Entity.gd
  8. 31
      scripts/Instance.gd
  9. 4
      scripts/Instance3D.gd
  10. 2
      scripts/Map.gd
  11. 4
      scripts/Selection.gd
  12. 36
      scripts/World.gd
  13. 2
      scripts/WorldGeneration.gd

@ -20,7 +20,7 @@ open_scenes=["res://scenes/WorldGeneration.tscn", "res://scenes/3DWorld.tscn", "
[ScriptEditor] [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"] open_help=["Callable", "GridMap", "Node3D", "Object", "PhysicsDirectBodyState3D", "PhysicsDirectSpaceState2D", "PhysicsDirectSpaceState3D", "RayCast3D", "RID", "Vector2"]
script_split_offset=70 script_split_offset=70
list_split_offset=0 list_split_offset=0

@ -1,12 +1,12 @@
2a60927148abd1d33b818b535e306557 2a60927148abd1d33b818b535e306557
::res://::1677783027 ::res://::1678475097
export_presets.cfg::TextFile::-1::1677194603::0::1::::<><>:: export_presets.cfg::TextFile::-1::1677194603::0::1::::<><>::
icon.png::CompressedTexture2D::4672001524465425285::1676578483::1676582905::1::::<><>:: icon.png::CompressedTexture2D::4672001524465425285::1676578483::1676582905::1::::<><>::
README.md::TextFile::-1::1676578483::0::1::::<><>:: README.md::TextFile::-1::1676578483::0::1::::<><>::
::res://assets/::1677704033 ::res://assets/::1678217906
environment.tres::Environment::8600195688495287911::1677704033::0::1::::<><>:: environment.tres::Environment::8600195688495287911::1678217906::0::1::::<><>::
water.gdshader::Shader::-1::1677174532::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.glb::PackedScene::7036506600244808940::1677174532::1677174532::1::::<><>::
0_sides_grass.png::CompressedTexture2D::4551055068461159203::1677174532::1677174532::1::::<><>:: 0_sides_grass.png::CompressedTexture2D::4551055068461159203::1677174532::1677174532::1::::<><>::
1_side.glb::PackedScene::5601417408597394875::1677174532::1677617210::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.glb::PackedScene::139420628202576697::1677174532::1677174532::1::::<><>::
4_sides_grass.png::CompressedTexture2D::7961782411803042998::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.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 ::res://assets/images/::1677174532
colors.png::CompressedTexture2D::2558953172502239658::1677174532::1677174532::1::::<><>:: colors.png::CompressedTexture2D::2558953172502239658::1677174532::1677174532::1::::<><>::
grass_top.png::CompressedTexture2D::3263707750932711970::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 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 ::res://assets/ui/::1677619095
bloc_select.png::CompressedTexture2D::1141775147858261351::1677618805::1677619095::1::::<><>:: bloc_select.png::CompressedTexture2D::1141775147858261351::1677618805::1677619095::1::::<><>::
::res://scenes/::1677704516 ::res://scenes/::1678233640
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 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 Game.tscn::PackedScene::9075056520838281688::1677174532::0::1::::<><>::uid://brjt1u0dgbmi4<>uid://d1sd7pxywgunu
HUD.tscn::PackedScene::9021570906125339886::1677704516::0::1::::<><>::res://scripts/Map.gd HUD.tscn::PackedScene::9021570906125339886::1678233640::0::1::::<><>::res://scripts/Map.gd
Instance.tscn::PackedScene::4220804716574195494::1677704516::0::1::::<><>::res://scripts/Instance3D.gd Instance.tscn::PackedScene::4220804716574195494::1678233640::0::1::::<><>::res://scripts/Instance3D.gd
WorldGeneration.tscn::PackedScene::1108513713840477990::1677704515::0::1::::<><>::res://scripts/WorldGeneration.gd WorldGeneration.tscn::PackedScene::1108513713840477990::1678233639::0::1::::<><>::res://scripts/WorldGeneration.gd
::res://scripts/::1677704514 ::res://scripts/::1678233622
Camera3D.gd::GDScript::-1::1677174532::0::1::::<>Camera3D<>:: Camera3D.gd::GDScript::-1::1677174532::0::1::::<>Camera3D<>::
Chunks.gd::GDScript::-1::1677704514::0::1::::<>GridMap<>:: Chunks.gd::GDScript::-1::1678219623::0::1::::<>GridMap<>::
Entity.gd::GDScript::-1::1677703359::0::1::::Entity<>Node<>:: Entity.gd::GDScript::-1::1678233622::0::1::::Entity<>Node<>::
Global.gd::GDScript::-1::1677174532::0::1::::<>Node<>:: Global.gd::GDScript::-1::1677174532::0::1::::<>Node<>::
Instance3D.gd::GDScript::-1::1677613303::0::1::::<>CharacterBody3D<>:: Instance3D.gd::GDScript::-1::1677873181::0::1::::<>CharacterBody3D<>::
Instance.gd::GDScript::-1::1677531382::0::1::::<>CharacterBody3D<>:: Map.gd::GDScript::-1::1678219623::0::1::::<>TextureRect<>::
Map.gd::GDScript::-1::1677190875::0::1::::<>TextureRect<>:: Selection.gd::GDScript::-1::1678222510::0::1::::<>Node3D<>::
World.gd::GDScript::-1::1677530828::0::1::::World<>Node<>:: World.gd::GDScript::-1::1678232290::0::1::::World<>Node<>::
WorldGeneration.gd::GDScript::-1::1677536001::0::1::::<>Control<>:: WorldGeneration.gd::GDScript::-1::1678232290::0::1::::<>Control<>::

@ -4,7 +4,5 @@ res://scenes/3DWorld.tscn
res://scenes/Instance.tscn res://scenes/Instance.tscn
res://assets/blocs/blocs.tscn res://assets/blocs/blocs.tscn
res://scenes/HUD.tscn res://scenes/HUD.tscn
res://scripts/Selection.gd
res://scripts/Chunks.gd
res://scripts/World.gd
res://scripts/Entity.gd res://scripts/Entity.gd
res://scripts/World.gd

@ -1,6 +1,6 @@
[editor_metadata] [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 tile_source_sort=0
use_advanced_connections=false use_advanced_connections=false
@ -12,7 +12,7 @@ run_reload_scripts=true
[recent_files] [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"] 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] [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) project_settings=Rect2(575, 408, 1531, 671)
search_help=Rect2(1192, 168, 1368, 912) search_help=Rect2(1192, 168, 1368, 912)
export=Rect2(468, 69, 1800, 1011) export=Rect2(468, 69, 1800, 1011)
editor_settings=Rect2(830, 227, 900, 700)
[project_settings] [project_settings]

@ -3,16 +3,12 @@
state={ state={
"bookmarks": PackedInt32Array(), "bookmarks": PackedInt32Array(),
"breakpoints": PackedInt32Array(), "breakpoints": PackedInt32Array(),
"column": 1, "column": 21,
"folded_lines": Array[int]([]), "folded_lines": Array[int]([]),
"h_scroll_position": 0, "h_scroll_position": 0,
"row": 11, "row": 15,
"scroll_position": 0.0, "scroll_position": 32.0,
"selection": true, "selection": false,
"selection_from_column": 1,
"selection_from_line": 11,
"selection_to_column": 17,
"selection_to_line": 11,
"syntax_highlighter": "GDScript" "syntax_highlighter": "GDScript"
} }
@ -24,8 +20,8 @@ state={
"column": 0, "column": 0,
"folded_lines": Array[int]([]), "folded_lines": Array[int]([]),
"h_scroll_position": 0, "h_scroll_position": 0,
"row": 10, "row": 90,
"scroll_position": 39.0, "scroll_position": 65.0,
"selection": false, "selection": false,
"syntax_highlighter": "GDScript" "syntax_highlighter": "GDScript"
} }
@ -35,11 +31,11 @@ state={
state={ state={
"bookmarks": PackedInt32Array(), "bookmarks": PackedInt32Array(),
"breakpoints": PackedInt32Array(), "breakpoints": PackedInt32Array(),
"column": 47, "column": 28,
"folded_lines": Array[int]([]), "folded_lines": Array[int]([]),
"h_scroll_position": 0, "h_scroll_position": 0,
"row": 90, "row": 78,
"scroll_position": 67.0, "scroll_position": 54.0,
"selection": false, "selection": false,
"syntax_highlighter": "GDScript" "syntax_highlighter": "GDScript"
} }
@ -49,11 +45,11 @@ state={
state={ state={
"bookmarks": PackedInt32Array(), "bookmarks": PackedInt32Array(),
"breakpoints": PackedInt32Array(), "breakpoints": PackedInt32Array(),
"column": 26, "column": 2,
"folded_lines": Array[int]([]), "folded_lines": Array[int]([]),
"h_scroll_position": 0, "h_scroll_position": 0,
"row": 32, "row": 82,
"scroll_position": 12.0, "scroll_position": 63.0,
"selection": false, "selection": false,
"syntax_highlighter": "GDScript" "syntax_highlighter": "GDScript"
} }
@ -96,24 +92,6 @@ state={
"h_scroll_position": 0, "h_scroll_position": 0,
"row": 2, "row": 2,
"scroll_position": 0.0, "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, "selection": false,
"syntax_highlighter": "GDScript" "syntax_highlighter": "GDScript"
} }
@ -123,10 +101,10 @@ state={
state={ state={
"bookmarks": PackedInt32Array(), "bookmarks": PackedInt32Array(),
"breakpoints": PackedInt32Array(), "breakpoints": PackedInt32Array(),
"column": 0, "column": 17,
"folded_lines": Array[int]([]), "folded_lines": Array[int]([]),
"h_scroll_position": 0, "h_scroll_position": 0,
"row": 15, "row": 3,
"scroll_position": 0.0, "scroll_position": 0.0,
"selection": false, "selection": false,
"syntax_highlighter": "GDScript" "syntax_highlighter": "GDScript"
@ -137,11 +115,11 @@ state={
state={ state={
"bookmarks": PackedInt32Array(), "bookmarks": PackedInt32Array(),
"breakpoints": PackedInt32Array(), "breakpoints": PackedInt32Array(),
"column": 22, "column": 3,
"folded_lines": Array[int]([]), "folded_lines": Array[int]([]),
"h_scroll_position": 0, "h_scroll_position": 0,
"row": 21, "row": 22,
"scroll_position": 10.0, "scroll_position": 0.0,
"selection": false, "selection": false,
"syntax_highlighter": "GDScript" "syntax_highlighter": "GDScript"
} }

@ -12,12 +12,12 @@ func setFloor():
clear() clear()
for mz in range(0, Global.world.width - 1): for mz in range(0, Global.world.width - 1):
for mx in range(0, Global.world.height - 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 my: float = bloc.position.y
var meshID var meshID
var mesh_rotation var mesh_rotation
if bloc.type != 0: 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: if neighbours == Global.directions_4.RIGHT:
meshID = Global.bloc_sides_id.SIDE_3 meshID = Global.bloc_sides_id.SIDE_3
mesh_rotation = Global.GRID_ROTATION[3] mesh_rotation = Global.GRID_ROTATION[3]
@ -77,15 +77,3 @@ func setEntities():
entity_instance.connect_to_world(entity_instance.id) entity_instance.connect_to_world(entity_instance.id)
entities.append(entity_instance) entities.append(entity_instance)
add_child(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)

@ -3,17 +3,17 @@ extends Node
class_name Entity class_name Entity
var position: Vector3 var position: Vector3
var position2D: Vector2 var position2D: Vector2i
var movement: int = -1 var movement: int = -1
var movement_step: float = 0.0 var movement_step: float = 0.0
var id = -1 var id = -1
signal moving signal moving
func _init(id: int, position: Vector2): func _init(id: int, position: Vector2i):
self.id = id self.id = id
self.position.x = position.x 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 self.position.z = position.y
position2D = position position2D = position
@ -25,13 +25,66 @@ func get_data():
return data return data
func move(new_position: Vector2): func move(new_position: Vector2i):
Global.world.blocs[position.x][position.y].entity = -1 Global.world.blocs[position.x][position.y].entity = -1
self.position.x = new_position.x 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 self.position.z = new_position.y
position2D = new_position position2D = new_position
Global.world.blocs[new_position.x][new_position.y].entity = id Global.world.blocs[new_position.x][new_position.y].entity = id
emit_signal("moving", self.position) 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

@ -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()

@ -1,7 +1,7 @@
extends CharacterBody3D extends CharacterBody3D
const SPEED = 10.0 const SPEED = 3.0
var id = -1 var id = -1
func connect_to_world(id): func connect_to_world(id):
@ -11,5 +11,5 @@ func connect_to_world(id):
func _on_entity_moving(new_position): func _on_entity_moving(new_position):
var tween = get_tree().create_tween() 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 pass

@ -6,7 +6,7 @@ func _ready():
for x in Global.world.width: for x in Global.world.width:
for z in Global.world.height: for z in Global.world.height:
var pixel_color = Color("#00F") 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: if bloc.type != 0:
pixel_color = Color("#0F0") pixel_color = Color("#0F0")
if bloc.entity != -1: if bloc.entity != -1:

@ -19,8 +19,8 @@ func _process(delta):
selection = reset_highlight() selection = reset_highlight()
if Input.is_action_just_pressed("main_command"): if Input.is_action_just_pressed("main_command"):
if selection.type == 1: 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): func _unhandled_input(event):
if event is InputEventMouseMotion: if event is InputEventMouseMotion:
highlight = get_highlight() highlight = get_highlight()

@ -12,7 +12,7 @@ var entities: Array
var width: int var width: int
var height: 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 neighbours = 0
var current_bloc = heightMap[point.x][point.y] var current_bloc = heightMap[point.x][point.y]
if heightMap[point.x-1][point.y-1] == current_bloc: 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 return neighbours
func get_neighbours_4_at_same_height(point: Vector2): func get_neighbours_4_at_same_height(point: Vector2i):
var neighbours = 0 var neighbours = 0
var current_bloc = heightMap[point.x][point.y] var current_bloc = heightMap[point.x][point.y]
if heightMap[point.x][point.y-1] == current_bloc: if heightMap[point.x][point.y-1] == current_bloc:
@ -48,7 +48,21 @@ func get_neighbours_4_at_same_height(point: Vector2):
return neighbours 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 = {} var bloc = {}
bloc["position"] = Vector3i(point.x, heightMap[point.x][point.y], point.y) bloc["position"] = Vector3i(point.x, heightMap[point.x][point.y], point.y)
for key in blocs[point.x][point.y].keys(): for key in blocs[point.x][point.y].keys():
@ -56,10 +70,22 @@ func get_bloc(point: Vector2):
return bloc return bloc
func get_height(point: Vector2): func get_height(point: Vector2i):
return heightMap[point.x][point.y] return heightMap[point.x][point.y]
func add_entity(position: Vector2): func add_entity(position: Vector2i):
entities.append(Entity.new(entities.size(), position)) entities.append(Entity.new(entities.size(), position))
blocs[position.x][position.y]["entity"] = entities.size() - 1 blocs[position.x][position.y]["entity"] = entities.size() - 1
pass 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

@ -12,7 +12,7 @@ func _ready():
# TEST À ENLEVER # TEST À ENLEVER
for x in range(0, 8): for x in range(0, 8):
for y 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 # FIN TEST À ENLEVER

Loading…
Cancel
Save