perlin based 2D tilemap
This commit is contained in:
parent
cf7ad3446b
commit
ce1d74eada
5 changed files with 90 additions and 6 deletions
File diff suppressed because one or more lines are too long
15
noisebased/2D/map_ressource.gd
Normal file
15
noisebased/2D/map_ressource.gd
Normal file
|
@ -0,0 +1,15 @@
|
|||
extends Resource
|
||||
class_name MapRessource
|
||||
|
||||
@export_group("Tiles value upper thresold")
|
||||
@export var DeepWater := 0.3
|
||||
@export var Water := 0.4
|
||||
@export var Sand := 0.45
|
||||
@export var Grass := 0.6
|
||||
@export var Soil := 0.65
|
||||
@export var Mountain := 0.75
|
||||
@export var HighMountain := 0.9
|
||||
|
||||
@export_group("Noise Texture")
|
||||
|
||||
@export var NoiseTexture : NoiseTexture2D
|
1
noisebased/2D/map_ressource.gd.uid
Normal file
1
noisebased/2D/map_ressource.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://h0kr2evnkuj6
|
25
noisebased/2D/map_ressource.tres
Normal file
25
noisebased/2D/map_ressource.tres
Normal file
|
@ -0,0 +1,25 @@
|
|||
[gd_resource type="Resource" script_class="MapRessource" load_steps=4 format=3 uid="uid://cfsgo1yb4mwis"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://h0kr2evnkuj6" path="res://noisebased/2D/map_ressource.gd" id="1_2yrfc"]
|
||||
|
||||
[sub_resource type="FastNoiseLite" id="FastNoiseLite_2yrfc"]
|
||||
seed = 57
|
||||
frequency = 0.0021
|
||||
offset = Vector3(-190, 360.45, 0)
|
||||
fractal_lacunarity = 2.05
|
||||
|
||||
[sub_resource type="NoiseTexture2D" id="NoiseTexture2D_2yrfc"]
|
||||
width = 2048
|
||||
height = 1024
|
||||
noise = SubResource("FastNoiseLite_2yrfc")
|
||||
|
||||
[resource]
|
||||
script = ExtResource("1_2yrfc")
|
||||
DeepWater = 0.4
|
||||
Water = 0.5
|
||||
Sand = 0.55
|
||||
Grass = 0.65
|
||||
Soil = 0.7
|
||||
Mountain = 0.75
|
||||
HighMountain = 0.8
|
||||
NoiseTexture = SubResource("NoiseTexture2D_2yrfc")
|
|
@ -5,12 +5,52 @@ extends TileMapLayer
|
|||
# Definir les variables
|
||||
# ---------------------
|
||||
|
||||
# largeur, longeur de la map
|
||||
@export var WIDTH : int
|
||||
@export var HEIGHT : int
|
||||
@export var Map : MapRessource # Non-natif, voir map_ressource.gd et map_ressource.tres
|
||||
|
||||
# Les boutons
|
||||
@export_tool_button("All") var all_action = generate_map
|
||||
@export_tool_button("Generate map") var generate_action = generate_map
|
||||
|
||||
|
||||
func get_tile_mean(world_coords : Vector2i) -> float:
|
||||
var height_mean := 0.0
|
||||
for dx in range(tile_set.tile_size.x):
|
||||
for dy in range(tile_set.tile_size.y):
|
||||
height_mean += Map.NoiseTexture.noise.get_noise_2d(world_coords.x+dx, world_coords.y+dy)
|
||||
|
||||
#print(height_mean)
|
||||
height_mean = height_mean / (tile_set.tile_size.x * tile_set.tile_size.y)
|
||||
height_mean = (height_mean + 1) / 2
|
||||
#print(height_mean)
|
||||
return height_mean
|
||||
|
||||
func place_cell_from_height(coords : Vector2i, height : float) -> void:
|
||||
if height < Map.DeepWater:
|
||||
# Le 3 eme parametre de set_cell (atlas_coords) est un identificateur de la tile qu'on met
|
||||
# 0, 0 et la tile en haut a gauche (coresspond a la tile bleu fonce de SpriteSheet.png)
|
||||
set_cell(coords, 0, Vector2i(0, 0))
|
||||
elif height < Map.Water:
|
||||
set_cell(coords, 0, Vector2i(1, 0))
|
||||
elif height < Map.Sand:
|
||||
set_cell(coords, 0, Vector2i(2, 0))
|
||||
elif height < Map.Grass:
|
||||
set_cell(coords, 0, Vector2i(3, 0))
|
||||
elif height < Map.Soil:
|
||||
set_cell(coords, 0, Vector2i(4, 0))
|
||||
elif height < Map.Mountain:
|
||||
set_cell(coords, 0, Vector2i(5, 0))
|
||||
elif height < Map.HighMountain:
|
||||
set_cell(coords, 0, Vector2i(6, 0))
|
||||
else:
|
||||
set_cell(coords, 0, Vector2i(7, 0)) # Neige
|
||||
|
||||
|
||||
func generate_map():
|
||||
return
|
||||
clear()
|
||||
var nb_tile_x = Map.NoiseTexture.width / tile_set.tile_size.x
|
||||
var nb_tile_y = Map.NoiseTexture.height / tile_set.tile_size.y
|
||||
|
||||
for tx in range(0, nb_tile_x):
|
||||
for ty in range(0, nb_tile_y):
|
||||
var world_coords = Vector2i(tx, ty) * tile_set.tile_size # Multiplication component par component
|
||||
var tiles_coords = Vector2i(tx, ty)
|
||||
place_cell_from_height(tiles_coords, get_tile_mean(world_coords))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue