khanat-client/scenes/player/player.gd

107 lines
4.5 KiB
GDScript

extends "res://ressources/scripts/entity.gd"
onready var player = $model/ra
onready var camera = $model/ra/spring_arm
onready var skin = $model/ra/model
export var max_speed: = 6.0
export var move_speed: = 5.0
export var gravity = -100.0
export var jump_impulse = 25
export(float, 0.1, 20.0, 0.1) var rotation_speed_factor: = 0.01
var velocity: = Vector3.ZERO
func _ready():
# $camera.make_current()
$model/ra/spring_arm/camera.make_current()
func _process( delta ):
var input_direction: = self.get_input_direction()
# Calculate a move direction vector relative to the camera
# The basis stores the (right, up, -forwards) vectors of our camera.
var forwards: Vector3 = self.camera.global_transform.basis.z * input_direction.z
var right: Vector3 = self.camera.global_transform.basis.x * input_direction.x
var move_direction: = forwards + right
if move_direction.length() > 1.0:
move_direction = move_direction.normalized()
move_direction.y = 0
# skin.move_direction = move_direction
# Rotation
# if move_direction:
# var target_direction = player.transform.looking_at(player.global_transform.origin + move_direction, Vector3.UP)
# player.transform = player.transform.interpolate_with(target_direction, rotation_speed_factor * delta)
player.rotate_y( rotation_speed_factor * (Input.get_action_strength("turn_left") - Input.get_action_strength("turn_right")) )
# Movement
velocity = self.calculate_velocity(velocity, move_direction, delta)
velocity = player.move_and_slide(velocity, Vector3.UP, true)
if move_direction:
$model/ra/model/AnimationPlayer.play( "walk" )
else:
$model/ra/model/AnimationPlayer.play( "idle" )
static func get_input_direction() -> Vector3:
return Vector3(
Input.get_action_strength("move_right") - Input.get_action_strength("move_left"),
0,
Input.get_action_strength("move_backward") - Input.get_action_strength("move_forward")
)
func calculate_velocity(
velocity_current: Vector3,
move_direction: Vector3,
delta: float
) -> Vector3:
var velocity_new := move_direction * move_speed
if velocity_new.length() > max_speed:
velocity_new = velocity_new.normalized() * max_speed
velocity_new.y = velocity_current.y + gravity * delta
return velocity_new
func load_creature( filename ):
self.creature = Creatures.Ra.new()
self.creature.from_file( filename )
# # version statique.
# $model/ra/model/body.set( "blend_shapes/Boobs", self.creature.female_boobs )
# $model/ra/model/body.set( "blend_shapes/Female_hip", self.creature.female_hip )
# $model/ra/model/body.set( "blend_shapes/Male_Pack", self.creature.male_pack )
# $model/ra/model/body.set( "blend_shapes/Male_Throat", self.creature.male_throat )
# $model/ra/model/body.set( "blend_shapes/Pregnant", self.creature.female_pregnant )
# $model/ra/model/body.set( "blend_shapes/Pregnant", self.creature.female_pregnant )
# $model/ra/model/body.get_surface_material( 0 ).set_shader_param( "albedo", self.creature.color )
#
# Version animée.
$model/ra/model/metarig/Skeleton/body.set( "blend_shapes/Boobs", self.creature.female_boobs )
$model/ra/model/metarig/Skeleton/body.set( "blend_shapes/Female_hip", self.creature.female_hip )
$model/ra/model/metarig/Skeleton/body.set( "blend_shapes/Male_Pack", self.creature.male_pack )
$model/ra/model/metarig/Skeleton/body.set( "blend_shapes/Male_Throat", self.creature.male_throat )
$model/ra/model/metarig/Skeleton/body.set( "blend_shapes/Pregnant", self.creature.female_pregnant )
$model/ra/model/metarig/Skeleton/body.set( "blend_shapes/Pregnant", self.creature.female_pregnant )
$model/ra/model/metarig/Skeleton/body.get_surface_material( 0 ).set_shader_param( "albedo", self.creature.color )
func rotate_camera_arm( p_axis, p_angle_degree ):
$model/ra/spring_arm.rotate( p_axis, p_angle_degree )
func rotate_camera( p_axis, p_angle_degree ):
$model/ra/spring_arm/camera.rotate( p_axis, p_angle_degree )
func move_camera( p_translation ):
$model/ra/spring_arm/camera.translate( p_translation )
func reset_camera():
$model/ra/spring_arm.translation = Vector3( 0, 2, 2 )
$model/ra/spring_arm.rotation_degrees = Vector3( 0.0, 0.0, 0.0 )
$model/ra/spring_arm/camera.translation = Vector3( 0.0, 0.0, 0.0 )
$model/ra/spring_arm/camera.rotation_degrees = Vector3( -14, 0, 0.328 )