Initial commit

This commit is contained in:
Crizomb 2025-04-01 03:38:03 +02:00
commit cf7ad3446b
26 changed files with 514 additions and 0 deletions

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,102 @@
@tool
extends TileMapLayer
# ---------------------
# Definir les variables
# ---------------------
# largeur, longeur du labyrinte
@export var WIDTH : int
@export var HEIGHT : int
# Les boutons
@export_tool_button("Init") var init_action = init_maze
@export_tool_button("Step") var step_action = generate_maze_step
@export_tool_button("All") var all_action = generate_map_all
@export_tool_button("All with wait") var all_wait_action = generate_map_all_wait
const TILE_FLOOR = Vector2i(0, 0)
const TILE_WALL = Vector2i(1, 0)
var current_pos = Vector2i(0, 0)
var undo_stack : Array[Vector2i] = [current_pos] # Pile des positions explores, on depopera la pile pour le backtracking
var explored : Dictionary[Vector2i, bool] # Stock les positions explores
# Pas besoin d'init a false pour explored, contrairement python la valeur d'une cle pas dans le dico, est null. Qui est interprete comme false en gdscript
# ---------------------
# Fonctions utilitaires
# ---------------------
func set_tile(pos : Vector2i, val : Vector2i) -> void:
assert(pos.x >= 0 && pos.x < WIDTH, "x has bad value, x is {}, but should be beetwen 0 and {}".format([pos.x, WIDTH], "{}"))
assert(pos.y >= 0 && pos.y < HEIGHT, "y has bad value, y is {}, but should be beetwen 0 and {}".format([pos.y, HEIGHT], "{}"))
set_cell(pos, 0, val)
# Renvoie une direction possible depuis une position dans le labyrinte
func get_random_possible_dir(current_pos : Vector2i):
var all_dir = [2*Vector2i.UP, 2*Vector2i.DOWN, 2*Vector2i.RIGHT, 2*Vector2i.LEFT]
var possible_dir = []
for dir in all_dir:
var next_pos = current_pos + dir
var can_be_explored = not explored.get(next_pos) \
&& next_pos.x >= 0 && next_pos.x < WIDTH && next_pos.y >= 0 && next_pos.y < HEIGHT
if can_be_explored:
possible_dir.append(dir)
if possible_dir.is_empty():
return null
return possible_dir.pick_random()
# Va dans la direction dir, pour agrandir le labyrinthe
func path_move(dir : Vector2i) -> void:
var next_pos = current_pos + dir
var passway = current_pos + dir / 2
set_tile(passway, TILE_FLOOR)
set_tile(next_pos, TILE_FLOOR)
explored[next_pos] = true
undo_stack.append(next_pos)
current_pos = next_pos
# Initialize le labyrinthe
func init_maze() -> void:
print("init map")
current_pos = Vector2i(0, 0)
undo_stack = [current_pos]
explored = {}
clear() # fonction de TileMapLayer, clear les tiles
current_pos = Vector2i(0, 0)
explored[current_pos] = true
for x in range(WIDTH):
for y in range(HEIGHT):
set_cell(Vector2i(x, y), 0, TILE_WALL) # fonction de TileMapLayer
func _ready() -> void:
print("ready")
# ----------------------------------
# Fonctions appeles par les boutons
# ----------------------------------
func generate_maze_step() -> void:
var next_dir = get_random_possible_dir(current_pos)
# Si pas de voisins (next_dir est vide), alors on va en arriere
while not next_dir:
print("going back")
current_pos = undo_stack.pop_back()
next_dir = get_random_possible_dir(current_pos)
print("path move")
path_move(next_dir)
func generate_map_all() -> void:
while not undo_stack.is_empty():
generate_maze_step()
func generate_map_all_wait() -> void:
while not undo_stack.is_empty():
generate_maze_step()
await get_tree().create_timer(0.01).timeout

View file

@ -0,0 +1 @@
uid://2fw281l5o16y

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 220 B

View file

