diff --git a/scenes/Player.gd b/scenes/Player.gd index 72c6068..8b04922 100644 --- a/scenes/Player.gd +++ b/scenes/Player.gd @@ -1,60 +1,58 @@ -extends RigidBody2D +extends KinematicBody2D # 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 +export (int) var max_speed = 600 +export (float) var start_acceleration = 0.075 +export (float) var stop_friction = 0.03 # Size of the game window. var screen_size +var speed = 0 -# A boolean that indicates whether users is dragging their -# touch or not -var dragging - -# The drag starting vector -var drag_start = Vector2() +# The click/touch position +var touch_point = Vector2() +var velocity = Vector2() +var released = true # 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 + if Input.is_action_pressed('click'): + released = false + touch_point = get_global_mouse_position() + if speed == 0: + speed = 1 + + speed += (speed * start_acceleration) + + if speed >= max_speed: + speed = max_speed + + velocity = (touch_point - position).normalized() * speed + + if released and speed > 0: + speed -= (speed * stop_friction) + + if speed <= 0: + speed = 0 + + velocity = velocity.normalized() * speed + + +func _input(event): + if event.is_action_released('click'): + released = true + +func _physics_process(delta): + rotation = velocity.angle() + velocity = move_and_slide(velocity) func _on_Player_body_entered(body): diff --git a/scenes/Player.tscn b/scenes/Player.tscn index 6c1dd25..5fcbdd4 100644 --- a/scenes/Player.tscn +++ b/scenes/Player.tscn @@ -3,37 +3,32 @@ [ext_resource path="res://scenes/Player.gd" type="Script" id=1] [ext_resource path="res://images/pixel_ship_red.png" type="Texture" id=2] -[sub_resource type="SpriteFrames" id=2] +[sub_resource type="SpriteFrames" id=1] animations = [ { -"frames": [ ExtResource( 2 ) ], +"frames": [ ExtResource( 2 ), ExtResource( 2 ) ], "loop": true, "name": "idle", "speed": 5.0 } ] -[sub_resource type="ConvexPolygonShape2D" id=1] -points = PoolVector2Array( -40, 30, 0, -40, 40, 30, 10, 40, -10, 40 ) +[sub_resource type="ConvexPolygonShape2D" id=2] +points = PoolVector2Array( -20, -5, -15, -20, -5, -20, 20, 0, -5, 20, -15, 20, -20, 5 ) -[node name="Player" type="RigidBody2D"] -position = Vector2( 0, -1 ) -gravity_scale = 0.0 +[node name="Player" type="KinematicBody2D"] script = ExtResource( 1 ) __meta__ = { -"_edit_horizontal_guides_": [ 40.0, -40.0, 20.0 ], -"_edit_vertical_guides_": [ -40.0, 40.0 ] +"_edit_horizontal_guides_": [ ], +"_edit_vertical_guides_": [ ] } -[node name="FrontPosition" type="Position2D" parent="."] -position = Vector2( 0, -39 ) - -[node name="CenterPosition" type="Position2D" parent="."] -position = Vector2( 0, 21 ) - [node name="Ship" type="AnimatedSprite" parent="."] -position = Vector2( 0, 21 ) -frames = SubResource( 2 ) +position = Vector2( 0, -1 ) +rotation = 1.5708 +scale = Vector2( 0.5, 0.5 ) +frames = SubResource( 1 ) animation = "idle" -offset = Vector2( 0, -25 ) +offset = Vector2( 0, -5 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="."] -shape = SubResource( 1 ) +position = Vector2( 0, -1 ) +shape = SubResource( 2 )