diff --git a/tutorial---basic-player-traits/bullet.gd b/tutorial---basic-player-traits/bullet.gd new file mode 100644 index 0000000..869a4c6 --- /dev/null +++ b/tutorial---basic-player-traits/bullet.gd @@ -0,0 +1,17 @@ +extends Area2D + +var speed = 1500 + + +func _physics_process(delta): + position = calculateTrajectory(delta, transform) + +func _on_Bullet_body_entered(body): + if body.is_in_group("mobs"): + body.queue_free() + queue_free() + +func calculateTrajectory(delta, the_transform): + var new_position = position + new_position += the_transform.y * -speed * delta + return new_position diff --git a/tutorial---basic-player-traits/bullet.tscn b/tutorial---basic-player-traits/bullet.tscn new file mode 100644 index 0000000..d7f9410 --- /dev/null +++ b/tutorial---basic-player-traits/bullet.tscn @@ -0,0 +1,34 @@ +[gd_scene load_steps=5 format=3 uid="uid://dphnf0cjfrcu7"] + +[ext_resource type="Script" path="res://bullet.gd" id="1_cpge0"] +[ext_resource type="Texture2D" uid="uid://bixef7um6g62c" path="res://assets/helenknives.png" id="2_cxno8"] + +[sub_resource type="SpriteFrames" id="SpriteFrames_8byar"] +animations = [{ +"frames": [{ +"duration": 1.0, +"texture": ExtResource("2_cxno8") +}], +"loop": true, +"name": &"default", +"speed": 5.0 +}] + +[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_sehhp"] +radius = 10.9589 +height = 43.5616 + +[node name="bullet" type="Area2D"] +script = ExtResource("1_cpge0") + +[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."] +modulate = Color(1, 1, 1, 0.509804) +sprite_frames = SubResource("SpriteFrames_8byar") +offset = Vector2(3.465, 0) + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2(0, -10) +scale = Vector2(1.095, 1.095) +shape = SubResource("CapsuleShape2D_sehhp") +one_way_collision_margin = 0.0 +debug_color = Color(0, 0.641316, 0.336247, 0.42) diff --git a/tutorial---basic-player-traits/player.gd b/tutorial---basic-player-traits/player.gd index 1127de1..28b2832 100644 --- a/tutorial---basic-player-traits/player.gd +++ b/tutorial---basic-player-traits/player.gd @@ -3,13 +3,17 @@ signal hit @export var speed = 600 var spriteRotateAngle = 0 var screen_size +@export var bullet : PackedScene +@export var firespeed = 10 +var timeBetweenBullets = 1.00000 / firespeed +var deltaSum = 0 # Called when the node enters the scene tree for the first time. func _ready() -> void: screen_size = get_viewport_rect().size # Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta: float) -> void: +func _physics_process(delta: float) -> void: var velocity = Vector2.ZERO var currentSpeed = speed if Input.is_action_pressed("move_up"): @@ -40,4 +44,17 @@ func _process(delta: float) -> void: position = position.clamp(Vector2.ZERO, screen_size) rotation_degrees = spriteRotateAngle - + if Input.is_action_pressed("shoot"): + deltaSum += delta + if deltaSum > timeBetweenBullets : + var remainder = fmod(deltaSum, timeBetweenBullets) + var numBullets = round ((deltaSum - remainder) / timeBetweenBullets) + deltaSum = remainder + for n in numBullets : + shoot() + + +func shoot(): + var b = bullet.instantiate() + owner.add_child(b) + b.transform = $FrontBulletSource.global_transform diff --git a/tutorial---basic-player-traits/player.tscn b/tutorial---basic-player-traits/player.tscn index 905bef3..30086a1 100644 --- a/tutorial---basic-player-traits/player.tscn +++ b/tutorial---basic-player-traits/player.tscn @@ -1,6 +1,7 @@ -[gd_scene load_steps=6 format=3 uid="uid://dn6vstynf4qyu"] +[gd_scene load_steps=7 format=3 uid="uid://dn6vstynf4qyu"] [ext_resource type="Script" path="res://player.gd" id="1_ro1ml"] +[ext_resource type="PackedScene" uid="uid://dphnf0cjfrcu7" path="res://bullet.tscn" id="2_5m6h0"] [ext_resource type="Texture2D" uid="uid://c01t7u0aoeir7" path="res://assets/HelenMidQuality.png" id="2_k2fmb"] [sub_resource type="SpriteFrames" id="SpriteFrames_77x2a"] @@ -21,7 +22,9 @@ radius = 67.42 radius = 9.06 [node name="Player" type="Area2D"] +z_index = 1 script = ExtResource("1_ro1ml") +bullet = ExtResource("2_5m6h0") metadata/_edit_group_ = true [node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."] @@ -35,3 +38,6 @@ shape = SubResource("CircleShape2D_emqsv") [node name="KillBox" type="CollisionShape2D" parent="."] shape = SubResource("CircleShape2D_tnmln") debug_color = Color(0.966315, 0, 0.481295, 0.42) + +[node name="FrontBulletSource" type="Marker2D" parent="."] +position = Vector2(0, -50) diff --git a/tutorial---basic-player-traits/project.godot b/tutorial---basic-player-traits/project.godot index e328bbb..dce2e3b 100644 --- a/tutorial---basic-player-traits/project.godot +++ b/tutorial---basic-player-traits/project.godot @@ -47,3 +47,8 @@ focus={ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194325,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) ] } +shoot={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":81,"key_label":0,"unicode":113,"location":0,"echo":false,"script":null) +] +} diff --git a/tutorial---basic-player-traits/testinggrounds.tscn b/tutorial---basic-player-traits/testinggrounds.tscn new file mode 100644 index 0000000..37127f5 --- /dev/null +++ b/tutorial---basic-player-traits/testinggrounds.tscn @@ -0,0 +1,7 @@ +[gd_scene load_steps=2 format=3 uid="uid://dgjpt6r8lqtp5"] + +[ext_resource type="PackedScene" uid="uid://dn6vstynf4qyu" path="res://player.tscn" id="1_oa02w"] + +[node name="Node" type="Node"] + +[node name="Player" parent="." instance=ExtResource("1_oa02w")]