godot-space-escape/scenes/Player.gd

70 lines
1.6 KiB
GDScript

extends RigidBody2D
# How fast the player will move (pixels/sec).
export var speed = 400
# The force constant
# TODO: replace this with a dynamic force from the length of the drag
export var force = 0.2
# Size of the game window.
var screen_size
# A boolean that indicates whether users is dragging their
# touch or not
var dragging
# The drag starting vector
var drag_start = Vector2()
# Signals
signal hit
func _input(event):
if event.is_action_pressed('click') and not dragging:
dragging = true
drag_start = get_global_mouse_position()
if event.is_action_released('click') and dragging:
dragging = false
var drag_end = get_global_mouse_position()
var direction = drag_end - drag_start
# if direction.y > 0:
# $Ship.flip_v = true
# else:
# $Ship.flip_v = false
# var angle = ($FrontPosition.position.normalized() - $CenterPosition.position.normalized()).dot(drag_end.normalized())
# var torque = ($FrontPosition.position - $CenterPosition.position).angle_to(drag_end)
# print(torque)
# print(angle)
# print("-----")
apply_impulse(position.normalized(), direction * force)
# apply_torque_impulse(torque * -100)
# #$Ship.rotate(torque)
func _ready():
# hide()
screen_size = get_viewport_rect().size
func _process(delta):
if linear_velocity.y > 0:
$Ship.flip_v = true
else:
$Ship.flip_v = false
func _on_Player_body_entered(body):
hide()
emit_signal('hit')
$CollisionShape2D.set_deferred('disabled', true)
func start(pos):
position = pos
show()
$CollisionShape2D.disabled = false