bodypartfighter/BodyConfig/bodyBuildingScript.gd

81 lines
2.1 KiB
GDScript3
Raw Normal View History

2020-01-31 23:02:24 +01:00
extends Spatial
2020-02-01 12:07:46 +01:00
export(NodePath) var torsoPath
export(NodePath) var cameraPath
export(NodePath) var rayCastPath
export(NodePath) var viewPortPath
2020-02-01 11:27:04 +01:00
const ROT_MOD = 500
const ROT_DECLINE = 0.1
2020-01-31 23:02:24 +01:00
var _viewRot = false
var _prev_mouse_pos
2020-02-01 12:07:46 +01:00
var _camera : Camera
var _rayCast : RayCast
var _torso : RigidBody
var _viewport : Viewport
2020-01-31 23:02:24 +01:00
2020-02-01 11:27:04 +01:00
var _velx = 0
var _vely = 0
2020-02-01 12:07:46 +01:00
2020-01-31 23:02:24 +01:00
# Called when the node enters the scene tree for the first time.
func _ready():
PhysicsServer.area_set_param(get_viewport().find_world().get_space(), PhysicsServer.AREA_PARAM_GRAVITY, 0)
2020-02-01 12:07:46 +01:00
_torso = get_node(torsoPath) as RigidBody
_camera = get_node(cameraPath) as Camera
_rayCast = get_node(rayCastPath) as RayCast
_viewport = get_node(viewPortPath) as Viewport
2020-02-01 11:27:04 +01:00
func _process(delta):
2020-01-31 23:02:24 +01:00
if _viewRot:
var mouse_pos = get_viewport().get_mouse_position()
2020-02-01 11:27:04 +01:00
_velx = (mouse_pos.x - _prev_mouse_pos.x) * delta
_vely = (mouse_pos.y - _prev_mouse_pos.y) * delta
2020-01-31 23:02:24 +01:00
2020-02-01 11:27:04 +01:00
_prev_mouse_pos = mouse_pos
if not(_velx < 0.001 and _velx > -0.001) or not(_vely < 0.001 and _vely > -0.001) or _viewRot:
_torso.rotate_x(_vely * ROT_MOD * delta * PI/180 - global_transform.basis.get_euler().x)
_torso.rotate_y(_velx * ROT_MOD * delta * PI/180 - global_transform.basis.get_euler().y)
2020-01-31 23:02:24 +01:00
2020-02-01 11:27:04 +01:00
var decline = (1 - ROT_DECLINE * (delta*100))
2020-01-31 23:02:24 +01:00
2020-02-01 11:27:04 +01:00
if decline > 1:
decline = 1
elif decline < 0:
decline = 0
2020-01-31 23:02:24 +01:00
2020-02-01 11:27:04 +01:00
_velx *= decline
_vely *= decline
2020-02-01 12:07:46 +01:00
2020-02-01 11:27:04 +01:00
func _physics_process(delta):
2020-02-01 12:07:46 +01:00
if _viewRot:
var mouse_pos = _viewport.get_viewport().get_mouse_position()
2020-01-31 23:02:24 +01:00
2020-02-01 12:07:46 +01:00
#set origin of rayCast
var orig = _camera.project_ray_origin(mouse_pos)
2020-01-31 23:02:24 +01:00
2020-02-01 12:07:46 +01:00
#set cast_to of rayCast
_rayCast.translation = orig
_rayCast.cast_to = orig + _camera.project_ray_normal(mouse_pos) * 1000.0
2020-01-31 23:02:24 +01:00
2020-02-01 12:07:46 +01:00
if _rayCast.is_colliding():
# collider will be the node hit
print(_rayCast.get_collider())
2020-01-31 23:02:24 +01:00
func _input(event):
if event is InputEventMouseButton:
if event.pressed and _viewport.get_viewport().get_mouse_position().x >= 0:
print("Mouse Click at: ", event.position)
_prev_mouse_pos = event.position
_viewRot = true
else:
print("mouse unpressed at: ", event.position)
_viewRot = false