Skip to content

Commit

Permalink
various tweaks, fixes, planegate3d
Browse files Browse the repository at this point in the history
  • Loading branch information
krazyjakee committed Apr 2, 2024
1 parent f2c9f08 commit d7896d8
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 139 deletions.
11 changes: 11 additions & 0 deletions addons/nodot/autoload/SaveManager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ func get_special_id(input_node: Node):
func set_value(key: String, value: Variant):
custom_values[key] = value

## Get a custom value
func get_value(key: String):
if custom_values.has(key):
return custom_values[key]
else:
return null

## Remove a custom value
func remove_value(key: String):
custom_values.erase(key)

## Resets the save data to default
func reset():
savers = []
Expand Down
68 changes: 68 additions & 0 deletions addons/nodot/debug/DebugArrowMesh.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
@tool
class_name DebugArrowMesh extends MeshInstance3D

@export var size: float = 1
@export var fat: float = 1
@export var color: Color = Color.WHITE_SMOKE

var arrowmesh: MeshInstance3D

# Called when the node enters the scene tree for the first time.
func _ready():
arrowmesh = fancyArrow( size , color )
add_child( arrowmesh )

# Create a material based on the color ( you can put transparency )
func getArrowMaterial() -> Material:
var material := StandardMaterial3D.new()
material.set_specular(0.5)
material.set_metallic(0.5)
material.set_transparency( material.TRANSPARENCY_ALPHA )
material.set_albedo(color)
return material

# convert blender coordenates to godot cords
# observe that to get the correct face : get the triangle main point, and go ant-hour
func vb(x:float,y:float,z:float) -> Vector3:
return Vector3(x,z,y)

# add a strip triangle with material to the mesh
func addStrip( m: ImmediateMesh, v1: Vector3, v2: Vector3, v3: Vector3 , mat: Material ):
m.surface_begin(Mesh.PRIMITIVE_TRIANGLE_STRIP, mat)
m.surface_add_vertex(v1)
m.surface_add_vertex(v2)
m.surface_add_vertex(v3)
m.surface_end()

# draw the fancy arrow
func fancyArrow( size: float, color = Color.RED) -> MeshInstance3D:
var mesh_instance := MeshInstance3D.new()
var immediate_mesh := ImmediateMesh.new()
var material := getArrowMaterial()

mesh_instance.mesh = immediate_mesh
mesh_instance.set_cast_shadows_setting( mesh_instance.SHADOW_CASTING_SETTING_OFF )

addStrip(immediate_mesh,vb(0,0,0),vb(0.05,-1.9,-0.025),vb(-0.05,-1.9,-0.025),material)
addStrip(immediate_mesh,vb(0,0,0),vb(-0.05,-1.9,-0.025),vb(-0.05,-1.9,0.025),material)
addStrip(immediate_mesh,vb(0,0,0),vb(-0.05,-1.9,0.025),vb(0.05,-1.9,0.025),material)
addStrip(immediate_mesh,vb(0,0,0),vb(0.05,-1.9,0.025),vb(0.05,-1.9,-0.025),material)

addStrip(immediate_mesh,vb(0.1,-1.9,0),vb(0.05,-1.9,-0.025),vb(0.05,-1.9,0.025),material)
addStrip(immediate_mesh,vb(-0.1,-1.9,0),vb(-0.05,-1.9,0.025),vb(-0.05,-1.9,-0.025),material)

addStrip(immediate_mesh,vb(0,-2,0),vb(-0.1,-1.9,0),vb(-0.05,-1.9,-0.025),material)
addStrip(immediate_mesh,vb(0,-2,0),vb(-0.05,-1.9,-0.025),vb(0.05,-1.9,-0.025),material)
addStrip(immediate_mesh,vb(0,-2,0),vb(0.05,-1.9,-0.025),vb(0.1,-1.9,0),material)

addStrip(immediate_mesh,vb(0,-2,0),vb(-0.05,-1.9,0.025),vb(-0.1,-1.9,0),material)
addStrip(immediate_mesh,vb(0,-2,0),vb(0.05,-1.9,0.025),vb(-0.05,-1.9,0.025),material)
addStrip(immediate_mesh,vb(0,-2,0),vb(0.1,-1.9,0),vb(0.05,-1.9,0.025),material)

material.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
material.albedo_color = color

mesh_instance.transform.origin = Vector3(0,0,0)
set_scale( Vector3(fat,fat,size/2) )

return mesh_instance
100 changes: 100 additions & 0 deletions addons/nodot/detection/PlaneGate3D.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
@tool
class_name PlaneGate3D extends Area3D

signal gate_entered(body: Node)
signal gate_exited(body: Node)

@export var size: Vector2 = Vector2(100, 50): set = set_size
@export var tolerence: float = 1.0: set = set_tolerance
@export_category("Debugging")
@export var show_debug_shape: bool = true: set = _set_show_debug_shape

