From 74561cfb2f8ddf9be1c79bd5ee29d852319228b8 Mon Sep 17 00:00:00 2001 From: fabianainu Date: Mon, 25 Nov 2024 20:52:12 -0500 Subject: [PATCH] Working force push, working bullets --- fabigame/project.godot | 13 +++++++ fabigame/scenes/bullet.tscn | 15 ++++++++ fabigame/scenes/dude.tscn | 27 ++++++++++++++ fabigame/scenes/game.tscn | 32 ++++++++--------- fabigame/scripts/bullet.gd | 18 ++++++++++ fabigame/scripts/dudecontroller.gd | 57 +++++++++++++++++++++++++++++- fabigame/scripts/scene_manager.gd | 31 ++++++++++++++++ 7 files changed, 175 insertions(+), 18 deletions(-) create mode 100644 fabigame/scenes/bullet.tscn create mode 100644 fabigame/scenes/dude.tscn create mode 100644 fabigame/scripts/bullet.gd create mode 100644 fabigame/scripts/scene_manager.gd diff --git a/fabigame/project.godot b/fabigame/project.godot index 24ef4e2..4e5bc2d 100644 --- a/fabigame/project.godot +++ b/fabigame/project.godot @@ -20,3 +20,16 @@ config/icon="res://icon.svg" folder_colors={ "res://scripts/": "green" } + +[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":83,"key_label":0,"unicode":115,"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/fabigame/scenes/bullet.tscn b/fabigame/scenes/bullet.tscn new file mode 100644 index 0000000..14b6733 --- /dev/null +++ b/fabigame/scenes/bullet.tscn @@ -0,0 +1,15 @@ +[gd_scene load_steps=3 format=3 uid="uid://b2tgt64a7m32v"] + +[ext_resource type="Script" path="res://scripts/bullet.gd" id="1_jag7h"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_eswsk"] +size = Vector2(7, 1) + +[node name="Area2D" type="Area2D"] +script = ExtResource("1_jag7h") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_eswsk") +debug_color = Color(0.827451, 1, 0, 0.419608) + +[connection signal="body_entered" from="." to="." method="_on_body_entered"] diff --git a/fabigame/scenes/dude.tscn b/fabigame/scenes/dude.tscn new file mode 100644 index 0000000..5e22aa8 --- /dev/null +++ b/fabigame/scenes/dude.tscn @@ -0,0 +1,27 @@ +[gd_scene load_steps=3 format=3 uid="uid://djgtbc1quqvwe"] + +[ext_resource type="Script" path="res://scripts/dudecontroller.gd" id="1_ay2gi"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_iice4"] +radius = 6.32456 + +[node name="CharacterBody2D" type="CharacterBody2D" groups=["player"]] +script = ExtResource("1_ay2gi") +metadata/_edit_group_ = true + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("CircleShape2D_iice4") +debug_color = Color(0.598404, 0.370441, 1, 0.42) + +[node name="RightRay" type="RayCast2D" parent="."] +target_position = Vector2(15, 0) + +[node name="LeftRay" type="RayCast2D" parent="."] +position = Vector2(1, 0) +target_position = Vector2(-16, 0) + +[node name="RightTarget" type="Node2D" parent="."] +position = Vector2(6, -5) + +[node name="LeftTarget" type="Node2D" parent="."] +position = Vector2(-6, -5) diff --git a/fabigame/scenes/game.tscn b/fabigame/scenes/game.tscn index 3007f77..e5d1f70 100644 --- a/fabigame/scenes/game.tscn +++ b/fabigame/scenes/game.tscn @@ -1,15 +1,14 @@ -[gd_scene load_steps=8 format=3 uid="uid://ciko2h5jj1a3k"] +[gd_scene load_steps=9 format=3 uid="uid://ciko2h5jj1a3k"] -[ext_resource type="Script" path="res://scripts/dudecontroller.gd" id="1_2426f"] +[ext_resource type="PackedScene" uid="uid://djgtbc1quqvwe" path="res://scenes/dude.tscn" id="1_4l1yw"] [ext_resource type="PackedScene" uid="uid://bw3gps1njvtgr" path="res://scenes/box.tscn" id="1_mg5q5"] +[ext_resource type="Script" path="res://scripts/scene_manager.gd" id="1_tpgoh"] [ext_resource type="Script" path="res://scripts/trigger.gd" id="2_a7w16"] +[ext_resource type="PackedScene" uid="uid://b2tgt64a7m32v" path="res://scenes/bullet.tscn" id="4_0ysyc"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_fuiwd"] size = Vector2(142, 10) -[sub_resource type="CircleShape2D" id="CircleShape2D_iice4"] -radius = 6.32456 - [sub_resource type="RectangleShape2D" id="RectangleShape2D_lbd3t"] size = Vector2(20, 4.47115) @@ -17,6 +16,10 @@ size = Vector2(20, 4.47115) [node name="Game" type="Node2D"] +[node name="SceneManager" type="Node" parent="."] +unique_name_in_owner = true +script = ExtResource("1_tpgoh") + [node name="Platform" type="StaticBody2D" parent="."] position = Vector2(394, 460) metadata/_edit_group_ = true @@ -25,14 +28,7 @@ metadata/_edit_group_ = true scale = Vector2(1.02381, 1.00385) shape = SubResource("RectangleShape2D_fuiwd") -[node name="CharacterBody2D" type="CharacterBody2D" parent="."] -position = Vector2(380, 429) -script = ExtResource("1_2426f") -metadata/_edit_group_ = true - -[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"] -shape = SubResource("CircleShape2D_iice4") -debug_color = Color(0.598404, 0.370441, 1, 0.42) +[node name="CharacterBody2D" parent="." instance=ExtResource("1_4l1yw")] [node name="Area2D" type="Area2D" parent="."] position = Vector2(392, 535) @@ -42,17 +38,16 @@ metadata/_edit_group_ = true [node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"] position = Vector2(0, -7.76443) -scale = Vector2(1, 1) shape = SubResource("RectangleShape2D_lbd3t") debug_color = Color(0.31865, 0.625237, 0, 0.42) -[node name="RigidBody2D" parent="." instance=ExtResource("1_mg5q5")] +[node name="RigidBody2D" parent="." groups=["shootable"] instance=ExtResource("1_mg5q5")] position = Vector2(432, 417) -[node name="RigidBody2D2" parent="." instance=ExtResource("1_mg5q5")] +[node name="RigidBody2D2" parent="." groups=["shootable"] instance=ExtResource("1_mg5q5")] position = Vector2(405, 385) -[node name="RigidBody2D3" parent="." instance=ExtResource("1_mg5q5")] +[node name="RigidBody2D3" parent="." groups=["shootable"] instance=ExtResource("1_mg5q5")] position = Vector2(422, 344) [node name="StaticBody2D" type="StaticBody2D" parent="."] @@ -62,4 +57,7 @@ metadata/_edit_group_ = true [node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"] shape = SubResource("WorldBoundaryShape2D_vdfbk") +[node name="Area2D2" parent="." instance=ExtResource("4_0ysyc")] +position = Vector2(269, 391) + [connection signal="body_entered" from="Area2D" to="Area2D" method="_on_body_entered"] diff --git a/fabigame/scripts/bullet.gd b/fabigame/scripts/bullet.gd new file mode 100644 index 0000000..afa6b4c --- /dev/null +++ b/fabigame/scripts/bullet.gd @@ -0,0 +1,18 @@ +class_name Bullet extends Area2D + +var speed = 750 +# did I hit someone? +signal hit(bullet, body) + +func setSpeed(speedValue): + speed = speedValue + +func _physics_process(delta): + position += transform.x * speed * delta + + +func _on_body_entered(body: Node2D) -> void: + print("Bullet hit something") + if not body.is_in_group("player"): + hit.emit(self, body) + diff --git a/fabigame/scripts/dudecontroller.gd b/fabigame/scripts/dudecontroller.gd index 6cf8ec6..cf40653 100644 --- a/fabigame/scripts/dudecontroller.gd +++ b/fabigame/scripts/dudecontroller.gd @@ -4,6 +4,17 @@ extends CharacterBody2D const SPEED = 300.0 const JUMP_VELOCITY = -400.0 const BUMP_FORCE = 50 +const PUSHFORCE = 750 +@onready var right_ray: RayCast2D = $RightRay +@onready var left_ray: RayCast2D = $LeftRay + +var faceLeft = false + +var pushTarget +var pushLefEnabled = false +var pushRightEnabled = false +@onready var right_target: Node2D = $RightTarget +@onready var left_target: Node2D = $LeftTarget func _physics_process(delta: float) -> void: @@ -18,13 +29,57 @@ func _physics_process(delta: float) -> void: # 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 Input.is_action_just_pressed("shove") && pushRightEnabled && faceLeft == false: + print("Shove a box") + pushTarget.apply_central_impulse(Vector2(1,0) * PUSHFORCE * 2) + pushRightEnabled = false + + if Input.is_action_just_pressed("shove") && faceLeft == true && pushLefEnabled == true: + pushTarget.apply_central_impulse(Vector2(-1,0) * PUSHFORCE * 2) + pushLefEnabled = false + + if Input.is_action_just_pressed("shoot"): + print("I'm an owl: Shoot shoot") + var myBullet = %SceneManager.bulletFactory() + if not faceLeft: + myBullet.transform = right_target.global_transform + myBullet.setSpeed(750) + if faceLeft == true: + myBullet.transform = left_target.global_transform + myBullet.setSpeed(-750) + if direction: velocity.x = direction * SPEED + if direction > 0: faceLeft = false + if direction < 0: faceLeft = true else: velocity.x = move_toward(velocity.x, 0, SPEED) move_and_slide() - + if right_ray.is_colliding(): + print("Something is on my right") + if not faceLeft: + var collider = right_ray.get_collider() + if collider is Node: + if collider.is_in_group("box"): + print("This is a pushable box") + pushTarget = collider + pushRightEnabled = true + else: + pushRightEnabled = false + + if left_ray.is_colliding(): + print("Something is on my left") + if faceLeft: + var collider = left_ray.get_collider() + if collider is Node: + if collider.is_in_group("box"): + print("I can shove this box") + pushTarget = collider + pushLefEnabled = true + else: + pushLefEnabled = false + for i in get_slide_collision_count(): var c = get_slide_collision(i) if c.get_collider() is RigidBody2D: diff --git a/fabigame/scripts/scene_manager.gd b/fabigame/scripts/scene_manager.gd new file mode 100644 index 0000000..56178d6 --- /dev/null +++ b/fabigame/scripts/scene_manager.gd @@ -0,0 +1,31 @@ +extends Node + +var bulletsFiredTotal := 0 +var bulletsMadeTotal := 0 +var bulletArray:Array = [] + +var bullet = preload("res://scenes/bullet.tscn") + +func bulletFactory(): + print("Make a bullet at the factory") + var mybullet + + #how many bullets have been made + if bulletArray.size() < 4: + #make a new bullet + mybullet = bullet.instantiate() + mybullet.connect("hit", onBulletHit) + owner.add_child(mybullet) + else: + mybullet = bulletArray.pop_back() + bulletArray.push_front(mybullet) + bulletsMadeTotal += 1 + return mybullet + +func onBulletHit(bullet, body): + print("Scene Manager knows a bullet hit something") + bullet.setSpeed(0) + if body.is_in_group("shootable"): + print("RIP") + body.queue_free() +