210 lines
7.1 KiB
GDScript
210 lines
7.1 KiB
GDScript
extends Spatial
|
|
|
|
var animation_object:AnimationPlayer = null
|
|
var rotation_speed_factor = 0.01
|
|
var orientation = 0.0
|
|
var direction = Vector3.ZERO
|
|
var velocity: = Vector3.ZERO
|
|
var rotatex = 0.0
|
|
var move_run: bool = false
|
|
var move_toggle_run: bool = false
|
|
export var gravity = -9.0
|
|
|
|
enum ACTION {
|
|
idle,
|
|
walk,
|
|
run,
|
|
scan,
|
|
teleport,
|
|
}
|
|
|
|
var current_action = ACTION.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
|
|
|
|
# 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'])
|
|
current_action = ACTION.idle
|
|
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")
|
|
|
|
|
|
func update_animation(action, anim_name):
|
|
# Change animation if we need
|
|
if current_action != action:
|
|
current_action = action
|
|
animation_object.play( anim_name )
|
|
|
|
|
|
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 ):
|
|
#Config.msg_info( "B:" + str(Input.get_action_strength("move_backward")) + " F:" + str(Input.get_action_strength("move_forward") ))
|
|
# $character.direction = self.get_input_direction()
|
|
#self.direction = self.get_input_direction()
|
|
#self.direction = self.get_input_direction()
|
|
$creature.rotate_y( self.rotation_speed_factor * self.orientation )
|
|
|
|
var input_direction: = self.get_input_direction()
|
|
|
|
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()
|
|
|
|
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
|
|
move_direction.y = 0
|
|
|
|
# Movement
|
|
velocity = self.calculate_velocity(velocity, move_direction, delta)
|
|
# 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))
|
|
velocity = $creature.move_and_slide(velocity, Vector3.UP, true)
|
|
var pos : Vector3 = $creature.get_global_transform().origin
|
|
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")) )
|
|
if input_direction:
|
|
if not input_direction.z == 0.0:
|
|
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() )
|
|
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
|
|
|
|
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
|
|
velocity_new.y = velocity_current.y + gravity * delta
|
|
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 ):
|
|
# self.rotate_y( event.relative.x *0.01 )
|
|
$creature.rotate_y( event.relative.x *0.01 )
|
|
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) :
|
|
var sum = event.relative.y *0.01
|
|
rotatex += sum
|
|
$creature/camera_fps.rotate_x( sum )
|
|
elif event.is_action_pressed("move_toggle_run"):
|
|
self.move_toggle_run = ! self.move_toggle_run
|
|
elif event.is_action_pressed( "camera_switch" ):
|
|
if $creature/camera_tps.current:
|
|
# Reset angle X for camera
|
|
$creature/camera_fps.rotate_x( - rotatex )
|
|
rotatex = 0.0
|
|
$creature/camera_fps.make_current()
|
|
else:
|
|
$creature/camera_tps.make_current()
|