From 0c164bff8a1b7a3ad2576168d7e8d5ed7a45f547 Mon Sep 17 00:00:00 2001 From: winniewk Date: Mon, 9 Sep 2024 21:02:15 -0400 Subject: [PATCH] bullets and force push working --- august26godotcompkieran/Scenes/Bullet.tscn | 16 +++++ august26godotcompkieran/Scenes/Crate.tscn | 2 - august26godotcompkieran/Scenes/Game.tscn | 31 +++------ august26godotcompkieran/Scenes/Player.tscn | 25 ++++++++ august26godotcompkieran/Sripts/Bullet.gd | 13 ++++ august26godotcompkieran/Sripts/player.gd | 73 +++++++++++++++++++++- august26godotcompkieran/project.godot | 13 ++++ 7 files changed, 148 insertions(+), 25 deletions(-) create mode 100644 august26godotcompkieran/Scenes/Bullet.tscn create mode 100644 august26godotcompkieran/Scenes/Player.tscn create mode 100644 august26godotcompkieran/Sripts/Bullet.gd diff --git a/august26godotcompkieran/Scenes/Bullet.tscn b/august26godotcompkieran/Scenes/Bullet.tscn new file mode 100644 index 0000000..cc9154d --- /dev/null +++ b/august26godotcompkieran/Scenes/Bullet.tscn @@ -0,0 +1,16 @@ +[gd_scene load_steps=3 format=3 uid="uid://djsdyfqvl85k1"] + +[ext_resource type="Script" path="res://Sripts/Bullet.gd" id="1_iqvku"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_0nhk6"] +size = Vector2(20, 11) + +[node name="Area2D" type="Area2D"] +script = ExtResource("1_iqvku") +metadata/_edit_group_ = true + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_0nhk6") +debug_color = Color(0.345732, 0.414118, 0.250065, 0.42) + +[connection signal="body_entered" from="." to="." method="_on_body_entered"] diff --git a/august26godotcompkieran/Scenes/Crate.tscn b/august26godotcompkieran/Scenes/Crate.tscn index 8ffecf6..eb626cb 100644 --- a/august26godotcompkieran/Scenes/Crate.tscn +++ b/august26godotcompkieran/Scenes/Crate.tscn @@ -5,8 +5,6 @@ [sub_resource type="RectangleShape2D" id="RectangleShape2D_amdo7"] [node name="RigidBody2D" type="RigidBody2D"] -position = Vector2(555, 296) -rotation = 0.785817 script = ExtResource("1_4l5o6") metadata/_edit_group_ = true diff --git a/august26godotcompkieran/Scenes/Game.tscn b/august26godotcompkieran/Scenes/Game.tscn index cb42488..c6877f6 100644 --- a/august26godotcompkieran/Scenes/Game.tscn +++ b/august26godotcompkieran/Scenes/Game.tscn @@ -1,15 +1,13 @@ -[gd_scene load_steps=7 format=3 uid="uid://bnrrbkgwh35jy"] +[gd_scene load_steps=6 format=3 uid="uid://bnrrbkgwh35jy"] [ext_resource type="PackedScene" uid="uid://bq1tvv23y61by" path="res://Scenes/Crate.tscn" id="1_8ldta"] -[ext_resource type="Script" path="res://Sripts/player.gd" id="2_2yqxe"] +[ext_resource type="PackedScene" uid="uid://crvsom7i1waff" path="res://Scenes/Player.tscn" id="2_vxspj"] [ext_resource type="PackedScene" uid="uid://cb1km20mpc3ag" path="res://Scenes/Trigger.tscn" id="3_sr8fi"] [sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_flxcs"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_6fyfv"] -[sub_resource type="CircleShape2D" id="CircleShape2D_t5iiu"] - [node name="World" type="Node2D"] [node name="BottomWorld" type="StaticBody2D" parent="."] @@ -40,23 +38,14 @@ metadata/_edit_group_ = true [node name="CollisionShape2D" type="CollisionShape2D" parent="platform3"] shape = SubResource("RectangleShape2D_6fyfv") -[node name="RigidBody2D" parent="." instance=ExtResource("1_8ldta")] -position = Vector2(554, 291) - -[node name="RigidBody2D2" parent="." instance=ExtResource("1_8ldta")] -position = Vector2(381, 149) - -[node name="RigidBody2D3" parent="." instance=ExtResource("1_8ldta")] -position = Vector2(380, 182) - -[node name="CharacterBody2D" type="CharacterBody2D" parent="."] -position = Vector2(381, 396) -script = ExtResource("2_2yqxe") -metadata/_edit_group_ = true - -[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"] -shape = SubResource("CircleShape2D_t5iiu") -debug_color = Color(0, 1, 0, 0.662745) +[node name="CharacterBody2D" parent="." instance=ExtResource("2_vxspj")] +position = Vector2(522, 600) [node name="Area2D" parent="." instance=ExtResource("3_sr8fi")] position = Vector2(683, 645) + +[node name="RigidBody2D" parent="." groups=["Pushable"] instance=ExtResource("1_8ldta")] +position = Vector2(298, 550) + +[node name="RigidBody2D2" parent="." groups=["Pushable"] instance=ExtResource("1_8ldta")] +position = Vector2(297, 527) diff --git a/august26godotcompkieran/Scenes/Player.tscn b/august26godotcompkieran/Scenes/Player.tscn new file mode 100644 index 0000000..ab77e43 --- /dev/null +++ b/august26godotcompkieran/Scenes/Player.tscn @@ -0,0 +1,25 @@ +[gd_scene load_steps=3 format=3 uid="uid://crvsom7i1waff"] + +[ext_resource type="Script" path="res://Sripts/player.gd" id="1_jdiv6"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_t5iiu"] + +[node name="CharacterBody2D" type="CharacterBody2D"] +script = ExtResource("1_jdiv6") +metadata/_edit_group_ = true + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("CircleShape2D_t5iiu") +debug_color = Color(0, 1, 0, 0.662745) + +[node name="RightRay" type="RayCast2D" parent="."] +target_position = Vector2(25, 0) + +[node name="LeftRay" type="RayCast2D" parent="."] +target_position = Vector2(-25, 0) + +[node name="MarkerRight" type="Node2D" parent="."] +position = Vector2(14, -2) + +[node name="MarkerLeft" type="Node2D" parent="."] +position = Vector2(-14, -2) diff --git a/august26godotcompkieran/Sripts/Bullet.gd b/august26godotcompkieran/Sripts/Bullet.gd new file mode 100644 index 0000000..51409ef --- /dev/null +++ b/august26godotcompkieran/Sripts/Bullet.gd @@ -0,0 +1,13 @@ +extends Area2D + +var speed := 700 +signal hit(bullet, body) + +func setSpeed(speedVal): + speed = speedVal + +func _physics_process(delta: float) -> void: + position += transform.x * speed * delta + +func _on_body_entered(body: Node2D) -> void: + print("Hit") diff --git a/august26godotcompkieran/Sripts/player.gd b/august26godotcompkieran/Sripts/player.gd index 1e8ceaf..cd2f02d 100644 --- a/august26godotcompkieran/Sripts/player.gd +++ b/august26godotcompkieran/Sripts/player.gd @@ -3,6 +3,20 @@ extends CharacterBody2D const SPEED = 300.0 const JUMP_VELOCITY = -400.0 +const PUSH_FORCE = 700 + +var faceLeft = false +# can I push right or left +var pushLeftEnabled = false +var pushRightEnabled = false +@onready var right_ray: RayCast2D = $RightRay +@onready var left_ray: RayCast2D = $LeftRay +@onready var marker_right: Node2D = $MarkerRight +@onready var marker_left: Node2D = $MarkerLeft + +var pushTarget + +var bullet = preload("res://Scenes/Bullet.tscn") func _physics_process(delta: float) -> void: @@ -13,10 +27,43 @@ func _physics_process(delta: float) -> void: # Handle jump. if Input.is_action_just_pressed("ui_accept") and is_on_floor(): velocity.y = JUMP_VELOCITY + + if Input.is_action_just_pressed("Shove") && pushLeftEnabled && faceLeft: + print("Shove left") + pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 1.5) + pushLeftEnabled = false + + if Input.is_action_just_pressed("Shove") && pushRightEnabled && not faceLeft: + print("Shove right") + pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 1.5) + pushRightEnabled = false + + if Input.is_action_just_pressed("Shoot"): + print ("Shooting") + var mybullet = bullet.instantiate() + + if faceLeft: + print("shoot left") + mybullet.setSpeed(-700) + #position bullet + mybullet.transform = marker_left.global_transform + else: + print("shoot right") + #position bullet + mybullet.transform = marker_right.global_transform + + + #add bullet + owner.add_child(mybullet) # Get the input direction and handle the movement/deceleration. # As good practice, you should replace UI actions with custom gameplay actions. var direction := Input.get_axis("ui_left", "ui_right") + if direction <0: + faceLeft = true + if direction >0: + faceLeft = false + if direction: velocity.x = direction * SPEED else: @@ -26,5 +73,27 @@ func _physics_process(delta: float) -> void: for i in get_slide_collision_count(): var c = get_slide_collision(i) if c. get_collider() is RigidBody2D: - c.get_collider().apply_central_impulse(-c.get_normal()* 200) - + c.get_collider().apply_central_impulse(-c.get_normal()* 50) + + + if left_ray.is_colliding(): + print("left ray collistion") + var collider = left_ray.get_collider() + if collider is Node: + if collider.is_in_group("Pushable"): + pushLeftEnabled = true + pushTarget = collider + else: + # do somthing else + pushLeftEnabled = false + + if right_ray.is_colliding(): + print("right ray collistion") + var collider = right_ray.get_collider() + if collider is Node: + if collider.is_in_group("Pushable"): + pushRightEnabled = true + pushTarget = collider + else: + + pushRightEnabled = false diff --git a/august26godotcompkieran/project.godot b/august26godotcompkieran/project.godot index c21dcb8..3d6a587 100644 --- a/august26godotcompkieran/project.godot +++ b/august26godotcompkieran/project.godot @@ -14,3 +14,16 @@ config/name="August26GodotcompKieran" run/main_scene="res://Scenes/Game.tscn" config/features=PackedStringArray("4.3", "Forward Plus") config/icon="res://icon.svg" + +[input] + +Shove={ +"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":90,"key_label":0,"unicode":122,"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":88,"key_label":0,"unicode":120,"location":0,"echo":false,"script":null) +] +}