From abd5200f21bebd598e01e0267675e866981c9e00 Mon Sep 17 00:00:00 2001 From: TaylorB Date: Sun, 17 Aug 2025 22:18:14 -0400 Subject: [PATCH] stuff --- Scenes/Crate.tscn | 14 +++++++ Scenes/bullet.tscn | 15 ++++++++ Scenes/player.tscn | 25 ++++++++++++ Scenes/reallygoodgame.tscn | 40 ++++++-------------- project.godot | 13 +++++++ scripts/Player.gd | 73 +++++++++++++++++++++++++++++++++--- scripts/bullet.gd | 18 +++++++++ scripts/bullet.gd.uid | 1 + scripts/crate.gd | 1 + scripts/crate.gd.uid | 1 + scripts/gamecontroller.gd | 7 ++++ scripts/scene_manager.gd | 33 ++++++++++++++++ scripts/scene_manager.gd.uid | 1 + 13 files changed, 209 insertions(+), 33 deletions(-) create mode 100644 Scenes/Crate.tscn create mode 100644 Scenes/bullet.tscn create mode 100644 Scenes/player.tscn create mode 100644 scripts/bullet.gd create mode 100644 scripts/bullet.gd.uid create mode 100644 scripts/crate.gd create mode 100644 scripts/crate.gd.uid create mode 100644 scripts/scene_manager.gd create mode 100644 scripts/scene_manager.gd.uid diff --git a/Scenes/Crate.tscn b/Scenes/Crate.tscn new file mode 100644 index 0000000..07a27f9 --- /dev/null +++ b/Scenes/Crate.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=3 format=3 uid="uid://lwju71dirkcy"] + +[ext_resource type="Script" uid="uid://diuug3xgjo1se" path="res://scripts/crate.gd" id="1_qgfyo"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_r8wv2"] + +[node name="Crate" type="RigidBody2D" groups=["pushable"]] +rotation = 0.90108 +script = ExtResource("1_qgfyo") +metadata/_edit_group_ = true + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("RectangleShape2D_r8wv2") +debug_color = Color(0.875314, 0.341029, 0.122297, 0.42) diff --git a/Scenes/bullet.tscn b/Scenes/bullet.tscn new file mode 100644 index 0000000..86343bd --- /dev/null +++ b/Scenes/bullet.tscn @@ -0,0 +1,15 @@ +[gd_scene load_steps=3 format=3 uid="uid://vycud8mssmb3"] + +[ext_resource type="Script" uid="uid://doxxkkrf00ef2" path="res://scripts/bullet.gd" id="1_xjght"] + +[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_4mw4s"] + +[node name="bullet" type="Area2D"] +script = ExtResource("1_xjght") +metadata/_edit_group_ = true + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +rotation = 1.58879 +shape = SubResource("CapsuleShape2D_4mw4s") + +[connection signal="body_entered" from="." to="." method="_on_body_entered"] diff --git a/Scenes/player.tscn b/Scenes/player.tscn new file mode 100644 index 0000000..3295495 --- /dev/null +++ b/Scenes/player.tscn @@ -0,0 +1,25 @@ +[gd_scene load_steps=3 format=3 uid="uid://c5uf1o0s4syv7"] + +[ext_resource type="Script" uid="uid://bxlrsqltut2xn" path="res://scripts/Player.gd" id="1_v0iea"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_ag0er"] + +[node name="Player" type="CharacterBody2D"] +script = ExtResource("1_v0iea") +metadata/_edit_group_ = true + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("CircleShape2D_ag0er") +debug_color = Color(3.08037e-06, 0.641589, 0.331866, 0.42) + +[node name="RightCast" type="RayCast2D" parent="."] +target_position = Vector2(48, 0) + +[node name="LeftCast" type="RayCast2D" parent="."] +target_position = Vector2(-47, 0) + +[node name="BulletSpawnPointRight" type="Node2D" parent="."] +position = Vector2(16, -5) + +[node name="BulletSpawnPointLeft" type="Node2D" parent="."] +position = Vector2(-16, -5) diff --git a/Scenes/reallygoodgame.tscn b/Scenes/reallygoodgame.tscn index e06e0bf..c605797 100644 --- a/Scenes/reallygoodgame.tscn +++ b/Scenes/reallygoodgame.tscn @@ -1,21 +1,23 @@ [gd_scene load_steps=8 format=3 uid="uid://ibkufgrwjwro"] [ext_resource type="Script" uid="uid://buhkldpol3slp" path="res://scripts/gamecontroller.gd" id="1_3d84a"] -[ext_resource type="Script" uid="uid://bxlrsqltut2xn" path="res://scripts/Player.gd" id="1_r8wv2"] [ext_resource type="PackedScene" uid="uid://4a4u47173cyb" path="res://Scenes/trigger.tscn" id="2_lptsb"] +[ext_resource type="Script" uid="uid://bo0opwq3l73ec" path="res://scripts/scene_manager.gd" id="2_t43dh"] +[ext_resource type="PackedScene" uid="uid://lwju71dirkcy" path="res://Scenes/Crate.tscn" id="3_u2cf2"] +[ext_resource type="PackedScene" uid="uid://c5uf1o0s4syv7" path="res://Scenes/player.tscn" id="4_f0he5"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_ag0er"] size = Vector2(82, 20) -[sub_resource type="RectangleShape2D" id="RectangleShape2D_r8wv2"] - [sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_ag0er"] -[sub_resource type="CircleShape2D" id="CircleShape2D_ag0er"] - [node name="Game" type="Node2D"] script = ExtResource("1_3d84a") +[node name="SCENEMANAGER" type="Node2D" parent="."] +unique_name_in_owner = true +script = ExtResource("2_t43dh") + [node name="StaticBody2D" type="StaticBody2D" parent="."] position = Vector2(113, 130) metadata/_edit_group_ = true @@ -23,23 +25,11 @@ metadata/_edit_group_ = true [node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D"] shape = SubResource("RectangleShape2D_ag0er") -[node name="RigidBody2D" type="RigidBody2D" parent="."] +[node name="Crate1" parent="." instance=ExtResource("3_u2cf2")] position = Vector2(128, 41) -rotation = 0.90108 -metadata/_edit_group_ = true -[node name="CollisionShape2D" type="CollisionShape2D" parent="RigidBody2D"] -shape = SubResource("RectangleShape2D_r8wv2") -debug_color = Color(0.875314, 0.341029, 0.122297, 0.42) - -[node name="RigidBody2D2" type="RigidBody2D" parent="."] -position = Vector2(114, -1) -rotation = 0.90108 -metadata/_edit_group_ = true - -[node name="CollisionShape2D" type="CollisionShape2D" parent="RigidBody2D2"] -shape = SubResource("RectangleShape2D_r8wv2") -debug_color = Color(0.875314, 0.341029, 0.122297, 0.42) +[node name="Crate2" parent="." instance=ExtResource("3_u2cf2")] +position = Vector2(171, 73) [node name="ground" type="StaticBody2D" parent="."] position = Vector2(186, 270) @@ -48,16 +38,10 @@ metadata/_edit_group_ = true [node name="CollisionShape2D" type="CollisionShape2D" parent="ground"] shape = SubResource("WorldBoundaryShape2D_ag0er") -[node name="CharacterBody2D" type="CharacterBody2D" parent="."] +[node name="Player" parent="." instance=ExtResource("4_f0he5")] position = Vector2(127, 108) -script = ExtResource("1_r8wv2") -metadata/_edit_group_ = true - -[node name="CollisionShape2D" type="CollisionShape2D" parent="CharacterBody2D"] -shape = SubResource("CircleShape2D_ag0er") -debug_color = Color(3.08037e-06, 0.641589, 0.331866, 0.42) [node name="Area2D" parent="." instance=ExtResource("2_lptsb")] -position = Vector2(88, 86) +position = Vector2(82, 87) [connection signal="areaTriggerSignal" from="Area2D" to="." method="_on_trigger"] diff --git a/project.godot b/project.godot index 6704db4..1c25b39 100644 --- a/project.godot +++ b/project.godot @@ -14,3 +14,16 @@ config/name="reallygoodgame" run/main_scene="uid://ibkufgrwjwro" config/features=PackedStringArray("4.4", "Forward Plus") config/icon="res://icon.svg" + +[input] + +ForcePush={ +"deadzone": 0.2, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":70,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) +] +} +Shoot={ +"deadzone": 0.2, +"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":69,"key_label":0,"unicode":101,"location":0,"echo":false,"script":null) +] +} diff --git a/scripts/Player.gd b/scripts/Player.gd index 417ccd2..fc8bedf 100644 --- a/scripts/Player.gd +++ b/scripts/Player.gd @@ -1,29 +1,92 @@ class_name Player extends CharacterBody2D +const bullet_scene = preload("res://Scenes/bullet.tscn") + +@onready var right_cast: RayCast2D = $RightCast +@onready var left_cast: RayCast2D = $LeftCast +@onready var bullet_spawn_point_right: Node2D = $BulletSpawnPointRight +@onready var bullet_spawn_point_left: Node2D = $BulletSpawnPointLeft const SPEED = 300.0 const JUMP_VELOCITY = -400.0 +const PUSH_POWER = 2000 + +var direction: float +enum FaceDirection{LEFT, RIGHT} +var facing: FaceDirection = FaceDirection.RIGHT +var pushTarget: Object +var pushEnabled: bool = false func _physics_process(delta: float) -> void: - # Add the gravity. - if not is_on_floor(): - velocity += get_gravity() * delta + handle_input(delta) + handle_movement(delta) + handle_collisions(delta) +func handle_input(delta: float) -> void: + var dir: int = 0 + match facing: + FaceDirection.LEFT: + dir = -1 + FaceDirection.RIGHT: + dir = 1 + # Handle jump. if Input.is_action_just_pressed("ui_accept") and is_on_floor(): velocity.y = JUMP_VELOCITY # 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") + direction = Input.get_axis("ui_left", "ui_right") + + if direction < 0: + facing = FaceDirection.LEFT + elif direction > 0: + facing = FaceDirection.RIGHT + + if Input.is_action_just_pressed("ForcePush") && pushEnabled: + pushTarget.apply_central_impulse(Vector2(dir, 0) * PUSH_POWER) + + if Input.is_action_just_pressed("Shoot"): + var spawnAt: Node2D = bullet_spawn_point_left + if dir == 1: + spawnAt = bullet_spawn_point_right + + print("make a bullet") + var b = %SCENEMANAGER.makeBullet(spawnAt.global_transform, dir) + +func handle_movement(delta: float) -> void: + handle_gravity(delta) if direction: velocity.x = direction * SPEED else: velocity.x = move_toward(velocity.x, 0, SPEED) - move_and_slide() + +func handle_collisions(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() * 100) + + if right_cast.is_colliding() && facing == FaceDirection.RIGHT: + var col = right_cast.get_collider() + if col != null && col.is_in_group("pushable"): + pushTarget = col + pushEnabled = true + print("right pushable") + elif left_cast.is_colliding() && facing == FaceDirection.LEFT: + var col = left_cast.get_collider() + if col != null && col.is_in_group("pushable"): + pushTarget = col + pushEnabled = true + print("left pushable") + else: + pushEnabled = false + pushTarget = null + + +func handle_gravity(delta: float) -> void: + # Add the gravity. + if not is_on_floor(): + velocity += get_gravity() * delta diff --git a/scripts/bullet.gd b/scripts/bullet.gd new file mode 100644 index 0000000..2255946 --- /dev/null +++ b/scripts/bullet.gd @@ -0,0 +1,18 @@ +class_name Bullet extends Area2D + +var speed: float = 700 +signal bulletDamageSignal(target: Node2D, bullet: Node2D) + +func setSpeed(val: float): + speed = val + +func _physics_process(delta: float) -> void: + position+=transform.x*speed*delta + +func _on_body_entered(body: Node2D) -> void: + if body is Player: + return + print("emit?") + if body is Crate && visible: + print("emit") + bulletDamageSignal.emit(body, self) diff --git a/scripts/bullet.gd.uid b/scripts/bullet.gd.uid new file mode 100644 index 0000000..e061878 --- /dev/null +++ b/scripts/bullet.gd.uid @@ -0,0 +1 @@ +uid://doxxkkrf00ef2 diff --git a/scripts/crate.gd b/scripts/crate.gd new file mode 100644 index 0000000..7f42195 --- /dev/null +++ b/scripts/crate.gd @@ -0,0 +1 @@ +class_name Crate extends RigidBody2D diff --git a/scripts/crate.gd.uid b/scripts/crate.gd.uid new file mode 100644 index 0000000..8066309 --- /dev/null +++ b/scripts/crate.gd.uid @@ -0,0 +1 @@ +uid://diuug3xgjo1se diff --git a/scripts/gamecontroller.gd b/scripts/gamecontroller.gd index 25c3cf8..5334040 100644 --- a/scripts/gamecontroller.gd +++ b/scripts/gamecontroller.gd @@ -13,5 +13,12 @@ func _process(delta: float) -> void: func _on_trigger(effect: Variant, body: Variant) -> void: print("GC see trigger " + effect) + if body.name == "world-boundary": + return if not body is Player: body.queue_free() + +func bulletDamage(target: Node2D, bullet: Node2D): + print("GC sees a hit") + target.queue_free() + bullet.visible = false diff --git a/scripts/scene_manager.gd b/scripts/scene_manager.gd new file mode 100644 index 0000000..25a2d30 --- /dev/null +++ b/scripts/scene_manager.gd @@ -0,0 +1,33 @@ +class_name SCENEMANAGER extends Node2D + +const bulletscn = preload("res://Scenes/bullet.tscn") +@onready var game: Node2D = $".." + +var bullets: Array[Bullet] = [] +const MAX_BULLETS = 10 +var current_bullet = 0 + +# makes and recycles bullets +func bulletFactory() -> Bullet: + if len(bullets) < MAX_BULLETS: + # create new bujllet + var b = bulletscn.instantiate() + b.bulletDamageSignal.connect(game.bulletDamage) + add_child(b) + bullets.append(b) + return b + else: + # recycle + var i = current_bullet + current_bullet = (current_bullet + 1) % MAX_BULLETS + bullets[i].visible = true + return bullets[i] + + +func makeBullet(pos: Transform2D, dir: int) -> Node2D: + var b = bulletFactory() + # b.position = pos.get_origin() + b.transform = pos + b.setSpeed(dir * 700) + return b + diff --git a/scripts/scene_manager.gd.uid b/scripts/scene_manager.gd.uid new file mode 100644 index 0000000..8f5b65f --- /dev/null +++ b/scripts/scene_manager.gd.uid @@ -0,0 +1 @@ +uid://bo0opwq3l73ec