Skip to content

Commit

Permalink
several improvements and vignette effect
Browse files Browse the repository at this point in the history
  • Loading branch information
krazyjakee committed Jan 19, 2024
1 parent 94de5b8 commit 13b4ae9
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 49 deletions.
2 changes: 1 addition & 1 deletion addons/nodot/Nodot2D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ class_name Nodot2D extends Node2D

## Toggle between visible and invisible
func toggle():
visible = !visible
visible = !visible
6 changes: 3 additions & 3 deletions addons/nodot/NodotRigidBody3D.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class_name NodotRigidBody3D extends RigidBody3D

signal character_collided(body: CharacterBody3D)
signal character_enter(body: CharacterBody3D)

func focussed() -> void:
for child in get_children():
Expand All @@ -13,5 +13,5 @@ func unfocussed() -> void:
if child.has_method("unfocussed"):
child.unfocussed()

func _on_character_collide(body: CharacterBody3D):
emit_signal("character_collided", body)
func _on_character_entered(body: CharacterBody3D):
emit_signal("character_entered", body)
26 changes: 25 additions & 1 deletion addons/nodot/interaction/Interaction3D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ signal interacted(interacted_node: Node3D, collision_point: Vector3, collision_n
@export var max_mass: float = 10.0
## The distance away from the raycast origin to carry the object
@export var carry_distance: float = 2.0
## The maximum distance away from the raycast origin before the object is dropped
@export var max_carry_distance: float = 2.0
## The force of throwing the carried body
@export var throw_force: float = 250.0;

# RigidBody3D or null being carried
var carried_body: RigidBody3D
var carried_body_gravity_scale: float = 1.0
var carried_body_width: float = 0.0
var label3d: Label3D
var last_collider: Node3D
Expand All @@ -37,6 +42,9 @@ func _enter_tree():
label3d.font_size = font_size
label3d.modulate = font_color
label3d.position.z = -2
label3d.render_priority = 5
label3d.outline_render_priority = 4
label3d.no_depth_test = true
add_child(label3d)

func _ready():
Expand Down Expand Up @@ -66,6 +74,13 @@ func _physics_process(delta):
if not multiplayer.is_server(): return

var carry_position = global_transform.origin - global_transform.basis.z.normalized() * (carry_distance + carried_body_width)
var current_carry_distance = carried_body.global_position.distance_to(global_position)
if current_carry_distance > carry_distance + max_carry_distance:
carry_end();
return
if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
throw();
return;
var speed = carried_body.global_position.distance_to(carry_position) * 500
carried_body.linear_velocity = carried_body.global_transform.origin.direction_to(carry_position) * speed * delta
var rotate_speed: float = 10.0 * delta
Expand Down Expand Up @@ -94,6 +109,7 @@ func _physics_process(delta):
func carry_begin(collider: Node):
if enable_pickup and is_instance_valid(collider) and collider is RigidBody3D and collider.mass <= max_mass:
carried_body = collider
carried_body_gravity_scale = collider.gravity_scale
var carried_body_mesh: MeshInstance3D = Nodot.get_first_child_of_type(carried_body, MeshInstance3D)
if carried_body_mesh:
var mesh_size = carried_body_mesh.get_aabb().size
Expand All @@ -103,7 +119,15 @@ func carry_begin(collider: Node):

func carry_end():
if is_instance_valid(carried_body):
carried_body.gravity_scale = 1.0
carried_body.gravity_scale = carried_body_gravity_scale
emit_signal("carry_ended", carried_body)
carried_body = null

func throw():
if is_instance_valid(carried_body):
carried_body.gravity_scale = carried_body_gravity_scale

carried_body.apply_force(-global_transform.basis.z * throw_force);
emit_signal("carry_ended", carried_body)
carried_body = null

Expand Down
2 changes: 0 additions & 2 deletions addons/nodot/interaction/RigidCollectable3D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ func _enter_tree():
actual_collectable_root_node = get_node(collectable_root_node)

CollectableManager.add(self)
if collect_on_collision:
connect("character_collided", interact)

func interact(player_node: CharacterBody3D = PlayerManager.node) -> void:
if !enabled or disable_player_collect or !player_node.has_method("collect") or !player_node.collect(self):
Expand Down
35 changes: 29 additions & 6 deletions addons/nodot/kits/FirstPerson/FirstPersonCharacter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ var health: Health
var submerge_handler: CharacterSwim3D
var inventory: CollectableInventory
var was_on_floor: bool = false
# Velocity of the previous frame
var previous_velocity: float = 0.0
# Peak velocity of the last 0.1 seconds
var peak_recent_velocity: Vector3 = Vector3.ZERO
var peak_recent_velocity_timer: float = 0.0
var character_colliders: UniqueSet = UniqueSet.new()
var terminal_velocity := 190.0

func _enter_tree() -> void:
if !sm:
Expand Down Expand Up @@ -67,12 +73,16 @@ func _ready() -> void:
func _physics_process(delta: float) -> void:
if not is_authority(): return

var collision = get_last_slide_collision()
if collision:
for i in collision.get_collision_count():
var collider = collision.get_collider(i)
if collider and collider.has_method("_on_character_collide"):
collider._on_character_collide(self)
peak_recent_velocity_timer += delta
if peak_recent_velocity_timer > 0.1:
peak_recent_velocity_timer = 0.0
else:
var max_velocity = max(abs(velocity.x), abs(velocity.y), abs(velocity.z))
var old_max_velocity = max(abs(peak_recent_velocity.x), abs(peak_recent_velocity.y), abs(peak_recent_velocity.z))
if old_max_velocity < max_velocity:
peak_recent_velocity = cap_velocity(velocity)
else:
peak_recent_velocity = lerp(peak_recent_velocity, Vector3.ZERO, delta)

if !health or fall_damage_multiplier <= 0.0:
return
Expand All @@ -89,6 +99,10 @@ func _physics_process(delta: float) -> void:
previous_velocity = velocity.y
was_on_floor = on_floor

func _is_current_player_changed(new_value: bool):
is_current_player = new_value
input_enabled = new_value

## Add collectables to collectable inventory
func collect(node: Node3D) -> bool:
if not is_host(): return false
Expand All @@ -102,3 +116,12 @@ func set_current_player():
is_current_player = true
PlayerManager.node = self
set_current_camera(camera)

func cap_velocity(velocity: Vector3) -> Vector3:
# Check if the velocity exceeds the terminal velocity
if velocity.length() > terminal_velocity:
# Cap the velocity to the terminal velocity, maintaining direction
return velocity.normalized() * terminal_velocity
else:
# If it's below terminal velocity, return it unchanged
return velocity
18 changes: 16 additions & 2 deletions addons/nodot/kits/Locomotion3D/CharacterClimb3D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class_name CharacterClimb3D extends CharacterExtensionBase3D

## How high the character can climb
@export var climb_velocity := 4.0
## First person mouse input to enable view direction based control (look up to ascend, look down to descend)
@export var first_person_mouse_input: FirstPersonMouseInput

@export_subgroup("Input Actions")
## The input action name for climbing
Expand Down Expand Up @@ -39,15 +41,27 @@ func physics(delta: float):
return

if sm.state == state_ids["climb"]:
var ascend_velocity = climb_velocity
var descend_velocity = -climb_velocity

if first_person_mouse_input:
var view_angle = first_person_mouse_input.head.rotation.x
if view_angle < 0.0:
ascend_velocity = -climb_velocity
descend_velocity = climb_velocity

if Input.is_action_pressed(climb_action):
character.velocity.y = climb_velocity
character.velocity.y = ascend_velocity
elif Input.is_action_pressed(descend_action):
character.velocity.y = -climb_velocity
character.velocity.y = descend_velocity
elif Input.is_action_pressed(jump_action):
sm.set_state(state_ids["idle"])
sm.set_state(state_ids["jump"])
else:
character.velocity.y = 0.0

character.velocity.x = lerp(character.velocity.x, 0.0, delta * 10.0)
character.velocity.z = lerp(character.velocity.z, 0.0, delta * 10.0)

var is_on_floor = character._is_on_floor()
if is_on_floor and was_on_floor == false:
Expand Down
9 changes: 8 additions & 1 deletion addons/nodot/kits/Locomotion3D/CharacterFly3D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ var direction: Vector3 = Vector3.ZERO
var fly_doubletap_timeleft: float = 0.0

func ready():
setup()

func _on_enabled_changed(new_value: bool):
enabled = new_value
setup()

func setup():
if !enabled:
return

InputManager.register_action(left_action, KEY_A)
InputManager.register_action(right_action, KEY_D)
InputManager.register_action(up_action, KEY_W)
Expand Down
2 changes: 2 additions & 0 deletions addons/nodot/kits/Locomotion3D/CharacterMover3D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,5 @@ func move_ground(delta: float) -> void:
if slide_distance > step_distance or !character._is_on_floor():
character.global_position = slide_position
# --- Step up logic ---

character.velocity.y = lerp(character.velocity.y, 0.0, delta * 2.0)
5 changes: 4 additions & 1 deletion addons/nodot/kits/NodotCharacter3D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class_name NodotCharacter3D extends CharacterBody3D
## (optional) The character state machine. If not assigned, is created automatically
@export var sm: StateMachine
## Is the character used by the player
@export var is_current_player: bool = false
@export var is_current_player: bool = false: set = _is_current_player_changed
@export var camera: Camera3D = Camera3D.new()

signal current_camera_changed(old_camera: Camera3D, new_camera: Camera3D)
Expand All @@ -18,6 +18,9 @@ func _is_on_floor() -> bool:
if collision_info.get_angle() > floor_max_angle: return false
if global_position.y - collision_info.get_position().y < 0: return false
return true

func _is_current_player_changed(new_value: bool):
is_current_player = new_value

## Change the active camera
func set_current_camera(camera3d: Camera3D):
Expand Down
53 changes: 22 additions & 31 deletions addons/nodot/scenes/menus/options_menu.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -84,36 +84,30 @@ func update_text():
text = str(VideoManager.screen)
"
[sub_resource type="GDScript" id="GDScript_g05di"]
resource_name = "fpslimit"
script/source = "extends TextEdit


# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.

[sub_resource type="GDScript" id="GDScript_j2da2"]
script/source = "extends MenuButton

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
@onready var popup = get_popup()

var frame_rates = [0, 25, 30, 60, 144, 240, 360]

func _on_text_changed():
var regex = RegEx.new()
regex.compile(\"[0-9]\")

var matches = regex.search_all(text)
var new_text: String = \"\"
if matches:
for r_match in matches:
new_text += r_match.get_string()
text = new_text
set_caret_column(text.length())
if text != \"\":
var fps_limit = int(text)
VideoManager.fps_limit = fps_limit
func _ready():
popup.connect(\"id_pressed\", _on_pressed)
update_text()

func _on_pressed(item_id: int):
match item_id:
0: VideoManager.fps_limit = frame_rates[0]
1: VideoManager.fps_limit = frame_rates[1]
2: VideoManager.fps_limit = frame_rates[2]
3: VideoManager.fps_limit = frame_rates[3]
4: VideoManager.fps_limit = frame_rates[4]
5: VideoManager.fps_limit = frame_rates[5]
6: VideoManager.fps_limit = frame_rates[6]
update_text()

func update_text():
text = popup.get_item_text(frame_rates.find(VideoManager.fps_limit))
"
[sub_resource type="GDScript" id="GDScript_f8lsk"]
Expand Down Expand Up @@ -424,11 +418,9 @@ script = SubResource("GDScript_61kd7")
layout_mode = 2
text = "FPS Limit"
[node name="FPSLimitTextInput" type="TextEdit" parent="Control/MarginContainer/VBoxContainer/VideoOptions/GridContainer"]
[node name="FPSLimitMenuButton" type="MenuButton" parent="Control/MarginContainer/VBoxContainer/VideoOptions/GridContainer"]
layout_mode = 2
text = "0"
scroll_fit_content_height = true
script = SubResource("GDScript_g05di")
script = SubResource("GDScript_j2da2")
[node name="Label4" type="Label" parent="Control/MarginContainer/VBoxContainer/VideoOptions/GridContainer"]
layout_mode = 2
Expand Down Expand Up @@ -616,7 +608,6 @@ text = "Press Any Key..."
[connection signal="tab_selected" from="Control/MarginContainer/VBoxContainer/HBoxContainer/TabBar" to="Control/MarginContainer/VBoxContainer/HBoxContainer/TabBar" method="_on_tab_selected"]
[connection signal="pressed" from="Control/MarginContainer/VBoxContainer/HBoxContainer/CloseButton" to="Control/MarginContainer/VBoxContainer/HBoxContainer/CloseButton" method="_on_pressed"]
[connection signal="text_changed" from="Control/MarginContainer/VBoxContainer/VideoOptions/GridContainer/FPSLimitTextInput" to="Control/MarginContainer/VBoxContainer/VideoOptions/GridContainer/FPSLimitTextInput" method="_on_text_changed"]
[connection signal="pressed" from="Control/MarginContainer/VBoxContainer/VideoOptions/GridContainer/VSyncCheckButton" to="Control/MarginContainer/VBoxContainer/VideoOptions/GridContainer/VSyncCheckButton" method="_on_pressed"]
[connection signal="value_changed" from="Control/MarginContainer/VBoxContainer/VideoOptions/GridContainer/BrightnessHSlider" to="Control/MarginContainer/VBoxContainer/VideoOptions/GridContainer/BrightnessHSlider" method="_on_value_changed"]
[connection signal="value_changed" from="Control/MarginContainer/VBoxContainer/VideoOptions/GridContainer/ContrastHSlider" to="Control/MarginContainer/VBoxContainer/VideoOptions/GridContainer/ContrastHSlider" method="_on_value_changed"]
Expand Down
14 changes: 14 additions & 0 deletions addons/nodot/shaders/vignette.gdshader
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
shader_type canvas_item;
uniform vec2 player_position = vec2(0.5, 0.5);
uniform vec4 color : source_color = vec4(0.305, 0.298, 0.341,1);

uniform float MULTIPLIER = 0.56;
uniform float SCALE = 0.5;
uniform float SOFTNESS = 0.45;

void fragment(){
float val = distance(vec2(UV.x , UV.y * MULTIPLIER), vec2(player_position.x , player_position.y * MULTIPLIER));
val = val / SCALE;
float vignette = smoothstep(0.2, SOFTNESS, val);
COLOR = vec4(color.rgb , vignette);
}
5 changes: 4 additions & 1 deletion addons/nodot/utility/StateHandler.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class_name StateHandler extends Nodot

## Enable/disable this node.
@export var enabled : bool = true
@export var enabled : bool = true: set = _on_enabled_changed
## The StateMachine to attach this handler to
@export var sm: StateMachine
## Run process/physics even when the state is unhandled
Expand Down Expand Up @@ -37,6 +37,9 @@ func _process(delta):

func _input(event: InputEvent) -> void:
input(event)

func _on_enabled_changed(new_value: bool):
enabled = new_value

## Registers a set of states that the node handles
func register_handled_states(new_states: Array[String]):
Expand Down

0 comments on commit 13b4ae9

Please sign in to comment.