Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.2 kB
2
Indexable
Never
class_name Character
extends CharacterBody3D


@export var speed = 5
@export var gravity = -5

var target = Vector3.ZERO
var clickPosition: Vector3 = Vector3.ZERO

#Character Select functionality
var unitSelect = {
	$".": true,
	$"../Character2": true,
	$"../Character3": true,
}


func _ready():
	#get_children()
	toggle_outline()
	#obj->connect("enter_tree", this, "_node_entered_tree")
	#_on_StaticBody_input_event(camera, event, click_position, click_normal, shape_idx)

#Movement
func _physics_process(delta):
	#calculating movement when signal is emitted
	for character_name in unitSelect.keys():
		if unitSelect[character_name]:
			var character_path = get_character_path(character_name)
			if character_path:
				velocity.y += gravity * delta
				if target:
					look_at(target, Vector3.UP)
					rotation.x = 0
					velocity = -transform.basis.z * speed
				if transform.origin.distance_to(target) < .5:
					target = Vector3.ZERO
					velocity = Vector3.ZERO
	move_and_slide()

#signal for position and movement from StaticBody3D
func _on_static_body_3d_input_event(camera, event, position, normal, shape_idx):
		if event is InputEventMouseButton:
			if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
				for character_name in unitSelect.keys():
					if unitSelect[character_name]:
						var character_path = get_character_path(character_name)
						if character_path:
							character_path.target = position
					#$".".target = position
					#$"../Character2".target = position
					#$"../Character3".target = position
					#$Marker.transform.origin = position
				
#Selection Logic
func _unhandled_input(event):
#Selection handling
	if event.is_action_pressed("select_all"):
		toggle_select_all()
		toggle_outline()
		
	if event.is_action_pressed("select_first"):
		toggle_select("Character")
		
	if event.is_action_pressed("select_second"):
		toggle_select("Character2")
		
	if event.is_action_pressed("select_third"):
		toggle_select("Character3")
		
func toggle_select_all():
	if is_selection_full():
		clear_selection()
	else:
		select_all()
		
func is_selection_full():
	for selected in unitSelect.values():
		if not selected:return false
	return true
	#return unitSelect["Character"] && unitSelect["Character2"] && unitSelect["Character3"]
	
func clear_selection():
	for character in unitSelect.keys():
		unitSelect[character] = false
		toggle_outline()
		
func select_all():
	for character in unitSelect.keys():
		unitSelect[character] = true
		toggle_outline()
		
func toggle_select(character_name):
	unitSelect[character_name] = not unitSelect[character_name]
	toggle_outline()
	
func toggle_outline():
		for character_name in unitSelect.keys():
			var character_path = get_character_path(character_name)
			#var mesh = $UnitSelect/CapsuleMesh/OutlineMesh #characterMesh[character]
			#var mesh = get_node(mesh_path)
			
			
			if character_path:
				var mesh = character_path.find_node("OutlineMesh")
				if mesh:
					mesh.visible = unitSelect[character_name]

func get_character_path(character_name):
	match character_name:
		"Character":
			return "Character"
		"Character2":
			return "Character2"
		"Character3":
			return "Character3"
	return ""
Leave a Comment