View-based placement of blocks with Y rotation

master
Marc Gilleron 2020-07-28 22:05:37 +01:00
parent cf8b6b4b22
commit 39f48b106c
4 changed files with 69 additions and 16 deletions

View File

@ -2,6 +2,12 @@ extends Resource
const ROTATION_TYPE_NONE = 0
const ROTATION_TYPE_AXIAL = 1
const ROTATION_TYPE_Y = 2
const ROTATION_Y_NEGATIVE_X = 0
const ROTATION_Y_NEGATIVE_Z = 1
const ROTATION_Y_POSITIVE_X = 2
const ROTATION_Y_POSITIVE_Z = 3
const ROOT = "res://blocky_game/blocks"
@ -68,8 +74,8 @@ func _init():
_create_block({
"name": "stairs",
"gui_model": "stairs_nx.obj",
"rotation_type": ROTATION_TYPE_NONE,
"voxels": ["stairs_nx"],
"rotation_type": ROTATION_TYPE_Y,
"voxels": ["stairs_nx", "stairs_nz", "stairs_px", "stairs_pz"],
"transparent": false
})
_create_block({

View File

@ -1,4 +1,4 @@
[gd_resource type="VoxelLibrary" load_steps=18 format=2]
[gd_resource type="VoxelLibrary" load_steps=24 format=2]
[ext_resource path="res://blocky_game/blocks/grass/grass.obj" type="ArrayMesh" id=1]
[ext_resource path="res://blocky_game/blocks/dirt/dirt.obj" type="ArrayMesh" id=2]
@ -8,6 +8,9 @@
[ext_resource path="res://blocky_game/blocks/stairs/stairs_nx.obj" type="ArrayMesh" id=6]
[ext_resource path="res://blocky_game/blocks/planks/planks.obj" type="ArrayMesh" id=7]
[ext_resource path="res://blocky_game/blocks/tall_grass/tall_grass.obj" type="ArrayMesh" id=8]
[ext_resource path="res://blocky_game/blocks/stairs/stairs_px.obj" type="ArrayMesh" id=9]
[ext_resource path="res://blocky_game/blocks/stairs/stairs_pz.obj" type="ArrayMesh" id=10]
[ext_resource path="res://blocky_game/blocks/stairs/stairs_nz.obj" type="ArrayMesh" id=11]
[sub_resource type="Voxel" id=1]
voxel_name = "air"
@ -19,58 +22,79 @@ custom_mesh = ExtResource( 2 )
collision_aabbs = [ AABB( 0, 0, 0, 1, 1, 1 ) ]
[sub_resource type="Voxel" id=3]
voxel_name = "stairs_px"
geometry_type = 2
custom_mesh = ExtResource( 9 )
collision_aabbs = [ AABB( 0, 0, 0, 1, 1, 1 ) ]
[sub_resource type="Voxel" id=4]
voxel_name = "stairs_pz"
geometry_type = 2
custom_mesh = ExtResource( 10 )
collision_aabbs = [ AABB( 0, 0, 0, 1, 1, 1 ) ]
[sub_resource type="Voxel" id=5]
voxel_name = "grass"
random_tickable = true
geometry_type = 2
custom_mesh = ExtResource( 1 )
collision_aabbs = [ AABB( 0, 0, 0, 1, 1, 1 ) ]
[sub_resource type="Voxel" id=4]
[sub_resource type="Voxel" id=6]
voxel_name = "log_x"
random_tickable = true
geometry_type = 2
custom_mesh = ExtResource( 4 )
collision_aabbs = [ AABB( 0, 0, 0, 1, 1, 1 ) ]
[sub_resource type="Voxel" id=5]
[sub_resource type="Voxel" id=7]
voxel_name = "log_y"
random_tickable = true
geometry_type = 2
custom_mesh = ExtResource( 3 )
collision_aabbs = [ AABB( 0, 0, 0, 1, 1, 1 ) ]
[sub_resource type="Voxel" id=6]
[sub_resource type="Voxel" id=8]
voxel_name = "log_z"
random_tickable = true
geometry_type = 2
custom_mesh = ExtResource( 5 )
collision_aabbs = [ AABB( 0, 0, 0, 1, 1, 1 ) ]
[sub_resource type="Voxel" id=7]
[sub_resource type="Voxel" id=9]
voxel_name = "stairs_nx"
geometry_type = 2
custom_mesh = ExtResource( 6 )
collision_aabbs = [ AABB( 0, 0, 0, 1, 1, 1 ) ]
[sub_resource type="Voxel" id=8]
[sub_resource type="Voxel" id=10]
voxel_name = "planks"
geometry_type = 2
custom_mesh = ExtResource( 7 )
collision_aabbs = [ AABB( 0, 0, 0, 1, 1, 1 ) ]
[sub_resource type="Voxel" id=9]
[sub_resource type="Voxel" id=11]
voxel_name = "tall_grass"
material_id = 2
geometry_type = 2
custom_mesh = ExtResource( 8 )
[sub_resource type="Voxel" id=12]
voxel_name = "stairs_nz"
geometry_type = 2
custom_mesh = ExtResource( 11 )
collision_aabbs = [ AABB( 0, 0, 0, 1, 1, 1 ) ]
[resource]
voxels/0 = SubResource( 1 )
voxels/1 = SubResource( 2 )
voxels/2 = SubResource( 3 )
voxels/3 = SubResource( 4 )
voxels/4 = SubResource( 5 )
voxels/5 = SubResource( 6 )
voxels/6 = SubResource( 7 )
voxels/7 = SubResource( 8 )
voxels/8 = SubResource( 9 )
voxels/2 = SubResource( 5 )
voxels/3 = SubResource( 6 )
voxels/4 = SubResource( 7 )
voxels/5 = SubResource( 8 )
voxels/6 = SubResource( 9 )
voxels/7 = SubResource( 10 )
voxels/8 = SubResource( 11 )
voxels/9 = SubResource( 12 )
voxels/10 = SubResource( 3 )
voxels/11 = SubResource( 4 )

View File

@ -124,6 +124,22 @@ func _can_place_voxel_at(pos: Vector3):
return hits.size() == 0
static func _get_y_rotation(dir: Vector3) -> int:
var a = Util.get_direction_id4(Vector2(dir.x, dir.z))
match a:
0:
return Blocks.ROTATION_Y_NEGATIVE_X
1:
return Blocks.ROTATION_Y_NEGATIVE_Z
2:
return Blocks.ROTATION_Y_POSITIVE_X
3:
return Blocks.ROTATION_Y_POSITIVE_Z
_:
assert(false)
return -1
func _place_single_block(pos: Vector3, block_id: int):
var block := Blocks.get_block(block_id)
var voxel_id := 0
@ -137,6 +153,10 @@ func _place_single_block(pos: Vector3, block_id: int):
var axis := Util.get_longest_axis(look_dir)
voxel_id = block.voxels[axis]
Blocks.ROTATION_TYPE_Y:
var look_dir := -_head.get_transform().basis.z
var rot := _get_y_rotation(look_dir)
voxel_id = block.voxels[rot]
_:
# Unknown value
assert(false)

View File

@ -95,3 +95,6 @@ static func get_longest_axis(v: Vector3) -> int:
return Vector3.AXIS_Y
return Vector3.AXIS_Z
static func get_direction_id4(dir: Vector2) -> int:
return int(4.0 * (dir.rotated(PI / 4.0).angle() + PI) / TAU)