test-client-godot/scenes/Game/Character/Character.gd

146 lines
4.8 KiB
GDScript3
Raw Normal View History

2018-07-25 07:36:19 +00:00
extends KinematicBody
var dir = Vector3()
const GRAVITY = -24.8
var vel = Vector3()
const MAX_SPEED = 20
const ACCEL= 4.5
const FLY_SPEED = 7
2018-07-25 07:36:19 +00:00
const DEACCEL= 16
const MAX_SLOPE_ANGLE = 40
var camera_rotation
var camera
2018-07-25 14:12:12 +00:00
var flashlight
2018-07-25 07:36:19 +00:00
var MOUSE_SENSITIVITY = 0.05
func _ready():
camera_rotation = $Camera_rotation_helper
camera = $Camera_rotation_helper/Camera
2018-07-27 19:01:34 +00:00
camera.make_current()
2018-07-25 14:12:12 +00:00
flashlight = $MeshInstance/Flashlight
2018-07-25 07:36:19 +00:00
func _process(delta):
process_input(delta)
process_movement(delta)
func process_input(delta):
# ----------------------------------
# Walking
dir = Vector3()
var cam_xform = camera.get_global_transform()
var input_movement_vector = Vector3()
2018-07-25 07:36:19 +00:00
var cam_scroll = 0.0
if Input.is_action_pressed("move_up"):
input_movement_vector.z += 1
2018-07-25 07:36:19 +00:00
if Input.is_action_pressed("move_down"):
input_movement_vector.z -= 1
2018-07-25 07:36:19 +00:00
if Input.is_action_pressed("move_left"):
input_movement_vector.x -= 1
if Input.is_action_pressed("move_right"):
input_movement_vector.x = 1
input_movement_vector = input_movement_vector.normalized()
dir += -cam_xform.basis.z.normalized() * input_movement_vector.z
2018-07-25 07:36:19 +00:00
dir += cam_xform.basis.x.normalized() * input_movement_vector.x
if Input.is_action_pressed("fly_up"):
vel.y = FLY_SPEED
elif Input.is_action_pressed("fly_down"):
vel.y = -FLY_SPEED
else:
vel.y = 0
2018-07-25 07:36:19 +00:00
2018-07-25 07:36:19 +00:00
func process_movement(delta):
dir.y = 0
dir = dir.normalized()
# vel.y += delta*GRAVITY
2018-07-25 07:36:19 +00:00
var hvel = vel
hvel.y = 0
var target = dir
target *= MAX_SPEED
var accel
if dir.dot(hvel) > 0:
accel = ACCEL
else:
accel = DEACCEL
hvel = hvel.linear_interpolate(target, accel*delta)
vel.x = hvel.x
vel.z = hvel.z
2018-07-25 14:12:12 +00:00
# vel = move_and_slide(vel,Vector3(0,1,0), 0.05, 4, deg2rad(MAX_SLOPE_ANGLE))
var collision_info = move_and_collide(vel * delta)
if collision_info:
vel = vel.bounce(collision_info.normal)
2018-07-26 17:15:50 +00:00
var obj = collision_info.collider
if obj.is_class( "RigidBody" ):
obj.sleeping = false
2018-07-27 07:27:46 +00:00
obj.apply_impulse( collision_info.position, -collision_info.normal*delta )
if not obj.get_node( "MeshInstance" ).get_surface_material(0).get("albedo_color") == null:
obj.get_node( "MeshInstance" ).get_surface_material(0).albedo_color = Color( 1, 0, 1, 1 )
2018-07-27 19:01:34 +00:00
get_node( "../../GUI/HUD/Jauges/douleur/ProgressBar" ).value += 0.25
if get_node( "../../GUI/HUD/Jauges/douleur/ProgressBar" ).value >= 100:
2018-07-27 19:01:34 +00:00
get_node( "../../GUI/HUD/Jauges/trauma/ProgressBar" ).value += 0.25
if get_node( "../../GUI/HUD/Jauges/trauma/ProgressBar" ).value >= 100:
2018-07-27 19:01:34 +00:00
get_node( "../../GUI/HUD/Jauges/oubli/ProgressBar" ).value += 0.25
2018-07-27 07:27:46 +00:00
# obj.get_node( "MeshInstance" ).get_surface_material(0).albedo_color = Color( collision_info.normal )
2018-07-26 17:15:50 +00:00
# if obj.has_method( "hit" ):
# obj.hit( collision_info.position, collision_info.collider_velocity )
2018-07-25 14:12:12 +00:00
# print(str(collision_info.collider.get_class()))
2018-07-25 07:36:19 +00:00
func _input(event):
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
camera_rotation.rotate_x(deg2rad(event.relative.y * MOUSE_SENSITIVITY * -1))
self.rotate_y(deg2rad(event.relative.x * MOUSE_SENSITIVITY * -1))
var camera_rot = camera_rotation.rotation_degrees
camera_rot.x = clamp(camera_rot.x, -30, 30)
camera_rotation.rotation_degrees = camera_rot
if event is InputEventMouseButton and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
# to prevent the cam sliding effect when clamp limit reached.
var old_x_translation = camera.translation.x
2018-07-25 10:11:56 +00:00
var old_y_translation = camera.translation.y
2018-07-25 07:36:19 +00:00
var cam_scroll = Vector3( 0.0, 0.0, 0.0 )
if event.button_index == BUTTON_WHEEL_UP:
cam_scroll.z = -1.0 * MOUSE_SENSITIVITY
if event.button_index == BUTTON_WHEEL_DOWN:
cam_scroll.z = 1.0 * MOUSE_SENSITIVITY
camera.translate( cam_scroll )
camera.translation.x = old_x_translation
2018-07-25 10:11:56 +00:00
camera.translation.y = old_y_translation
camera.translation.z = clamp(camera.translation.z, 0, 5)
2018-07-25 07:36:19 +00:00
2018-07-25 14:12:12 +00:00
# if event.is_action_pressed( "game_flashlight" ) and not event.is_echo():
# get_node( "Flashlight" ).visible = not get_node( "Flashlight" ).visible
if Input.is_action_just_pressed("game_flashlight"):
if flashlight.is_visible_in_tree():
flashlight.hide()
else:
flashlight.show()
2018-07-25 10:11:56 +00:00
if Input.is_action_pressed( "hide_char" ):
$MeshInstance.visible = not $MeshInstance.visible