khanat-client/scenes/player/playerB.gd

153 lines
5.1 KiB
GDScript3
Raw Normal View History

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 10:49:56 +00:00
export var gravity = -9.0
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'])
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 _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 10:49:56 +00:00
self.rotate_y( self.rotation_speed_factor * self.orientation )
2021-09-04 11:37:09 +00:00
var input_direction: = self.get_input_direction()
# 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
# Movement
#velocity = self.calculate_velocity(velocity, input_direction, delta)
#velocity = $creature.move_and_slide(velocity, Vector3.UP, true)
#Config.msg_info("X:" + str($creature.))
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
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 ):
2021-09-04 11:37:09 +00:00
self.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) :
$creature/camera_fps.rotate_x( event.relative.y *0.01 )
elif event.is_action_pressed( "camera_switch" ):
if $creature/camera_tps.current:
# Reset angle X for camera
var current = $creature/camera_fps.get_rotation()
current.x = 0.0
$creature/camera_fps.set_rotation(current)
$creature/camera_fps.make_current()
else:
$creature/camera_tps.make_current()