added base bullet and a basic fire
authorEduardo <[email protected]>
Tue, 7 May 2024 00:51:20 +0000 (02:51 +0200)
committerEduardo <[email protected]>
Tue, 7 May 2024 00:51:20 +0000 (02:51 +0200)
CONSTANTS.gd
components/base_bullet/BaseBullet.tscn [new file with mode: 0644]
components/base_bullet/base_bullet.gd [new file with mode: 0644]
components/player/Player.tscn
components/player/player.gd

index 94269f6b47632655e409ff9ec9033d893fea8fe3..76ef443e7d3cc0c828aa981f3c93cb7f25c15ae3 100644 (file)
@@ -5,6 +5,7 @@ const SplashScreen_path = "res://scenes/splash_screen/SplashScreen.tscn"
 const MainMenu_path = "res://scenes/main_menu/MainMenu.tscn"
 const Settings_path = "res://scenes/settings/Settings.tscn"
 const BaseLevel_path = "res://scenes/base_level/BaseLevel.tscn"
+const BaseBullet_path = "res://components/base_bullet/BaseBullet.tscn"
 
 # others
 const default_changeScene_config = { 
diff --git a/components/base_bullet/BaseBullet.tscn b/components/base_bullet/BaseBullet.tscn
new file mode 100644 (file)
index 0000000..0046b4d
--- /dev/null
@@ -0,0 +1,18 @@
+[gd_scene load_steps=3 format=3 uid="uid://dh2f807ny4qh2"]
+
+[ext_resource type="Script" path="res://components/base_bullet/base_bullet.gd" id="1_0faoa"]
+
+[sub_resource type="CircleShape2D" id="CircleShape2D_b7ejx"]
+radius = 4.96679
+
+[node name="BaseBullet" type="CharacterBody2D"]
+script = ExtResource("1_0faoa")
+
+[node name="Polygon2D" type="Polygon2D" parent="."]
+scale = Vector2(1.79425, 1.69033)
+color = Color(0.828474, 0.826652, 1.54018e-06, 1)
+polygon = PackedVector2Array(2.53263, -1.21713, 2.07215, -1.94741, 1.15119, -2.67769, 0, -2.92112, -1.15119, -2.67769, -2, -2, -2.53263, -1.21713, -2.76287, 0, -2.53263, 1.21713, -2.07215, 1.94741, -1.15119, 2.67769, 0, 2.92112, 1.15119, 2.67769, 2, 2, 2.53263, 1.21713, 2.76287, 0)
+
+[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
+scale = Vector2(1.00669, 1.00809)
+shape = SubResource("CircleShape2D_b7ejx")
diff --git a/components/base_bullet/base_bullet.gd b/components/base_bullet/base_bullet.gd
new file mode 100644 (file)
index 0000000..3bf660d
--- /dev/null
@@ -0,0 +1,25 @@
+extends CharacterBody2D
+
+@export var SPEED = 200.0
+var origin: Node2D
+var target: Node2D
+
+
+func _ready():
+       if origin:
+               position = origin.position
+
+
+func _process(_delta):
+       if target:
+               velocity = position.direction_to(target.position) * SPEED
+               move_and_slide()
+
+
+func set_origin(node: Node2D):
+       origin = node
+       position = origin.position
+
+
+func set_target(node: Node2D):
+       target = node
index fd7eb5b5809990edec319fc03f97253112f016d6..d82a0b5a9b2c1cf49c8b141c05351febc55c742b 100644 (file)
@@ -14,5 +14,4 @@ color = Color(0, 0.564706, 0.858824, 1)
 polygon = PackedVector2Array(-0.267257, -4.94762, -0.367478, 0, -0.267257, 4.94762, 0, 6.80297, 0.267257, 4.94762, 0.367478, 0, 0.267257, -4.94762, 0, -6.80297)
 
 [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
-scale = Vector2(1, 1)
 shape = SubResource("CircleShape2D_oxutk")
index 555c869fae7513fe6170b7e6991fe8cc73681243..32a635cb25521a291c0dff630ea3a3510dff4384 100644 (file)
@@ -2,6 +2,31 @@ extends CharacterBody2D
 
 
 const SPEED = 300.0
+var fire_timer: Timer
+var nearest_enemy: Node2D
+
+func _init():
+       fire_timer = Timer.new()
+       fire_timer.one_shot = false
+       fire_timer.autostart = true
+       fire_timer.wait_time = .5
+       fire_timer.timeout.connect(on_fire_timer_timeout)
+       add_child(fire_timer)
+
+
+func _process(delta):
+       var nearest_node: Node2D
+
+       for node in get_parent().get_children():
+               if node is CharacterBody2D:
+                       if node.name.contains("Enemy"):
+                               if !nearest_node:
+                                       nearest_node = node
+                               elif (position.distance_squared_to(node.position) 
+                                       < position.distance_squared_to(nearest_node.position)):
+                                               nearest_node = node
+       
+       nearest_enemy = nearest_node
 
 
 func _physics_process(_delta):
@@ -17,3 +42,16 @@ func _physics_process(_delta):
        
        velocity = Vector2(x_force, y_force) * SPEED
        move_and_slide()
+
+
+func on_fire_timer_timeout():
+       if nearest_enemy:
+               var bullet = load(Constants.BaseBullet_path)
+               var bullet_instance = bullet.instantiate() as CharacterBody2D
+               get_parent().add_child(bullet_instance)
+               var n2d = Node2D.new()
+               n2d.position = position
+               bullet_instance.set_origin(n2d)
+               bullet_instance.set_target(nearest_enemy)
+
+