From 887bdfbed355cb0f7a2edb8af3d3f6bde766082e Mon Sep 17 00:00:00 2001 From: OddlyTimbot Date: Mon, 9 Sep 2024 21:02:01 -0400 Subject: [PATCH] bullets and force push working --- augustgamecourse/project.godot | 13 ++++++ augustgamecourse/scenes/bullet.tscn | 16 ++++++++ augustgamecourse/scenes/crate.tscn | 2 - augustgamecourse/scenes/game.tscn | 32 +++++---------- augustgamecourse/scenes/player.tscn | 25 +++++++++++ augustgamecourse/scripts/bullet.gd | 13 ++++++ augustgamecourse/scripts/player.gd | 64 +++++++++++++++++++++++++++++ 7 files changed, 141 insertions(+), 24 deletions(-) create mode 100644 augustgamecourse/scenes/bullet.tscn create mode 100644 augustgamecourse/scenes/player.tscn create mode 100644 augustgamecourse/scripts/bullet.gd diff --git a/augustgamecourse/project.godot b/augustgamecourse/project.godot index 8479cc8..84fba20 100644 --- a/augustgamecourse/project.godot +++ b/augustgamecourse/project.godot @@ -14,3 +14,16 @@ config/name="augustgamecourse" 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) +] +} diff --git a/augustgamecourse/scenes/bullet.tscn b/augustgamecourse/scenes/bullet.tscn new file mode 100644 index 0000000..e149942 --- /dev/null +++ b/augustgamecourse/scenes/bullet.tscn @@ -0,0 +1,16 @@ +[gd_scene load_steps=3 format=3 uid="uid://dkf3cij6wexnr"] + +[ext_resource type="Script" path="res://scripts/bullet.gd" id="1_u02r5"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_5agw0"] +size = Vector2(12, 4) + +[node name="Area2D" type="Area2D"] +script = ExtResource("1_u02r5") +metadata/_edit_group_ = true + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_5agw0") +debug_color = Color(0.209612, 0.626644, 0.380971, 0.42) + +[connection signal="body_entered" from="." to="." method="_on_body_entered"] diff --git a/augustgamecourse/scenes/crate.tscn b/augustgamecourse/scenes/crate.tscn index 2669c81..4f9140d 100644 --- a/augustgamecourse/scenes/crate.tscn +++ b/augustgamecourse/scenes/crate.tscn @@ -3,8 +3,6 @@ [sub_resource type="RectangleShape2D" id="RectangleShape2D_3vc5e"] [node name="RigidBody2D" type="RigidBody2D"] -position = Vector2(546, 329) -rotation = 0.879359 metadata/_edit_group_ = true [node name="CollisionShape2D" type="CollisionShape2D" parent="."] diff --git a/augustgamecourse/scenes/game.tscn b/augustgamecourse/scenes/game.tscn index 88dc96b..53bd3b9 100644 --- a/augustgamecourse/scenes/game.tscn +++ b/augustgamecourse/scenes/game.tscn @@ -1,15 +1,13 @@ -[gd_scene load_steps=7 format=3 uid="uid://diss5ty7548p3"] +[gd_scene load_steps=6 format=3 uid="uid://diss5ty7548p3"] [ext_resource type="PackedScene" uid="uid://0j0320rcnxo7" path="res://scenes/crate.tscn" id="1_8ekmb"] -[ext_resource type="Script" path="res://scripts/player.gd" id="2_8vtyd"] +[ext_resource type="PackedScene" uid="uid://dbgc24hrbtvxm" path="res://scenes/player.tscn" id="2_dlxtb"] [ext_resource type="PackedScene" uid="uid://csewven6s5npw" path="res://scenes/trigger.tscn" id="3_t3xge"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_byea1"] [sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_nl2iw"] -[sub_resource type="CircleShape2D" id="CircleShape2D_mcosy"] - [node name="World" type="Node2D"] [node name="Platform" type="StaticBody2D" parent="."] @@ -57,24 +55,14 @@ metadata/_edit_group_ = true [node name="CollisionShape2D" type="CollisionShape2D" parent="Floor"] shape = SubResource("WorldBoundaryShape2D_nl2iw") -[node name="RigidBody2D" parent="." instance=ExtResource("1_8ekmb")] - -[node name="RigidBody2D2" parent="." instance=ExtResource("1_8ekmb")] -position = Vector2(540, 175) -rotation = -0.0302685 - -[node name="RigidBody2D3" parent="." instance=ExtResource("1_8ekmb")] -position = Vector2(626, 349) -rotation = 0.872828 - -[node name="CharacterBody2D" type="CharacterBody2D" parent="."] -position = Vector2(537, 362) -script = ExtResource("2_8vtyd") -metadata/_edit_group_ = true - -[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"] -shape = SubResource("CircleShape2D_mcosy") -debug_color = Color(0.826895, 0.282133, 0.703752, 0.42) +[node name="CharacterBody2D" parent="." instance=ExtResource("2_dlxtb")] +position = Vector2(650, 362) [node name="Area2D" parent="." instance=ExtResource("3_t3xge")] position = Vector2(739, 500) + +[node name="RigidBody2D" parent="." groups=["pushables"] instance=ExtResource("1_8ekmb")] +position = Vector2(605, 327) + +[node name="RigidBody2D2" parent="." groups=["pushables"] instance=ExtResource("1_8ekmb")] +position = Vector2(718, 331) diff --git a/augustgamecourse/scenes/player.tscn b/augustgamecourse/scenes/player.tscn new file mode 100644 index 0000000..079a61c --- /dev/null +++ b/augustgamecourse/scenes/player.tscn @@ -0,0 +1,25 @@ +[gd_scene load_steps=3 format=3 uid="uid://dbgc24hrbtvxm"] + +[ext_resource type="Script" path="res://scripts/player.gd" id="1_5qpif"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_mcosy"] + +[node name="CharacterBody2D" type="CharacterBody2D"] +script = ExtResource("1_5qpif") +metadata/_edit_group_ = true + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("CircleShape2D_mcosy") +debug_color = Color(0.826895, 0.282133, 0.703752, 0.42) + +[node name="RightRay" type="RayCast2D" parent="."] +target_position = Vector2(16, 0) + +[node name="LeftRay" type="RayCast2D" parent="."] +target_position = Vector2(-17, 0) + +[node name="MarkerRight" type="Node2D" parent="."] +position = Vector2(12, -5) + +[node name="MarkerLeft" type="Node2D" parent="."] +position = Vector2(-15, -5) diff --git a/augustgamecourse/scripts/bullet.gd b/augustgamecourse/scripts/bullet.gd new file mode 100644 index 0000000..c66f0fa --- /dev/null +++ b/augustgamecourse/scripts/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("I done hit somethin") diff --git a/augustgamecourse/scripts/player.gd b/augustgamecourse/scripts/player.gd index c61943e..cfea604 100644 --- a/augustgamecourse/scripts/player.gd +++ b/augustgamecourse/scripts/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,39 @@ 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 a box on the 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 a box on the right") + pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 1.5) + pushRightEnabled=false + + if Input.is_action_just_pressed("shoot"): + print("I will shoot") + var mybullet = bullet.instantiate() + + if faceLeft: + print("shoot left") + mybullet.setSpeed(-700) + mybullet.transform = marker_left.global_transform + else: + print("shoot right") + mybullet.transform = marker_right.global_transform + + 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: @@ -27,3 +70,24 @@ func _physics_process(delta: float) -> void: var c = get_slide_collision(i) if c.get_collider() is RigidBody2D: c.get_collider().apply_central_impulse(-c.get_normal()* 100) + + if left_ray.is_colliding(): + print("left ray collision") + var collider = left_ray.get_collider() + if collider is Node: + if collider.is_in_group("pushables"): + pushLeftEnabled = true + pushTarget = collider + else: + #do something else + pushLeftEnabled = false + + if right_ray.is_colliding(): + print("right ray collision") + var collider = right_ray.get_collider() + if collider is Node: + if collider.is_in_group("pushables"): + pushRightEnabled = true + pushTarget = collider + else: + pushRightEnabled = false