2021-09-04 10:49:56 +00:00
|
|
|
extends Spatial
|
|
|
|
|
|
|
|
var animation_object:AnimationPlayer = null
|
|
|
|
var rotation_speed_factor = 0.01
|
|
|
|
var orientation = 0.0
|
|
|
|
var direction = Vector3.ZERO
|
2021-09-04 11:37:09 +00:00
|
|
|
var velocity: = Vector3.ZERO
|
2021-09-04 13:37:40 +00:00
|
|
|
var rotatex = 0.0
|
2021-09-04 15:49:11 +00:00
|
|
|
var move_run: bool = false
|
|
|
|
var move_toggle_run: bool = false
|
2021-09-04 10:49:56 +00:00
|
|
|
export var gravity = -9.0
|
|
|
|
|
2021-09-04 14:33:34 +00:00
|
|
|
enum ACTION {
|
|
|
|
idle,
|
|
|
|
walk,
|
|
|
|
run,
|
|
|
|
scan,
|
|
|
|
teleport,
|
|
|
|
}
|
|
|
|
|
|
|
|
var current_action = ACTION.idle
|
|
|
|
|
2021-09-04 10:49:56 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
match Globals.player['Race']:
|
|
|
|
"arche":
|
|
|
|
self.change_creature( "res://scenes/player/creature_raference.tscn" )
|
|
|
|
"isidor":
|
|
|
|
self.change_creature( "res://scenes/player/creature_raference.tscn" )
|
|
|
|
"raference":
|
|
|
|
self.change_creature( "res://scenes/player/creature_raference.tscn" )
|
|
|
|
_:
|
|
|
|
self.change_creature( "res://scenes/player/creature_raference.tscn" )
|
|
|
|
# search_animation($character)
|
|
|
|
# Globals.player['blend_shape']
|
|
|
|
update_blend_shapes($creature, Globals.player['blend_shape'])
|
2021-09-04 14:33:34 +00:00
|
|
|
current_action = ACTION.idle
|
2021-09-04 10:49:56 +00:00
|
|
|
var idlename = "idle"
|
|
|
|
if $creature.has_method("get_animation_idle"):
|
|
|
|
idlename = $creature.get_animation_idle()
|
|
|
|
self.rotation_speed_factor = $creature.rotation_speed_factor
|
|
|
|
animation_object.play( idlename )
|
|
|
|
animation_object.connect("animation_finished", self, "_on_AnimationPlayer_animation_finished")
|
|
|
|
|
|
|
|
|
2021-09-04 14:33:34 +00:00
|
|
|
func update_animation(action, anim_name):
|
|
|
|
# Change animation if we need
|
|
|
|
if current_action != action:
|
|
|
|
current_action = action
|
|
|
|
animation_object.play( anim_name )
|
|
|
|
|
|
|
|
|
2021-09-04 10:49:56 +00:00
|
|
|
func _on_AnimationPlayer_animation_finished(anim_name):
|
|
|
|
Config.msg_debug("Animation finished:" + anim_name)
|
|
|
|
animation_object.play( anim_name )
|
|
|
|
|
|
|
|
|
|
|
|
func change_creature( new_model_path ):
|
|
|
|
if $creature:
|
|
|
|
var old_model = $creature
|
|
|
|
self.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"
|
|
|
|
self.add_child( new_model )
|
|
|
|
#new_model.connect( "animation_finished", self, "_on_creature_animation_finished" )
|
|
|
|
# new_model.duplicate_meshes()
|
|
|
|
search_animation( new_model )
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
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 _process( delta ):
|
2021-09-04 11:37:09 +00:00
|
|
|
#Config.msg_info( "B:" + str(Input.get_action_strength("move_backward")) + " F:" + str(Input.get_action_strength("move_forward") ))
|
2021-09-04 10:49:56 +00:00
|
|
|
# $character.direction = self.get_input_direction()
|
|
|
|
#self.direction = self.get_input_direction()
|
2021-09-04 11:37:09 +00:00
|
|
|
#self.direction = self.get_input_direction()
|
2021-09-04 13:37:40 +00:00
|
|
|
$creature.rotate_y( self.rotation_speed_factor * self.orientation )
|
2021-09-04 10:49:56 +00:00
|
|
|
|
2021-09-04 11:37:09 +00:00
|
|
|
var input_direction: = self.get_input_direction()
|
2021-09-04 15:49:11 +00:00
|
|
|
|
2021-09-04 13:37:40 +00:00
|
|
|
var forwards: Vector3 = $creature/camera_tps.global_transform.basis.z * input_direction.z
|
|
|
|
var right: Vector3 = $creature/camera_tps.global_transform.basis.x * input_direction.x
|
|
|
|
var move_direction: = forwards + right
|
|
|
|
if move_direction.length() > 1.0:
|
|
|
|
move_direction = move_direction.normalized()
|
2021-09-04 15:49:11 +00:00
|
|
|
|
|
|
|
var step_move
|
|
|
|
if (self.move_toggle_run or Input.get_action_strength("move_run")) and input_direction.z < 0.0:
|
|
|
|
self.move_run = true
|
|
|
|
step_move = $creature.run_speed
|
|
|
|
else:
|
|
|
|
self.move_run = false
|
|
|
|
step_move = $creature.move_speed
|
|
|
|
|
|
|
|
move_direction = step_move * move_direction
|
2021-09-04 13:37:40 +00:00
|
|
|
move_direction.y = 0
|
2021-09-04 11:37:09 +00:00
|
|
|
|
|
|
|
# Movement
|
2021-09-04 13:37:40 +00:00
|
|
|
velocity = self.calculate_velocity(velocity, move_direction, delta)
|
2021-09-04 13:50:31 +00:00
|
|
|
# Config.msg_info("move_direction X:" + str(move_direction.x) + " Y:" + str(move_direction.y) + " Z:" + str(move_direction.z))
|
|
|
|
# Config.msg_info("velocity X:" + str(velocity.x) + " Y:" + str(velocity.y) + " Z:" + str(velocity.z))
|
2021-09-04 13:37:40 +00:00
|
|
|
velocity = $creature.move_and_slide(velocity, Vector3.UP, true)
|
|
|
|
var pos : Vector3 = $creature.get_global_transform().origin
|
2021-09-04 15:49:11 +00:00
|
|
|
Config.msg_info("pos X:" + str(pos.x) + " Y:" + str(pos.y) + " Z:" + str(pos.z) + " run:" + str(Input.get_action_strength("move_run")) + str(Input.get_action_strength("move_toggle_run")) )
|
2021-09-04 14:33:34 +00:00
|
|
|
if input_direction:
|
|
|
|
if not input_direction.z == 0.0:
|
2021-09-04 15:49:11 +00:00
|
|
|
if input_direction.z < 0.0:
|
|
|
|
if self.move_run:
|
|
|
|
update_animation( ACTION.run, $creature.animation_run )
|
|
|
|
else:
|
|
|
|
update_animation( ACTION.walk, $creature.animation_walk )
|
|
|
|
else:
|
|
|
|
update_animation( ACTION.idle, $creature.get_animation_idle() )
|
2021-09-04 14:33:34 +00:00
|
|
|
elif input_direction.x > 0.0:
|
|
|
|
# $model/ra/model/AnimationPlayer.play( "strafe_right" )
|
|
|
|
update_animation( ACTION.idle, $creature.get_animation_idle() )
|
|
|
|
elif input_direction.x < 0.0:
|
|
|
|
# $model/ra/model/AnimationPlayer.play( "strafe_left" )
|
|
|
|
update_animation( ACTION.idle, $creature.get_animation_idle() )
|
|
|
|
else:
|
|
|
|
update_animation( ACTION.idle, $creature.get_animation_idle() )
|
|
|
|
else:
|
|
|
|
# $model/ra/model/AnimationPlayer.play( "idle" )
|
|
|
|
update_animation( ACTION.idle, $creature.get_animation_idle() )
|
|
|
|
pass
|
2021-09-04 10:49:56 +00:00
|
|
|
|
|
|
|
func calculate_velocity(
|
|
|
|
velocity_current: Vector3,
|
|
|
|
move_direction: Vector3,
|
|
|
|
delta: float
|
|
|
|
) -> Vector3:
|
|
|
|
var velocity_new = Vector3.ZERO
|
|
|
|
velocity_new = move_direction
|
|
|
|
|
|
|
|
velocity_new *= $creature.move_speed
|
|
|
|
if velocity_new.length() > $creature.max_speed:
|
|
|
|
velocity_new = velocity_new.normalized() * $creature.max_speed
|
2021-09-04 13:50:31 +00:00
|
|
|
velocity_new.y = velocity_current.y + gravity * delta
|
2021-09-04 10:49:56 +00:00
|
|
|
return velocity_new
|
|
|
|
|
|
|
|
|
|
|
|
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 ):
|
2021-09-04 13:37:40 +00:00
|
|
|
# self.rotate_y( event.relative.x *0.01 )
|
|
|
|
$creature.rotate_y( event.relative.x *0.01 )
|
2021-09-04 11:37:09 +00:00
|
|
|
if $creature/camera_fps.current:
|
|
|
|
# On FPS - we can move head to see up or down
|
|
|
|
var current = $creature/camera_fps.get_rotation()
|
|
|
|
# Config.msg_info("X:" + str(current.x) + " v:" + str(event.relative.y))
|
|
|
|
if (event.relative.y > 0.0 and current.x > -1.4) or (event.relative.y < 0.0 and current.x < 1.4) :
|
2021-09-04 13:37:40 +00:00
|
|
|
var sum = event.relative.y *0.01
|
|
|
|
rotatex += sum
|
|
|
|
$creature/camera_fps.rotate_x( sum )
|
2021-09-04 15:49:11 +00:00
|
|
|
elif event.is_action_pressed("move_toggle_run"):
|
|
|
|
self.move_toggle_run = ! self.move_toggle_run
|
2021-09-04 11:37:09 +00:00
|
|
|
elif event.is_action_pressed( "camera_switch" ):
|
|
|
|
if $creature/camera_tps.current:
|
|
|
|
# Reset angle X for camera
|
2021-09-04 13:37:40 +00:00
|
|
|
$creature/camera_fps.rotate_x( - rotatex )
|
|
|
|
rotatex = 0.0
|
2021-09-04 11:37:09 +00:00
|
|
|
$creature/camera_fps.make_current()
|
|
|
|
else:
|
|
|
|
$creature/camera_tps.make_current()
|