completely rewrite all this for me so it fits:
func _physics_process(delta):
apply_gravity(delta)
var input_vector = Input.get_axis("move_left","move_right")
if input_vector != 0:
apply_acceleration(input_vector, delta)
else:
apply_friction(delta)
if Input.is_action_just_pressed("SwapPlane"):
_on_swapPlane()
if Input.is_action_just_pressed("jump") and is_on_floor():
jump()
if Input.is_action_just_pressed("shoot"):
animator.play("Attacking")
if bullets_amount:
guns_animator.play("Shoot")
move_and_slide()
animate(input_vector)
func apply_acceleration(input_vector, delta):
velocity.x = move_toward(velocity.x, movement_data.max_speed * input_vector, movement_data.acceleration * delta)
func apply_friction(delta):
velocity.x = move_toward(velocity.x, 0, movement_data.friction * delta)
func apply_gravity(delta):
if not is_on_floor():
velocity.y += movement_data.gravity * movement_data.gravity_scale * delta
func knockback(vector):
velocity = velocity.move_toward(vector * movement_data.knockback_force, movement_data.acceleration)
func jump():
velocity.y = -movement_data.jump_strength
AudioManager.play_sound(AudioManager.JUMP)
func shoot():
state = State.ATTACK
attack_animation = "Attack" + str(randi_range(1, 2))
animator.play(attack_animation)
$Hand/AttackArea/CollisionShape2D.disabled = false;
await animator.animation_looped
EventManager.update_bullet_ui.emit()
var mouse_position : Vector2 = (get_global_mouse_position() - global_position).normalized()
var muzzle = muzzle_load.instantiate()
var bullet = bullet_load.instantiate()
pistol_bullet_marker.add_child(muzzle)
bullet.global_position = pistol_bullet_marker.global_position
bullet.target_vector = mouse_position
bullet.rotation = mouse_position.angle()
get_tree().current_scene.add_child(bullet)
AudioManager.play_sound(AudioManager.SHOOT)
$Hand/AttackArea/CollisionShape2D.disabled = true;
state = State.IDLE
func animate(input_vector):
var mouse_position : Vector2 = (get_global_mouse_position() - global_position).normalized()
if mouse_position.x > 0 and animator.flip_h:
animator.flip_h = false
elif mouse_position.x < 0 and not animator.flip_h:
animator.flip_h = true
hand.rotation = mouse_position.angle()
if hand.scale.y == 1 and mouse_position.x < 0:
hand.scale.y = -1
elif hand.scale.y == -1 and mouse_position.x > 0:
hand.scale.y = 1
if state != State.ATTACK:
if is_on_floor():
if input_vector != 0:
animator.play("Run")
else:
animator.play("Idle")
else:
if velocity.y > 0:
animator.play("Jump")
# Check if the current animation is the attack animation
func _on_hurtbox_area_entered(_area):
if not _area.is_in_group("Bullet"):
hit_animator.play("Hit")
EventManager.update_health_ui.emit()
if stats.health <= 0:
die()
else:
AudioManager.play_sound(AudioManager.HURT)
EventManager.frame_freeze.emit()
func _on_swapPlane():
PlayerState.set_last_2d_position(global_position);
get_tree().change_scene_to_file("res://demo/Scenes/main_scene.tscn")
func die():
AudioManager.play_sound(AudioManager.DEATH)
animator.play("Died")
var death_particle = death_particle_load.instantiate()
death_particle.global_position = global_position
get_tree().current_scene.add_child(death_particle)
EventManager.player_died.emit()
queue_free()