var is_editor: bool = Engine.is_editor_hint()
var collider: CollisionShape3D = CollisionShape3D.new()
var debug_mesh := MeshInstance3D.new()
var debug_arrow := DebugArrowMesh.new()
var debug_material := StandardMaterial3D.new()

func _init():
create_nodes()

func _ready():
set_size(size)
connect("body_exited", _on_body_exited)

if is_editor:
add_child(debug_mesh)
add_child(debug_arrow)
debug_material.cull_mode = BaseMaterial3D.CULL_DISABLED
debug_material.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
debug_material.albedo_color = Color(Color.RED, 0.5)
debug_material.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
debug_draw()

func _on_body_exited(body: Node):
var direction = (body.transform.origin - transform.origin).normalized()
var forward_vector = -transform.basis.z.normalized()
var dot = direction.dot(forward_vector)
if dot > 0:
emit_signal("gate_entered", body)
else:
emit_signal("gate_exited", body)

func _set_show_debug_shape(new_value: bool):
show_debug_shape = new_value
debug_draw()

func debug_draw():
debug_mesh.mesh = null
if !show_debug_shape: return
var immediate_mesh = ImmediateMesh.new()
immediate_mesh.surface_begin(Mesh.PRIMITIVE_TRIANGLES, debug_material)

var x = size.x / 2
var y = size.y / 2
# Define the vertices of the plane
var v1 = Vector3(-x, -y, 0.0) # Bottom left
var v2 = Vector3(x, -y, 0.0) # Bottom right
var v3 = Vector3(x, y, 0.0) # Top right
var v4 = Vector3(-x, y, 0.0) # Top left

# Normal pointing up
var normal = Vector3(0.0, 1.0, 0.0)

# Draw the first triangle (v1, v2, v3)
immediate_mesh.surface_add_vertex(v1)
immediate_mesh.surface_set_normal(normal)
immediate_mesh.surface_add_vertex(v2)
immediate_mesh.surface_set_normal(normal)
immediate_mesh.surface_add_vertex(v3)
immediate_mesh.surface_set_normal(normal)

# Draw the second triangle (v1, v3, v4)
immediate_mesh.surface_add_vertex(v1)
immediate_mesh.surface_set_normal(normal)
immediate_mesh.surface_add_vertex(v3)
immediate_mesh.surface_set_normal(normal)
immediate_mesh.surface_add_vertex(v4)
immediate_mesh.surface_set_normal(normal)

# End drawing
immediate_mesh.surface_end()
debug_mesh.mesh = immediate_mesh

func set_tolerance(new_tolerance: float):
tolerence = new_tolerance
set_size(size)

func set_size(new_size: Vector2):
size = new_size
if collider:
collider.shape.size = Vector3(size.x, size.y, tolerence)
if is_editor:
debug_draw()
else:
create_nodes()

func create_nodes():
collider.shape = BoxShape3D.new()
add_child(collider)
set_size(size)
36 changes: 19 additions & 17 deletions addons/nodot/interaction/Interaction3D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,22 @@ signal interacted(interacted_node: Node3D, collision_point: Vector3, collision_n
@export var throw_force: float = 250.0;
## The Close Carry body position Node
@export var carry_position_node: Node3D;
## Carry collision layer
@export_flags_3d_physics var carry_collision_layer: int = 1
## Carry collision mask
@export_flags_3d_physics var carry_collision_mask: int = 1


# 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 is_close_body_carry: bool = false;
var label3d: Label3D
var last_collider: Node3D
var last_focussed_collider: Node3D;
var carried_body_prev_mask: int = 1;
var carried_body_prev_layer: int = 1;
var carried_body_physics_material: PhysicsMaterial

func _enter_tree():
label3d = Label3D.new()
Expand Down Expand Up @@ -82,6 +87,7 @@ func _physics_process(delta):
if is_instance_valid(carried_body):
if not multiplayer.is_server(): return
if not is_close_body_carry:
var point = get_collision_point()
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:
Expand All @@ -94,13 +100,10 @@ func _physics_process(delta):
carried_body.linear_velocity = carried_body.global_transform.origin.direction_to(carry_position) * speed * delta
else:
var carry_position = carry_position_node.global_position;
carried_body.set_collision_layer_value(1, false);
carried_body.global_position = carry_position;
if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT):
var prev_carried_body = carried_body;
throw();
await get_tree().create_timer(0.35).timeout;
prev_carried_body.set_collision_layer_value(1, carried_body_prev_mask);
return;
var rotate_speed: float = 10.0 * delta
carried_body.global_rotation.x = lerp_angle(carried_body.global_rotation.x, 0.0, rotate_speed)
Expand All @@ -116,8 +119,8 @@ func _physics_process(delta):
last_focussed_collider = collider;
collider.focussed();
else:
if last_focussed_collider and last_focussed_collider.has_method("unfocussed"):
last_focussed_collider.unfocussed();
if is_instance_valid(last_focussed_collider) and last_focussed_collider.has_method("unfocussed"):
last_focussed_collider.unfocussed();
else:
if not is_multiplayer_authority(): return

