summaryrefslogtreecommitdiff
path: root/GravityGun.gd
diff options
context:
space:
mode:
Diffstat (limited to 'GravityGun.gd')
-rw-r--r--GravityGun.gd73
1 files changed, 73 insertions, 0 deletions
diff --git a/GravityGun.gd b/GravityGun.gd
new file mode 100644
index 0000000..0237fc6
--- /dev/null
+++ b/GravityGun.gd
@@ -0,0 +1,73 @@
+extends Node
+
+var using : bool
+var capturing : bool
+
+var prev_using : bool
+
+@export var area : Area2D
+@export var captured_body : RigidBody2D
+@export var capture_anchor : Node2D
+@export_range(0.0, 0.1) var current_strength : float
+@export var close_range : float
+@export var grab_speed : float
+@export var throw_velocity : float
+
+var stored_velovity : Vector2
+
+# Called when the node enters the scene tree for the first time.
+func _ready():
+ pass # Replace with function body.
+
+# Called every frame. 'delta' is the elapsed time since the previous frame.
+func _physics_process(delta):
+ area.visible = using
+
+ if using and capturing and captured_body != null:
+ var dist : Vector2 = capture_anchor.global_position - captured_body.global_position
+ var dir : Vector2 = dist.normalized()
+ var dis : float = sqrt(abs(abs(dist.x * dist.x) - abs(dist.y * dist.y))) # phytagoras!
+ print(dis)
+
+ # captured_body.global_position = capture_anchor.global_position
+
+ #if dis > close_range:
+ captured_body.apply_central_impulse(dir * grab_speed)# * (1 - (dis / close_range)))
+
+ PhysicsServer2D.body_set_state(
+ captured_body.get_rid(),
+ PhysicsServer2D.BODY_STATE_TRANSFORM,
+ Transform2D.IDENTITY.translated(captured_body.global_position.lerp(capture_anchor.global_position, 0.1))
+ )
+
+ # stored_velovity = captured_body.linear_velocity
+ #captured_body.linear_velocity = captured_body.linear_velocity.clamp(Vector2.ZERO, Vector2.ONE * 200)
+ #else:
+ # captured_body.linear_velocity = stored_velovity * (dis / close_range) # dis nuts
+ # captured_body.apply_central_impulse(dir * 1)
+
+ if using != prev_using:
+ prev_using = using
+
+ if !using && capturing and captured_body != null:
+ captured_body.persistent_linear_velocity = Vector2(sin($"..".rotation + deg_to_rad(90)), cos($"..".rotation + deg_to_rad(-90))) * throw_velocity
+ captured_body.killer = true
+ captured_body.start_timebomb()
+ captured_body = null
+ capturing = false
+
+func _input(event):
+ using = Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT)
+
+func _on_gravity_area_2d_body_entered(body):
+ print(body)
+ if using && captured_body == null:
+ captured_body = body
+ captured_body.posessed = true
+ capturing = true
+
+func _on_gravity_area_2d_area_entered(area):
+ print(area)
+ if using && captured_body == null:
+ captured_body = area
+ capturing = true