Moved wirecube cursor creation to utility script

master
Marc Gilleron 2019-09-12 20:18:05 +01:00
parent 63c18a8883
commit 377b30ae69
4 changed files with 231 additions and 260 deletions

View File

@ -1,5 +1,7 @@
extends Node
const Util = preload("res://common/util.gd")
const COLLISION_LAYER_AVATAR = 2
export(NodePath) var terrain_path = null
@ -24,7 +26,15 @@ func _ready():
terrain_path = _terrain.get_path() # For correctness
else:
_terrain = get_node(terrain_path)
_cursor = _make_cursor()
var mesh = Util.create_wirecube_mesh(Color(0,0,0))
var mesh_instance = MeshInstance.new()
mesh_instance.mesh = mesh
if cursor_material != null:
mesh_instance.material_override = cursor_material
mesh_instance.set_scale(Vector3(1,1,1)*1.01)
_cursor = mesh_instance
_terrain.add_child(_cursor)
_terrain_tool = _terrain.get_voxel_tool()
@ -43,7 +53,7 @@ func _physics_process(delta):
var hit = get_pointed_voxel()
if hit != null:
_cursor.show()
_cursor.set_translation(hit.position + Vector3(1,1,1)*0.5)
_cursor.set_translation(hit.position)
get_parent().get_node("debug_label").text = str(hit.position)
else:
_cursor.hide()
@ -112,66 +122,8 @@ func can_place_voxel_at(pos):
return hits.size() == 0
# Makes a 3D wireframe cube cursor
func _make_cursor():
var st = SurfaceTool.new()
st.begin(Mesh.PRIMITIVE_LINES)
_add_wireframe_cube(st, -Vector3(1,1,1)*0.5, 1, Color(0,0,0))
var mesh = st.commit()
var mesh_instance = MeshInstance.new()
mesh_instance.mesh = mesh
if cursor_material != null:
mesh_instance.material_override = cursor_material
mesh_instance.set_scale(Vector3(1,1,1)*1.01)
return mesh_instance
func do_sphere(center, r, type):
_terrain_tool.channel = VoxelBuffer.CHANNEL_TYPE
_terrain_tool.value = type
#_terrain_tool.do_sphere(center, r)
_terrain_tool.do_point(center)
static func _add_wireframe_cube(st, pos, step, color):
st.add_color(color)
st.add_vertex(pos)
st.add_vertex(pos + Vector3(step, 0, 0))
st.add_vertex(pos + Vector3(step, 0, 0))
st.add_vertex(pos + Vector3(step, 0, step))
st.add_vertex(pos + Vector3(step, 0, step))
st.add_vertex(pos + Vector3(0, 0, step))
st.add_vertex(pos + Vector3(0, 0, step))
st.add_vertex(pos)
st.add_vertex(pos + Vector3(0, step, 0))
st.add_vertex(pos + Vector3(step, step, 0))
st.add_vertex(pos + Vector3(step, step, 0))
st.add_vertex(pos + Vector3(step, step, step))
st.add_vertex(pos + Vector3(step, step, step))
st.add_vertex(pos + Vector3(0, step, step))
st.add_vertex(pos + Vector3(0, step, step))
st.add_vertex(pos + Vector3(0, step, 0))
st.add_vertex(pos)
st.add_vertex(pos + Vector3(0, step, 0))
st.add_vertex(pos + Vector3(step, 0, 0))
st.add_vertex(pos + Vector3(step, step, 0))
st.add_vertex(pos + Vector3(step, 0, step))
st.add_vertex(pos + Vector3(step, step, step))
st.add_vertex(pos + Vector3(0, 0, step))
st.add_vertex(pos + Vector3(0, step, step))

View File

@ -18,9 +18,6 @@ var _box_mover = VoxelBoxMover.new()
func _ready():
_head = get_node(head)
# FIX
#set_shape_transform(0, Transform().rotated(Vector3(1,0,0), PI/2.0))
func _physics_process(delta):
@ -60,21 +57,5 @@ func _physics_process(delta):
assert(delta > 0)
_velocity = motion / delta
#var rem = move(motion)
# TODO Fix it, obsolete code
# if is_colliding():
# var n = get_collision_normal()
# var k = 1.0#clamp(n.y, 0, 1)
# rem = rem.slide(n)*k
# _velocity = _velocity.slide(n)*k
# #rem = n.slide(rem)*k
# #_velocity = n.slide(_velocity)*k
# _grounded = true
# move(rem)
# else:
# _grounded = false
#get_node("debug").set_text("Grounded=" + str(_grounded))

View File

@ -9,8 +9,6 @@
[ext_resource path="res://blocky_terrain/profiling_gui.gd" type="Script" id=8]
[ext_resource path="res://blocky_terrain/debug3d.gd" type="Script" id=9]
[sub_resource type="ProceduralSky" id=1]
sky_top_color = Color( 0.268204, 0.522478, 0.847656, 1 )
sky_horizon_color = Color( 0.556863, 0.823529, 0.909804, 1 )
@ -86,7 +84,6 @@ stream = ExtResource( 1 )
voxel_library = SubResource( 6 )
view_distance = 256
viewer_path = NodePath("../CharacterAvatar")
generate_collisions = true
material/0 = ExtResource( 2 )
material/1 = ExtResource( 3 )

41
project/common/util.gd Normal file
View File

@ -0,0 +1,41 @@
static func create_wirecube_mesh(color = Color(1,1,1)):
var positions = PoolVector3Array([
Vector3(0, 0, 0),
Vector3(1, 0, 0),
Vector3(1, 0, 1),
Vector3(0, 0, 1),
Vector3(0, 1, 0),
Vector3(1, 1, 0),
Vector3(1, 1, 1),
Vector3(0, 1, 1),
])
var colors = PoolColorArray([
color, color, color, color,
color, color, color, color,
])
var indices = PoolIntArray([
0, 1,
1, 2,
2, 3,
3, 0,
4, 5,
5, 6,
6, 7,
7, 4,
0, 4,
1, 5,
2, 6,
3, 7
])
var arrays = []
arrays.resize(Mesh.ARRAY_MAX)
arrays[Mesh.ARRAY_VERTEX] = positions
arrays[Mesh.ARRAY_COLOR] = colors
arrays[Mesh.ARRAY_INDEX] = indices
var mesh = ArrayMesh.new()
mesh.add_surface_from_arrays(Mesh.PRIMITIVE_LINES, arrays)
return mesh