Basic Movement and Map Generation #22

Merged
Snoweuph merged 17 commits from dev-base into stable 2023-04-08 00:41:10 +00:00
2 changed files with 79 additions and 40 deletions
Showing only changes of commit 579faa06df - Show all commits

View file

@ -1,13 +1,14 @@
[gd_scene load_steps=3 format=3 uid="uid://chfp8mm3vwh2e"] [gd_scene load_steps=3 format=3 uid="uid://chfp8mm3vwh2e"]
[ext_resource type="TileSet" uid="uid://b7cqbf6xdbeal" path="res://Assets/Test_Tileset.tres" id="1_8lepy"] [ext_resource type="TileSet" uid="uid://bj7uu2180mie3" path="res://Assets/Tileset.tres" id="1_a2htf"]
[ext_resource type="Script" path="res://Scripts/MapGenerator.gd" id="2_qvwn8"] [ext_resource type="Script" path="res://Scripts/MapGenerator.gd" id="2_qvwn8"]
[node name="Node2D" type="Node2D"] [node name="Node2D" type="Node2D"]
[node name="TileMap" type="TileMap" parent="."] [node name="TileMap" type="TileMap" parent="."]
tile_set = ExtResource("1_8lepy") tile_set = ExtResource("1_a2htf")
cell_quadrant_size = 8 cell_quadrant_size = 8
collision_visibility_mode = 1 collision_visibility_mode = 1
format = 2 format = 2
script = ExtResource("2_qvwn8") script = ExtResource("2_qvwn8")
height = 33

View file

