167 lines
5 KiB
GDScript3
167 lines
5 KiB
GDScript3
|
extends Spatial
|
||
|
|
||
|
var animation_object:AnimationPlayer = null
|
||
|
|
||
|
var rotation_speed_factor = 0.01
|
||
|
var move_speed = 2.5
|
||
|
var run_speed = 5.0
|
||
|
var max_speed = 12.0
|
||
|
export var gravity = -9.0
|
||
|
|
||
|
var orientation = 0.0
|
||
|
var direction = Vector3.ZERO
|
||
|
var velocity: = Vector3.ZERO
|
||
|
var idle_animation = "idle"
|
||
|
|
||
|
func search_animation( obj ) -> bool:
|
||
|
var ret:bool = false
|
||
|
for i in obj.get_children():
|
||
|
if i.get_name() == "AnimationPlayer":
|
||
|
animation_object = i
|
||
|
return true
|
||
|
else:
|
||
|
ret = search_animation(i)
|
||
|
if ret == true:
|
||
|
return ret
|
||
|
return false
|
||
|
|
||
|
|
||
|
func _ready():
|
||
|
match Globals.player['Race']:
|
||
|
"arche":
|
||
|
idle_animation = "idle"
|
||
|
self.change_creature( "res://scenes/creature_creation/arche.tscn" )
|
||
|
"isidor":
|
||
|
idle_animation = "idle"
|
||
|
self.change_creature( "res://scenes/creature_creation/isidor.tscn" )
|
||
|
"raference":
|
||
|
idle_animation = "_bip01_ca_female_idle"
|
||
|
self.change_creature( "res://scenes/creature_creation/raference.tscn" )
|
||
|
_:
|
||
|
idle_animation = "idle"
|
||
|
self.change_creature( "res://scenes/creature_creation/arche.tscn" )
|
||
|
# search_animation($kinematic_body)
|
||
|
# Globals.player['blend_shape']
|
||
|
update_blend_shapes($kinematic_body/creature, Globals.player['blend_shape'])
|
||
|
animation_object.play( idle_animation )
|
||
|
animation_object.connect("animation_finished", self, "_on_AnimationPlayer_animation_finished")
|
||
|
|
||
|
func _on_AnimationPlayer_animation_finished(anim_name):
|
||
|
Config.msg_debug("Animation finished:" + anim_name)
|
||
|
animation_object.play( anim_name )
|
||
|
|
||
|
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 calculate_velocity(
|
||
|
velocity_current: Vector3,
|
||
|
move_direction: Vector3,
|
||
|
delta: float
|
||
|
) -> Vector3:
|
||
|
var velocity_new = Vector3.ZERO
|
||
|
velocity_new = move_direction
|
||
|
|
||
|
velocity_new *= 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 _process( delta ):
|
||
|
|
||
|
#$character.direction = self.get_input_direction()
|
||
|
self.rotate_y( self.rotation_speed_factor * self.orientation )
|
||
|
|
||
|
# var forwards: Vector3 = 1.0 * direction.z
|
||
|
# var right: Vector3 = 1.0 * direction.x
|
||
|
# if forwards:
|
||
|
# right = Vector3.ZERO
|
||
|
# var move_direction: = forwards + right
|
||
|
# if move_direction.length() > 1.0:
|
||
|
# move_direction = move_direction.normalized()
|
||
|
# move_direction.y = 0
|
||
|
# velocity = self.calculate_velocity(velocity, move_direction, delta)
|
||
|
## $creature.move_and_slide(velocity, Vector3.UP, true)
|
||
|
# velocity = $kinematic_body.move_and_slide(velocity, Vector3.UP, true)
|
||
|
|
||
|
|
||
|
func _input( event ):
|
||
|
# Config.msg_debug(str(event))
|
||
|
self.direction = self.get_input_direction()
|
||
|
if not Input.is_key_pressed( KEY_SHIFT ):
|
||
|
self.orientation = (Input.get_action_strength("move_turn_left") - Input.get_action_strength("move_turn_right"))
|
||
|
else:
|
||
|
self.orientation = 0.0
|
||
|
if event is InputEventMouseMotion and Input.is_mouse_button_pressed( 2 ):
|
||
|
if $kinematic_body/creature:
|
||
|
self.rotate_y( event.relative.x *0.01 )
|
||
|
elif event.is_action( "camera_zoom_in" ):
|
||
|
# self.zoom_level += 1
|
||
|
self.move_speed
|
||
|
pass
|
||
|
elif event.is_action( "camera_zoom_out" ):
|
||
|
# self.zoom_level -= 1
|
||
|
pass
|
||
|
elif event.is_action_pressed( "camera_switch" ):
|
||
|
if $camera_tps.current:
|
||
|
$camera_fps.make_current()
|
||
|
#$character/spring_arm.rotation = Vector3.ZERO
|
||
|
# self.hide()
|
||
|
else:
|
||
|
$camera_tps.make_current()
|
||
|
# self.show()
|
||
|
elif event.is_action( "move_backward" ):
|
||
|
Config.msg_info( "move_backward" )
|
||
|
pass
|
||
|
elif event.is_action( "move_forward" ):
|
||
|
Config.msg_info( "move_forward" )
|
||
|
pass
|
||
|
if animation_object != null:
|
||
|
# Lance une animation d'attente si on ne fait rien
|
||
|
if not animation_object.is_playing() and animation_object.has_animation("idle"):
|
||
|
animation_object.play( idle_animation )
|
||
|
|
||
|
|
||
|
func change_creature( new_model_path ):
|
||
|
if $kinematic_body/creature:
|
||
|
var old_model = $kinematic_body/creature
|
||
|
$kinematic_body.remove_child( old_model )
|
||
|
old_model.queue_free()
|
||
|
self.animation_object = null
|
||
|
var new_model = load( new_model_path )
|
||
|
if new_model:
|
||
|
new_model = new_model.instance()
|
||
|
new_model.name = "creature"
|
||
|
$kinematic_body.add_child( new_model )
|
||
|
#new_model.connect( "animation_finished", self, "_on_creature_animation_finished" )
|
||
|
# new_model.duplicate_meshes()
|
||
|
search_animation( new_model )
|
||
|
|
||
|
|
||
|
func _on_creature_animation_finished(anim_name):
|
||
|
Config.msg_debug("anim_name:" + anim_name)
|
||
|
|
||
|
func update_blend_shapes( obj , param):
|
||
|
#blend_shapes = {}
|
||
|
update_blend_shapes_step(obj, param, "")
|
||
|
pass
|
||
|
|
||
|
|
||
|
func update_blend_shapes_step( obj, param, father = "" ):
|
||
|
for i in obj.get_children():
|
||
|
var root = father + str(i.name) + "."
|
||
|
if i is MeshInstance:
|
||
|
for key in i.get_property_list():
|
||
|
if key.name.substr(0, 13) == "blend_shapes/":
|
||
|
var blend = key.name.substr(13)
|
||
|
if param.has(blend):
|
||
|
i.set( "blend_shapes/"+blend, param[blend] )
|
||
|
update_blend_shapes_step( i, param, root)
|
||
|
|