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

192 lines
6 KiB
GDScript3
Raw Normal View History

2018-07-25 07:36:19 +00:00
extends KinematicBody
########
#### Caracteristic
var pseudo = ""
var color = Color( 1.0, 0.25, 0.25, 1.0 )
var gender = 1
var size = 1.0
var slot = 0
2018-07-25 07:36:19 +00:00
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 MOUSE_SENSITIVITY = 0.05
onready var camera_rotation = $Camera_rotation_helper
onready var camera = $Camera_rotation_helper/Camera
onready var player_infos_billboard = $infos_spatial/character_infos_billboard
onready var player_mesh = $MeshInstance
onready var flashlight = $MeshInstance/Flashlight
2018-08-18 08:22:19 +00:00
### Caractéristiques du personnage.
var douleur = 0
var trauma = 0
var oubli = 0
func set_info_billboard_position():
var above_head = $infos_spatial
player_infos_billboard.get_node("label").text = self.pseudo
var offset = Vector2(-(player_infos_billboard.get_size().x/2), 0)
var unprojected_translation = camera.unproject_position(above_head.global_transform.xform(Vector3(0,0,0)))
player_infos_billboard.rect_position = (unprojected_translation + offset)
2018-07-25 07:36:19 +00:00
func _ready():
self.show_third_person_camera()
self.set_info_billboard_position()
self.update()
#func _enter_tree():
# print("qsdfghjklkjhgfdsqsdfghj")
# if self.get_parent().has_node( "start_position" ):
# self.translation = self.get_parent().get_node( "start_position" ).translation
func update( start_position = null ):
if start_position:
self.translation = start_position.translation
self.rotation = start_position.rotation
self.player_mesh.scale = Vector3( self.size, self.size, self.size )
self.player_mesh.get_surface_material(0).set_shader_param( "albedo", self.color )
2018-07-25 07:36:19 +00:00
func _process(delta):
process_input(delta)
process_movement(delta)
set_info_billboard_position()
2018-07-25 07:36:19 +00:00
func process_input(delta):
2018-07-28 18:34:08 +00:00
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
# ----------------------------------
# Walking
dir = Vector3()
var cam_xform = camera.get_global_transform()
2018-07-28 18:34:08 +00:00
var input_movement_vector = Vector3()
var cam_scroll = 0.0
2018-07-28 18:34:08 +00:00
if Input.is_action_pressed("move_up"):
input_movement_vector.z += 1
if Input.is_action_pressed("move_down"):
input_movement_vector.z -= 1
if Input.is_action_pressed("move_left"):
input_movement_vector.x -= 1
if Input.is_action_pressed("move_right"):
input_movement_vector.x += 1
2018-07-28 18:34:08 +00:00
input_movement_vector = input_movement_vector.normalized()
2018-07-28 18:34:08 +00:00
dir += -cam_xform.basis.z.normalized() * input_movement_vector.z
dir += cam_xform.basis.x.normalized() * input_movement_vector.x
2018-07-28 18:34:08 +00:00
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):
2018-07-29 06:28:48 +00:00
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
dir.y = 0
dir = dir.normalized()
2018-07-29 06:28:48 +00:00
# vel.y += delta*GRAVITY
2018-07-29 06:28:48 +00:00
var hvel = vel
hvel.y = 0
2018-07-29 06:28:48 +00:00
var target = dir
target *= MAX_SPEED
2018-07-29 06:28:48 +00:00
var accel
if dir.dot(hvel) > 0:
accel = ACCEL
else:
accel = DEACCEL
2018-07-29 06:28:48 +00:00
hvel = hvel.linear_interpolate(target, accel*delta)
vel.x = hvel.x
vel.z = hvel.z
2018-07-29 06:28:48 +00:00
var collision_info = move_and_collide(vel * delta)
if collision_info:
vel = vel.bounce(collision_info.normal)
var obj = collision_info.collider
2018-08-20 09:10:33 +00:00
if obj.is_class( "RigidBody" ):
2018-07-29 06:28:48 +00:00
obj.sleeping = false
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-08-18 08:22:19 +00:00
self.douleur += 0.25
if self.douleur >= 100:
self.trauma += 0.25
if self.trauma >= 100:
self.oubli += 0.25
2018-07-25 07:36:19 +00:00
func _input(event):
2018-07-28 18:34:08 +00:00
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
if event is InputEventMouseMotion :
camera_rotation.rotate_x(deg2rad(event.relative.y * MOUSE_SENSITIVITY * -1))
self.rotate_y(deg2rad(event.relative.x * MOUSE_SENSITIVITY * -1))
2018-07-28 18:34:08 +00:00
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:
2018-07-28 18:34:08 +00:00
# to prevent the cam sliding effect when clamp limit reached.
var old_x_translation = camera.translation.x
var old_y_translation = camera.translation.y
2018-07-28 18:34:08 +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
2018-07-28 18:34:08 +00:00
camera.translate( cam_scroll )
2018-07-28 18:34:08 +00:00
camera.translation.x = old_x_translation
camera.translation.y = old_y_translation
camera.translation.z = clamp(camera.translation.z, 0, 5)
2018-07-25 07:36:19 +00:00
# TODO trouver pourquoi cela ne se fait plus:
2018-07-25 14:12:12 +00:00
if Input.is_action_just_pressed("game_flashlight"):
# flashlight.visible = not flashlight.visible
2018-07-25 14:12:12 +00:00
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" ):
if self.visible:
self.hide()
else:
self.show()
func hide_infos():
$infos_spatial/character_infos_billboard.hide()
func show_infos():
$infos_spatial/character_infos_billboard.show()
########
#### Cameras
func show_third_person_camera():
$Camera_rotation_helper/Camera.make_current()