khanat-client/scenes/creatures/creature.gd
2021-06-22 18:22:58 +02:00

422 lines
20 KiB
GDScript

extends CollisionShape
signal animation_finished( anim_name )
signal equip( p_slot, p_item )
signal unequip( p_slot )
signal is_dead
var creature = Datas.Human.new()
var eye_color_list = [ "blue_eye", "bluegreen_eye","brown_eye","deepblue_eye","green_eye","grey_eye","ice_eye","lightblue_eye" ]
class Slot:
var item = null
var attachment = null
func _init( p_attachment = null ):
self.attachment = p_attachment
var slots = { Datas.Equipment.SLOT.cloth_hand: Slot.new( "cloths" )\
, Datas.Equipment.SLOT.cloth_face: Slot.new( "cloths" )\
, Datas.Equipment.SLOT.cloth_head: Slot.new( "cloths" )\
, Datas.Equipment.SLOT.cloth_torso: Slot.new( "cloths" )\
, Datas.Equipment.SLOT.cloth_legs: Slot.new( "cloths" )\
, Datas.Equipment.SLOT.cloth_feet: Slot.new( "cloths" )\
, Datas.Equipment.SLOT.cloth_back: Slot.new( "cloths" )\
, Datas.Equipment.SLOT.weapon_hand_right: Slot.new( "attachment_hand_R" )\
, Datas.Equipment.SLOT.weapon_hand_left: Slot.new( "attachment_hand_L" )\
, Datas.Equipment.SLOT.weapon_hands: Slot.new( "attachment_hand_R" )\
, Datas.Equipment.SLOT.weapon_hip_right: Slot.new( "attachment_hips_R" )\
, Datas.Equipment.SLOT.weapon_hip_left: Slot.new( "attachment_hips_L" )\
, Datas.Equipment.SLOT.weapon_back_right: Slot.new()\
, Datas.Equipment.SLOT.weapon_back_left: Slot.new()\
, Datas.Equipment.SLOT.weapon_holster_right: Slot.new()\
, Datas.Equipment.SLOT.weapon_holster_left: Slot.new() }
func _ready():
self.duplicate_meshes()
func duplicate_meshes():
for body in $body_parts.get_children():
for mesh in body.get_node( "skeleton" ).get_children():
if mesh is MeshInstance:
var new_mat = mesh.get( "material/0" ).duplicate()
var new_mesh = mesh.mesh.duplicate()
mesh.mesh = new_mesh
mesh.set( "material/0", new_mat )
for body in $hair_parts.get_children():
for mesh in body.get_node( "skeleton" ).get_children():
if mesh is MeshInstance:
var new_mat = mesh.get( "material/0" ).duplicate()
var new_mesh = mesh.mesh.duplicate()
mesh.mesh = new_mesh
mesh.set( "material/0", new_mat )
for body in $cloths.get_children():
for mesh in body.get_node( "skeleton" ).get_children():
if mesh is MeshInstance:
var new_mat = mesh.get( "material/0" ).duplicate()
var new_mesh = mesh.mesh.duplicate()
mesh.mesh = new_mesh
mesh.set( "material/0", new_mat )
self.update()
func set_blend_shape( p_blend_shape_name, p_value ):
for child in $body_parts.get_children():
var skeleton = child.get_node( "skeleton" )
for node in skeleton.get_children():
if node is MeshInstance:
node.set( "blend_shapes/"+p_blend_shape_name, p_value )
for child in $hair_parts.get_children():
var skeleton = child.get_node( "skeleton" )
for node in skeleton.get_children():
if node is MeshInstance:
node.set( "blend_shapes/"+p_blend_shape_name, p_value )
for child in $cloths.get_children():
var skeleton = child.get_node( "skeleton" )
for node in skeleton.get_children():
if node is MeshInstance:
node.set( "blend_shapes/"+p_blend_shape_name, p_value )
func load_from_name( p_name, p_emplacement = "usr" ):
var new_creature = Datas.Human.new()
var name_split = p_name.split( "_" )
new_creature.set_data( "first_name", name_split[0] )
new_creature.set_data( "last_name", name_split[1] )
new_creature.load( null, p_emplacement )
self.creature = new_creature
self.update()
func update():
Config.msg_debug("Update")
if self.creature:
Config.msg_debug("scale:" + str(self.scale) + " y:" + str(self.translation.y))
if self.scale.y != self.creature.get_data( "size" ):
var delta_y = self.translation.y - self.scale.y
Config.msg_debug("scale:" + str(self.scale) + " y:" + str(self.translation.y) + " deltaY:" + str(delta_y))
self.scale = Vector3( self.creature.get_data( "size" ), self.creature.get_data( "size" ), self.creature.get_data( "size" ) )
self.translation.y = self.scale.y + delta_y
Config.msg_debug("scale:" + str(self.scale) + " y:" + str(self.translation.y) + " deltaY:" + str(self.translation.y - self.scale.y))
self.set_blend_shape( "caucasian", self.creature.get_data( "caucasian" ) )
self.set_blend_shape( "african", self.creature.get_data( "african" ) )
self.set_blend_shape( "asian", self.creature.get_data( "asian" ) )
if self.creature.get_data( "sex" ) == Globals.SEX.female:
if self.creature.get_data( "skin" ) == 0:
self.get_node( "body_parts/body/skeleton/body" ).get_surface_material( 0 ).set_shader_param( "texture_albedo", preload( "res://assets/creatures/human/textures/young_lightskinned_female_diffuse.png" ) )
elif self.creature.get_data( "skin" ) == 1:
self.get_node( "body_parts/body/skeleton/body" ).get_surface_material( 0 ).set_shader_param( "texture_albedo", preload( "res://assets/creatures/human/textures/young_lightskinned_female_diffuse3.png" ) )
elif self.creature.get_data( "skin" ) == 2:
self.get_node( "body_parts/body/skeleton/body" ).get_surface_material( 0 ).set_shader_param( "texture_albedo", preload( "res://assets/creatures/human/textures/young_darkskinned_female_diffuse.png" ) )
elif self.creature.get_data( "sex" ) == Globals.SEX.male:
if self.creature.get_data( "skin" ) == 0:
self.get_node( "body_parts/body/skeleton/body" ).get_surface_material( 0 ).set_shader_param( "texture_albedo", preload( "res://assets/creatures/human/textures/young_lightskinned_male_diffuse2.png" ) )
elif self.creature.get_data( "skin" ) == 1:
self.get_node( "body_parts/body/skeleton/body" ).get_surface_material( 0 ).set_shader_param( "texture_albedo", preload( "res://assets/creatures/human/textures/young_lightskinned_male_diffuse3.png" ) )
elif self.creature.get_data( "skin" ) == 2:
self.get_node( "body_parts/body/skeleton/body" ).get_surface_material( 0 ).set_shader_param( "texture_albedo", preload( "res://assets/creatures/human/textures/young_darkskinned_male_diffuse.png" ) )
if self.creature.get_data( "sex" ) == Globals.SEX.female:
if self.creature.get_data( "hair" ) == 0:
self.get_node( "hair_parts/mh_human_female_hair" ).hide()
elif self.creature.get_data( "hair" ) == 1:
self.get_node( "hair_parts/mh_human_female_hair" ).show()
self.get_node( "hair_parts/mh_human_female_hair/skeleton/hair" ).get_surface_material( 0 ).albedo_texture = preload( "res://assets/creatures/human/textures/human_female_hair_001.png" )
elif self.creature.get_data( "hair" ) == 2:
self.get_node( "hair_parts/mh_human_female_hair" ).show()
self.get_node( "hair_parts/mh_human_female_hair/skeleton/hair" ).get_surface_material( 0 ).albedo_texture = preload( "res://assets/creatures/human/textures/human_female_hair_002.png" )
elif self.creature.get_data( "sex" ) == Globals.SEX.male:
if self.creature.get_data( "hair" ) == 0:
self.get_node( "hair_parts/mh_human_male_hair" ).hide()
elif self.creature.get_data( "hair" ) == 1:
self.get_node( "hair_parts/mh_human_male_hair" ).show()
self.get_node( "hair_parts/mh_human_male_hair/skeleton/hair" ).get_surface_material( 0 ).albedo_texture = preload( "res://assets/creatures/human/textures/human_male_hair.png" )
for hair in $hair_parts.get_children():
if hair.has_node( "skeleton/hair" ) and hair.get_node( "skeleton/hair" ).get_surface_material( 0 ):
hair.get_node( "skeleton/hair" ).get_surface_material( 0 ).albedo_color = self.creature.get_data( "hair_color" )
if self.creature.get_data( "muscles" ) > 0.0:
self.set_blend_shape( "muscles_min", 0.0 )
self.set_blend_shape( "muscles_max", self.creature.get_data( "muscles" ) )
elif self.creature.get_data( "muscles" ) < 0.0:
self.set_blend_shape( "muscles_min", -self.creature.get_data( "muscles" ) )
self.set_blend_shape( "muscles_max", 0.0 )
else:
self.set_blend_shape( "muscles_min", 0.0 )
self.set_blend_shape( "muscles_max", 0.0 )
if self.creature.get_data( "fat" ) > 0.0:
self.set_blend_shape( "fat_min", 0.0 )
self.set_blend_shape( "fat_max", self.creature.get_data( "fat" ) )
elif self.creature.get_data( "fat" ) < 0.0:
self.set_blend_shape( "fat_min", -self.creature.get_data( "fat" ) )
self.set_blend_shape( "fat_max", 0.0 )
else:
self.set_blend_shape( "fat_min", 0.0 )
self.set_blend_shape( "fat_max", 0.0 )
if self.creature.get_data( "proportion" ) > 0.0:
self.set_blend_shape( "proportion_min", 0.0 )
self.set_blend_shape( "proportion_max", self.creature.get_data( "proportion" ) )
elif self.creature.get_data( "proportion" ) < 0.0:
self.set_blend_shape( "proportion_min", -self.creature.get_data( "proportion" ) )
self.set_blend_shape( "proportion_max", 0.0 )
else:
self.set_blend_shape( "proportion_min", 0.0 )
self.set_blend_shape( "proportion_max", 0.0 )
if self.creature.get_data( "breast" ) > 0.0:
self.set_blend_shape( "breast_min", 0.0 )
self.set_blend_shape( "breast_max", self.creature.get_data( "breast" ) )
elif self.creature.get_data( "breast" ) < 0.0:
self.set_blend_shape( "breast_min", -self.creature.get_data( "breast" ) )
self.set_blend_shape( "breast_max", 0.0 )
else:
self.set_blend_shape( "breast_min", 0.0 )
self.set_blend_shape( "breast_max", 0.0 )
if self.creature.get_data( "sex" ) == Globals.SEX.female:
self.set_blend_shape( "pregnancy", self.creature.get_data( "pregnancy" ) )
# Skin tone.
if self.get_node( "body_parts/body/skeleton/body" ).get( "material/0" ) and self.get_node( "body_parts/body/skeleton/body" ).get( "material/0" ) is ShaderMaterial:
var color = self.get_node( "body_parts/body/skeleton/body" ).get( "material/0" ).get_shader_param( "albedo" )
color.v = 1.0 - self.creature.get_data( "skin_tone" )
self.get_node( "body_parts/body/skeleton/body" ).get( "material/0" ).set_shader_param( "albedo", color )
self.set_blend_shape( "cleft_chin", self.creature.get_data( "cleft_chin" ) )
self.set_blend_shape( "chin_angle", self.creature.get_data( "chin_angle" ) )
if self.creature.get_data( "mouth_horiz" ) > 0.0:
self.set_blend_shape( "mouth_horiz_min", 0.0 )
self.set_blend_shape( "mouth_horiz_max", self.creature.get_data( "mouth_horiz" ) )
elif self.creature.get_data( "mouth_horiz" ) < 0.0:
self.set_blend_shape( "mouth_horiz_min", -self.creature.get_data( "mouth_horiz" ) )
self.set_blend_shape( "mouth_horiz_max", 0.0 )
else:
self.set_blend_shape( "mouth_horiz_min", 0.0 )
self.set_blend_shape( "mouth_horiz_max", 0.0 )
if self.creature.get_data( "mouth_vert" ) > 0.0:
self.set_blend_shape( "mouth_vert_min", 0.0 )
self.set_blend_shape( "mouth_vert_max", self.creature.get_data( "mouth_vert" ) )
elif self.creature.get_data( "mouth_vert" ) < 0.0:
self.set_blend_shape( "mouth_vert_min", -self.creature.get_data( "mouth_vert" ) )
self.set_blend_shape( "mouth_vert_max", 0.0 )
else:
self.set_blend_shape( "mouth_vert_min", 0.0 )
self.set_blend_shape( "mouth_vert_max", 0.0 )
if self.creature.get_data( "nose_galbe" ) > 0.0:
self.set_blend_shape( "nose_galbe_min", 0.0 )
self.set_blend_shape( "nose_galbe_max", self.creature.get_data( "nose_galbe" ) )
elif self.creature.get_data( "nose_galbe" ) < 0.0:
self.set_blend_shape( "nose_galbe_min", -self.creature.get_data( "nose_galbe" ) )
self.set_blend_shape( "nose_galbe_max", 0.0 )
else:
self.set_blend_shape( "nose_galbe_min", 0.0 )
self.set_blend_shape( "nose_galbe_max", 0.0 )
if self.creature.get_data( "nose_grec" ) > 0.0:
self.set_blend_shape( "nose_grec_min", 0.0 )
self.set_blend_shape( "nose_grec_max", self.creature.get_data( "nose_grec" ) )
elif self.creature.get_data( "nose_grec" ) < 0.0:
self.set_blend_shape( "nose_grec_min", -self.creature.get_data( "nose_grec" ) )
self.set_blend_shape( "nose_grec_max", 0.0 )
else:
self.set_blend_shape( "nose_grec_min", 0.0 )
self.set_blend_shape( "nose_grec_max", 0.0 )
if self.creature.get_data( "nose_horiz" ) > 0.0:
self.set_blend_shape( "nose_horiz_min", 0.0 )
self.set_blend_shape( "nose_horiz_max", self.creature.get_data( "nose_horiz" ) )
elif self.creature.get_data( "nose_horiz" ) < 0.0:
self.set_blend_shape( "nose_horiz_min", -self.creature.get_data( "nose_horiz" ) )
self.set_blend_shape( "nose_horiz_max", 0.0 )
else:
self.set_blend_shape( "nose_horiz_min", 0.0 )
self.set_blend_shape( "nose_horiz_max", 0.0 )
if self.creature.get_data( "nose_depth" ) > 0.0:
self.set_blend_shape( "nose_depth_min", 0.0 )
self.set_blend_shape( "nose_depth_max", self.creature.get_data( "nose_depth" ) )
elif self.creature.get_data( "nose_depth" ) < 0.0:
self.set_blend_shape( "nose_depth_min", -self.creature.get_data( "nose_depth" ) )
self.set_blend_shape( "nose_depth_max", 0.0 )
else:
self.set_blend_shape( "nose_depth_min", 0.0 )
self.set_blend_shape( "nose_depth_max", 0.0 )
self.get_node( "body_parts/body/skeleton/eyes" ).get_surface_material( 0 ).albedo_texture = load( "res://assets/creatures/human/textures/"+self.eye_color_list[ self.creature.get_data( "eyes_color", 0 ) ]+".png" )
if self.creature.get_data( "eyes_vert" ) > 0.0:
self.set_blend_shape( "eyes_vert_min", 0.0 )
self.set_blend_shape( "eyes_vert_max", self.creature.get_data( "eyes_vert" ) )
elif self.creature.get_data( "eyes_vert" ) < 0.0:
self.set_blend_shape( "eyes_vert_min", -self.creature.get_data( "eyes_vert" ) )
self.set_blend_shape( "eyes_vert_max", 0.0 )
else:
self.set_blend_shape( "eyes_vert_min", 0.0 )
self.set_blend_shape( "eyes_vert_max", 0.0 )
if self.creature.get_data( "eyes_gap" ) > 0.0:
self.set_blend_shape( "eyes_gap_min", 0.0 )
self.set_blend_shape( "eyes_gap_max", self.creature.get_data( "eyes_gap" ) )
elif self.creature.get_data( "eyes_gap" ) < 0.0:
self.set_blend_shape( "eyes_gap_min", -self.creature.get_data( "eyes_gap" ) )
self.set_blend_shape( "eyes_gap_max", 0.0 )
else:
self.set_blend_shape( "eyes_gap_min", 0.0 )
self.set_blend_shape( "eyes_gap_max", 0.0 )
self.set_blend_shape( "earlobes", self.creature.get_data( "earlobes" ) )
self.set_blend_shape( "ears_wing", self.creature.get_data( "ears_wing" ) )
if self.creature.get_data( "lips_up_shape" ) > 0.0:
self.set_blend_shape( "lips_up_shape_min", 0.0 )
self.set_blend_shape( "lips_up_shape_max", self.creature.get_data( "lips_up_shape" ) )
elif self.creature.get_data( "lips_up_shape" ) < 0.0:
self.set_blend_shape( "lips_up_shape_min", -self.creature.get_data( "lips_up_shape" ) )
self.set_blend_shape( "lips_up_shape_max", 0.0 )
else:
self.set_blend_shape( "lips_up_shape_min", 0.0 )
self.set_blend_shape( "lips_up_shape_max", 0.0 )
if self.creature.get_data( "lips_up_shape_2" ) > 0.0:
self.set_blend_shape( "lips_up_shape_2_min", 0.0 )
self.set_blend_shape( "lips_up_shape_2_max", self.creature.get_data( "lips_up_shape_2" ) )
elif self.creature.get_data( "lips_up_shape_2" ) < 0.0:
self.set_blend_shape( "lips_up_shape_2_min", -self.creature.get_data( "lips_up_shape_2" ) )
self.set_blend_shape( "lips_up_shape_2_max", 0.0 )
else:
self.set_blend_shape( "lips_up_shape_2_min", 0.0 )
self.set_blend_shape( "lips_up_shape_2_max", 0.0 )
self.set_blend_shape( "nipple_flat", 1.0 )
func play( anim_name, speed = 1.0 ):
for child in $body_parts.get_children():
child.get_node( "skeleton/AnimationPlayer" ).playback_speed = speed
if child.get_node( "skeleton/AnimationPlayer" ).has_animation( anim_name ):
child.get_node( "skeleton/AnimationPlayer" ).play( anim_name )
for child in $hair_parts.get_children():
child.get_node( "skeleton/AnimationPlayer" ).playback_speed = speed
if child.get_node( "skeleton/AnimationPlayer" ).has_animation( anim_name ):
child.get_node( "skeleton/AnimationPlayer" ).play( anim_name )
for child in $cloths.get_children():
child.get_node( "skeleton/AnimationPlayer" ).playback_speed = speed
if child.get_node( "skeleton/AnimationPlayer" ).has_animation( anim_name ):
child.get_node( "skeleton/AnimationPlayer" ).play( anim_name )
func play_backwards( anim_name, speed = 1.0 ):
for child in $body_parts.get_children():
child.get_node( "skeleton/AnimationPlayer" ).playback_speed = speed
if child.get_node( "skeleton/AnimationPlayer" ).has_animation( anim_name ):
child.get_node( "skeleton/AnimationPlayer" ).play_backwards( anim_name )
for child in $hair_parts.get_children():
child.get_node( "skeleton/AnimationPlayer" ).playback_speed = speed
if child.get_node( "skeleton/AnimationPlayer" ).has_animation( anim_name ):
child.get_node( "skeleton/AnimationPlayer" ).play_backwards( anim_name )
for child in $cloths.get_children():
child.get_node( "skeleton/AnimationPlayer" ).playback_speed = speed
if child.get_node( "skeleton/AnimationPlayer" ).has_animation( anim_name ):
child.get_node( "skeleton/AnimationPlayer" ).play_backwards( anim_name )
func can_turn():
var animation_player = $body_parts/body/skeleton/AnimationPlayer
return (animation_player.current_animation.begins_with( "turn_") and animation_player.current_animation_position >= 1.2) or (not animation_player.current_animation.begins_with( "turn_") and not animation_player.current_animation.begins_with( "idle"))
func equip( p_item, p_slot = null ):
var model = load( p_item.get_data( "model_equiped", "") )
if not model:
model = load( p_item.get_data( "model", "") )
if model:
model = model.instance()
var attachment = null
if not p_slot:
p_slot = p_item.get_data( "default_slot" )
if p_slot and int(p_slot) in self.slots:
self.unequip( p_slot )
attachment = self.slots[ int(p_slot) ].attachment
self.slots[ int(p_slot) ].item = p_item
if attachment:
if attachment == "cloths":
model.name = str(int(p_slot))
$cloths.add_child( model )
var animation = $body_parts/body/skeleton/AnimationPlayer.current_animation
var animation_position = $body_parts/body/skeleton/AnimationPlayer.current_animation_position
var animation_speed = $body_parts/body/skeleton/AnimationPlayer.playback_speed
model.get_node( "skeleton/AnimationPlayer" ).stop()
model.get_node( "skeleton/AnimationPlayer" ).set( "playback_speed", animation_speed )
model.get_node( "skeleton/AnimationPlayer" ).play( animation )
model.get_node( "skeleton/AnimationPlayer" ).advance( animation_position )
else:
if $body_parts/body/skeleton.has_node( attachment ):
$body_parts/body/skeleton.get_node( attachment ).get_node( "handle" ).add_child( model )
emit_signal( "equip", int(p_slot), p_item )
func unequip( p_slot ):
if p_slot and int(p_slot) in self.slots:
if self.slots[ int(p_slot) ].item:
var attachment = self.slots[ int(p_slot) ].attachment
self.slots[ int(p_slot) ].item = null
if attachment:
if attachment == "cloths":
var node = $cloths.get_node( str(p_slot) )
if node:
$cloths.remove_child( node )
node.queue_free()
else:
for child in $body_parts/body/skeleton.get_node( attachment ).get_node( "handle" ).get_children():
$body_parts/body/skeleton.get_node( attachment ).get_node( "handle" ).remove_child( child )
child.queue_free()
emit_signal( "unequip", int(p_slot) )
func drop_item( p_item ):
var new_inventory = self.creature.get_data( "inventory" )
var item_index = new_inventory.find( p_item )
if item_index != -1:
if new_inventory[ item_index ].get_data( "dropable", true ):
new_inventory[ item_index ].set_data( "number", new_inventory[ item_index ].get_data( "number" )-1 )
if new_inventory[ item_index ].get_data( "number" ) <= 0:
new_inventory.remove( item_index )
var pickup_item_node = preload( "res://scenes/items/pickup_item.tscn" ).instance()
pickup_item_node.item_filename = p_item.get_filename()
pickup_item_node.global_transform = $drop_point.global_transform
var pickup_item = Datas.PickupItem.new( p_item.get_filename(), $drop_point.global_transform, p_item )
pickup_item_node.item = pickup_item
if self.get_tree().get_root().has_node( "main/scene/game" ):
self.get_tree().get_root().get_node( "main/scene/game" ).get_node( "pickups" ).add_child( pickup_item_node )
func _on_body_animation_finished( anim_name ):
emit_signal( "animation_finished", anim_name )
func _on_body_limb_body_entered(limb, body):
if body.is_in_group( "bullet" ):
if limb == Datas.Cloth.LIMB.head:
var blood_particle = preload( "res://scenes/fx/blood_particles.tscn" ).instance()
$body_parts/body/skeleton/limb_head.add_child( blood_particle )
blood_particle.emit()
emit_signal( "is_dead" )
self.creature.set_data( "current_life", 0.0 )
else:
var blood_particle = preload( "res://scenes/fx/blood_particles.tscn" ).instance()
$body_parts/body/skeleton/limb_torso_1.add_child( blood_particle )
blood_particle.emit()