fff

PHOTO EMBED

Fri May 17 2024 04:41:00 GMT+0000 (Coordinated Universal Time)

Saved by @azariel #glsl

extends CharacterBody3D

# Enum for weapon states


enum WeaponState {
	UNEQUIPPED,
	EQUIPPED
}
var current_state = WeaponState.UNEQUIPPED
@onready var mesh_root = $MeshRoot

@export var S_H = 0.1
@export var S_v = 0.1
@onready var animation_player = $MeshRoot/AuxScene/AnimationPlayer
@onready var C_M = $Node3D
@onready var weapon = $MeshRoot/AuxScene/ALS_AnimMan_CharacterBP/Skeleton3D/BoneAttachment3D2/Sketchfab_Scene


var SPEED = 2
const JUMP_VELOCITY = 4.5
var running = false
var aiming = false

var running_speed = 5
var walking_speed = 2

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")

func _ready():
	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
	weapon.hide()  # Hide the weapon initially

func _input(event):
	if event is InputEventMouseMotion:
		rotate_y(deg_to_rad(-event.relative.x * S_H))
		C_M.rotate_x(deg_to_rad(-event.relative.y * S_v))

	if event.is_action_pressed("equip_weapon"): 
		if current_state == WeaponState.UNEQUIPPED:
			equip_weapon()
		elif current_state == WeaponState.EQUIPPED:
			unequip_weapon()

	if event.is_action_pressed("aim_weapon") and current_state == WeaponState.EQUIPPED:
		start_aiming()
	elif event.is_action_released("aim_weapon") and current_state == WeaponState.EQUIPPED:
		stop_aiming()

	if event.is_action_pressed("fire_weapon") and current_state == WeaponState.EQUIPPED and aiming:
		fire_weapon()

func _physics_process(delta):
	if Input.is_action_pressed("run"):
		SPEED = running_speed
		running = true
	else:
		SPEED = walking_speed
		running = false

	# Add the gravity.
	if not is_on_floor():
		velocity.y -= gravity * delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
	var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
		if running:
			if current_state == WeaponState.EQUIPPED:
				if animation_player.current_animation != "gun_running":
					animation_player.play("RifleRun(2)0")
			else:
				if animation_player.current_animation != "running":
					animation_player.play("Running(2)0")
		else:
			if current_state == WeaponState.EQUIPPED:
				if animation_player.current_animation != "gun_walking":
					animation_player.play("RifleWalk(1)0")
			else:
				if animation_player.current_animation != "walking":
					animation_player.play("WalkWithBriefcase0")
					
					
		mesh_root.look_at(position + direction)
						
		velocity.x = direction.x * SPEED
		velocity.z = direction.z * SPEED
	else:
		if current_state == WeaponState.EQUIPPED:
			if aiming:
				if animation_player.current_animation != "gun_aiming":
					animation_player.play("aim")
			else:
				if animation_player.current_animation != "gun_idle":
					animation_player.play("RifleIdle(1)0")
		else:
			if animation_player.current_animation != "idle":
				animation_player.play("Idle(3)0")
		velocity.x = move_toward(velocity.x, 0, SPEED)
		velocity.z = move_toward(velocity.z, 0, SPEED)

	move_and_slide()

# Function to equip the weapon
func equip_weapon():
	current_state = WeaponState.EQUIPPED
	animation_player.play("RiflePullOut(1)0")  # Play equip animation
	weapon.show()  # Make weapon visible

# Function to unequip the weapon
func unequip_weapon():
	current_state = WeaponState.UNEQUIPPED
	animation_player.play("RiflePutAway(1)0")  # Play unequip animation
	weapon.hide()  # Hide weapon after unequip

# Function to start aiming
func start_aiming():
	aiming = true
	animation_player.play("aim")  # Play aiming animation

# Function to stop aiming
func stop_aiming():
	aiming = false
	animation_player.play("gun_idle")  # Play idle animation with gun

# Function to handle firing the weapon
func fire_weapon():
	animation_player.play("FiringRifle(2)0")  # Play fire animation

# Optionally, handle animation finished signal to perform actions after animation ends
func _on_AnimationPlayer_animation_finished(anim_name):
	if anim_name == "unequip":
		weapon.hide()
	elif anim_name == "equip":
		weapon.show()
	elif anim_name == "gun_fire":
		if aiming:
			animation_player.play("aim")  # Return to aiming animation after firing
		else:
			animation_player.play("RifleIdle(1)0")
content_copyCOPY