mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2026-01-06 14:10:55 +03:00
Format files using updated file_format.sh
This commit is contained in:
@@ -53,41 +53,41 @@ func _ready():
|
||||
if not has_node("Target"):
|
||||
target = Spatial.new()
|
||||
add_child(target)
|
||||
|
||||
|
||||
if Engine.editor_hint:
|
||||
if get_tree() != null:
|
||||
if get_tree().edited_scene_root != null:
|
||||
target.set_owner(get_tree().edited_scene_root)
|
||||
|
||||
|
||||
target.name = "Target"
|
||||
else:
|
||||
target = $Target
|
||||
|
||||
|
||||
# If we are in the editor, we want to make a sphere at this node
|
||||
if Engine.editor_hint:
|
||||
_make_editor_sphere_at_node(target, Color.magenta)
|
||||
|
||||
|
||||
if middle_joint_target == null:
|
||||
if not has_node("MiddleJoint"):
|
||||
middle_joint_target = Spatial.new()
|
||||
add_child(middle_joint_target)
|
||||
|
||||
|
||||
if Engine.editor_hint:
|
||||
if get_tree() != null:
|
||||
if get_tree().edited_scene_root != null:
|
||||
middle_joint_target.set_owner(get_tree().edited_scene_root)
|
||||
|
||||
|
||||
middle_joint_target.name = "MiddleJoint"
|
||||
else:
|
||||
middle_joint_target = get_node("MiddleJoint")
|
||||
|
||||
|
||||
# If we are in the editor, we want to make a sphere at this node
|
||||
if Engine.editor_hint:
|
||||
_make_editor_sphere_at_node(middle_joint_target, Color(1, 0.24, 1, 1))
|
||||
|
||||
|
||||
# Make all of the bone nodes for each bone in the IK chain
|
||||
_make_bone_nodes()
|
||||
|
||||
|
||||
# Make sure we're using the right update mode
|
||||
_set_update_mode(update_mode)
|
||||
|
||||
@@ -119,12 +119,12 @@ func update_skeleton():
|
||||
if first_call:
|
||||
_set_skeleton_path(skeleton_path)
|
||||
first_call = false
|
||||
|
||||
|
||||
if skeleton == null:
|
||||
_set_skeleton_path(skeleton_path)
|
||||
|
||||
|
||||
return
|
||||
|
||||
|
||||
if bones_in_chain == null:
|
||||
if debug_messages:
|
||||
printerr(name, " - IK_FABRIK: No Bones in IK chain defined!")
|
||||
@@ -133,34 +133,34 @@ func update_skeleton():
|
||||
if debug_messages:
|
||||
printerr(name, " - IK_FABRIK: No Bone lengths in IK chain defined!")
|
||||
return
|
||||
|
||||
|
||||
if bones_in_chain.size() != bones_in_chain_lengths.size():
|
||||
if debug_messages:
|
||||
printerr(name, " - IK_FABRIK: bones_in_chain and bones_in_chain_lengths!")
|
||||
return
|
||||
|
||||
|
||||
################################
|
||||
|
||||
|
||||
# Set all of the bone IDs in bone_IDs, if they are not already made
|
||||
var i = 0
|
||||
if bone_IDs.size() <= 0:
|
||||
for bone_name in bones_in_chain:
|
||||
bone_IDs[bone_name] = skeleton.find_bone(bone_name)
|
||||
|
||||
|
||||
# Set the bone node to the currect bone position
|
||||
bone_nodes[i].global_transform = get_bone_transform(i)
|
||||
# If this is not the last bone in the bone chain, make it look at the next bone in the bone chain
|
||||
if i < bone_IDs.size()-1:
|
||||
bone_nodes[i].look_at(get_bone_transform(i+1).origin + skeleton.global_transform.origin, Vector3.UP)
|
||||
|
||||
|
||||
i += 1
|
||||
|
||||
|
||||
# Set the total length of the bone chain, if it is not already set
|
||||
if total_length == INF:
|
||||
total_length = 0
|
||||
for bone_length in bones_in_chain_lengths:
|
||||
total_length += bone_length
|
||||
|
||||
|
||||
# Solve the bone chain
|
||||
solve_chain()
|
||||
|
||||
@@ -172,10 +172,10 @@ func solve_chain():
|
||||
return
|
||||
else:
|
||||
chain_iterations = 0
|
||||
|
||||
|
||||
# Update the origin with the current bone's origin
|
||||
chain_origin = get_bone_transform(0).origin
|
||||
|
||||
|
||||
# Get the direction of the final bone by using the next to last bone if there is more than 2 bones.
|
||||
# If there are only 2 bones, we use the target's forward Z vector instead (not ideal, but it works fairly well)
|
||||
var dir
|
||||
@@ -183,35 +183,35 @@ func solve_chain():
|
||||
dir = bone_nodes[bone_nodes.size()-2].global_transform.basis.z.normalized()
|
||||
else:
|
||||
dir = -target.global_transform.basis.z.normalized()
|
||||
|
||||
|
||||
# Get the target position (accounting for the final bone and it's length)
|
||||
var target_pos = target.global_transform.origin + (dir * bones_in_chain_lengths[bone_nodes.size()-1])
|
||||
|
||||
|
||||
# If we are using middle joint target (and have more than 2 bones), move our middle joint towards it!
|
||||
if use_middle_joint_target:
|
||||
if bone_nodes.size() > 2:
|
||||
var middle_point_pos = middle_joint_target.global_transform.origin
|
||||
var middle_point_pos_diff = (middle_point_pos - bone_nodes[bone_nodes.size()/2].global_transform.origin)
|
||||
bone_nodes[bone_nodes.size()/2].global_transform.origin += middle_point_pos_diff.normalized()
|
||||
|
||||
|
||||
# Get the difference between our end effector (the final bone in the chain) and the target
|
||||
var dif = (bone_nodes[bone_nodes.size()-1].global_transform.origin - target_pos).length()
|
||||
|
||||
|
||||
# Check to see if the distance from the end effector to the target is within our error margin (CHAIN_TOLERANCE).
|
||||
# If it not, move the chain towards the target (going forwards, backwards, and then applying rotation)
|
||||
while dif > CHAIN_TOLERANCE:
|
||||
chain_backward()
|
||||
chain_forward()
|
||||
chain_apply_rotation()
|
||||
|
||||
|
||||
# Update the difference between our end effector (the final bone in the chain) and the target
|
||||
dif = (bone_nodes[bone_nodes.size()-1].global_transform.origin - target_pos).length()
|
||||
|
||||
|
||||
# Add one to chain_iterations. If we have reached our max iterations, then break
|
||||
chain_iterations = chain_iterations + 1
|
||||
if chain_iterations >= CHAIN_MAX_ITER:
|
||||
break
|
||||
|
||||
|
||||
# Reset the bone node transforms to the skeleton bone transforms
|
||||
for i in range(0, bone_nodes.size()):
|
||||
var reset_bone_trans = get_bone_transform(i)
|
||||
@@ -227,17 +227,17 @@ func chain_backward():
|
||||
dir = bone_nodes[bone_nodes.size() - 2].global_transform.basis.z.normalized()
|
||||
else:
|
||||
dir = -target.global_transform.basis.z.normalized()
|
||||
|
||||
|
||||
# Set the position of the end effector (the final bone in the chain) to the target position
|
||||
bone_nodes[bone_nodes.size()-1].global_transform.origin = target.global_transform.origin + (dir * bones_in_chain_lengths[bone_nodes.size()-1])
|
||||
|
||||
|
||||
# For all of the other bones, move them towards the target
|
||||
var i = bones_in_chain.size() - 1
|
||||
while i >= 1:
|
||||
var prev_origin = bone_nodes[i].global_transform.origin
|
||||
i -= 1
|
||||
var curr_origin = bone_nodes[i].global_transform.origin
|
||||
|
||||
|
||||
var r = prev_origin - curr_origin
|
||||
var l = bones_in_chain_lengths[i] / r.length()
|
||||
# Apply the new joint position
|
||||
@@ -248,12 +248,12 @@ func chain_backward():
|
||||
func chain_forward():
|
||||
# Set root at initial position
|
||||
bone_nodes[0].global_transform.origin = chain_origin
|
||||
|
||||
|
||||
# Go through every bone in the bone chain
|
||||
for i in range(bones_in_chain.size() - 1):
|
||||
var curr_origin = bone_nodes[i].global_transform.origin
|
||||
var next_origin = bone_nodes[i + 1].global_transform.origin
|
||||
|
||||
|
||||
var r = next_origin - curr_origin
|
||||
var l = bones_in_chain_lengths[i] / r.length()
|
||||
# Apply the new joint position, (potentially with constraints), to the bone node
|
||||
@@ -274,27 +274,27 @@ func chain_apply_rotation():
|
||||
# Get the bone node for this bone, and the previous bone
|
||||
var b_target = bone_nodes[i].global_transform
|
||||
var b_target_two = bone_nodes[i-1].global_transform
|
||||
|
||||
|
||||
# Convert the bone nodes positions from world space to bone/skeleton space
|
||||
b_target.origin = skeleton.global_transform.xform_inv(b_target.origin)
|
||||
b_target_two.origin = skeleton.global_transform.xform_inv(b_target_two.origin)
|
||||
|
||||
|
||||
# Get the direction that the previous bone is pointing towards
|
||||
var dir = (target.global_transform.origin - b_target_two.origin).normalized()
|
||||
|
||||
|
||||
# Make this bone look in the same the direction as the last bone
|
||||
bone_trans = bone_trans.looking_at(b_target.origin + dir, Vector3.UP)
|
||||
|
||||
|
||||
# Set the position of the bone to the bone target.
|
||||
# Prior to Godot 3.2, this was not necessary, but because we can now completely
|
||||
# override bone transforms, we need to set the position as well as rotation.
|
||||
bone_trans.origin = b_target.origin
|
||||
|
||||
|
||||
else:
|
||||
var b_target = target.global_transform
|
||||
b_target.origin = skeleton.global_transform.xform_inv(b_target.origin)
|
||||
bone_trans = bone_trans.looking_at(b_target.origin, Vector3.UP)
|
||||
|
||||
|
||||
# A bit of a hack. Because we only have two bones, we have to use the previous
|
||||
# bone to position the last bone in the chain.
|
||||
var last_bone = bone_nodes[i-1].global_transform
|
||||
@@ -303,29 +303,29 @@ func chain_apply_rotation():
|
||||
# bone on the Z axis.
|
||||
# This will place the position of the bone at the end of the last bone
|
||||
bone_trans.origin = last_bone.origin - last_bone.basis.z.normalized() * bones_in_chain_lengths[i-1]
|
||||
|
||||
|
||||
# If this is NOT the last bone in the bone chain, rotate the bone to look at the next
|
||||
# bone in the bone chain.
|
||||
else:
|
||||
# Get the bone node for this bone, and the next bone
|
||||
var b_target = bone_nodes[i].global_transform
|
||||
var b_target_two = bone_nodes[i+1].global_transform
|
||||
|
||||
|
||||
# Convert the bone nodes positions from world space to bone/skeleton space
|
||||
b_target.origin = skeleton.global_transform.xform_inv(b_target.origin)
|
||||
b_target_two.origin = skeleton.global_transform.xform_inv(b_target_two.origin)
|
||||
|
||||
|
||||
# Get the direction towards the next bone
|
||||
var dir = (b_target_two.origin - b_target.origin).normalized()
|
||||
|
||||
|
||||
# Make this bone look towards the direction of the next bone
|
||||
bone_trans = bone_trans.looking_at(b_target.origin + dir, Vector3.UP)
|
||||
|
||||
|
||||
# Set the position of the bone to the bone target.
|
||||
# Prior to Godot 3.2, this was not necessary, but because we can now completely
|
||||
# override bone transforms, we need to set the position as well as rotation.
|
||||
bone_trans.origin = b_target.origin
|
||||
|
||||
|
||||
# The the bone's (updated) transform
|
||||
set_bone_transform(i, bone_trans)
|
||||
|
||||
@@ -333,12 +333,12 @@ func chain_apply_rotation():
|
||||
func get_bone_transform(bone, convert_to_world_space = true):
|
||||
# Get the global transform of the bone
|
||||
var ret: Transform = skeleton.get_bone_global_pose(bone_IDs[bones_in_chain[bone]])
|
||||
|
||||
|
||||
# If we need to convert the bone position from bone/skeleton space to world space, we
|
||||
# use the Xform of the skeleton (because bone/skeleton space is relative to the position of the skeleton node).
|
||||
if convert_to_world_space:
|
||||
ret.origin = skeleton.global_transform.xform(ret.origin)
|
||||
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
@@ -378,11 +378,11 @@ func _make_editor_sphere_at_node(node, color):
|
||||
|
||||
func _set_update_mode(new_value):
|
||||
update_mode = new_value
|
||||
|
||||
|
||||
set_process(false)
|
||||
set_physics_process(false)
|
||||
set_notify_transform(false)
|
||||
|
||||
|
||||
if update_mode == 0:
|
||||
set_process(true)
|
||||
elif update_mode == 1:
|
||||
@@ -400,24 +400,24 @@ func _set_skeleton_path(new_value):
|
||||
if first_call:
|
||||
skeleton_path = new_value
|
||||
return
|
||||
|
||||
|
||||
skeleton_path = new_value
|
||||
|
||||
|
||||
if skeleton_path == null:
|
||||
if debug_messages:
|
||||
printerr(name, " - IK_FABRIK: No Nodepath selected for skeleton_path!")
|
||||
return
|
||||
|
||||
|
||||
var temp = get_node(skeleton_path)
|
||||
if temp != null:
|
||||
# If it has the method "get_bone_global_pose" it is likely a Skeleton
|
||||
if temp.has_method("get_bone_global_pose"):
|
||||
skeleton = temp
|
||||
bone_IDs = {}
|
||||
|
||||
|
||||
# (Delete all of the old bone nodes and) Make all of the bone nodes for each bone in the IK chain
|
||||
_make_bone_nodes()
|
||||
|
||||
|
||||
if debug_messages:
|
||||
printerr(name, " - IK_FABRIK: Attached to a new skeleton")
|
||||
# If not, then it's (likely) not a Skeleton node
|
||||
@@ -435,25 +435,25 @@ func _set_skeleton_path(new_value):
|
||||
func _make_bone_nodes():
|
||||
# Remove all of the old bone nodes
|
||||
# TODO: (not a huge concern, as these can be removed in the editor)
|
||||
|
||||
|
||||
for bone in range(0, bones_in_chain.size()):
|
||||
|
||||
|
||||
var bone_name = bones_in_chain[bone]
|
||||
if not has_node(bone_name):
|
||||
var new_node = Spatial.new()
|
||||
bone_nodes[bone] = new_node
|
||||
add_child(bone_nodes[bone])
|
||||
|
||||
|
||||
if Engine.editor_hint:
|
||||
if get_tree() != null:
|
||||
if get_tree().edited_scene_root != null:
|
||||
bone_nodes[bone].set_owner(get_tree().edited_scene_root)
|
||||
|
||||
|
||||
bone_nodes[bone].name = bone_name
|
||||
|
||||
|
||||
else:
|
||||
bone_nodes[bone] = get_node(bone_name)
|
||||
|
||||
|
||||
# If we are in the editor, we want to make a sphere at this node
|
||||
if Engine.editor_hint:
|
||||
_make_editor_sphere_at_node(bone_nodes[bone], Color(0.65, 0, 1, 1))
|
||||
@@ -461,7 +461,7 @@ func _make_bone_nodes():
|
||||
|
||||
func _set_bone_chain_bones(new_value):
|
||||
bones_in_chain = new_value
|
||||
|
||||
|
||||
_make_bone_nodes()
|
||||
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ func _ready():
|
||||
set_process(false)
|
||||
set_physics_process(false)
|
||||
set_notify_transform(false)
|
||||
|
||||
|
||||
if update_mode == 0:
|
||||
set_process(true)
|
||||
elif update_mode == 1:
|
||||
@@ -34,7 +34,7 @@ func _ready():
|
||||
else:
|
||||
if debug_messages:
|
||||
print(name, " - IK_LookAt: Unknown update mode. NOT updating skeleton")
|
||||
|
||||
|
||||
if Engine.editor_hint:
|
||||
_setup_for_editor()
|
||||
|
||||
@@ -59,29 +59,29 @@ func update_skeleton():
|
||||
first_call = false
|
||||
if skeleton_to_use == null:
|
||||
_set_skeleton_path(skeleton_path)
|
||||
|
||||
|
||||
|
||||
|
||||
# If we do not have a skeleton and/or we're not supposed to update, then return.
|
||||
if skeleton_to_use == null:
|
||||
return
|
||||
if update_mode >= 3:
|
||||
return
|
||||
|
||||
|
||||
# Get the bone index.
|
||||
var bone: int = skeleton_to_use.find_bone(bone_name)
|
||||
|
||||
|
||||
# If no bone is found (-1), then return and optionally printan error.
|
||||
if bone == -1:
|
||||
if debug_messages:
|
||||
print(name, " - IK_LookAt: No bone in skeleton found with name [", bone_name, "]!")
|
||||
return
|
||||
|
||||
|
||||
# get the bone's global transform pose.
|
||||
var rest = skeleton_to_use.get_bone_global_pose(bone)
|
||||
|
||||
|
||||
# Convert our position relative to the skeleton's transform.
|
||||
var target_pos = skeleton_to_use.global_transform.xform_inv(global_transform.origin)
|
||||
|
||||
|
||||
# Call helper's look_at function with the chosen up axis.
|
||||
if look_at_axis == 0:
|
||||
rest = rest.looking_at(target_pos, Vector3.RIGHT)
|
||||
@@ -93,15 +93,15 @@ func update_skeleton():
|
||||
rest = rest.looking_at(target_pos, Vector3.UP)
|
||||
if debug_messages:
|
||||
print(name, " - IK_LookAt: Unknown look_at_axis value!")
|
||||
|
||||
|
||||
# Get the rotation euler of the bone and of this node.
|
||||
var rest_euler = rest.basis.get_euler()
|
||||
var self_euler = global_transform.basis.orthonormalized().get_euler()
|
||||
|
||||
|
||||
# Flip the rotation euler if using negative rotation.
|
||||
if use_negative_our_rot:
|
||||
self_euler = -self_euler
|
||||
|
||||
|
||||
# Apply this node's rotation euler on each axis, if wanted/required.
|
||||
if use_our_rotation_x:
|
||||
rest_euler.x = self_euler.x
|
||||
@@ -109,23 +109,23 @@ func update_skeleton():
|
||||
rest_euler.y = self_euler.y
|
||||
if use_our_rotation_z:
|
||||
rest_euler.z = self_euler.z
|
||||
|
||||
|
||||
# Make a new basis with the, potentially, changed euler angles.
|
||||
rest.basis = Basis(rest_euler)
|
||||
|
||||
|
||||
# Apply additional rotation stored in additional_rotation to the bone.
|
||||
if additional_rotation != Vector3.ZERO:
|
||||
rest.basis = rest.basis.rotated(rest.basis.x, deg2rad(additional_rotation.x))
|
||||
rest.basis = rest.basis.rotated(rest.basis.y, deg2rad(additional_rotation.y))
|
||||
rest.basis = rest.basis.rotated(rest.basis.z, deg2rad(additional_rotation.z))
|
||||
|
||||
|
||||
# If the position is set using an additional bone, then set the origin
|
||||
# based on that bone and its length.
|
||||
if position_using_additional_bone:
|
||||
var additional_bone_id = skeleton_to_use.find_bone(additional_bone_name)
|
||||
var additional_bone_pos = skeleton_to_use.get_bone_global_pose(additional_bone_id)
|
||||
rest.origin = additional_bone_pos.origin - additional_bone_pos.basis.z.normalized() * additional_bone_length
|
||||
|
||||
|
||||
# Finally, apply the new rotation to the bone in the skeleton.
|
||||
skeleton_to_use.set_bone_global_pose_override(bone, rest, 1.0, true)
|
||||
|
||||
@@ -150,7 +150,7 @@ func _setup_for_editor():
|
||||
indicator_material.flags_unshaded = true
|
||||
indicator_material.albedo_texture = preload("editor_gizmo_texture.png")
|
||||
indicator_material.albedo_color = Color(1, 0.5, 0, 1)
|
||||
|
||||
|
||||
# Assign the material and mesh to the MeshInstance.
|
||||
indicator_mesh.material = indicator_material
|
||||
_editor_indicator.mesh = indicator_mesh
|
||||
@@ -158,12 +158,12 @@ func _setup_for_editor():
|
||||
|
||||
func _set_update(new_value):
|
||||
update_mode = new_value
|
||||
|
||||
|
||||
# Set all of our processes to false.
|
||||
set_process(false)
|
||||
set_physics_process(false)
|
||||
set_notify_transform(false)
|
||||
|
||||
|
||||
# Based on the value of passed to update, enable the correct process.
|
||||
if update_mode == 0:
|
||||
set_process(true)
|
||||
@@ -188,15 +188,15 @@ func _set_skeleton_path(new_value):
|
||||
if first_call:
|
||||
skeleton_path = new_value
|
||||
return
|
||||
|
||||
|
||||
# Assign skeleton_path to whatever value is passed.
|
||||
skeleton_path = new_value
|
||||
|
||||
|
||||
if skeleton_path == null:
|
||||
if debug_messages:
|
||||
print(name, " - IK_LookAt: No Nodepath selected for skeleton_path!")
|
||||
return
|
||||
|
||||
|
||||
# Get the node at that location, if there is one.
|
||||
var temp = get_node(skeleton_path)
|
||||
if temp != null:
|
||||
|
||||
@@ -58,7 +58,7 @@ onready var pistol_end = $CameraHolder/Weapon/Pistol/PistolEnd
|
||||
|
||||
func _ready():
|
||||
anim_player.connect("animation_finished", self, "animation_finished")
|
||||
|
||||
|
||||
set_physics_process(true)
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
||||
set_process_input(true)
|
||||
@@ -70,12 +70,12 @@ func _physics_process(delta):
|
||||
|
||||
|
||||
func process_input(delta):
|
||||
|
||||
|
||||
# Reset dir, so our previous movement does not effect us
|
||||
dir = Vector3()
|
||||
# Get the camera's global transform so we can use its directional vectors
|
||||
var cam_xform = camera.get_global_transform()
|
||||
|
||||
|
||||
# ----------------------------------
|
||||
# Walking
|
||||
if Input.is_key_pressed(KEY_UP) or Input.is_key_pressed(KEY_W):
|
||||
@@ -86,17 +86,17 @@ func process_input(delta):
|
||||
dir += -cam_xform.basis[0]
|
||||
if Input.is_key_pressed(KEY_RIGHT) or Input.is_key_pressed(KEY_D):
|
||||
dir += cam_xform.basis[0]
|
||||
|
||||
|
||||
if Input.is_action_just_pressed("ui_cancel"):
|
||||
if Input.get_mouse_mode() == Input.MOUSE_MODE_VISIBLE:
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
||||
else:
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
|
||||
|
||||
|
||||
if Input.is_mouse_button_pressed(2):
|
||||
if not right_mouse_down:
|
||||
right_mouse_down = true
|
||||
|
||||
|
||||
if anim_done:
|
||||
if current_anim != "Aiming":
|
||||
anim_player.play("Aiming")
|
||||
@@ -104,15 +104,15 @@ func process_input(delta):
|
||||
else:
|
||||
anim_player.play("Idle")
|
||||
current_anim = "Idle"
|
||||
|
||||
|
||||
anim_done = false
|
||||
else:
|
||||
right_mouse_down = false
|
||||
|
||||
|
||||
if Input.is_mouse_button_pressed(1):
|
||||
if left_mouse_timer <= 0:
|
||||
left_mouse_timer = LEFT_MOUSE_FIRE_TIME
|
||||
|
||||
|
||||
# Create a bullet
|
||||
var new_bullet = simple_bullet.instance()
|
||||
get_tree().root.add_child(new_bullet)
|
||||
@@ -121,8 +121,8 @@ func process_input(delta):
|
||||
if left_mouse_timer > 0:
|
||||
left_mouse_timer -= delta
|
||||
# ----------------------------------
|
||||
|
||||
|
||||
|
||||
|
||||
# ----------------------------------
|
||||
# Sprinting
|
||||
if Input.is_key_pressed(KEY_SHIFT):
|
||||
@@ -130,7 +130,7 @@ func process_input(delta):
|
||||
else:
|
||||
is_sprinting = false
|
||||
# ----------------------------------
|
||||
|
||||
|
||||
# ----------------------------------
|
||||
# Jumping
|
||||
if Input.is_key_pressed(KEY_SPACE):
|
||||
@@ -141,8 +141,8 @@ func process_input(delta):
|
||||
else:
|
||||
jump_button_down = false
|
||||
# ----------------------------------
|
||||
|
||||
|
||||
|
||||
|
||||
# ----------------------------------
|
||||
# Leaninng
|
||||
if Input.is_key_pressed(KEY_Q):
|
||||
@@ -158,7 +158,7 @@ func process_input(delta):
|
||||
lean_value += 1 * delta
|
||||
if lean_value > 0.5:
|
||||
lean_value = 0.5
|
||||
|
||||
|
||||
lean_value = clamp(lean_value, 0, 1)
|
||||
path_follow_node.unit_offset = lean_value
|
||||
if lean_value < 0.5:
|
||||
@@ -171,24 +171,24 @@ func process_input(delta):
|
||||
|
||||
|
||||
func process_movement(delta):
|
||||
|
||||
|
||||
var grav = norm_grav
|
||||
|
||||
|
||||
dir.y = 0
|
||||
dir = dir.normalized()
|
||||
|
||||
|
||||
vel.y += delta*grav
|
||||
|
||||
|
||||
var hvel = vel
|
||||
hvel.y = 0
|
||||
|
||||
|
||||
var target = dir
|
||||
if is_sprinting:
|
||||
target *= MAX_SPRINT_SPEED
|
||||
else:
|
||||
target *= MAX_SPEED
|
||||
|
||||
|
||||
|
||||
|
||||
var accel
|
||||
if dir.dot(hvel) > 0:
|
||||
if not is_sprinting:
|
||||
@@ -197,32 +197,32 @@ func process_movement(delta):
|
||||
accel = SPRINT_ACCEL
|
||||
else:
|
||||
accel = DEACCEL
|
||||
|
||||
|
||||
hvel = hvel.linear_interpolate(target, accel*delta)
|
||||
|
||||
|
||||
vel.x = hvel.x
|
||||
vel.z = hvel.z
|
||||
|
||||
|
||||
vel = move_and_slide(vel,Vector3(0,1,0))
|
||||
|
||||
|
||||
# Mouse based camera movement
|
||||
func _input(event):
|
||||
|
||||
|
||||
if event is InputEventMouseMotion && Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
|
||||
|
||||
|
||||
rotate_y(deg2rad(event.relative.x * MOUSE_SENSITIVITY * -1))
|
||||
camera_holder.rotate_x(deg2rad(event.relative.y * MOUSE_SENSITIVITY))
|
||||
|
||||
|
||||
# We need to clamp the camera's rotation so we cannot rotate ourselves upside down
|
||||
var camera_rot = camera_holder.rotation_degrees
|
||||
if camera_rot.x < -40:
|
||||
camera_rot.x = -40
|
||||
elif camera_rot.x > 60:
|
||||
camera_rot.x = 60
|
||||
|
||||
|
||||
camera_holder.rotation_degrees = camera_rot
|
||||
|
||||
|
||||
else:
|
||||
pass
|
||||
|
||||
|
||||
@@ -8,10 +8,10 @@ onready var targets = $Targets
|
||||
|
||||
func _process(_delta):
|
||||
var mouse_to_world = project_local_ray_normal(get_viewport().get_mouse_position()) * MOVEMENT_SPEED
|
||||
|
||||
|
||||
if flip_axis:
|
||||
mouse_to_world = -mouse_to_world
|
||||
else:
|
||||
mouse_to_world.z *= -1
|
||||
|
||||
|
||||
targets.transform.origin = mouse_to_world
|
||||
|
||||
@@ -14,10 +14,10 @@ func start_timer(timeout):
|
||||
_timer.connect("timeout", self, "_on_timer_done")
|
||||
else:
|
||||
cancel_timer()
|
||||
|
||||
|
||||
_timer.start(timeout)
|
||||
_timer_started = true
|
||||
|
||||
|
||||
return _timer
|
||||
|
||||
|
||||
|
||||
@@ -19,12 +19,12 @@ func _ready():
|
||||
yield(start_timer(0.5), "timeout")
|
||||
if is_timer_canceled():
|
||||
return
|
||||
|
||||
|
||||
while $DynamicShapes.get_child_count():
|
||||
var type_node = $DynamicShapes.get_child(0)
|
||||
_object_templates.push_back(type_node)
|
||||
$DynamicShapes.remove_child(type_node)
|
||||
|
||||
|
||||
$Options.add_menu_item(OPTION_TYPE_ALL)
|
||||
$Options.add_menu_item(OPTION_TYPE_BOX)
|
||||
$Options.add_menu_item(OPTION_TYPE_CAPSULE)
|
||||
@@ -32,15 +32,15 @@ func _ready():
|
||||
$Options.add_menu_item(OPTION_TYPE_CONVEX)
|
||||
$Options.add_menu_item(OPTION_TYPE_SPHERE)
|
||||
$Options.connect("option_selected", self, "_on_option_selected")
|
||||
|
||||
|
||||
_start_all_types()
|
||||
|
||||
|
||||
func _on_option_selected(option):
|
||||
cancel_timer()
|
||||
|
||||
|
||||
_despawn_objects()
|
||||
|
||||
|
||||
match option:
|
||||
OPTION_TYPE_ALL:
|
||||
_start_all_types()
|
||||
@@ -61,7 +61,7 @@ func _find_type_index(type_name):
|
||||
var type_node = _object_templates[type_index]
|
||||
if type_node.name.find(type_name) > -1:
|
||||
return type_index
|
||||
|
||||
|
||||
Log.print_error("Invalid shape type: " + type_name)
|
||||
return -1
|
||||
|
||||
@@ -71,25 +71,25 @@ func _start_type(type_index):
|
||||
return
|
||||
if type_index >= _object_templates.size():
|
||||
return
|
||||
|
||||
|
||||
yield(start_timer(1.0), "timeout")
|
||||
if is_timer_canceled():
|
||||
return
|
||||
|
||||
|
||||
_spawn_objects(type_index)
|
||||
|
||||
|
||||
yield(start_timer(1.0), "timeout")
|
||||
if is_timer_canceled():
|
||||
return
|
||||
|
||||
|
||||
_activate_objects()
|
||||
|
||||
|
||||
yield(start_timer(5.0), "timeout")
|
||||
if is_timer_canceled():
|
||||
return
|
||||
|
||||
|
||||
_despawn_objects()
|
||||
|
||||
|
||||
Log.print_log("* Done.")
|
||||
|
||||
|
||||
@@ -98,21 +98,21 @@ func _start_all_types():
|
||||
yield(start_timer(1.0), "timeout")
|
||||
if is_timer_canceled():
|
||||
return
|
||||
|
||||
|
||||
_spawn_objects(type_index)
|
||||
|
||||
|
||||
yield(start_timer(1.0), "timeout")
|
||||
if is_timer_canceled():
|
||||
return
|
||||
|
||||
|
||||
_activate_objects()
|
||||
|
||||
|
||||
yield(start_timer(5.0), "timeout")
|
||||
if is_timer_canceled():
|
||||
return
|
||||
|
||||
|
||||
_despawn_objects()
|
||||
|
||||
|
||||
Log.print_log("* Done.")
|
||||
|
||||
|
||||
@@ -120,9 +120,9 @@ func _spawn_objects(type_index):
|
||||
var template_node = _object_templates[type_index]
|
||||
for spawn in spawns:
|
||||
var spawn_parent = get_node(spawn)
|
||||
|
||||
|
||||
Log.print_log("* Spawning: " + template_node.name)
|
||||
|
||||
|
||||
for _index in range(spawn_multipiler):
|
||||
for _node_index in spawn_count / spawn_multipiler:
|
||||
var node = template_node.duplicate() as Spatial
|
||||
@@ -131,9 +131,9 @@ func _spawn_objects(type_index):
|
||||
|
||||
func _activate_objects():
|
||||
var spawn_parent = $SpawnTarget1
|
||||
|
||||
|
||||
Log.print_log("* Activating")
|
||||
|
||||
|
||||
for node_index in spawn_parent.get_child_count():
|
||||
var node = spawn_parent.get_child(node_index) as RigidBody
|
||||
node.set_sleeping(false)
|
||||
@@ -142,12 +142,12 @@ func _activate_objects():
|
||||
func _despawn_objects():
|
||||
for spawn in spawns:
|
||||
var spawn_parent = get_node(spawn)
|
||||
|
||||
|
||||
if spawn_parent.get_child_count() == 0:
|
||||
return
|
||||
|
||||
|
||||
Log.print_log("* Despawning")
|
||||
|
||||
|
||||
while spawn_parent.get_child_count():
|
||||
var node_index = spawn_parent.get_child_count() - 1
|
||||
var node = spawn_parent.get_child(node_index)
|
||||
|
||||
@@ -27,7 +27,7 @@ func add_test(id, scene_path):
|
||||
test_data.id = id
|
||||
test_data.scene_path = scene_path
|
||||
_test_list.append(test_data)
|
||||
|
||||
|
||||
add_menu_item(id)
|
||||
|
||||
|
||||
@@ -39,15 +39,15 @@ func _on_option_selected(item_path):
|
||||
|
||||
func _start_test(test):
|
||||
_current_test = test
|
||||
|
||||
|
||||
if _current_test_scene:
|
||||
_current_test_scene.queue_free()
|
||||
_current_test_scene = null
|
||||
|
||||
|
||||
Log.print_log("*** STARTING TEST: " + test.id)
|
||||
var scene = load(test.scene_path)
|
||||
_current_test_scene = scene.instance()
|
||||
get_tree().root.add_child(_current_test_scene)
|
||||
|
||||
|
||||
var label_test = get_node("../LabelTest")
|
||||
label_test.test_name = test.id
|
||||
|
||||
@@ -17,10 +17,10 @@ func _unhandled_input(event):
|
||||
if mouse_button_event.button_index == BUTTON_LEFT:
|
||||
_rotation_enabled = mouse_button_event.pressed
|
||||
return
|
||||
|
||||
|
||||
if not _rotation_enabled:
|
||||
return
|
||||
|
||||
|
||||
var mouse_motion_event = event as InputEventMouseMotion
|
||||
if mouse_motion_event:
|
||||
var rotation_delta = mouse_motion_event.relative.x
|
||||
|
||||
@@ -8,7 +8,7 @@ var _entry_template
|
||||
|
||||
func _enter_tree():
|
||||
Log.connect("entry_logged", self, "_on_log_entry")
|
||||
|
||||
|
||||
_entry_template = get_child(0) as Label
|
||||
remove_child(_entry_template)
|
||||
|
||||
@@ -22,16 +22,16 @@ func clear():
|
||||
|
||||
func _on_log_entry(message, type):
|
||||
var new_entry = _entry_template.duplicate() as Label
|
||||
|
||||
|
||||
new_entry.set_text(message)
|
||||
if type == Log.LogType.ERROR:
|
||||
new_entry.modulate = Color.red
|
||||
else:
|
||||
new_entry.modulate = Color.white
|
||||
|
||||
|
||||
if get_child_count() >= MAX_ENTRIES:
|
||||
var first_entry = get_child(0) as Label
|
||||
remove_child(first_entry)
|
||||
first_entry.queue_free()
|
||||
|
||||
|
||||
add_child(new_entry)
|
||||
|
||||
@@ -15,16 +15,16 @@ func _ready():
|
||||
func _process(_delta):
|
||||
if _attachment == null:
|
||||
return
|
||||
|
||||
|
||||
var viewport = get_viewport()
|
||||
if viewport == null:
|
||||
return
|
||||
|
||||
|
||||
var camera = viewport.get_camera()
|
||||
if camera == null:
|
||||
return
|
||||
|
||||
|
||||
var world_pos = world_offset + _attachment.global_transform.origin
|
||||
var screen_pos = camera.unproject_position(world_pos)
|
||||
|
||||
|
||||
rect_position = _pos_offset + screen_pos - 0.5 * rect_size
|
||||
|
||||
@@ -9,14 +9,14 @@ func add_menu_item(item_path):
|
||||
var path_elements = item_path.split("/", false)
|
||||
var path_element_count = path_elements.size()
|
||||
assert(path_element_count > 0)
|
||||
|
||||
|
||||
var path = ""
|
||||
var popup = get_popup()
|
||||
for element_index in path_element_count - 1:
|
||||
var popup_label = path_elements[element_index]
|
||||
path += popup_label + "/"
|
||||
popup = _add_popup(popup, path, popup_label)
|
||||
|
||||
|
||||
_add_item(popup, path_elements[path_element_count - 1])
|
||||
|
||||
|
||||
@@ -30,15 +30,15 @@ func _add_popup(parent_popup, path, label):
|
||||
var popup_menu = popup_node as PopupMenu
|
||||
assert(popup_menu)
|
||||
return popup_menu
|
||||
|
||||
|
||||
var popup_menu = PopupMenu.new()
|
||||
popup_menu.name = label
|
||||
|
||||
|
||||
parent_popup.add_child(popup_menu)
|
||||
parent_popup.add_submenu_item(label, label)
|
||||
|
||||
|
||||
popup_menu.connect("index_pressed", self, "_on_item_pressed", [popup_menu, path])
|
||||
|
||||
|
||||
return popup_menu
|
||||
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ var _engine = PhysicsEngine.OTHER
|
||||
|
||||
func _enter_tree():
|
||||
get_tree().debug_collisions_hint = true
|
||||
|
||||
|
||||
var engine_string = ProjectSettings.get_setting("physics/3d/physics_engine")
|
||||
match engine_string:
|
||||
"DEFAULT":
|
||||
@@ -28,7 +28,7 @@ func _enter_tree():
|
||||
func _process(_delta):
|
||||
if Input.is_action_just_pressed("toggle_full_screen"):
|
||||
OS.window_fullscreen = not OS.window_fullscreen
|
||||
|
||||
|
||||
if Input.is_action_just_pressed("toggle_debug_collision"):
|
||||
var debug_collision_enabled = not _is_debug_collision_enabled()
|
||||
_set_debug_collision_enabled(debug_collision_enabled)
|
||||
@@ -36,7 +36,7 @@ func _process(_delta):
|
||||
Log.print_log("Debug Collision ON")
|
||||
else:
|
||||
Log.print_log("Debug Collision OFF")
|
||||
|
||||
|
||||
if Input.is_action_just_pressed("exit"):
|
||||
get_tree().quit()
|
||||
|
||||
|
||||
@@ -24,15 +24,15 @@ func _integrate_forces(state):
|
||||
|
||||
lv += g * delta # Apply gravity.
|
||||
var up = -g.normalized()
|
||||
|
||||
|
||||
if dying:
|
||||
state.set_linear_velocity(lv)
|
||||
return
|
||||
|
||||
|
||||
for i in range(state.get_contact_count()):
|
||||
var cc = state.get_contact_collider_object(i)
|
||||
var dp = state.get_contact_local_normal(i)
|
||||
|
||||
|
||||
if cc:
|
||||
if cc is preload("res://player/bullet/bullet.gd") and cc.enabled:
|
||||
set_mode(MODE_RIGID)
|
||||
@@ -43,15 +43,15 @@ func _integrate_forces(state):
|
||||
cc.enabled = false
|
||||
get_node("SoundHit").play()
|
||||
return
|
||||
|
||||
|
||||
var col_floor = get_node("Armature/RayFloor").is_colliding()
|
||||
var col_wall = get_node("Armature/RayWall").is_colliding()
|
||||
|
||||
|
||||
var advance = col_floor and not col_wall
|
||||
|
||||
|
||||
var dir = get_node("Armature").get_transform().basis[2].normalized()
|
||||
var deaccel_dir = dir
|
||||
|
||||
|
||||
if advance:
|
||||
if dir.dot(lv) < max_speed:
|
||||
lv += dir * accel * delta
|
||||
@@ -59,17 +59,17 @@ func _integrate_forces(state):
|
||||
else:
|
||||
if prev_advance:
|
||||
rot_dir = 1
|
||||
|
||||
|
||||
dir = Basis(up, rot_dir * rot_speed * delta).xform(dir)
|
||||
get_node("Armature").set_transform(Transform().looking_at(-dir, up))
|
||||
|
||||
|
||||
var dspeed = deaccel_dir.dot(lv)
|
||||
dspeed -= deaccel * delta
|
||||
if dspeed < 0:
|
||||
dspeed = 0
|
||||
|
||||
|
||||
lv = lv - deaccel_dir * deaccel_dir.dot(lv) + deaccel_dir * dspeed
|
||||
|
||||
|
||||
state.set_linear_velocity(lv)
|
||||
prev_advance = advance
|
||||
|
||||
|
||||
@@ -30,15 +30,15 @@ func _ready():
|
||||
|
||||
func _physics_process(delta):
|
||||
linear_velocity += gravity * delta
|
||||
|
||||
|
||||
var anim = ANIM_FLOOR
|
||||
|
||||
|
||||
var vv = linear_velocity.y # Vertical velocity.
|
||||
var hv = Vector3(linear_velocity.x, 0, linear_velocity.z) # Horizontal velocity.
|
||||
|
||||
|
||||
var hdir = hv.normalized() # Horizontal direction.
|
||||
var hspeed = hv.length() # Horizontal speed.
|
||||
|
||||
|
||||
# Player input.
|
||||
var cam_basis = get_node("Target/Camera").get_global_transform().basis
|
||||
var dir = Vector3() # Where does the player intend to walk to.
|
||||
@@ -46,45 +46,45 @@ func _physics_process(delta):
|
||||
dir += (Input.get_action_strength("move_backwards") - Input.get_action_strength("move_forward")) * cam_basis[2]
|
||||
dir.y = 0
|
||||
dir = dir.normalized()
|
||||
|
||||
|
||||
var jump_attempt = Input.is_action_pressed("jump")
|
||||
var shoot_attempt = Input.is_action_pressed("shoot")
|
||||
|
||||
|
||||
if is_on_floor():
|
||||
var sharp_turn = hspeed > 0.1 and rad2deg(acos(dir.dot(hdir))) > sharp_turn_threshold
|
||||
|
||||
|
||||
if dir.length() > 0.1 and !sharp_turn:
|
||||
if hspeed > 0.001:
|
||||
hdir = adjust_facing(hdir, dir, delta, 1.0 / hspeed * TURN_SPEED, Vector3.UP)
|
||||
else:
|
||||
hdir = dir
|
||||
|
||||
|
||||
if hspeed < max_speed:
|
||||
hspeed += accel * delta
|
||||
else:
|
||||
hspeed -= deaccel * delta
|
||||
if hspeed < 0:
|
||||
hspeed = 0
|
||||
|
||||
|
||||
hv = hdir * hspeed
|
||||
|
||||
|
||||
var mesh_xform = get_node("Armature").get_transform()
|
||||
var facing_mesh = -mesh_xform.basis[0].normalized()
|
||||
facing_mesh = (facing_mesh - Vector3.UP * facing_mesh.dot(Vector3.UP)).normalized()
|
||||
|
||||
|
||||
if hspeed > 0:
|
||||
facing_mesh = adjust_facing(facing_mesh, dir, delta, 1.0 / hspeed * TURN_SPEED, Vector3.UP)
|
||||
var m3 = Basis(-facing_mesh, Vector3.UP, -facing_mesh.cross(Vector3.UP).normalized()).scaled(CHAR_SCALE)
|
||||
|
||||
|
||||
get_node("Armature").set_transform(Transform(m3, mesh_xform.origin))
|
||||
|
||||
|
||||
if not jumping and jump_attempt:
|
||||
vv = 7.0
|
||||
jumping = true
|
||||
get_node("SoundJump").play()
|
||||
else:
|
||||
anim = ANIM_AIR
|
||||
|
||||
|
||||
if dir.length() > 0.1:
|
||||
hv += dir * (accel * 0.2 * delta)
|
||||
if hv.length() > max_speed:
|
||||
@@ -95,22 +95,22 @@ func _physics_process(delta):
|
||||
if hspeed < 0:
|
||||
hspeed = 0
|
||||
hv = hdir * hspeed
|
||||
|
||||
|
||||
if jumping and vv < 0:
|
||||
jumping = false
|
||||
|
||||
|
||||
linear_velocity = hv + Vector3.UP * vv
|
||||
|
||||
|
||||
if is_on_floor():
|
||||
movement_dir = linear_velocity
|
||||
|
||||
|
||||
linear_velocity = move_and_slide(linear_velocity, -gravity.normalized())
|
||||
|
||||
|
||||
if shoot_blend > 0:
|
||||
shoot_blend -= delta * SHOOT_SCALE
|
||||
if (shoot_blend < 0):
|
||||
shoot_blend = 0
|
||||
|
||||
|
||||
if shoot_attempt and not prev_shoot:
|
||||
shoot_blend = SHOOT_TIME
|
||||
var bullet = preload("res://player/bullet/bullet.tscn").instance()
|
||||
@@ -119,12 +119,12 @@ func _physics_process(delta):
|
||||
bullet.set_linear_velocity(get_node("Armature/Bullet").get_global_transform().basis[2].normalized() * 20)
|
||||
bullet.add_collision_exception_with(self) # Add it to bullet.
|
||||
get_node("SoundShoot").play()
|
||||
|
||||
|
||||
prev_shoot = shoot_attempt
|
||||
|
||||
|
||||
if is_on_floor():
|
||||
$AnimationTree["parameters/walk/blend_amount"] = hspeed / max_speed
|
||||
|
||||
|
||||
$AnimationTree["parameters/state/current"] = anim
|
||||
$AnimationTree["parameters/air_dir/blend_amount"] = clamp(-linear_velocity.y / 4 + 0.5, 0, 1)
|
||||
$AnimationTree["parameters/gun/blend_amount"] = min(shoot_blend, 1.0)
|
||||
@@ -133,15 +133,15 @@ func _physics_process(delta):
|
||||
func adjust_facing(p_facing, p_target, p_step, p_adjust_rate, current_gn):
|
||||
var n = p_target # Normal.
|
||||
var t = n.cross(current_gn).normalized()
|
||||
|
||||
|
||||
var x = n.dot(p_facing)
|
||||
var y = t.dot(p_facing)
|
||||
|
||||
|
||||
var ang = atan2(y,x)
|
||||
|
||||
|
||||
if abs(ang) < 0.001: # Too small.
|
||||
return p_facing
|
||||
|
||||
|
||||
var s = sign(ang)
|
||||
ang = ang * s
|
||||
var turn = ang * p_adjust_rate * p_step
|
||||
@@ -151,5 +151,5 @@ func adjust_facing(p_facing, p_target, p_step, p_adjust_rate, current_gn):
|
||||
else:
|
||||
a = turn
|
||||
ang = (ang - a) * s
|
||||
|
||||
|
||||
return (n * cos(ang) + t * sin(ang)) * p_facing.length()
|
||||
|
||||
@@ -17,7 +17,7 @@ func _ready():
|
||||
break
|
||||
else:
|
||||
node = node.get_parent()
|
||||
|
||||
|
||||
# This detaches the camera transform from the parent spatial node.
|
||||
set_as_toplevel(true)
|
||||
|
||||
@@ -25,25 +25,25 @@ func _ready():
|
||||
func _physics_process(_delta):
|
||||
var target = get_parent().get_global_transform().origin
|
||||
var pos = get_global_transform().origin
|
||||
|
||||
|
||||
var from_target = pos - target
|
||||
|
||||
|
||||
# Check ranges.
|
||||
if from_target.length() < min_distance:
|
||||
from_target = from_target.normalized() * min_distance
|
||||
elif from_target.length() > max_distance:
|
||||
from_target = from_target.normalized() * max_distance
|
||||
|
||||
|
||||
# Check upper and lower height.
|
||||
if from_target.y > max_height:
|
||||
from_target.y = max_height
|
||||
if from_target.y < min_height:
|
||||
from_target.y = min_height
|
||||
|
||||
|
||||
pos = target + from_target
|
||||
|
||||
|
||||
look_at_from_position(pos, target, Vector3.UP)
|
||||
|
||||
|
||||
# Turn a little up or down
|
||||
var t = get_transform()
|
||||
t.basis = Basis(t.basis[0], deg2rad(angle_v_adjust)) * t.basis
|
||||
|
||||
@@ -9,15 +9,15 @@ export var engine_force_value = 40
|
||||
|
||||
func _physics_process(delta):
|
||||
var fwd_mps = transform.basis.xform_inv(linear_velocity).x
|
||||
|
||||
|
||||
steer_target = Input.get_action_strength("turn_left") - Input.get_action_strength("turn_right")
|
||||
steer_target *= STEER_LIMIT
|
||||
|
||||
|
||||
if Input.is_action_pressed("accelerate"):
|
||||
engine_force = engine_force_value
|
||||
else:
|
||||
engine_force = 0
|
||||
|
||||
|
||||
if Input.is_action_pressed("reverse"):
|
||||
if (fwd_mps >= -1):
|
||||
engine_force = -engine_force_value
|
||||
@@ -25,5 +25,5 @@ func _physics_process(delta):
|
||||
brake = 1
|
||||
else:
|
||||
brake = 0.0
|
||||
|
||||
|
||||
steering = move_toward(steering, steer_target, STEER_SPEED * delta)
|
||||
|
||||
@@ -8,7 +8,7 @@ onready var voxel_world = $"../VoxelWorld"
|
||||
func _process(_delta):
|
||||
if Input.is_action_just_pressed("debug"):
|
||||
visible = !visible
|
||||
|
||||
|
||||
text = "Position: " + _vector_to_string_appropriate_digits(player.transform.origin)
|
||||
text += "\nEffective render distance: " + str(voxel_world.effective_render_distance)
|
||||
text += "\nLooking: " + _cardinal_string_from_radians(player.transform.basis.get_euler().y)
|
||||
@@ -26,7 +26,7 @@ func _vector_to_string_appropriate_digits(vector):
|
||||
factors[i] = factors[i] / 10
|
||||
if abs(vector[i]) > 524288:
|
||||
factors[i] = factors[i] / 10
|
||||
|
||||
|
||||
return "(" + \
|
||||
str(round(vector.x * factors[0]) / factors[0]) + ", " + \
|
||||
str(round(vector.y * factors[1]) / factors[1]) + ", " + \
|
||||
|
||||
@@ -23,7 +23,7 @@ func _process(_delta):
|
||||
_mouse_motion.y = clamp(_mouse_motion.y, -1550, 1550)
|
||||
transform.basis = Basis(Vector3(0, _mouse_motion.x * -0.001, 0))
|
||||
head.transform.basis = Basis(Vector3(_mouse_motion.y * -0.001, 0, 0))
|
||||
|
||||
|
||||
# Block selection.
|
||||
var position = raycast.get_collision_point()
|
||||
var normal = raycast.get_collision_normal()
|
||||
@@ -41,7 +41,7 @@ func _process(_delta):
|
||||
# Set the appropriate texture.
|
||||
var uv = Chunk.calculate_block_uvs(_selected_block)
|
||||
selected_block_texture.texture.region = Rect2(uv[0] * 512, Vector2.ONE * 64)
|
||||
|
||||
|
||||
# Block breaking/placing.
|
||||
if crosshair.visible and raycast.is_colliding():
|
||||
var breaking = Input.is_action_just_pressed("break")
|
||||
@@ -49,7 +49,7 @@ func _process(_delta):
|
||||
# Either both buttons were pressed or neither are, so stop.
|
||||
if breaking == placing:
|
||||
return
|
||||
|
||||
|
||||
if breaking:
|
||||
var block_global_position = (position - normal / 2).floor()
|
||||
voxel_world.set_block_global_position(block_global_position, 0)
|
||||
@@ -65,20 +65,20 @@ func _physics_process(delta):
|
||||
head.transform.origin = Vector3(0, 1.2, 0)
|
||||
else:
|
||||
head.transform.origin = Vector3(0, 1.6, 0)
|
||||
|
||||
|
||||
# Keyboard movement.
|
||||
var movement = transform.basis.xform(Vector3(
|
||||
Input.get_action_strength("move_right") - Input.get_action_strength("move_left"),
|
||||
0,
|
||||
Input.get_action_strength("move_back") - Input.get_action_strength("move_forward")
|
||||
).normalized() * (1 if crouching else 5))
|
||||
|
||||
|
||||
# Gravity.
|
||||
velocity.y -= gravity * delta
|
||||
|
||||
|
||||
#warning-ignore:return_value_discarded
|
||||
velocity = move_and_slide(Vector3(movement.x, velocity.y, movement.z), Vector3.UP)
|
||||
|
||||
|
||||
# Jumping, applied next frame.
|
||||
if is_on_floor() and Input.is_action_pressed("jump"):
|
||||
velocity.y = 5
|
||||
|
||||
@@ -15,7 +15,7 @@ func _enter_tree():
|
||||
printerr("Please delete the instance at: " + get_path())
|
||||
else:
|
||||
Settings._loaded = true
|
||||
|
||||
|
||||
var file = File.new()
|
||||
if file.file_exists(_save_path):
|
||||
file.open(_save_path, File.READ)
|
||||
|
||||
@@ -25,7 +25,7 @@ func _ready():
|
||||
data = TerrainGenerator.random_blocks()
|
||||
else:
|
||||
data = TerrainGenerator.flat(chunk_position)
|
||||
|
||||
|
||||
# We can only add colliders in the main thread due to physics limitations.
|
||||
_generate_chunk_collider()
|
||||
# However, we can use a thread for mesh generation.
|
||||
@@ -38,7 +38,7 @@ func regenerate():
|
||||
for c in get_children():
|
||||
remove_child(c)
|
||||
c.queue_free()
|
||||
|
||||
|
||||
# Then generate new ones.
|
||||
_generate_chunk_collider()
|
||||
_generate_chunk_mesh(0)
|
||||
@@ -51,7 +51,7 @@ func _generate_chunk_collider():
|
||||
collision_layer = 0
|
||||
collision_mask = 0
|
||||
return
|
||||
|
||||
|
||||
# For each block, generate a collider. Ensure collision layers are enabled.
|
||||
collision_layer = 0xFFFFF
|
||||
collision_mask = 0xFFFFF
|
||||
@@ -64,15 +64,15 @@ func _generate_chunk_collider():
|
||||
func _generate_chunk_mesh(_this_argument_exists_due_to_bug_9924):
|
||||
if data.empty():
|
||||
return
|
||||
|
||||
|
||||
var surface_tool = SurfaceTool.new()
|
||||
surface_tool.begin(Mesh.PRIMITIVE_TRIANGLES)
|
||||
|
||||
|
||||
# For each block, add data to the SurfaceTool and generate a collider.
|
||||
for block_position in data.keys():
|
||||
var block_id = data[block_position]
|
||||
_draw_block_mesh(surface_tool, block_position, block_id)
|
||||
|
||||
|
||||
# Create the chunk's mesh from the SurfaceTool data.
|
||||
surface_tool.generate_normals()
|
||||
surface_tool.generate_tangents()
|
||||
@@ -89,7 +89,7 @@ func _draw_block_mesh(surface_tool, block_sub_position, block_id):
|
||||
var uvs = calculate_block_uvs(block_id)
|
||||
var top_uvs = uvs
|
||||
var bottom_uvs = uvs
|
||||
|
||||
|
||||
# Bush blocks get drawn in their own special way.
|
||||
if block_id == 27 or block_id == 28:
|
||||
_draw_block_face(surface_tool, [verts[2], verts[0], verts[7], verts[5]], uvs)
|
||||
@@ -97,7 +97,7 @@ func _draw_block_mesh(surface_tool, block_sub_position, block_id):
|
||||
_draw_block_face(surface_tool, [verts[3], verts[1], verts[6], verts[4]], uvs)
|
||||
_draw_block_face(surface_tool, [verts[6], verts[4], verts[3], verts[1]], uvs)
|
||||
return
|
||||
|
||||
|
||||
# Allow some blocks to have different top/bottom textures.
|
||||
if block_id == 3: # Grass.
|
||||
top_uvs = calculate_block_uvs(0)
|
||||
@@ -111,7 +111,7 @@ func _draw_block_mesh(surface_tool, block_sub_position, block_id):
|
||||
elif block_id == 19: # Bookshelf.
|
||||
top_uvs = calculate_block_uvs(4)
|
||||
bottom_uvs = top_uvs
|
||||
|
||||
|
||||
# Main rendering code for normal blocks.
|
||||
var other_block_position = block_sub_position + Vector3.LEFT
|
||||
var other_block_id = 0
|
||||
@@ -121,7 +121,7 @@ func _draw_block_mesh(surface_tool, block_sub_position, block_id):
|
||||
other_block_id = data[other_block_position]
|
||||
if block_id != other_block_id and is_block_transparent(other_block_id):
|
||||
_draw_block_face(surface_tool, [verts[2], verts[0], verts[3], verts[1]], uvs)
|
||||
|
||||
|
||||
other_block_position = block_sub_position + Vector3.RIGHT
|
||||
other_block_id = 0
|
||||
if other_block_position.x == CHUNK_SIZE:
|
||||
@@ -130,7 +130,7 @@ func _draw_block_mesh(surface_tool, block_sub_position, block_id):
|
||||
other_block_id = data[other_block_position]
|
||||
if block_id != other_block_id and is_block_transparent(other_block_id):
|
||||
_draw_block_face(surface_tool, [verts[7], verts[5], verts[6], verts[4]], uvs)
|
||||
|
||||
|
||||
other_block_position = block_sub_position + Vector3.FORWARD
|
||||
other_block_id = 0
|
||||
if other_block_position.z == -1:
|
||||
@@ -139,7 +139,7 @@ func _draw_block_mesh(surface_tool, block_sub_position, block_id):
|
||||
other_block_id = data[other_block_position]
|
||||
if block_id != other_block_id and is_block_transparent(other_block_id):
|
||||
_draw_block_face(surface_tool, [verts[6], verts[4], verts[2], verts[0]], uvs)
|
||||
|
||||
|
||||
other_block_position = block_sub_position + Vector3.BACK
|
||||
other_block_id = 0
|
||||
if other_block_position.z == CHUNK_SIZE:
|
||||
@@ -148,7 +148,7 @@ func _draw_block_mesh(surface_tool, block_sub_position, block_id):
|
||||
other_block_id = data[other_block_position]
|
||||
if block_id != other_block_id and is_block_transparent(other_block_id):
|
||||
_draw_block_face(surface_tool, [verts[3], verts[1], verts[7], verts[5]], uvs)
|
||||
|
||||
|
||||
other_block_position = block_sub_position + Vector3.DOWN
|
||||
other_block_id = 0
|
||||
if other_block_position.y == -1:
|
||||
@@ -157,7 +157,7 @@ func _draw_block_mesh(surface_tool, block_sub_position, block_id):
|
||||
other_block_id = data[other_block_position]
|
||||
if block_id != other_block_id and is_block_transparent(other_block_id):
|
||||
_draw_block_face(surface_tool, [verts[4], verts[5], verts[0], verts[1]], bottom_uvs)
|
||||
|
||||
|
||||
other_block_position = block_sub_position + Vector3.UP
|
||||
other_block_id = 0
|
||||
if other_block_position.y == CHUNK_SIZE:
|
||||
@@ -172,7 +172,7 @@ func _draw_block_face(surface_tool, verts, uvs):
|
||||
surface_tool.add_uv(uvs[1]); surface_tool.add_vertex(verts[1])
|
||||
surface_tool.add_uv(uvs[2]); surface_tool.add_vertex(verts[2])
|
||||
surface_tool.add_uv(uvs[3]); surface_tool.add_vertex(verts[3])
|
||||
|
||||
|
||||
surface_tool.add_uv(uvs[2]); surface_tool.add_vertex(verts[2])
|
||||
surface_tool.add_uv(uvs[1]); surface_tool.add_vertex(verts[1])
|
||||
surface_tool.add_uv(uvs[0]); surface_tool.add_vertex(verts[0])
|
||||
@@ -190,7 +190,7 @@ static func calculate_block_uvs(block_id):
|
||||
# This method only supports square texture sheets.
|
||||
var row = block_id / TEXTURE_SHEET_WIDTH
|
||||
var col = block_id % TEXTURE_SHEET_WIDTH
|
||||
|
||||
|
||||
return [
|
||||
TEXTURE_TILE_SIZE * Vector2(col, row),
|
||||
TEXTURE_TILE_SIZE * Vector2(col, row + 1),
|
||||
|
||||
@@ -7,7 +7,7 @@ onready var voxel_world = $"../VoxelWorld"
|
||||
func _process(delta):
|
||||
environment.fog_enabled = Settings.fog_enabled
|
||||
environment.dof_blur_far_enabled = Settings.fog_enabled
|
||||
|
||||
|
||||
var target_distance = clamp(voxel_world.effective_render_distance, 2, voxel_world.render_distance - 1) * Chunk.CHUNK_SIZE
|
||||
var rate = delta * 4
|
||||
if environment.fog_depth_end > target_distance:
|
||||
|
||||
@@ -23,14 +23,14 @@ static func random_blocks():
|
||||
|
||||
static func flat(chunk_position):
|
||||
var data = {}
|
||||
|
||||
|
||||
if chunk_position.y != -1:
|
||||
return data
|
||||
|
||||
|
||||
for x in range(CHUNK_SIZE):
|
||||
for z in range(CHUNK_SIZE):
|
||||
data[Vector3(x, 0, z)] = 3
|
||||
|
||||
|
||||
return data
|
||||
|
||||
|
||||
@@ -38,5 +38,5 @@ static func flat(chunk_position):
|
||||
static func origin_grass(chunk_position):
|
||||
if chunk_position == Vector3.ZERO:
|
||||
return {Vector3.ZERO: 3}
|
||||
|
||||
|
||||
return {}
|
||||
|
||||
@@ -20,17 +20,17 @@ onready var player = $"../Player"
|
||||
func _process(_delta):
|
||||
_set_render_distance(Settings.render_distance)
|
||||
var player_chunk = (player.transform.origin / Chunk.CHUNK_SIZE).round()
|
||||
|
||||
|
||||
if _deleting or player_chunk != _old_player_chunk:
|
||||
_delete_far_away_chunks(player_chunk)
|
||||
_generating = true
|
||||
|
||||
|
||||
if not _generating:
|
||||
return
|
||||
|
||||
|
||||
# Try to generate chunks ahead of time based on where the player is moving.
|
||||
player_chunk.y += round(clamp(player.velocity.y, -render_distance / 4, render_distance / 4))
|
||||
|
||||
|
||||
# Check existing chunks within range. If it doesn't exist, create it.
|
||||
for x in range(player_chunk.x - effective_render_distance, player_chunk.x + effective_render_distance):
|
||||
for y in range(player_chunk.y - effective_render_distance, player_chunk.y + effective_render_distance):
|
||||
@@ -38,16 +38,16 @@ func _process(_delta):
|
||||
var chunk_position = Vector3(x, y, z)
|
||||
if player_chunk.distance_to(chunk_position) > render_distance:
|
||||
continue
|
||||
|
||||
|
||||
if _chunks.has(chunk_position):
|
||||
continue
|
||||
|
||||
|
||||
var chunk = Chunk.new()
|
||||
chunk.chunk_position = chunk_position
|
||||
_chunks[chunk_position] = chunk
|
||||
add_child(chunk)
|
||||
return
|
||||
|
||||
|
||||
# If we didn't generate any chunks (and therefore didn't return), what next?
|
||||
if effective_render_distance < render_distance:
|
||||
# We can move on to the next stage by increasing the effective distance.
|
||||
@@ -76,7 +76,7 @@ func set_block_global_position(block_global_position, block_id):
|
||||
else:
|
||||
chunk.data[sub_position] = block_id
|
||||
chunk.regenerate()
|
||||
|
||||
|
||||
# We also might need to regenerate some neighboring chunks.
|
||||
if Chunk.is_block_transparent(block_id):
|
||||
if sub_position.x == 0:
|
||||
@@ -108,7 +108,7 @@ func _delete_far_away_chunks(player_chunk):
|
||||
_old_player_chunk = player_chunk
|
||||
# If we need to delete chunks, give the new chunk system a chance to catch up.
|
||||
effective_render_distance = max(1, effective_render_distance - 1)
|
||||
|
||||
|
||||
var deleted_this_frame = 0
|
||||
# We should delete old chunks more aggressively if moving fast.
|
||||
# An easy way to calculate this is by using the effective render distance.
|
||||
@@ -128,7 +128,7 @@ func _delete_far_away_chunks(player_chunk):
|
||||
# Continue deleting next frame.
|
||||
_deleting = true
|
||||
return
|
||||
|
||||
|
||||
# We're done deleting.
|
||||
_deleting = false
|
||||
|
||||
|
||||
Reference in New Issue
Block a user