@ -0,0 +1,34 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://hwfveek70gga"
path="res://.godot/imported/tileset.png-d096f978b03dd56545ecbab3756af876.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://mazegeneration/2D/tileset.png"
dest_files=["res://.godot/imported/tileset.png-d096f978b03dd56545ecbab3756af876.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

View file

@ -0,0 +1,93 @@
@tool
extends GridMap
# Moralement c'est un labyrinthe 2D, mais les graphiques sont en 3D
# Le code sera comme tile_map_layer_maze mais avec des gridmaps au lieu des tilemaps
# ---------------------
# Definir les variables
# ---------------------
# largeur, longeur du labyrinte
@export var WIDTH : int
@export var HEIGHT : int
# Les boutons
@export_tool_button("Init") var init_action = init_maze
@export_tool_button("Step") var step_action = generate_maze_step
@export_tool_button("All") var all_action = generate_map_all
@export_tool_button("All with wait") var all_wait_action = generate_map_all_wait
const TILE_FLOOR = 1
const TILE_WALL = 0
var current_pos = Vector3i(0, 0, 0)
var undo_stack : Array[Vector3i]
var explored : Dictionary[Vector3i, bool]
func set_grid(pos : Vector3i, val : int):
assert(pos.x >= 0 && pos.x < WIDTH, "x has bad value, x is {}, but should be beetwen 0 and {}".format([pos.x, WIDTH], "{}"))
assert(pos.z >= 0 && pos.z < HEIGHT, "z has bad value, z is {}, but should be beetwen 0 and {}".format([pos.z, HEIGHT], "{}"))
set_cell_item(pos, val) #Fonction de gridmap
# Renvoie une direction possible depuis une position dans le labyrinte
func get_random_possible_dir(current_pos : Vector3i):
var all_dir = [2*Vector3i.FORWARD, 2*Vector3i.LEFT, 2*Vector3i.RIGHT, 2*Vector3i.BACK]
var possible_dir = []
for dir in all_dir:
var next_pos = current_pos + dir
var can_be_explored = not explored.get(next_pos) \
&& next_pos.x >= 0 && next_pos.x < WIDTH && next_pos.z >= 0 && next_pos.z < HEIGHT
if can_be_explored:
possible_dir.append(dir)
if possible_dir.is_empty():
return null
return possible_dir.pick_random()
# Va dans la direction dir, pour agrandir le labyrinthe
func path_move(dir : Vector3i) -> void:
var next_pos = current_pos + dir
var passway = current_pos + dir / 2
set_grid(passway, TILE_FLOOR)
set_grid(next_pos, TILE_FLOOR)
explored[next_pos] = true
undo_stack.append(next_pos)
current_pos = next_pos
# Initialize le labyrinthe
func init_maze() -> void:
print("init map")
current_pos = Vector3i(0, 0, 0)
undo_stack = [current_pos]
explored = {}
clear() # fonction de GridMap, clear les tiles
current_pos = Vector3i(0, 0, 0)
explored[current_pos] = true
for x in range(WIDTH):
for z in range(HEIGHT):
set_cell_item(Vector3i(x, 0, z), TILE_WALL) # fonction de GridMap
func generate_maze_step() -> void:
var next_dir = get_random_possible_dir(current_pos)
# Si pas de voisins (next_dir est vide), alors on va en arriere
while not next_dir:
print("going back")
current_pos = undo_stack.pop_back()
next_dir = get_random_possible_dir(current_pos)
print("path move")
path_move(next_dir)
func generate_map_all() -> void:
while not undo_stack.is_empty():
generate_maze_step()
func generate_map_all_wait() -> void:
while not undo_stack.is_empty():
generate_maze_step()
await get_tree().create_timer(0.01).timeout

View file

@ -0,0 +1 @@
uid://b0bhljevk3h8r

View file

@ -0,0 +1,11 @@
[gd_scene load_steps=3 format=3 uid="uid://brb3771fda52c"]
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_vu8eu"]
albedo_color = Color(3.08037e-06, 0.959929, 0.976304, 1)
[sub_resource type="BoxMesh" id="BoxMesh_vu8eu"]
material = SubResource("StandardMaterial3D_vu8eu")
size = Vector3(2, 4, 2)
[node name="Wall" type="MeshInstance3D"]
mesh = SubResource("BoxMesh_vu8eu")

View file

@ -0,0 +1,11 @@
[gd_scene load_steps=3 format=3 uid="uid://c20by0tgxdjpc"]
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_t6cuw"]
albedo_color = Color(0.188162, 0.571815, 1.84822e-05, 1)
[sub_resource type="BoxMesh" id="BoxMesh_t6cuw"]
material = SubResource("StandardMaterial3D_t6cuw")
size = Vector3(2, 2, 2)
[node name="Wall" type="MeshInstance3D"]
mesh = SubResource("BoxMesh_t6cuw")

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,10 @@
[gd_scene load_steps=3 format=3 uid="uid://qdw4en85l0eq"]
[ext_resource type="PackedScene" uid="uid://brb3771fda52c" path="res://3D/grids_tiles/wall.tscn" id="1_2npxr"]
[ext_resource type="PackedScene" uid="uid://c20by0tgxdjpc" path="res://3D/grids_tiles/way.tscn" id="2_t4liu"]
[node name="MeshLibraryScene" type="Node3D"]
[node name="Wall" parent="." instance=ExtResource("1_2npxr")]
[node name="Way" parent="." instance=ExtResource("2_t4liu")]