Moved wirecube cursor creation to utility script

This commit is contained in:
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,177 +1,129 @@
extends Node extends Node
const COLLISION_LAYER_AVATAR = 2 const Util = preload("res://common/util.gd")
export(NodePath) var terrain_path = null const COLLISION_LAYER_AVATAR = 2
export(Material) var cursor_material = null
export(NodePath) var terrain_path = null
onready var _light = get_node("../../DirectionalLight") # For debug shadow toggle export(Material) var cursor_material = null
onready var _head = get_parent().get_node("Camera")
onready var _light = get_node("../../DirectionalLight") # For debug shadow toggle
var _terrain = null onready var _head = get_parent().get_node("Camera")
var _terrain_tool = null
var _cursor = null var _terrain = null
var _action_place = false var _terrain_tool = null
var _action_remove = false var _cursor = null
var _action_place = false
var _inventory = [1, 2] var _action_remove = false
var _inventory_index = 0
var _inventory = [1, 2]
var _inventory_index = 0
func _ready():
if terrain_path == null:
_terrain = get_parent().get_node(get_parent().terrain) func _ready():
terrain_path = _terrain.get_path() # For correctness if terrain_path == null:
else: _terrain = get_parent().get_node(get_parent().terrain)
_terrain = get_node(terrain_path) terrain_path = _terrain.get_path() # For correctness
_cursor = _make_cursor() else:
_terrain.add_child(_cursor) _terrain = get_node(terrain_path)
_terrain_tool = _terrain.get_voxel_tool()
var mesh = Util.create_wirecube_mesh(Color(0,0,0))
var mesh_instance = MeshInstance.new()
func get_pointed_voxel(): mesh_instance.mesh = mesh
var origin = _head.get_global_transform().origin if cursor_material != null:
var forward = -_head.get_transform().basis.z.normalized() mesh_instance.material_override = cursor_material
var hit = _terrain_tool.raycast(origin, forward, 10) mesh_instance.set_scale(Vector3(1,1,1)*1.01)
return hit _cursor = mesh_instance
_terrain.add_child(_cursor)
func _physics_process(delta): _terrain_tool = _terrain.get_voxel_tool()
if _terrain == null:
return
func get_pointed_voxel():
var hit = get_pointed_voxel() var origin = _head.get_global_transform().origin
if hit != null: var forward = -_head.get_transform().basis.z.normalized()
_cursor.show() var hit = _terrain_tool.raycast(origin, forward, 10)
_cursor.set_translation(hit.position + Vector3(1,1,1)*0.5) return hit
get_parent().get_node("debug_label").text = str(hit.position)
else:
_cursor.hide() func _physics_process(delta):
get_parent().get_node("debug_label").text = "---" if _terrain == null:
return
# These inputs have to be in _fixed_process because they rely on collision queries
if hit != null: var hit = get_pointed_voxel()
var has_cube = _terrain_tool.get_voxel(hit.position) != 0 if hit != null:
_cursor.show()
if _action_place and has_cube: _cursor.set_translation(hit.position)
var pos = hit.position get_parent().get_node("debug_label").text = str(hit.position)
do_sphere(pos, 5, 0) else:
_cursor.hide()
elif _action_remove: get_parent().get_node("debug_label").text = "---"
var pos = hit.previous_position
if has_cube == false: # These inputs have to be in _fixed_process because they rely on collision queries
pos = hit.position if hit != null:
if can_place_voxel_at(pos): var has_cube = _terrain_tool.get_voxel(hit.position) != 0
do_sphere(pos, 4, _inventory[_inventory_index])
print("Place voxel at ", pos) if _action_place and has_cube:
else: var pos = hit.position
print("Can't place here!") do_sphere(pos, 5, 0)
_action_place = false elif _action_remove:
_action_remove = false var pos = hit.previous_position
if has_cube == false:
pos = hit.position
func _input(event): if can_place_voxel_at(pos):
if event is InputEventMouseButton: do_sphere(pos, 4, _inventory[_inventory_index])
if event.pressed: print("Place voxel at ", pos)
match event.button_index: else:
BUTTON_LEFT: print("Can't place here!")
_action_place = true
BUTTON_RIGHT: _action_place = false
_action_remove = true _action_remove = false
elif event is InputEventKey:
if event.pressed: func _input(event):
match event.scancode: if event is InputEventMouseButton:
KEY_1: if event.pressed:
select_inventory(0) match event.button_index:
KEY_2: BUTTON_LEFT:
select_inventory(1) _action_place = true
KEY_L: BUTTON_RIGHT:
_light.shadow_enabled = not _light.shadow_enabled _action_remove = true
elif event is InputEventKey:
func select_inventory(i): if event.pressed:
if i < 0 or i >= len(_inventory): match event.scancode:
return KEY_1:
_inventory_index = i select_inventory(0)
var vi = _inventory[i] KEY_2:
print("Inventory select ", _terrain.voxel_library.get_voxel(vi).voxel_name, " (", vi, ")") select_inventory(1)
KEY_L:
_light.shadow_enabled = not _light.shadow_enabled
func can_place_voxel_at(pos):
var space_state = get_viewport().get_world().get_direct_space_state()
var params = PhysicsShapeQueryParameters.new() func select_inventory(i):
params.collision_mask = COLLISION_LAYER_AVATAR if i < 0 or i >= len(_inventory):
params.transform = Transform(Basis(), pos + Vector3(1,1,1)*0.5) return
var shape = BoxShape.new() _inventory_index = i
var ex = 0.5 var vi = _inventory[i]
shape.extents = Vector3(ex, ex, ex) print("Inventory select ", _terrain.voxel_library.get_voxel(vi).voxel_name, " (", vi, ")")
params.set_shape(shape)
var hits = space_state.intersect_shape(params)
return hits.size() == 0 func can_place_voxel_at(pos):
var space_state = get_viewport().get_world().get_direct_space_state()
var params = PhysicsShapeQueryParameters.new()
# Makes a 3D wireframe cube cursor params.collision_mask = COLLISION_LAYER_AVATAR
func _make_cursor(): params.transform = Transform(Basis(), pos + Vector3(1,1,1)*0.5)
var st = SurfaceTool.new() var shape = BoxShape.new()
st.begin(Mesh.PRIMITIVE_LINES) var ex = 0.5
_add_wireframe_cube(st, -Vector3(1,1,1)*0.5, 1, Color(0,0,0)) shape.extents = Vector3(ex, ex, ex)
var mesh = st.commit() params.set_shape(shape)
var mesh_instance = MeshInstance.new() var hits = space_state.intersect_shape(params)
mesh_instance.mesh = mesh return hits.size() == 0
if cursor_material != null:
mesh_instance.material_override = cursor_material
mesh_instance.set_scale(Vector3(1,1,1)*1.01) func do_sphere(center, r, type):
return mesh_instance _terrain_tool.channel = VoxelBuffer.CHANNEL_TYPE
_terrain_tool.value = type
_terrain_tool.do_point(center)
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

