khanat-client/scenes/player/player.gd

224 lines
8.6 KiB
GDScript

extends Spatial
signal new_focused_object( p_name )
signal equip( p_slot, p_item )
signal unequip( p_slot )
var mouse_delta = Vector2.ZERO
var zoom_level = 0
var camera_position = Vector3( 0.0, 2.0, -2.0 )
onready var camera_tps = $character/spring_arm/camera_tps
onready var camera_fps = $character/spring_arm/camera_fps
var proximity_objects = []
var focused_object = null
var focus_index = 0
static func get_input_direction() -> Vector3:
return Vector3(
Input.get_action_strength("move_strafe_right") - Input.get_action_strength("move_strafe_left"),
0,
Input.get_action_strength("move_backward") - Input.get_action_strength("move_forward")
)
func _input( event ):
# Run.
if event.is_action_pressed( "move_toggle_run" ):
$character.is_running = not $character.is_running
# Camera.
if event.is_action( "camera_zoom_in" ):
self.zoom_level += 1
elif event.is_action( "camera_zoom_out" ):
self.zoom_level -= 1
if event.is_action_pressed( "camera_reset_view" ):
$character/spring_arm.rotation = Vector3.ZERO
self.zoom_level = 0
if event.is_action_pressed( "camera_switch" ):
if self.camera_tps.current:
self.camera_fps.make_current()
$character/spring_arm.rotation = Vector3.ZERO
# self.hide()
else:
self.camera_tps.make_current()
# self.show()
if event is InputEventMouseMotion:
self.mouse_delta = event.relative
# Jump.
if event.is_action_pressed( "move_jump" ) and not $character.is_falling:
$character.is_jump_started = true
if event.is_action_pressed( "interact" ):
self.interact()
if event.is_action_pressed( "focus_next" ):
self.focus_next()
# if not self.get_tree().get_root().get_node( "main/scene/game" ).is_on_ui:
# if event.is_action_pressed( "action_ready_weapon" ):
# $character.is_readying_weapon = true
# if event.is_action_released( "action_ready_weapon" ):
# $character.is_readying_weapon = false
# $character.is_weapon_ready = false
# if $character.is_weapon_ready:
# if event.is_action_pressed( "action_attack" ):
# $character.attack()
func _process( delta ):
$character.direction = self.get_input_direction()
# Rotation.
if not Input.is_key_pressed( KEY_SHIFT ) and not $character.is_jump_started:
$character.orientation = (Input.get_action_strength("move_turn_left") - Input.get_action_strength("move_turn_right"))
else:
$character.orientation = 0.0
# Camera.
if not self.mouse_delta == Vector2.ZERO and self.camera_tps.current:
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
$character/spring_arm.rotate_y( -self.mouse_delta.x * 0.01 )
self.camera_tps.rotate_x( self.mouse_delta.y * 0.01 )
# if not $character.velocity.x == 0.0 or not $character.velocity.z == 0.0:
# var velocity_without_gravity = $character.velocity
# velocity_without_gravity.y = 0.0
## $crosshair.material.set_shader_param( "spread", velocity_without_gravity.length() )
# $character/target_spring_arm/crosshair.scale = Vector3( 1.0, 1.0, 1.0 ) * velocity_without_gravity.length()
# else:
## $crosshair.material.set_shader_param( "spread", 1 )
# $character/target_spring_arm/crosshair.scale = Vector3( 1.0, 1.0, 1.0 )
if $character.is_weapon_ready:
if $character.get_main_weapon_node():
$character/crosshair.show()
# $character/target_spring_arm.global_transform.origin = $character.get_main_weapon_node().get_node( "bullet_exit" ).global_transform.origin
# $character/target_spring_arm.look_at_from_position( $character.get_main_weapon_node().get_node( "bullet_exit" ).global_transform.origin, $character.get_main_weapon_node().get_node( "target" ).global_transform.origin, Vector3( 0.0, 0.0, -1.0 ) )
# $character/target_spring_arm.global_transform.basis.y = $character.get_main_weapon_node().get_node( "bullet_exit" ).global_transform.basis.y
$character/crosshair.global_transform.origin = $character.get_main_weapon_node().get_node( "bullet_exit/target_spring_arm/target" ).global_transform.origin
else:
$character/target_spring_arm.hide()
# Values reset.
self.mouse_delta = Vector2.ZERO
# Debug.
#var old_collider = null
func _physics_process(delta):
var space_state = get_world().direct_space_state
# Camera.
self.camera_position = Vector3( 0.0, 2 - 0.1*self.zoom_level, -2 + 0.1*self.zoom_level )
$character/spring_arm/camera_position.translation = self.camera_position
var global_camera_position = $character/spring_arm/camera_position.global_transform.origin
var result = space_state.intersect_ray($character/look_at.global_transform.origin, global_camera_position, [self])
if result:
self.camera_tps.global_transform.origin = result.position
# self.camera_tps.look_at( $character/look_at.global_transform.origin, Vector3( 0.0, 1.0, 0.0) )
else:
self.camera_tps.translation = self.camera_position
# self.camera_tps.look_at( $character/look_at.global_transform.origin, Vector3( 0.0, 1.0, 0.0) )
func interact( p_object = null ):
if p_object and p_object.has_method( "interact" ):
p_object.interact( $character/creature.creature )
else:
if self.focused_object and self.focused_object.has_method( "interact" ):
self.focused_object.interact( $character/creature.creature )
elif self.focused_object and self.focused_object.get_parent() and self.focused_object.get_parent().has_method( "interact" ):
self.focused_object.get_parent().interact( $character/creature.creature )
if not self.focused_object:
self.proximity_objects.erase( self.focused_object )
self.update_focused_object()
func focus_next():
self.focus_index += 1
if self.focus_index >= self.proximity_objects.size():
self.focus_index = 0
self.update_focused_object()
func set_focus_on( p_object, p_focus ):
if p_object and p_object.has_method( "set_focus" ):
p_object.set_focus( p_focus )
elif p_object and p_object.get_parent() and p_object.get_parent().has_method( "set_focus" ):
p_object.get_parent().set_focus( p_focus )
func update_focused_object():
if self.focus_index >= self.proximity_objects.size():
self.focus_index = 0
var old_focused_object = self.focused_object
if self.proximity_objects.size() > 0:
self.focused_object = self.proximity_objects[ self.focus_index ]
else:
self.focused_object = null
if old_focused_object:
self.set_focus_on( old_focused_object, false )
if self.focused_object:
self.set_focus_on( self.focused_object, true )
emit_signal( "new_focused_object", self.focused_object.name )
func _on_interact_area_body_entered(body):
if not body == $character:
self.proximity_objects.push_back( body )
self.update_focused_object()
func _on_interact_area_body_exited(body):
if not body == $character:
self.proximity_objects.erase( body )
self.update_focused_object()
func _on_character_equip(p_slot, p_item):
emit_signal( "equip", p_slot, p_item )
var weapons = $character.get_weapons()
if weapons.size() > 0:
var min_range = null
for weapon in weapons:
if not min_range:
min_range = weapon.get_data( "range" )
else:
min_range = min( min_range, weapon.get_data( "range" ) )
$character.get_main_weapon_node().get_node( "bullet_exit/target_spring_arm" ).spring_length = min_range
func _on_character_unequip(p_slot):
emit_signal( "unequip", p_slot )
func _on_crosshair_area_body_entered(body):
if not body == $character:
if body is preload( "res://scenes/characters/character.gd" ):
if body.player_relation == body.PLAYER_RELATION.ennemy:
$character/crosshair.get_surface_material( 0 ).albedo_color = Color.red
elif body.player_relation == body.PLAYER_RELATION.friend:
$character/crosshair.get_surface_material( 0 ).albedo_color = Color.green
else:
$character/crosshair.get_surface_material( 0 ).albedo_color = Color.white
func _on_crosshair_area_body_exited(body):
if not body == $character:
$character/target_spring_arm/crosshair.get_surface_material( 0 ).albedo_color = Color.white