Can now remove and place cubes + light tweaks

master
Marc Gilleron 2017-03-29 22:41:26 +02:00
parent c1cfb966e1
commit a36afe652b
9 changed files with 123 additions and 19 deletions

View File

@ -3,33 +3,70 @@ extends Node
export(NodePath) var terrain_path = null
export(Material) var cursor_material = null
const COLLISION_LAYER_AVATAR = 2
var _terrain = null
var _cursor = null
onready var _head = get_parent().get_node("Camera")
func _ready():
_terrain = get_node(terrain_path)
if terrain_path == null:
_terrain = get_parent().get_node(get_parent().terrain)
terrain_path = _terrain.get_path() # For correctness
else:
_terrain = get_node(terrain_path)
_cursor = _make_cursor()
_terrain.add_child(_cursor)
func get_pointed_voxel():
var origin = _head.get_global_transform().origin
var forward = -_head.get_transform().basis.z.normalized()
var hit = _terrain.raycast(origin, forward, 10)
return hit
func _fixed_process(delta):
if _terrain == null:
return
var origin = _head.get_global_transform().origin
var forward = -_head.get_transform().basis.z.normalized()
var hit = _terrain.raycast(origin, forward, 10)
var label = get_parent().get_node("debug_label")
var hit = get_pointed_voxel()
if hit != null:
label.text = str(hit.position)
_cursor.show()
_cursor.set_translation(hit.position + Vector3(1,1,1)*0.5)
get_parent().get_node("debug_label").text = str(hit.position)
else:
label.text = "---"
_cursor.hide()
get_parent().get_node("debug_label").text = "---"
# These inputs have to be in _fixed_process because they rely on collision queries
if hit != null:
if Input.is_action_just_pressed("action1"):
var pos = hit.position
_terrain.get_storage().set_voxel_v(0, pos)
_terrain.make_voxel_dirty(pos)
elif Input.is_action_just_pressed("action2"):
var pos = hit.prev_position
if can_place_voxel_at(pos):
_terrain.get_storage().set_voxel_v(1, pos)
_terrain.make_voxel_dirty(pos)
else:
print("Can't place here!")
func can_place_voxel_at(pos):
var space_state = get_viewport().get_world().get_direct_space_state()
var params = PhysicsShapeQueryParameters.new()
params.set_layer_mask(COLLISION_LAYER_AVATAR)
params.set_transform(Transform(Basis(), pos + Vector3(1,1,1)*0.5))
var shape = BoxShape.new()
var ex = 0.5
shape.extents = Vector3(ex, ex, ex)
params.set_shape(shape)
var hits = space_state.intersect_shape(params)
return hits.size() == 0
func _make_cursor():

8
project/center.gd Normal file
View File

@ -0,0 +1,8 @@
extends Sprite
# TODO Get rid if this once viewport modes get fixed!
func _process(delta):
var rect = get_viewport().get_visible_rect()
position = rect.size/2.0

View File

@ -1,8 +1,10 @@
[gd_scene load_steps=6 format=2]
[gd_scene load_steps=8 format=2]
[ext_resource path="res://character_controller.gd" type="Script" id=1]
[ext_resource path="res://mouse_look.gd" type="Script" id=2]
[ext_resource path="res://avatar_interaction.gd" type="Script" id=3]
[ext_resource path="res://crosshair.png" type="Texture" id=4]
[ext_resource path="res://center.gd" type="Script" id=5]
[sub_resource type="CapsuleShape" id=1]
@ -52,7 +54,7 @@ shape_count = 1
shapes/0/shape = SubResource( 1 )
shapes/0/transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 )
shapes/0/trigger = false
collision_layer = 1
collision_layer = 3
collision_mask = 1
collide_with/static = true
collide_with/kinematic = true
@ -64,6 +66,7 @@ speed = 5.0
gravity = 9.8
jump_force = 8.0
head = NodePath("Camera")
terrain = null
[node name="CollisionShape" type="CollisionShape" parent="."]
@ -110,4 +113,10 @@ percent_visible = 1.0
lines_skipped = 0
max_lines_visible = -1
[node name="crosshair" type="Sprite" parent="."]
position = Vector2( 510.693, 285.793 )
texture = ExtResource( 4 )
script = ExtResource( 5 )

View File

@ -5,6 +5,10 @@ export var gravity = 9.8
export var jump_force = 5.0
export(NodePath) var head = null
# Not used in this script, but might be useful for child nodes because
# this controller will most likely be on the root
export(NodePath) var terrain = null
var _velocity = Vector3()
var _grounded = false
var _head = null

BIN
project/crosshair.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 240 B

View File

@ -0,0 +1,20 @@
[remap]
importer="texture"
type="StreamTexture"
path="res://.import/crosshair.png-b7f9cb2e0fc32a087972fec001d3421e.stex"
[params]
compress/mode=0
compress/lossy_quality=0.7
flags/repeat=0
flags/filter=false
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=false
process/premult_alpha=false
stream=false
size_limit=0
detect_3d=true

View File

@ -5,6 +5,16 @@ name="Voxel Game"
main_scene="res://main.tscn"
icon="res://icon.png"
[input]
action1=[ InputEvent(MBUTTON,1) ]
action2=[ InputEvent(MBUTTON,2) ]
[layer_names]
3d_physics/layer_1="Default"
3d_physics/layer_2="Avatar"
[memory]
multithread/thread_rid_pool_prealloc=60
@ -13,3 +23,5 @@ multithread/thread_rid_pool_prealloc=60
viewport/default_clear_color=Color( 0.407806, 0.628906, 0.618542, 1 )
quality/use_nearest_mipmap_filter=true
shadows/directional_shadow_size=4096
quality/hdr=false

View File

@ -236,11 +236,11 @@ light_energy = 1.0
light_negative = false
light_specular = 0.5
light_cull_mask = -1
shadow_enabled = false
shadow_enabled = true
shadow_color = Color( 0, 0, 0, 1 )
shadow_bias = 0.1
shadow_bias = 0.2
shadow_contact = 0.0
shadow_max_distance = 0.0
shadow_max_distance = 100.0
editor_only = false
directional_shadow_mode = 2
directional_shadow_split_1 = 0.1
@ -276,14 +276,28 @@ directional_shadow_bias_split_scale = 0.1
[node name="CharacterAvatar" parent="." instance=ExtResource( 4 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 22.0569, 0 )
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 16.4052, 0 )
speed = 4.0
gravity = 30.0
jump_force = 10.0
jump_force = 9.0
terrain = NodePath("../VoxelTerrain")
[node name="interaction" parent="CharacterAvatar"]
[node name="TestCube2" type="TestCube" parent="."]
terrain_path = NodePath("../../VoxelTerrain")
_import_transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 )
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.230466, -1.27516, -5.9037 )
layers = 1
material_override = null
cast_shadow = 1
extra_cull_margin = 0.0
use_as_billboard = false
use_as_y_billboard = false
use_depth_scale = false
visible_in_all_rooms = false
use_in_baked_light = false
lod_min_distance = 0.0
lod_min_hysteresis = 0.0
lod_max_distance = 0.0
lod_max_hysteresis = 0.0
[editable path="CharacterAvatar"]

View File

@ -47,7 +47,7 @@ func _ready():
#set_provider(CustomProvider.new())
var provider = VoxelProviderTest.new()
provider.set_mode(VoxelProviderTest.MODE_WAVES)
provider.set_pattern_size(Vector3(10,5,10))
provider.set_pattern_size(Vector3(10,8,10))
set_provider(provider)
make_blocks_dirty(Vector3(-16,-4,-16), Vector3(33,8,33))