Now the dungeon (mostly) properly gets written to chunks based on it's map's data. I implemented wall post processing with bit shifting, and since using bit shifting with scripting languages is not as common I threw in a few comments on how it works, hopefully someone will find it useful. Also fix 2 line's indentation level.

This commit is contained in:
Relintai 2020-07-20 15:03:57 +02:00
parent 3e3b480ac3
commit 8997fae8c2

View File

@ -43,7 +43,9 @@ var enemies : Array = []
var entrance_position : Transform = Transform() var entrance_position : Transform = Transform()
var inner_entrance_position : Vector3 = Vector3() var inner_entrance_position : Vector3 = Vector3()
enum Tile { None, Floor, Wall, Door } # in binary: WallXP = 00001, WallXN = 0010, WallZP = 0100, WallZN = 1000
enum NeighbourCaseCodeFlags { WallXP = 1, WallXN = 2, WallZP = 4, WallZN = 8 }
enum Tile { Wall, Floor, Door, Empty }
func _setup(): func _setup():
if sizex == 0 || sizey == 0 || sizez == 0: if sizex == 0 || sizey == 0 || sizez == 0:
@ -69,6 +71,8 @@ func _setup():
# #
# add_dungeon_start_room(dung) # add_dungeon_start_room(dung)
posy = 7
build() build()
# #
#func _setup_library(library): #func _setup_library(library):
@ -82,7 +86,7 @@ func _generate_chunk(chunk, spawn_mobs):
# for x in range(chunk.size_x): # for x in range(chunk.size_x):
# for z in range(chunk.size_z): # for z in range(chunk.size_z):
# chunk.add_mesh_data_resourcev(Vector3(x, 5, z), dung_ceiling) # chunk.add_mesh_data_resourcev(Vector3(x, 5, z), dung_ceiling)
var aabb : AABB = AABB(Vector3(posx, posy, posz) * chunk.get_voxel_scale(), Vector3(sizex, sizey, sizez) * chunk.get_voxel_scale()) var aabb : AABB = AABB(Vector3(posx, posy, posz) * chunk.get_voxel_scale(), Vector3(sizex, sizey, sizez) * chunk.get_voxel_scale())
var chunk_aabb : AABB = AABB(chunk.get_position() * Vector3(chunk.size_x, chunk.size_y, chunk.size_z) * chunk.get_voxel_scale(), Vector3(chunk.size_x, chunk.size_y, chunk.size_z) * chunk.get_voxel_scale()) var chunk_aabb : AABB = AABB(chunk.get_position() * Vector3(chunk.size_x, chunk.size_y, chunk.size_z) * chunk.get_voxel_scale(), Vector3(chunk.size_x, chunk.size_y, chunk.size_z) * chunk.get_voxel_scale())
@ -95,10 +99,79 @@ func _generate_chunk(chunk, spawn_mobs):
if !aabb.intersects(chunk_aabb): if !aabb.intersects(chunk_aabb):
return return
var px : int = chunk.position_x * chunk.size_x - posx
var pz : int = chunk.position_z * chunk.size_z - posz
var tox : int = px + chunk.size_x
var toz : int = pz + chunk.size_z
if tox > sizex:
tox = sizex
if toz > sizez:
toz = sizez
var floor_pos : int = chunk.position_y * chunk.size_y - posy
var ceiling_pos : int = floor_pos + sizey
var draw_floor : bool = true
var draw_ceiling : bool = true
if floor_pos < 0:
floor_pos = 0
draw_floor = false
if ceiling_pos > chunk.size_y:
ceiling_pos = chunk.size_y
draw_ceiling = false
var xx : int = 0
var zz : int = 0
for x in range(px, tox):
for z in range(pz, toz):
var tile : int = map[x][z]
#we can safely check like this as wall is 0
if tile > Tile.Wall:
#grab the wall data, by just right shifting it back. (binary) XXXXYYYY -> 0000XXXX
var walls : int = tile >> 4
if walls > 0:
#(binary) XXXX & 0001 -> 000X
if walls & NeighbourCaseCodeFlags.WallXP != 0:
add_wall(chunk, xx, zz, floor_pos, ceiling_pos, dung_wall_xp)
#(binary) XXXX & 0010 -> 00X0
if walls & NeighbourCaseCodeFlags.WallXN != 0:
#+ 1 offsets it to be at the proper place
add_wall(chunk, xx + 1, zz, floor_pos, ceiling_pos, dung_wall_xn)
#etc
if walls & NeighbourCaseCodeFlags.WallZP != 0:
add_wall(chunk, xx, zz - 1, floor_pos, ceiling_pos, dung_wall_zp)
if walls & NeighbourCaseCodeFlags.WallZN != 0:
#+ 1 offsets it to be at the proper place
add_wall(chunk, xx, zz, floor_pos, ceiling_pos, dung_wall_zn)
if draw_floor:
chunk.add_mesh_data_resourcev(Vector3(xx, floor_pos, zz), dung_floor)
if draw_ceiling:
chunk.add_mesh_data_resourcev(Vector3(xx, ceiling_pos, zz), dung_ceiling)
zz += 1
xx += 1
zz = 0
# for i in range(get_dungeon_start_room_count()): # for i in range(get_dungeon_start_room_count()):
# get_dungeon_start_room(i).generate_chunk(chunk, spawn_mobs) # get_dungeon_start_room(i).generate_chunk(chunk, spawn_mobs)
func add_wall(chunk : VoxelChunk, x : int, z : int, floor_pos : int, ceiling_pos : int, wall : MeshDataResource):
for y in range(floor_pos, ceiling_pos):
chunk.add_mesh_data_resourcev(Vector3(x, y, z), wall)
func spawn_teleporter_scene(scene : PackedScene, transform : Transform, chunk : VoxelChunk, teleports_to : Vector3): func spawn_teleporter_scene(scene : PackedScene, transform : Transform, chunk : VoxelChunk, teleports_to : Vector3):
var s = scene.instance() var s = scene.instance()
chunk.get_voxel_world().add_child(s) chunk.get_voxel_world().add_child(s)
@ -168,6 +241,38 @@ func build_level():
break break
connect_rooms() connect_rooms()
#post process walls, so they have the correct type
#0 : xn
#1 : xp
#2 : zn
#3 : zp
var neighbours : int = 0
for x in range(sizex):
for z in range(sizez):
if map[x][z] == Tile.Floor:
if x != 0:
if map[x - 1][z] <= Tile.Wall:
neighbours |= NeighbourCaseCodeFlags.WallXP
if x != sizex - 1:
if map[x + 1][z] <= Tile.Wall:
neighbours |= NeighbourCaseCodeFlags.WallXN
if z != 0:
if map[x][z - 1] <= Tile.Wall:
neighbours |= NeighbourCaseCodeFlags.WallZP
if z != sizez - 1:
if map[x][z + 1] <= Tile.Wall:
neighbours |= NeighbourCaseCodeFlags.WallZN
#left shift all bits by 4 -> (binary) 0000XXXX -> XXXX0000
neighbours = neighbours << 4
#bitwise or them together -> (Tile.Floor = 1 = 00000001) -> (binary) 000000001 | XXXX0000 -> XXXX0001
map[x][z] = Tile.Floor | neighbours
neighbours = 0
func connect_rooms(): func connect_rooms():
var stone_graph : AStar2D = AStar2D.new() var stone_graph : AStar2D = AStar2D.new()
@ -199,9 +304,8 @@ func connect_rooms():
point_id += 1 point_id += 1
#Add random connections until everything is connected #Add random connections until everything is connected
while !is_everything_connected(room_graph):
while !is_everything_connected(room_graph): add_random_connection(stone_graph, room_graph)
add_random_connection(stone_graph, room_graph)
func is_everything_connected(graph : AStar2D): func is_everything_connected(graph : AStar2D):
var points = graph.get_points() var points = graph.get_points()