Added the body.gd from the 3d project. Started refactoring it.

This commit is contained in:
Relintai 2021-09-21 13:36:11 +02:00
parent fabd04833d
commit de1971545a
4 changed files with 822 additions and 67 deletions

View File

@ -1,6 +1,6 @@
[gd_scene load_steps=4 format=2]
[ext_resource path="res://player/Body.gd" type="Script" id=1]
[ext_resource path="res://player/3DBody.gd" type="Script" id=1]
[ext_resource path="res://modules/species/Human/models/armature_huf.tscn" type="PackedScene" id=2]
[sub_resource type="CapsuleShape" id=1]

View File

@ -1,6 +1,5 @@
[gd_scene load_steps=42 format=2]
[gd_scene load_steps=41 format=2]
[ext_resource path="res://modules/species/Human/visuals/test_base_v3.tres" type="ModelVisual" id=1]
[ext_resource path="res://player/CharacterSkeletonAttachPoint.gd" type="Script" id=2]
[ext_resource path="res://modules/species/Human/models/dead.tres" type="Animation" id=4]
[ext_resource path="res://modules/species/Human/models/armature.gd" type="Script" id=5]
@ -138,29 +137,18 @@ attach_point_paths/9_weapon_left_back = NodePath("armature/Armature/Skeleton/Lef
attach_point_paths/10_weapon_right_back = NodePath("armature/Armature/Skeleton/RightWeaponBack")
attach_point_paths/11_weapon_shield_left = NodePath("armature/Armature/Skeleton/Shield")
script = ExtResource( 8 )
automatic_build = true
mesh_instance_path = NodePath("armature/Armature/Skeleton/MeshInstance")
skeleton_path = NodePath("armature/Armature/Skeleton")
viss = [ ExtResource( 1 ) ]
[node name="armature" parent="." instance=ExtResource( 6 )]
script = ExtResource( 5 )
[node name="Skeleton" parent="armature/Armature" index="0"]
transform = Transform( 1, -6.84064e-18, 1.54087e-25, 6.84064e-18, 1, 1.05378e-42, -1.54087e-25, 0, 1, 0, 0, 0 )
bones/0/bound_children = [ NodePath("RootAttachment") ]
bones/1/bound_children = [ NodePath("RightHipAttachment"), NodePath("LeftHipAttachment") ]
bones/10/bound_children = [ NodePath("RightBackAttachment"), NodePath("LeftBackAttachment") ]
bones/14/bound_children = [ NodePath("RightHandAttachment") ]
bones/21/bound_children = [ NodePath("ShieldAttachment") ]
bones/22/bound_children = [ NodePath("LeftHandAttachment") ]
bones/27/bound_children = [ NodePath("NeckAttachment") ]
[node name="MeshInstance" type="TMeshInstance" parent="armature/Armature/Skeleton" index="0"]
[node name="MeshInstance" type="MeshInstance" parent="armature/Armature/Skeleton" index="0"]
visible = false
[node name="LeftHandAttachment" type="BoneAttachment" parent="armature/Armature/Skeleton" index="1"]
transform = Transform( 0.190725, 0.147102, -0.970559, 0.0931773, -0.986957, -0.131278, -0.97721, -0.065396, -0.201944, 0.244939, 0.774361, -0.0217047 )
transform = Transform( 0.188157, 0.145046, -0.971369, 0.0882702, -0.98753, -0.130361, -0.978163, -0.0612146, -0.198614, 0.244036, 0.773574, -0.0200296 )
bone_name = "left_hand"
[node name="LeftHandAP" type="Spatial" parent="armature/Armature/Skeleton/LeftHandAttachment"]
@ -168,14 +156,14 @@ transform = Transform( -0.0646502, 0.683772, 0.726832, -0.148514, -0.726783, 0.6
script = ExtResource( 2 )
[node name="ShieldAttachment" type="BoneAttachment" parent="armature/Armature/Skeleton" index="2"]
transform = Transform( -0.0433176, 0.125638, 0.99113, -0.0774754, -0.989496, 0.122045, 0.996052, -0.0715015, 0.0525965, 0.214702, 1.0125, -0.00449639 )
transform = Transform( -0.0406676, 0.12356, 0.991503, -0.0727625, -0.990056, 0.120395, 0.996519, -0.0672481, 0.0492538, 0.214299, 1.01185, -0.00384494 )
bone_name = "left_forearm"
[node name="LeftHandShield" type="Spatial" parent="armature/Armature/Skeleton/ShieldAttachment"]
transform = Transform( 0.122585, 0.992132, -0.0242311, 0.987746, -0.124342, -0.0941345, -0.0964273, -0.0123912, -0.995239, 0.0157415, 0.108918, 0.0213161 )
[node name="RightHandAttachment" type="BoneAttachment" parent="armature/Armature/Skeleton" index="3"]
transform = Transform( -0.0270232, -0.0924788, -0.995348, -0.0997119, -0.990496, 0.0947352, -0.99465, 0.101808, 0.0175453, -0.248237, 0.77238, -0.0111511 )
transform = Transform( -0.0370837, -0.0893548, -0.995309, -0.0909409, -0.99156, 0.0924068, -0.995166, 0.0939413, 0.0286449, -0.248013, 0.771704, -0.00950206 )
bone_name = "right_hand"
[node name="RightHandAP" type="Spatial" parent="armature/Armature/Skeleton/RightHandAttachment"]
@ -191,7 +179,7 @@ transform = Transform( 1, 0, 0, 0, -1.62921e-07, 1, 0, -1, -1.62921e-07, 0, 0, 0
script = ExtResource( 2 )
[node name="NeckAttachment" type="BoneAttachment" parent="armature/Armature/Skeleton" index="5"]
transform = Transform( 0.999992, 0.00218317, -0.00333621, -0.00195909, 0.997835, 0.0657461, 0.00347252, -0.065739, 0.997831, 0.000876503, 1.31821, 0.00123351 )
transform = Transform( 0.999994, 0.00106162, -0.00327271, -0.000842232, 0.997799, 0.0663152, 0.00333591, -0.066312, 0.997793, 0.000883431, 1.31766, 0.000961275 )
bone_name = "neck"
[node name="TorsoAP" type="Spatial" parent="armature/Armature/Skeleton/NeckAttachment"]
@ -199,28 +187,28 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.04, -0.09 )
script = ExtResource( 2 )
[node name="RightHipAttachment" type="BoneAttachment" parent="armature/Armature/Skeleton" index="6"]
transform = Transform( 0.999504, 0.0314799, 1.49316e-08, -0.0314799, 0.999504, 4.7162e-07, -7.76685e-11, -4.71856e-07, 1, 0, 0.776724, 0 )
transform = Transform( 0.999554, 0.0298487, 1.41579e-08, -0.0298487, 0.999554, 4.71643e-07, -7.36469e-11, -4.71856e-07, 1, 0.000646701, 0.77617, 0 )
bone_name = "pelvis"
[node name="Spatial" type="Spatial" parent="armature/Armature/Skeleton/RightHipAttachment"]
transform = Transform( 0.993372, -0.0110498, 0.114411, 0.0109836, 0.999939, 0.00120905, -0.114417, 5.56017e-05, 0.993433, -0.141825, 0.176866, 0.16621 )
[node name="LeftHipAttachment" type="BoneAttachment" parent="armature/Armature/Skeleton" index="7"]
transform = Transform( 0.999504, 0.0314799, 1.49316e-08, -0.0314799, 0.999504, 4.7162e-07, -7.76685e-11, -4.71856e-07, 1, 0, 0.776724, 0 )
transform = Transform( 0.999554, 0.0298487, 1.41579e-08, -0.0298487, 0.999554, 4.71643e-07, -7.36469e-11, -4.71856e-07, 1, 0.000646701, 0.77617, 0 )
bone_name = "pelvis"
[node name="Spatial" type="Spatial" parent="armature/Armature/Skeleton/LeftHipAttachment"]
transform = Transform( 0.99343, -0.00240871, -0.114417, 0.00240855, 0.999997, -0.000139626, 0.114417, -0.00013687, 0.993433, 0.144429, 0.18077, 0.165634 )
[node name="RightBackAttachment" type="BoneAttachment" parent="armature/Armature/Skeleton" index="8"]
transform = Transform( 0.999993, 0.00165794, -0.00337115, -0.00168643, 0.999963, -0.00847085, 0.00335698, 0.00847647, 0.999959, 0.000565477, 1.13062, -0.000356666 )
transform = Transform( 0.999994, 0.000544192, -0.00322462, -0.000569631, 0.999969, -0.00790054, 0.00322021, 0.00790234, 0.999964, 0.000781342, 1.13007, -0.000521188 )
bone_name = "spine_2"
[node name="RightBackAP" type="Spatial" parent="armature/Armature/Skeleton/RightBackAttachment"]
transform = Transform( 0.999997, -0.00244424, -0.000479204, 0.00243884, 0.999966, -3.47705e-05, 0.000479292, 3.44458e-05, 1, 0.000560207, -0.00363708, -0.120332 )
[node name="LeftBackAttachment" type="BoneAttachment" parent="armature/Armature/Skeleton" index="9"]
transform = Transform( 0.999993, 0.00165794, -0.00337115, -0.00168643, 0.999963, -0.00847085, 0.00335698, 0.00847647, 0.999959, 0.000565477, 1.13062, -0.000356666 )
transform = Transform( 0.999994, 0.000544192, -0.00322462, -0.000569631, 0.999969, -0.00790054, 0.00322021, 0.00790234, 0.999964, 0.000781342, 1.13007, -0.000521188 )
bone_name = "spine_2"
[node name="LeftBackAP" type="Spatial" parent="armature/Armature/Skeleton/LeftBackAttachment"]
@ -229,26 +217,21 @@ transform = Transform( 0.999994, -0.00350821, -0.000687771, 0.00350043, 0.999949
[node name="LeftWeapon" type="Spatial" parent="armature/Armature/Skeleton" index="10"]
transform = Transform( 0.993142, 0.0242247, -0.11438, -0.0240499, 0.999706, 0.00290812, 0.114417, -0.000137342, 0.993433, 0.151114, 0.951937, 0.165634 )
script = ExtResource( 2 )
positions = [ NodePath("../LeftHipAttachment/Spatial"), NodePath("../LeftHandAttachment/LeftHandAP") ]
[node name="RightWeapon" type="Spatial" parent="armature/Armature/Skeleton" index="11"]
script = ExtResource( 2 )
positions = [ NodePath("../RightHipAttachment/Spatial"), NodePath("../RightHandAttachment/RightHandAP") ]
[node name="LeftWeaponBack" type="Spatial" parent="armature/Armature/Skeleton" index="12"]
transform = Transform( 1, 0, 0, 0, 0.999886, 0, 0, 0, 1, 0, 0, 0 )
script = ExtResource( 2 )
positions = [ NodePath("../LeftBackAttachment/LeftBackAP"), NodePath("../LeftHandAttachment/LeftHandAP") ]
[node name="Shield" type="Spatial" parent="armature/Armature/Skeleton" index="13"]
transform = Transform( 1, 0, 0, 0, 0.999886, 0, 0, 0, 1, 0, 0, 0 )
script = ExtResource( 2 )
positions = [ NodePath("../LeftBackAttachment/LeftBackAP"), NodePath("../ShieldAttachment/LeftHandShield") ]
[node name="RightWeaponBack" type="Spatial" parent="armature/Armature/Skeleton" index="14"]
transform = Transform( 1, 0, 0, 0, 0.999899, 0, 0, 0, 1, 0, 0, 0 )
script = ExtResource( 2 )
positions = [ NodePath("../RightBackAttachment/RightBackAP"), NodePath("../RightHandAttachment/RightHandAP") ]
[node name="AnimationPlayer" parent="armature" index="1"]
autoplay = "rest"

805
game/player/3DBody.gd Normal file
View File

@ -0,0 +1,805 @@
# Copyright Péter Magyar relintai@gmail.com
# MIT License, functionality from this class needs to be protable to the entity spell system
# Copyright (c) 2019-2021 Péter Magyar
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
extends KinematicBody
export(float) var max_visible_distance : float = 120 setget set_max_visible_distance
var max_visible_distance_squared : float = max_visible_distance * max_visible_distance
export(float) var MOUSE_SENSITIVITY : float = 0.05
export(String) var world_path : String = "../.."
export(NodePath) var contact_path : NodePath = "Contact"
export(NodePath) var model_path : NodePath = "Rotation_Helper/Model"
export(NodePath) var character_skeleton_path : NodePath = "Rotation_Helper/Model/character"
const ray_length = 1000
const MAX_SLOPE_ANGLE : float = deg2rad(70.0)
const MOUSE_TARGET_MAX_OFFSET : int = 10
#flying
const FLY_ACCEL = 8
var flying : bool = false
#waling
const GRAVITY : float = -24.8
const MAX_SPEED = 10
const MAX_RUNNING_SPEED = 16
const ACCEL : float = 100.0
const DEACCEL : float = 100.0
#jumping
var jump_height = 7.6
var has_contact : bool = false
var double_jumped : bool = false
var _on : bool = true
var _controlled : bool = false
var y_rot : float = 0.0
var vel : Vector3 = Vector3()
var dir : Vector3 = Vector3()
var input_dir : Vector2 = Vector2()
var mouse_dir : Vector2 = Vector2()
var mouse_move_dir : Vector2 = Vector2()
var mouse_left_down : bool = false
var mouse_right_down : bool = false
var touchpad_dir : Vector2 = Vector2()
var mouse_down_delta : Vector2 = Vector2()
var queued_camera_rotaions : Vector2 = Vector2()
var target_movement_direction : Vector2 = Vector2()
var key_left : bool = false
var key_right : bool = false
var key_up : bool = false
var key_down : bool = false
var key_jump : bool = false
var cursor_grabbed : bool = false
var last_cursor_pos : Vector2 = Vector2()
var mouse_down_pos : Vector2 = Vector2()
var total_down_mouse_delta : Vector2 = Vector2()
var camera : Camera
var camera_pivot : Spatial
var animation_tree : AnimationTree
var anim_node_state_machine : AnimationNodeStateMachinePlayback = null
var animation_run : bool = false
var moving : bool = false
var casting_anim : bool = false
var sleep : bool = false
var sleep_recheck_timer : float = 0
var dead : bool = false
var death_timer : float = 0
var last_mouse_over : Entity = null
var entity : Entity
var model_rotation_node : Spatial
var character_skeleton : CharacterSkeleton3D
var visibility_update_timer : float = 0
var placed : bool = false
var just_place : bool = false
var contact : RayCast = null
var _nameplate : Node = null
#var los : bool = false
func _ready() -> void:
camera = get_node_or_null("CameraPivot/Camera") as Camera
camera_pivot = get_node_or_null("CameraPivot") as Spatial
model_rotation_node = get_node(model_path)
character_skeleton = get_node(character_skeleton_path)
contact = get_node(contact_path)
entity = get_node("..")
entity.set_character_skeleton(character_skeleton)
entity.connect("notification_ccast", self, "on_notification_ccast")
entity.connect("diesd", self, "on_diesd")
entity.connect("onc_entity_controller_changed", self, "on_c_controlled_changed")
owner = entity
on_c_controlled_changed()
transform = entity.get_transform_3d(true)
animation_tree = character_skeleton.get_animation_tree()
if animation_tree != null:
anim_node_state_machine = animation_tree["parameters/playback"]
animation_tree["parameters/run-loop/blend_position"] = Vector2(0, -1)
# set_process(false)
# set_process_input(false)
# set_process_unhandled_input(false)
func _enter_tree():
set_physics_process(true)
func _process(delta : float) -> void:
if entity.ai_state == EntityEnums.AI_STATE_OFF:
return
visibility_update_timer += delta
if visibility_update_timer < 1:
return
visibility_update_timer = 0
var camera : Camera = get_tree().get_root().get_camera() as Camera
if camera == null:
return
var cam_pos : Vector3 = camera.global_transform.xform(Vector3())
var dstv : Vector3 = cam_pos - translation
dstv.y = 0
var dst : float = dstv.length_squared()
if dst > max_visible_distance_squared:
if visible:
hide()
#todo check whether its needed or not
#contact.enabled = false
return
else:
# var lod_level : int = int(dst / max_visible_distance_squared * 3.0)
if dst < 400: #20^2
entity.get_character_skeleton().set_lod_level(0)
elif dst > 400 and dst < 900: #20^2, 30^2
entity.get_character_skeleton().set_lod_level(1)
else:
entity.get_character_skeleton().set_lod_level(2)
if not visible:
show()
#contact.enabled = true
func _physics_process(delta : float) -> void:
if not _on:
return
if entity.sentity_data == null:
return
if dead:
return
if not placed:
placed = true
if entity.getc_is_controlled():
process_input(delta)
process_movement_player(delta)
else:
# var camera : Camera = get_tree().get_root().get_camera() as Camera
#
# if camera != null:
# var res = get_world().get_direct_space_state().intersect_ray(get_transform().origin, camera.transform.origin, [ self ], 1)
#
# if res:
# los = true
# else:
# los = false
if sleep:
sleep_recheck_timer += delta
if sleep_recheck_timer < 0.5:
return
sleep_recheck_timer = 0
process_movement_mob(delta)
func process_input(delta: float) -> void:
var key_dir : Vector2 = Vector2()
if key_up:
key_dir.y -= 1
if key_down:
key_dir.y += 1
if key_left:
key_dir.x -= 1
if key_right:
key_dir.x += 1
input_dir = key_dir + mouse_dir + touchpad_dir + mouse_move_dir
var state : int = entity.getc_state()
if state & EntityEnums.ENTITY_STATE_TYPE_FLAG_ROOT != 0 or state & EntityEnums.ENTITY_STATE_TYPE_FLAG_STUN != 0:
input_dir = Vector2()
return
var input_length : float = input_dir.length_squared()
if input_length > 0.1:
if anim_node_state_machine != null and not animation_run:
anim_node_state_machine.travel("run-loop")
animation_run = true
input_dir = input_dir.normalized()
animation_tree["parameters/run-loop/blend_position"] = input_dir
if (input_dir.y < 0.1):
model_rotation_node.transform.basis = Basis(Vector3(0, acos(input_dir.x) - PI / 2.0, 0))
else:
model_rotation_node.transform.basis = Basis()
else:
if anim_node_state_machine != null and animation_run:
anim_node_state_machine.travel("idle-loop")
animation_run = false
if queued_camera_rotaions.length_squared() > 1:
camera_pivot.rotate_delta(queued_camera_rotaions.x * 2.0, queued_camera_rotaions.y)
queued_camera_rotaions = Vector2()
if input_length > 0.1:
rotate_delta(camera_pivot.get_y_rot())
camera_pivot.set_y_rot(0.0)
func process_movement_player(delta : float) -> void:
var state : int = entity.getc_state()
if state & EntityEnums.ENTITY_STATE_TYPE_FLAG_ROOT != 0 or state & EntityEnums.ENTITY_STATE_TYPE_FLAG_STUN != 0:
moving = false
key_jump = false
return
if flying:
player_fly(delta)
else:
player_walk(delta)
func player_fly(delta : float) -> void:
if input_dir.x > 0.1 or input_dir.y > 0.1 or input_dir.x < -0.1 or input_dir.y < -0.1:
var forward : Vector3 = Vector3(0, 0, 1).rotated(Vector3(0, 1, 0), deg2rad(y_rot))
var right : Vector3 = forward.cross(Vector3(0, 1, 0)) * -input_dir.x
forward *= input_dir.y #only potentially make it zero after getting the right vector
dir = forward
dir += right
if dir.length_squared() > 0.1:
dir = dir.normalized()
moving = true
entity.moved()
else:
dir = Vector3()
moving = false
return
if key_jump:
dir.y += 1
# var hvel : Vector3 = vel
# hvel.y = 0
#
# var target : Vector3 = dir
# target *= entity.getc_speed().current_value / 100.0 * 4.2
#
# var accel
# if dir.dot(hvel) > 0:
# accel = ACCEL
# else:
# accel = DEACCEL
#
# hvel = hvel.linear_interpolate(target, accel * delta) as Vector3
# vel.x = hvel.x
# vel.z = hvel.z
# vel = move_and_slide(vel, Vector3(0,1,0), true, 4, deg2rad(MAX_SLOPE_ANGLE))
var target : Vector3 = dir * entity.getc_speed().current_value / 100.0 * 4.2
vel = vel.linear_interpolate(target, FLY_ACCEL * delta)
move_and_slide(vel)
crequest_set_position(translation, rotation)
func player_walk(delta : float) -> void:
if input_dir.x > 0.1 or input_dir.y > 0.1 or input_dir.x < -0.1 or input_dir.y < -0.1:
var forward : Vector3 = Vector3(0, 0, 1).rotated(Vector3(0, 1, 0), deg2rad(y_rot))
var right : Vector3 = forward.cross(Vector3(0, 1, 0)) * -input_dir.x
forward *= input_dir.y #only potentially make it zero after getting the right vector
dir = forward
dir += right
if dir.length_squared() > 0.1:
dir = dir.normalized()
moving = true
entity.moved()
else:
dir = Vector3()
moving = false
if is_on_floor():
has_contact = true
else:
if !contact.is_colliding():
has_contact = false
if has_contact and !is_on_floor():
move_and_collide(Vector3(0, -1, 0))
vel.y += delta * GRAVITY
var hvel : Vector3 = vel
hvel.y = 0
var target : Vector3 = dir
target *= entity.getc_speed().current_value / 100.0 * 4.2
var accel
if dir.dot(hvel) > 0:
accel = ACCEL
else:
accel = DEACCEL
hvel = hvel.linear_interpolate(target, accel * delta) as Vector3
vel.x = hvel.x
vel.z = hvel.z
if has_contact and key_jump:
key_jump = false
vel.y = jump_height
has_contact = false
# if not foot_audio.playing and last_sound_timer >= MIN_SOUND_TIME_LIMIT:
# foot_audio.play()
# last_sound_timer = 0
#
# step_timer = 0
vel = move_and_slide(vel, Vector3(0,1,0), true, 4, MAX_SLOPE_ANGLE)
# if not has_contact and is_on_floor():
# if not foot_audio.playing and last_sound_timer >= MIN_SOUND_TIME_LIMIT:
# foot_audio.play()
# last_sound_timer = 0
#
# step_timer = 0
# var v : Vector3 = vel
# v.y = 0
# if has_contact and v.length() > 1:
# step_timer += delta
#
# if step_timer >= WALK_STEP_TIME:
# step_timer = 0
#
# if not foot_audio.playing and last_sound_timer >= MIN_SOUND_TIME_LIMIT:
# foot_audio.play()
# last_sound_timer = 0
crequest_set_position(translation, rotation)
func process_movement_mob(delta : float) -> void:
if entity.starget != null:
look_at(entity.starget.get_body().translation, Vector3(0, 1, 0))
var state : int = entity.getc_state()
if state & EntityEnums.ENTITY_STATE_TYPE_FLAG_ROOT != 0 or state & EntityEnums.ENTITY_STATE_TYPE_FLAG_STUN != 0:
moving = false
return
if target_movement_direction.length_squared() > 0.1:
if anim_node_state_machine != null and not animation_run:
anim_node_state_machine.travel("run-loop")
animation_run = true
target_movement_direction = target_movement_direction.normalized()
moving = true
else:
if anim_node_state_machine != null and animation_run:
anim_node_state_machine.travel("idle-loop")
animation_run = false
moving = false
if target_movement_direction.x > 0.1 or target_movement_direction.y > 0.1 or target_movement_direction.x < -0.1 or target_movement_direction.y < -0.1:
y_rot = Vector2(0, 1).angle_to(target_movement_direction)
var forward : Vector3 = Vector3(0, 0, 1).rotated(Vector3(0, 1, 0), deg2rad(y_rot))
var right : Vector3 = forward.cross(Vector3(0, 1, 0)) * -target_movement_direction.x
forward *= target_movement_direction.y #only potentially make it zero after getting the right vector
dir = forward
dir += right
if dir.length_squared() > 0.1:
dir = dir.normalized()
moving = true
else:
dir = Vector3()
moving = false
if not moving and sleep:
return
if moving and sleep:
sleep = false
if is_on_floor():
has_contact = true
else:
if !contact.is_colliding():
has_contact = false
if has_contact and !is_on_floor():
move_and_collide(Vector3(0, -1, 0))
vel.y += delta * GRAVITY
var hvel : Vector3 = vel
hvel.y = 0
var target : Vector3 = dir
target *= entity.getc_speed().current_value / 100.0 * 4.2
var accel
if dir.dot(hvel) > 0:
accel = ACCEL
else:
accel = DEACCEL
hvel = hvel.linear_interpolate(target, accel * delta) as Vector3
vel.x = hvel.x
vel.z = hvel.z
var facing : Vector3 = vel
facing.y = 0
vel = move_and_slide(vel, Vector3(0,1,0), true, 4, MAX_SLOPE_ANGLE)
crequest_set_position(translation, rotation)
if vel.length_squared() < 0.12:
sleep = true
if translation.y < -2000.0:
print("killed mob with fall damage")
var sdi : SpellDamageInfo = SpellDamageInfo.new()
sdi.damage_source_type = SpellDamageInfo.DAMAGE_SOURCE_UNKNOWN
sdi.damage = 999999999
entity.stake_damage(sdi)
func _input(event: InputEvent) -> void:
if not cursor_grabbed:
set_process_input(false)
return
if event is InputEventMouseMotion and event.device != -1:
var s : float = ProjectSettings.get("display/mouse_cursor/sensitivity")
var relx : float = event.relative.x * s
var rely : float = event.relative.y * s
mouse_down_delta.x += relx
mouse_down_delta.y += rely
total_down_mouse_delta.x += relx
total_down_mouse_delta.y += rely
get_tree().set_input_as_handled()
if (mouse_right_down or mouse_left_down) and event.device != -1:
if mouse_right_down:
camera_pivot.rotate_delta(0.0, -event.relative.y)
rotate_delta(-event.relative.x)
else:
camera_pivot.rotate_delta(-relx, -rely)
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_type():
if event.is_action("move_forward"):
key_up = event.pressed
get_tree().set_input_as_handled()
return
elif event.is_action("move_backward"):
key_down = event.pressed
get_tree().set_input_as_handled()
return
elif event.is_action("move_left"):
key_left = event.pressed
get_tree().set_input_as_handled()
return
elif event.is_action("move_right"):
key_right = event.pressed
get_tree().set_input_as_handled()
return
elif event.is_action("jump"):
key_jump = event.pressed
get_tree().set_input_as_handled()
return
if event is InputEventMouseMotion and not (mouse_right_down or mouse_left_down) and event.device != -1:
cmouseover(event)
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT and event.device != -1:
mouse_left_down = event.pressed
if mouse_left_down:
mouse_down_delta = Vector2()
mouse_down_pos = event.position
if event.button_index == BUTTON_RIGHT and event.device != -1:
mouse_right_down = event.pressed
if mouse_right_down:
rotate_delta(camera_pivot.get_y_rot())
camera_pivot.set_y_rot(0.0)
if mouse_left_down and mouse_right_down:
mouse_move_dir.y = -1
else:
mouse_move_dir.y = 0
if event.is_pressed() and event.device != -1:
if event.button_index == BUTTON_WHEEL_UP:
camera_pivot.camera_distance_set_delta(-0.2)
if event.button_index == BUTTON_WHEEL_DOWN:
camera_pivot.camera_distance_set_delta(0.2)
if not event.pressed and event.button_index == BUTTON_LEFT and event.device != -1:
if mouse_down_delta.length() < MOUSE_TARGET_MAX_OFFSET:
target(event.position)
if event is InputEventScreenTouch and event.pressed:
target(event.position)
update_cursor_mode()
func update_cursor_mode():
if mouse_left_down or mouse_right_down:
if not cursor_grabbed:
set_process_input(true)
total_down_mouse_delta = Vector2()
cursor_grabbed = true
last_cursor_pos = get_viewport().get_mouse_position()
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
else:
if cursor_grabbed:
set_process_input(false)
cursor_grabbed = false
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
get_viewport().warp_mouse(last_cursor_pos)
if total_down_mouse_delta.length_squared() < 8:
target(last_cursor_pos)
func rotate_delta(x_delta : float) -> void:
y_rot += x_delta
while y_rot > 360:
y_rot -= 360
while y_rot < 0:
y_rot += 360
rotation_degrees = Vector3(0.0, y_rot, 0.0)
func target(position : Vector2):
var from = camera.project_ray_origin(position)
var to = from + camera.project_ray_normal(position) * ray_length
var space_state = get_world().direct_space_state
var result = space_state.intersect_ray(from, to, [], collision_mask)
if result:
if result.collider and result.collider.owner is Entity:
var ent : Entity = result.collider.owner as Entity
entity.target_crequest_change(ent.get_path())
return
entity.target_crequest_change(NodePath())
else:
entity.target_crequest_change(NodePath())
func cmouseover(event):
var from = camera.project_ray_origin(event.position)
var to = from + camera.project_ray_normal(event.position) * ray_length
var space_state = get_world().direct_space_state
var result = space_state.intersect_ray(from, to, [], collision_mask)
if result:
if result.collider:# and result.collider.owner is Entity:
var mo : Entity = result.collider.owner as Entity
if mo == null:
return
if last_mouse_over != null and last_mouse_over != mo:
if is_instance_valid(last_mouse_over):
last_mouse_over.notification_cmouse_exit()
last_mouse_over = null
if last_mouse_over == null:
mo.notification_cmouse_enter()
last_mouse_over = mo
return
if last_mouse_over != null:
last_mouse_over.notification_cmouse_exit()
last_mouse_over = null
func analog_force_change(vector, touchpad):
if touchpad.padname == "TouchPad":
touchpad_dir = vector
touchpad_dir.y *= -1
elif touchpad.padname == "TargetPad":
#try to target
return
func queue_camera_rotation(rot : Vector2) -> void:
queued_camera_rotaions += rot
remote func sset_position(position : Vector3, protation : Vector3) -> void:
if multiplayer.network_peer and multiplayer.is_network_server():
for i in range(entity.seen_by_gets_count()):
var e : Entity = entity.seen_by_gets(i)
if e == entity:
#todo make sure this doesn't happen!
continue
if is_instance_valid(e):
var nm : int = e.get_network_master()
if nm != 1:
rpc_id(nm, "cset_position", position, protation)
#if _controlled && get_network_master() == 1:
cset_position(position, protation)
func crequest_set_position(position : Vector3, protation : Vector3) -> void:
if multiplayer.network_peer && !multiplayer.is_network_server():
rpc_id(1, "sset_position", translation, protation)
else:
sset_position(position, protation)
remote func cset_position(position : Vector3, protation : Vector3) -> void:
translation = position
rotation = protation
func on_notification_ccast(what : int, info : SpellCastInfo) -> void:
if what == SpellEnums.NOTIFICATION_CAST_STARTED:
if anim_node_state_machine != null and not casting_anim:
anim_node_state_machine.travel("casting-loop")
casting_anim = true
animation_run = false
elif what == SpellEnums.NOTIFICATION_CAST_FAILED:
if anim_node_state_machine != null and casting_anim:
anim_node_state_machine.travel("idle-loop")
casting_anim = false
if animation_run:
anim_node_state_machine.travel("run-loop")
elif what == SpellEnums.NOTIFICATION_CAST_FINISHED:
if anim_node_state_machine != null:
anim_node_state_machine.travel("cast-end")
casting_anim = false
if animation_run:
anim_node_state_machine.travel("run-loop")
elif what == SpellEnums.NOTIFICATION_CAST_SUCCESS:
if anim_node_state_machine != null:
anim_node_state_machine.travel("cast-end")
casting_anim = false
if animation_run:
anim_node_state_machine.travel("run-loop")
func on_c_controlled_changed():
#create camera and pivot if true
_controlled = entity.getc_is_controlled()
if _controlled:
if _nameplate:
_nameplate.queue_free()
var cam_scene : PackedScene = ResourceLoader.load("res://player/camera/CameraPivot.tscn")
camera_pivot = cam_scene.instance() as Spatial
add_child(camera_pivot)
camera = camera_pivot.get_node("Camera") as Camera
# var uiscn : PackedScene = ResourceLoader.load("res://ui/player_ui/player_ui.tscn")
# var ui = uiscn.instance()
var ui = DataManager.request_instance(DataManager.PLAYER_UI_INSTANCE)
add_child(ui)
set_process_input(true)
set_process_unhandled_input(true)
else:
if camera_pivot:
camera_pivot.queue_free()
camera_pivot = null
camera = null
set_process_input(false)
set_process_unhandled_input(false)
var nameplatescn : PackedScene = ResourceLoader.load("res://ui/nameplates/NamePlate.tscn")
_nameplate = nameplatescn.instance()
add_child(_nameplate)
func on_diesd(entity):
if dead:
return
dead = true
anim_node_state_machine.travel("dead")
set_physics_process(false)
remote func set_position(position : Vector3, rotation : Vector3) -> void:
if get_tree().is_network_server():
rpc("set_position", position, rotation)
func set_max_visible_distance(var value : float) -> void:
max_visible_distance_squared = value * value
max_visible_distance = value
func teleport(teleport_to : Vector3):
transform.origin = teleport_to
placed = false
# just_place = true
func get_entity() -> Entity:
return entity

View File

@ -1,14 +1,10 @@
[gd_scene load_steps=14 format=2]
[gd_scene load_steps=12 format=2]
[ext_resource path="res://spritesheet_generator_3d/Spatial.tscn" type="PackedScene" id=1]
[ext_resource path="res://spritesheet_generator_3d/Spatial.tscn" type="PackedScene" id=2]
[ext_resource path="res://spritesheet_generator/SheetGen.gd" type="Script" id=3]
[ext_resource path="res://spritesheet_generator_3d/Spatial.tscn" type="PackedScene" id=4]
[ext_resource path="res://spritesheet_generator/SpriteMover.gd" type="Script" id=5]
[ext_resource path="res://spritesheet_generator/Sprite3Ds.tscn" type="PackedScene" id=6]
[sub_resource type="ProceduralSky" id=1]
[sub_resource type="Environment" id=2]
@ -51,17 +47,14 @@ params_billboard_mode = 1
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
environment = SubResource( 2 )
[node name="player2" parent="." instance=ExtResource( 1 )]
[node name="player2" parent="." instance=ExtResource( 4 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -34.2601 )
visible = false
input_ray_pickable = false
[node name="player" parent="." instance=ExtResource( 1 )]
[node name="player" parent="." instance=ExtResource( 4 )]
visible = false
input_ray_pickable = false
[node name="MicamoChar" parent="." instance=ExtResource( 2 )]
editor/display_folded = true
[node name="MicamoChar" parent="." instance=ExtResource( 4 )]
[node name="Viewport" type="Viewport" parent="."]
size = Vector2( 32, 32 )
@ -75,34 +68,9 @@ render_target_update_mode = 3
script = ExtResource( 3 )
_sprite_size = 32
_sprite_num = 16
_directions = 16
save_texture = true
_atlas_show_sprite_path = NodePath("../Sprite3")
_sprite_path = NodePath("../Sprite3D")
_animation_player_path = NodePath("MicamoChar/AnimationPlayer")
_player_path = NodePath("MicamoChar")
[node name="MicamoChar" parent="Viewport" instance=ExtResource( 4 )]
editor/display_folded = true
[node name="Skeleton" parent="Viewport/MicamoChar" index="0"]
bones/0/bound_children = [ ]
bones/1/bound_children = [ ]
bones/3/bound_children = [ ]
bones/4/bound_children = [ ]
bones/5/bound_children = [ ]
bones/8/bound_children = [ ]
bones/9/bound_children = [ ]
bones/10/bound_children = [ ]
bones/32/bound_children = [ ]
bones/33/bound_children = [ ]
bones/34/bound_children = [ ]
bones/55/bound_children = [ ]
bones/56/bound_children = [ ]
bones/57/bound_children = [ ]
bones/60/bound_children = [ ]
bones/61/bound_children = [ ]
bones/62/bound_children = [ ]
[node name="Camera" type="Camera" parent="Viewport"]
transform = Transform( 0.707107, -0.353553, 0.612372, 0, 0.866025, 0.5, -0.707107, -0.353553, 0.612372, 2.95796, 3.39896, 2.92486 )
@ -134,8 +102,8 @@ omni_range = 4.3
position = Vector2( 837.448, 475.588 )
centered = false
flip_v = true
vframes = 16
hframes = 16
vframes = 16
script = ExtResource( 5 )
[node name="Camera" type="Camera" parent="."]
@ -159,12 +127,11 @@ material_override = SubResource( 7 )
cast_shadow = 0
flip_v = true
pixel_size = 0.0156
vframes = 16
hframes = 16
vframes = 16
script = ExtResource( 5 )
[node name="Sprite3D2" parent="." instance=ExtResource( 6 )]
visible = false
[editable path="Viewport/MicamoChar"]