moved weapons to functions and added circle weapon
authorEduardo <[email protected]>
Thu, 23 May 2024 17:15:09 +0000 (19:15 +0200)
committerEduardo <[email protected]>
Thu, 23 May 2024 17:15:09 +0000 (19:15 +0200)
components/base_weapon/base_weapon.gd

index 5cfd318b2b4d64dd86155870a1c6d71bb8453174..7c6b8856fd8471daec4236a03d94ad9b8c2e5df4 100644 (file)
@@ -2,8 +2,12 @@ extends Area2D
 
 enum weapon_type_enum {long_line, circle, boomerang, spiral}
 
-const LONG_LINE_SPEED = 100
-const CIRCLE_SPEED    = 100
+const LONG_LINE_SPEED = 200
+const LONG_LINE_DISTANCE = 20
+
+const CIRCLE_SPEED    = 360
+const CIRCLE_DISTANCE = 30
+
 const BOOMERANG_SPEED = 100
 const SPIRAL_SPEED    = 100
 
@@ -12,17 +16,97 @@ const SPIRAL_SPEED    = 100
 @export var weapon_type: weapon_type_enum = 0
 ## time between weapon attacks
 @export_range(0, 10, 0.1, "or_greater") var fire_time: float = 1.0
-@onready var fire_timer = $FireTimer
+
+@onready var fire_timer: Timer = $FireTimer
+var fire: bool = false
+var position_helper: float = 0
 
 func _ready():
        fire_timer.wait_time = fire_time
        fire_timer.start()
 
-# Called every frame. 'delta' is the elapsed time since the previous frame.
-func _process(delta):
-       pass
+
+func _physics_process(delta):
+       if fire:
+               fire = fire_weapon(delta)
 
 
 # when the timer ends launch the attack
 func _on_fire_timer_timeout():
-       print("done")
+       fire = true
+       print("Fire!!")
+
+
+# returns true when attacking, false when finished
+func fire_weapon(delta):
+       match weapon_type:
+               weapon_type_enum.long_line:
+                       return long_line_attack(delta)
+                       
+               weapon_type_enum.circle:
+                       return circle_attack(delta)
+                       
+               weapon_type_enum.boomerang:
+                       pass
+                       
+               weapon_type_enum.spiral:
+                       pass
+       
+       return true
+
+
+func _on_body_entered(body: Node2D):
+       # if it is an enemy do some damage
+       if body.name.to_lower().contains("enemy"):
+               body.take_damage(3)
+
+
+#region attacks
+
+func long_line_attack(delta: float) -> bool:
+       if position_helper == 2 * LONG_LINE_DISTANCE:
+               position_helper = 0
+               fire_timer.start()
+               return false
+               
+       elif position_helper < LONG_LINE_DISTANCE:
+               position += Vector2(1, 0) * LONG_LINE_SPEED * delta
+               position_helper += 1
+               return true
+               
+       elif position_helper >= LONG_LINE_DISTANCE:
+               position -= Vector2(1, 0) * LONG_LINE_SPEED * delta
+               position_helper += 1
+               print(position_helper)
+               return true
+       
+       return false
+
+func circle_attack(delta: float) -> bool:
+       # reference: 
+       # https://www.advanced-ict.info/mathematics/circles.html
+       # we want x, y
+       # hipotenuse is CIRCLE_DISTANCE
+       # n is the angle
+       # we can do:
+       # x = hipotenuse * cos(n)
+       # y = hipotenuse * sin(n)
+       
+       if position_helper > 360:
+               position_helper = 0
+               position = Vector2()
+               fire_timer.start()
+               return false
+               
+       else:
+               var x = CIRCLE_DISTANCE * cos(deg_to_rad(position_helper))
+               var y = CIRCLE_DISTANCE * sin(deg_to_rad(position_helper))
+               
+               position = Vector2(x, y)
+               position_helper += 1 * CIRCLE_SPEED * delta
+               
+               return true
+       
+       return false
+
+#endregion