bodypartfighter/BodyConfig/bodyBuildingScript.gd
2020-02-02 16:17:07 +01:00

180 lines
5.2 KiB
GDScript

extends Spatial
export(NodePath) var torsoPath
export(NodePath) var cameraPath
export(NodePath) var rayCastPath
export(NodePath) var viewPortPath
export(NodePath) var playerNumPath
export(NodePath) var costProgressPath
const ROT_MOD = 500
const ROT_DECLINE = 0.1
var _viewRot = false
var _prev_mouse_pos
var _camera : Camera
var _rayCast : RayCast
var _torso : RigidBody
var _viewport : Viewport
var _playerNum : Label
var _costProgress : ProgressBar
var _velx = 0
var _vely = 0
var _attachment_point : Spatial
var _default_grav
var done_player_count = 0
signal start_fight()
# Called when the node enters the scene tree for the first time.
func _ready():
_torso = get_node(torsoPath) as RigidBody
_camera = get_node(cameraPath) as Camera
_rayCast = get_node(rayCastPath) as RayCast
_viewport = get_node(viewPortPath) as Viewport
_playerNum = get_node(playerNumPath) as Label
_costProgress = get_node(costProgressPath) as ProgressBar
# Keybinds of first player
_torso.get_node("DownFrontLeft").key = KEY_D
_torso.get_node("DownFrontRight").key = KEY_F
_torso.get_node("DownBackLeft").key = KEY_A
_torso.get_node("DownBackRight").key = KEY_S
_torso.get_node("UpFrontLeft").key = KEY_E
_torso.get_node("UpFrontRight").key = KEY_R
_torso.get_node("UpBackLeft").key = KEY_W
_torso.get_node("UpBackRight").key = KEY_Q
func change_count_ui():
if done_player_count == null:
done_player_count = 0
_playerNum.text = str(done_player_count + 1) + "/" + str(InGameState.player_count)
# TODO: Move somewhere else because it's not UI!
if done_player_count == 1:
# Keybinds of second player
_torso.get_node("DownFrontLeft").key = KEY_H
_torso.get_node("DownFrontRight").key = KEY_J
_torso.get_node("DownBackLeft").key = KEY_K
_torso.get_node("DownBackRight").key = KEY_L
_torso.get_node("UpFrontLeft").key = KEY_Z
_torso.get_node("UpFrontRight").key = KEY_U
_torso.get_node("UpBackLeft").key = KEY_I
_torso.get_node("UpBackRight").key = KEY_O
func _process(delta):
# TODO: Would be nicer to just disable gravity, but then it can't be re-enabled...
_torso.translation = Vector3.ZERO
if _viewRot:
var mouse_pos = get_viewport().get_mouse_position()
_velx = (mouse_pos.x - _prev_mouse_pos.x) * delta
_vely = (mouse_pos.y - _prev_mouse_pos.y) * delta
_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)
var decline = (1 - ROT_DECLINE * (delta*100))
if decline > 1:
decline = 1
elif decline < 0:
decline = 0
_velx *= decline
_vely *= decline
func _physics_process(delta):
if _viewRot:
var mouse_pos = _viewport.get_viewport().get_mouse_position()
#set origin of rayCast
var orig = _camera.project_ray_origin(mouse_pos)
#set cast_to of rayCast
_rayCast.translation = orig
_rayCast.cast_to = orig + _camera.project_ray_normal(mouse_pos) * 1000.0
if _rayCast.is_colliding():
# collider will be the node hit
make_it_shine(false)
var temp = _rayCast.get_collider().get_parent()
if temp.has_node("OmniLight"):
_attachment_point = temp
make_it_shine(true)
func _input(event):
if event is InputEventMouseButton:
if event.pressed:
if _viewport.get_viewport().get_mouse_position().x >= 0:
# if event.button_index == BUTTON_WHEEL_UP and _attachment_point != null:
# if _attachment_point.get_node("Limb") != null:
# _attachment_point.get_node("Limb").rotate_z(0.1)
# elif event.button_index == BUTTON_WHEEL_DOWN and _attachment_point != null:
# if _attachment_point.get_node("Limb") != null:
# _attachment_point.get_node("Limb").rotate_z(-0.1)
# else:
_prev_mouse_pos = event.position
_viewRot = true
else:
_viewRot = false
func body_part_chosen(params):
#no _attachment_point
if _attachment_point != null:
var limb = _attachment_point.get_node("Limb")
var new_part = BodyPartLoader.bodyparts[params].instance()
# Check if we have enough cost points left
var current_cost = 0
# If we already have something in that slot, we can place
# the new one if the total cost WITHOUT that existing part
# is low enough
if limb.get_child(0) != null:
current_cost += limb.get_child(0).cost
if _costProgress.value + new_part.cost - current_cost <= _costProgress.max_value:
#if spot already taken, DELETE
if limb.get_child(0) != null:
delete_body_part()
limb.add_child(new_part)
# Add the cost of that part
_costProgress.value += new_part.cost
func make_it_shine(highlight : bool):
if _attachment_point != null:
_attachment_point.get_node("OmniLight").visible = highlight
func delete_body_part():
if _attachment_point != null:
var body_part = _attachment_point.get_node("Limb").get_child(0)
if body_part != null:
_costProgress.value -= body_part.cost
#delete collider with id
var old_col_path = "LimbCollider" + str(_attachment_point.id)
_torso.get_node(old_col_path).queue_free()
body_part.queue_free()
func _on_Start_pressed():
make_it_shine(false)
emit_signal("start_fight", _torso)