@ -1,80 +1,61 @@
extends Spatial extends Spatial
export var speed = 5.0 export var speed = 5.0
export var gravity = 9.8 export var gravity = 9.8
export var jump_force = 5.0 export var jump_force = 5.0
export(NodePath) var head = null export(NodePath) var head = null
# Not used in this script, but might be useful for child nodes because # Not used in this script, but might be useful for child nodes because
# this controller will most likely be on the root # this controller will most likely be on the root
export(NodePath) var terrain = null export(NodePath) var terrain = null
var _velocity = Vector3() var _velocity = Vector3()
var _grounded = false var _grounded = false
var _head = null var _head = null
var _box_mover = VoxelBoxMover.new() var _box_mover = VoxelBoxMover.new()
func _ready(): func _ready():
_head = get_node(head) _head = get_node(head)
# FIX
#set_shape_transform(0, Transform().rotated(Vector3(1,0,0), PI/2.0)) func _physics_process(delta):
var forward = _head.get_transform().basis.z.normalized()
func _physics_process(delta): forward = Plane(Vector3(0, 1, 0), 0).project(forward)
var right = _head.get_transform().basis.x.normalized()
var forward = _head.get_transform().basis.z.normalized() var motor = Vector3()
forward = Plane(Vector3(0, 1, 0), 0).project(forward)
var right = _head.get_transform().basis.x.normalized() if Input.is_key_pressed(KEY_UP) or Input.is_key_pressed(KEY_Z) or Input.is_key_pressed(KEY_W):
var motor = Vector3() motor -= forward
if Input.is_key_pressed(KEY_DOWN) or Input.is_key_pressed(KEY_S):
if Input.is_key_pressed(KEY_UP) or Input.is_key_pressed(KEY_Z) or Input.is_key_pressed(KEY_W): motor += forward
motor -= forward if Input.is_key_pressed(KEY_LEFT) or Input.is_key_pressed(KEY_Q) or Input.is_key_pressed(KEY_A):
if Input.is_key_pressed(KEY_DOWN) or Input.is_key_pressed(KEY_S): motor -= right
motor += forward if Input.is_key_pressed(KEY_RIGHT) or Input.is_key_pressed(KEY_D):
if Input.is_key_pressed(KEY_LEFT) or Input.is_key_pressed(KEY_Q) or Input.is_key_pressed(KEY_A): motor += right
motor -= right
if Input.is_key_pressed(KEY_RIGHT) or Input.is_key_pressed(KEY_D): motor = motor.normalized() * speed
motor += right
_velocity.x = motor.x
motor = motor.normalized() * speed _velocity.z = motor.z
_velocity.y -= gravity * delta
_velocity.x = motor.x
_velocity.z = motor.z #if _grounded and Input.is_key_pressed(KEY_SPACE):
_velocity.y -= gravity * delta if Input.is_key_pressed(KEY_SPACE):
_velocity.y = jump_force
#if _grounded and Input.is_key_pressed(KEY_SPACE): #_grounded = false
if Input.is_key_pressed(KEY_SPACE):
_velocity.y = jump_force var motion = _velocity * delta
#_grounded = false
if has_node(terrain):
var motion = _velocity * delta var aabb = AABB(Vector3(-0.4, -0.9, -0.4), Vector3(0.8, 1.8, 0.8))
var terrain_node = get_node(terrain)
if has_node(terrain): motion = _box_mover.get_motion(get_translation(), motion, aabb, terrain_node)
var aabb = AABB(Vector3(-0.4, -0.9, -0.4), Vector3(0.8, 1.8, 0.8)) global_translate(motion)
var terrain_node = get_node(terrain)
motion = _box_mover.get_motion(get_translation(), motion, aabb, terrain_node) assert(delta > 0)
global_translate(motion) _velocity = motion / 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/profiling_gui.gd" type="Script" id=8]
[ext_resource path="res://blocky_terrain/debug3d.gd" type="Script" id=9] [ext_resource path="res://blocky_terrain/debug3d.gd" type="Script" id=9]
[sub_resource type="ProceduralSky" id=1] [sub_resource type="ProceduralSky" id=1]
sky_top_color = Color( 0.268204, 0.522478, 0.847656, 1 ) sky_top_color = Color( 0.268204, 0.522478, 0.847656, 1 )
sky_horizon_color = Color( 0.556863, 0.823529, 0.909804, 1 ) sky_horizon_color = Color( 0.556863, 0.823529, 0.909804, 1 )
@ -86,7 +84,6 @@ stream = ExtResource( 1 )
voxel_library = SubResource( 6 ) voxel_library = SubResource( 6 )
view_distance = 256 view_distance = 256
viewer_path = NodePath("../CharacterAvatar") viewer_path = NodePath("../CharacterAvatar")
generate_collisions = true
material/0 = ExtResource( 2 ) material/0 = ExtResource( 2 )
material/1 = ExtResource( 3 ) 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