@ -1,12 +1,9 @@
extends TileMap extends TileMap
@export var width := 128 @export var width := 60
@export var height := 75 @export var height := 32
@export var fill_percentage := 0.65 @export var fill_percentage := 0.65
@export var solid_id := 0
@export var non_solid_id := 1
@export var solid_threshold := 7 @export var solid_threshold := 7
@export var nonsolid_threshold := 4 @export var nonsolid_threshold := 4
@ -16,6 +13,8 @@ extends TileMap
@export var cave_gen_iterations := 3 @export var cave_gen_iterations := 3
@export var cave_mine_size_threshold := 80 @export var cave_mine_size_threshold := 80
var tile_array : Array
func _ready(): func _ready():
var time = generate() var time = generate()
print("time for generation: " + str(time)) print("time for generation: " + str(time))
@ -27,38 +26,45 @@ func generate() -> float:
var start_time = Time.get_unix_time_from_system() var start_time = Time.get_unix_time_from_system()
randomize() randomize()
setup_tile_data_array()
fill_random_noise() fill_random_noise()
for i in cave_gen_iterations: for i in cave_gen_iterations:
change_cells_by_neighbor_thresholds() change_cells_by_neighbor_thresholds()
set_borders_solid() set_borders_solid()
prepare_player_start_area() prepare_player_start_area()
connect_caves(get_caves()) connect_caves(get_caves())
for i in cave_gen_iterations: for i in cave_gen_iterations:
change_cells_by_neighbor_thresholds() change_cells_by_neighbor_thresholds()
set_borders_solid() set_borders_solid()
tile_array_to_terrain()
return Time.get_unix_time_from_system() - start_time return Time.get_unix_time_from_system() - start_time
func setup_tile_data_array():
tile_array.clear()
tile_array = []
for x in width:
tile_array.append([])
for y in height:
tile_array[x].append([])
func fill_random_noise(): func fill_random_noise():
for x in width: for x in width:
for y in height: for y in height:
if randf() < fill_percentage: if randf() < fill_percentage:
self.set_cell(0, Vector2i(x,y),solid_id, Vector2i(0, 0)) set_tile_solid(x,y)
else: else:
self.set_cell(0, Vector2i(x,y),non_solid_id, Vector2i(0, 0)) set_tile_non_solid(x,y)
pass pass
func set_borders_solid(): func set_borders_solid():
for x in width: for x in width:
self.set_cell(0, Vector2i(x,0),solid_id, Vector2i(0, 0)) set_tile_solid(x,0)
self.set_cell(0, Vector2i(x,height-1),solid_id, Vector2i(0, 0)) set_tile_solid(x,height-1)
for y in height: for y in height:
self.set_cell(0, Vector2i(0,y),solid_id, Vector2i(0, 0)) set_tile_solid(0,y)
self.set_cell(0, Vector2i(width-1,y),solid_id, Vector2i(0, 0)) set_tile_solid(width-1,y)
pass pass
func prepare_player_start_area(): func prepare_player_start_area():
@ -76,32 +82,32 @@ func prepare_player_start_area():
for y in range(center.y - start_area_size, center.y + start_area_size): for y in range(center.y - start_area_size, center.y + start_area_size):
# Decide if the tile is part of the Corner or not # Decide if the tile is part of the Corner or not
if !(p2 > 0 or p2 <= - (start_area_size*2 - p*2)): if !(p2 > 0 or p2 <= - (start_area_size*2 - p*2)):
self.set_cell(0, Vector2i(x,y),non_solid_id, Vector2i(0, 0)) set_tile_non_solid(x,y)
p2 -= 1 p2 -= 1
pass pass
func change_cells_by_neighbor_thresholds(): func change_cells_by_neighbor_thresholds():
for x in range(1, width): for x in range(1, width-1):
for y in range(1, height): for y in range(1, height-1):
# Count Solid Neighbor Cells # Count Solid Neighbor Cells
var count := 0 var count := 0
#y-1 #y-1
if self.get_cell_source_id(0, Vector2i(x-1, y-1)) == solid_id: count +=1 if get_tile_solid(x-1, y-1): count +=1
if self.get_cell_source_id(0, Vector2i(x, y-1)) == solid_id: count +=1 if get_tile_solid(x, y-1): count +=1
if self.get_cell_source_id(0, Vector2i(x+1, y-1)) == solid_id: count +=1 if get_tile_solid(x+1, y-1): count +=1
#y #y
if self.get_cell_source_id(0, Vector2i(x-1, y)) == solid_id: count +=1 if get_tile_solid(x-1, y): count +=1
if self.get_cell_source_id(0, Vector2i(x+1, y)) == solid_id: count +=1 if get_tile_solid(x+1, y): count +=1
#y+1 #y+1
if self.get_cell_source_id(0, Vector2i(x-1, y+1)) == solid_id: count +=1 if get_tile_solid(x-1, y+1): count +=1
if self.get_cell_source_id(0, Vector2i(x, y+1)) == solid_id: count +=1 if get_tile_solid(x, y+1): count +=1
if self.get_cell_source_id(0, Vector2i(x+1, y+1)) == solid_id: count +=1 if get_tile_solid(x+1, y+1): count +=1
# Check Threshold # Check Threshold
if count < nonsolid_threshold: if count < nonsolid_threshold:
self.set_cell(0, Vector2i(x,y),non_solid_id, Vector2i(0, 0)) set_tile_non_solid(x,y)
if count >= solid_threshold: if count >= solid_threshold:
self.set_cell(0, Vector2i(x,y),solid_id, Vector2i(0, 0)) set_tile_solid(x,y)
pass pass
func get_caves() -> Array: func get_caves() -> Array:
@ -110,12 +116,12 @@ func get_caves() -> Array:
for x in range (2, width-2): for x in range (2, width-2):
for y in range (2, height-2): for y in range (2, height-2):
if self.get_cell_source_id(0, Vector2i(x, y)) == non_solid_id: if get_tile_non_solid(x, y):
flood_fill(x,y, caves) flood_fill(x,y, caves)
for cave in caves: for cave in caves:
for tile in cave: for tile in cave:
self.set_cell(0, Vector2i(tile.x,tile.y),non_solid_id, Vector2i(0, 0)) set_tile_non_solid(tile.x,tile.y)
return caves return caves
func flood_fill(tilex, tiley, caves) -> Array: func flood_fill(tilex, tiley, caves) -> Array:
@ -126,7 +132,7 @@ func flood_fill(tilex, tiley, caves) -> Array:
if !cave.has(tile): if !cave.has(tile):
cave.append(tile) cave.append(tile)
self.set_cell(0, Vector2i(tile.x,tile.y),solid_id, Vector2i(0, 0)) set_tile_solid(tile.x,tile.y)
#check adjacent cells #check adjacent cells
var north = Vector2i(tile.x, tile.y+1) var north = Vector2i(tile.x, tile.y+1)
@ -135,7 +141,7 @@ func flood_fill(tilex, tiley, caves) -> Array:
var west = Vector2i(tile.x-1, tile.y) var west = Vector2i(tile.x-1, tile.y)
for dir in [north,south,east,west]: for dir in [north,south,east,west]:
if self.get_cell_source_id(0, dir) == non_solid_id: if get_tile_non_solid(dir.x, dir.y):
if !to_fill.has(dir) and !cave.has(dir): if !to_fill.has(dir) and !cave.has(dir):
to_fill.append(dir) to_fill.append(dir)
if cave.size() >= cave_mine_size_threshold: if cave.size() >= cave_mine_size_threshold:
@ -223,9 +229,41 @@ func create_tunnel(point1, point2, cave):
drunk_x += dx drunk_x += dx
drunk_y += dy drunk_y += dy
if self.get_cell_source_id(0, Vector2i(drunk_x, drunk_y)) == solid_id: if get_tile_solid(drunk_x, drunk_y):
self.set_cell(0, Vector2i(drunk_x, drunk_y),non_solid_id, Vector2i(0, 0)) set_tile_non_solid(drunk_x, drunk_y)
#make tunnel wider #make tunnel wider
self.set_cell(0, Vector2i(drunk_x+1, drunk_y),non_solid_id, Vector2i(0, 0)) set_tile_non_solid(drunk_x+1, drunk_y)
self.set_cell(0, Vector2i(drunk_x+1, drunk_y+1),non_solid_id, Vector2i(0, 0)) set_tile_non_solid(drunk_x+1, drunk_y+1)
pass
func tile_array_to_terrain():
for x in width:
for y in height:
match tile_array[x][y]:
0: set_terrain_tile_non_solid(x,y)
1: set_terrain_tile_solid(x,y)
pass
# Getter
func get_tile_solid(x : int, y : int) -> bool:
return tile_array[x][y] == 1
func get_tile_non_solid(x : int, y : int) -> bool:
return tile_array[x][y] == 0
# Setter
func set_tile_solid(x : int, y : int):
tile_array[x][y] = 1
pass
func set_tile_non_solid(x : int, y : int):
tile_array[x][y] = 0
pass
func set_terrain_tile_solid(x : int, y : int):
self.set_cells_terrain_connect(0, [Vector2i(x,y)], 0, 0, false)
pass
func set_terrain_tile_non_solid(x : int, y : int):
self.set_cell(0, Vector2i(x,y), -1)
pass pass