Expand All @@ -142,35 +145,34 @@ 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
is_close_body_carry = carried_body.has_meta("carry_close") and carried_body.get_meta("carry_close")
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
carried_body_width = max(mesh_size.x * carried_body_mesh.scale.x, mesh_size.y * carried_body_mesh.scale.y, mesh_size.z * carried_body_mesh.scale.z)
carried_body.gravity_scale = 0.0
carried_body_prev_mask = carried_body.get_collision_mask_value(1);
carried_body_prev_layer = carried_body.collision_layer
carried_body_prev_mask = carried_body.collision_mask
carried_body_physics_material = carried_body.physics_material_override
carried_body.physics_material_override = null
carried_body.collision_layer = carry_collision_layer
carried_body.collision_mask = carry_collision_mask
emit_signal("carry_started", carried_body)
GlobalSignal.trigger_signal("carry_started", carried_body);


func carry_end():
if is_instance_valid(carried_body):
if is_close_body_carry:
carried_body.set_collision_layer_value(1, carried_body_prev_mask);
carried_body.gravity_scale = carried_body_gravity_scale
carried_body.collision_layer = carried_body_prev_layer
carried_body.collision_mask = carried_body_prev_mask
carried_body.physics_material_override = carried_body_physics_material
emit_signal("carry_ended", carried_body)
GlobalSignal.trigger_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);
GlobalSignal.trigger_signal("carry_ended", carried_body);
carried_body = null

carry_end()

func collide_ended(body: Node3D):
label3d.text = ""
Expand Down
14 changes: 2 additions & 12 deletions addons/nodot/kits/FirstPerson/FirstPersonCharacter.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ class_name FirstPersonCharacter extends NodotCharacter3D
@export var input_enabled := true
## The camera field of view
@export var fov := 75.0
## The head position
@export var head_position := Vector3.ZERO
## Minimum amount of fall damage before it is actually applied
@export var minimum_fall_damage: float = 5.0
## The amount of fall damage to inflict when hitting the ground at velocity (0 for disabled)
Expand Down Expand Up @@ -55,14 +53,6 @@ func _ready() -> void:
if has_node("Head"):
head = get_node("Head")
camera = get_node("Head/Camera3D")

if has_node("HeadPosition"):
var head_position_node: Node = get_node("HeadPosition")
head.position = head_position_node.position
head_position = head.position
head_position_node.queue_free()
else:
head.position = head_position

if is_authority() and is_current_player:
set_current_player()
Expand All @@ -86,7 +76,7 @@ func _physics_process(delta: float) -> void:

if !health or fall_damage_multiplier <= 0.0:
return

var on_floor = _is_on_floor()
if !was_on_floor:
if on_floor:
Expand All @@ -97,7 +87,7 @@ func _physics_process(delta: float) -> void:
emit_signal("fall_damage", damage)
else:
previous_velocity = velocity.y
was_on_floor = on_floor != null
was_on_floor = on_floor

func _is_current_player_changed(new_value: bool):
is_current_player = new_value
Expand Down
13 changes: 13 additions & 0 deletions addons/nodot/kits/FirstPerson/FirstPersonItemsContainer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ func change_item(new_index: int) -> void:
if active_item_index < item_count:
emit_signal("item_change", items[active_item_index])

func activate_current_item():
is_item_active = true;
var items: Array = get_all_items();
if GameManager.has_item(active_item_index):
await (items[active_item_index] as FirstPersonItem).activate();

func deactivate_current_item():
is_item_active = false;
var items: Array = get_all_items();
var item_count: int = items.size();
if active_item_index < item_count:
await (items[active_item_index] as FirstPersonItem).deactivate();

func unlock_item(item_index: int):
var items: Array = get_all_items()
if items.size() <= item_index: return
Expand Down
12 changes: 9 additions & 3 deletions addons/nodot/kits/Locomotion3D/CharacterSwim3D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,15 @@ func swim(delta: float) -> void:

var new_y_velocity = clamp(character.velocity.y - submerged_gravity * delta, -3.0, 3.0)
character.velocity.y = lerp(character.velocity.y, new_y_velocity, 0.025)
var jump_pressed: bool = Input.is_action_pressed(ascend_action)
if jump_pressed:
character.velocity.y = lerp(character.velocity.y, submerge_speed, delta)
var ascend_pressed: bool = Input.is_action_pressed(ascend_action)
var descend_pressed: bool = Input.is_action_pressed(descend_action)
if ascend_pressed:
if is_head_submerged:
character.velocity.y = lerp(character.velocity.y, submerge_speed, delta)
else:
character.velocity.y = lerp(character.velocity.y, submerge_speed*7, delta)
elif descend_pressed:
character.velocity.y = lerp(character.velocity.y, -submerge_speed, delta)

if direction == Vector3.ZERO:
character.velocity.x = move_toward(character.velocity.x, 0, 0.1)
Expand Down
Loading

0 comments on commit d7896d8

Please sign in to comment.