From 458b3e25fccc5accb353ddacefb82dc3761f4295 Mon Sep 17 00:00:00 2001 From: Anthony Russo Date: Mon, 29 Jun 2026 20:58:50 -0400 Subject: [PATCH] Week 2 commits: force push, multiple triggers, force push, teleporting crates. --- project.godot | 13 ++++++++ scenes/bullet.tscn | 15 +++++++++ scenes/crate.tscn | 2 +- scenes/game.tscn | 71 +++++++++++++++++++++------------------- scenes/player.tscn | 26 +++++++++++++++ scenes/trigger.tscn | 17 ++++++++++ scripts/bullet.gd | 15 +++++++++ scripts/bullet.gd.uid | 1 + scripts/crate.gd | 11 +++++++ scripts/game.gd | 31 +++++++++++++----- scripts/player.gd | 63 +++++++++++++++++++++++++++++++++-- scripts/scene_manager.gd | 53 ++++++++++++++++++++++++++++-- scripts/trigger.gd | 9 +++-- 13 files changed, 277 insertions(+), 50 deletions(-) create mode 100644 scenes/bullet.tscn create mode 100644 scenes/player.tscn create mode 100644 scenes/trigger.tscn create mode 100644 scripts/bullet.gd create mode 100644 scripts/bullet.gd.uid diff --git a/project.godot b/project.godot index 643cedc..39aade6 100644 --- a/project.godot +++ b/project.godot @@ -15,6 +15,19 @@ run/main_scene="uid://cu1mgc6337asm" config/features=PackedStringArray("4.6", "Forward Plus") config/icon="res://icon.svg" +[input] + +shove={ +"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":88,"key_label":0,"unicode":120,"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":90,"key_label":0,"unicode":122,"location":0,"echo":false,"script":null) +] +} + [physics] 3d/physics_engine="Jolt Physics" diff --git a/scenes/bullet.tscn b/scenes/bullet.tscn new file mode 100644 index 0000000..8f0d016 --- /dev/null +++ b/scenes/bullet.tscn @@ -0,0 +1,15 @@ +[gd_scene format=3 uid="uid://vdkcnj4ebu6r"] + +[ext_resource type="Script" uid="uid://dcqok1j5pliwd" path="res://scripts/bullet.gd" id="1_mkf8s"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_h1aey"] +size = Vector2(10, 4) + +[node name="Bullet" type="Area2D" unique_id=165117688] +script = ExtResource("1_mkf8s") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=488200767] +shape = SubResource("RectangleShape2D_h1aey") +debug_color = Color(0.7560967, 0, 0.13750124, 0.41960785) + +[connection signal="body_entered" from="." to="." method="_on_body_entered"] diff --git a/scenes/crate.tscn b/scenes/crate.tscn index 1c0dc99..4321e33 100644 --- a/scenes/crate.tscn +++ b/scenes/crate.tscn @@ -4,7 +4,7 @@ [sub_resource type="RectangleShape2D" id="RectangleShape2D_uwrxv"] -[node name="Crate" type="RigidBody2D" unique_id=1451413033] +[node name="Crate" type="RigidBody2D" unique_id=1451413033 groups=["shootable"]] rotation = 0.7475211 script = ExtResource("1_b66cd") metadata/_edit_group_ = true diff --git a/scenes/game.tscn b/scenes/game.tscn index 2c50994..db1d453 100644 --- a/scenes/game.tscn +++ b/scenes/game.tscn @@ -1,16 +1,14 @@ [gd_scene format=3 uid="uid://cu1mgc6337asm"] [ext_resource type="Script" uid="uid://cv4ex134wdu7j" path="res://scripts/game.gd" id="1_lnu2h"] -[ext_resource type="Script" uid="uid://dy0ahrwhakd4d" path="res://scripts/trigger.gd" id="1_uwrxv"] [ext_resource type="Script" uid="uid://cclb4boli603j" path="res://scripts/scene_manager.gd" id="2_lbhrr"] -[ext_resource type="Script" uid="uid://dy5o70pk5y8b2" path="res://scripts/player.gd" id="2_yqjtg"] [ext_resource type="PackedScene" uid="uid://b2rjhbe4f20hw" path="res://scenes/crate.tscn" id="3_iywne"] +[ext_resource type="PackedScene" uid="uid://csp8sgbqjmndx" path="res://scenes/player.tscn" id="5_iywne"] +[ext_resource type="PackedScene" uid="uid://cq5l3f6hna0kg" path="res://scenes/trigger.tscn" id="5_p57ef"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_8cj0n"] -[sub_resource type="CircleShape2D" id="CircleShape2D_8cj0n"] - -[sub_resource type="CircleShape2D" id="CircleShape2D_uwrxv"] +[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_iywne"] [node name="Game" type="Node2D" unique_id=304719081] script = ExtResource("1_lnu2h") @@ -18,58 +16,63 @@ script = ExtResource("1_lnu2h") [node name="SceneManager" type="Node2D" parent="." unique_id=277183513] script = ExtResource("2_lbhrr") +[node name="Player" parent="." unique_id=589925870 instance=ExtResource("5_iywne")] +position = Vector2(229.99994, 377) + [node name="Crates" type="Node2D" parent="." unique_id=1384881921] [node name="Crate" parent="Crates" unique_id=1451413033 instance=ExtResource("3_iywne")] -position = Vector2(796.99994, 300) +position = Vector2(480.99994, 293) [node name="Crate2" parent="Crates" unique_id=1863377672 instance=ExtResource("3_iywne")] -position = Vector2(938, 312) +position = Vector2(622, 305) -[node name="StaticBody2D" type="StaticBody2D" parent="." unique_id=2124897251] -position = Vector2(581, 420) +[node name="LevelELements" type="Node2D" parent="." unique_id=715855559] + +[node name="StaticBody2D" type="StaticBody2D" parent="LevelELements" unique_id=2124897251] +position = Vector2(265, 413) scale = Vector2(14.68, 1) metadata/_edit_group_ = true -[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D" unique_id=1284307723] +[node name="CollisionShape2D" type="CollisionShape2D" parent="LevelELements/StaticBody2D" unique_id=1284307723] shape = SubResource("RectangleShape2D_8cj0n") -[node name="StaticBody2D2" type="StaticBody2D" parent="." unique_id=1217525521] -position = Vector2(989, 424) +[node name="StaticBody2D2" type="StaticBody2D" parent="LevelELements" unique_id=1217525521] +position = Vector2(673, 417) scale = Vector2(14.68, 1) metadata/_edit_group_ = true -[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D2" unique_id=1959735816] +[node name="CollisionShape2D" type="CollisionShape2D" parent="LevelELements/StaticBody2D2" unique_id=1959735816] shape = SubResource("RectangleShape2D_8cj0n") -[node name="StaticBody2D3" type="StaticBody2D" parent="." unique_id=160438336] -position = Vector2(790.00006, 338) +[node name="StaticBody2D3" type="StaticBody2D" parent="LevelELements" unique_id=160438336] +position = Vector2(474.00006, 331) scale = Vector2(4.880001, 1) metadata/_edit_group_ = true -[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D3" unique_id=751554906] +[node name="CollisionShape2D" type="CollisionShape2D" parent="LevelELements/StaticBody2D3" unique_id=751554906] shape = SubResource("RectangleShape2D_8cj0n") -[node name="Trigger" type="Area2D" parent="." unique_id=1862352787] -position = Vector2(-36, 119) -script = ExtResource("1_uwrxv") +[node name="StaticBody2D4" type="StaticBody2D" parent="LevelELements" unique_id=849902454] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="LevelELements/StaticBody2D4" unique_id=1145034531] +position = Vector2(1, 588) +shape = SubResource("WorldBoundaryShape2D_iywne") metadata/_edit_group_ = true -[node name="CollisionShape2D" type="CollisionShape2D" parent="Trigger" unique_id=2003354144] -position = Vector2(817, 376.99997) -scale = Vector2(7.72, 7.72) -shape = SubResource("CircleShape2D_8cj0n") -debug_color = Color(0, 0.643742, 0.3001382, 0.41960785) +[node name="Triggers" type="Node2D" parent="." unique_id=969407448] -[node name="Player" type="CharacterBody2D" parent="." unique_id=1577861899] -position = Vector2(545.99994, 384) -scale = Vector2(1.8000001, 1.8000001) -script = ExtResource("2_yqjtg") -metadata/_edit_group_ = true +[node name="Trigger" parent="Triggers" unique_id=1612521889 instance=ExtResource("5_p57ef")] +position = Vector2(-352, 112) -[node name="CollisionShape2D" type="CollisionShape2D" parent="Player" unique_id=833165919] -shape = SubResource("CircleShape2D_uwrxv") -debug_color = Color(0.8463823, 0.0009889907, 0.9098306, 0.41960785) +[node name="Trigger2" parent="Triggers" unique_id=260830386 instance=ExtResource("5_p57ef")] +position = Vector2(80, 134) +intent = "fan" -[connection signal="areaTriggerSignal" from="Trigger" to="." method="_on_trigger"] -[connection signal="body_entered" from="Trigger" to="Trigger" method="_on_body_entered"] +[node name="Trigger5" parent="Triggers" unique_id=1243704039 instance=ExtResource("5_p57ef")] +position = Vector2(265, 56) +intent = "teleport" + +[connection signal="areaTriggerSignal" from="Triggers/Trigger" to="." method="_on_trigger"] +[connection signal="areaTriggerSignal" from="Triggers/Trigger2" to="." method="_on_trigger"] +[connection signal="areaTriggerSignal" from="Triggers/Trigger5" to="." method="_on_trigger"] diff --git a/scenes/player.tscn b/scenes/player.tscn new file mode 100644 index 0000000..2595561 --- /dev/null +++ b/scenes/player.tscn @@ -0,0 +1,26 @@ +[gd_scene format=3 uid="uid://csp8sgbqjmndx"] + +[ext_resource type="Script" uid="uid://dy5o70pk5y8b2" path="res://scripts/player.gd" id="1_3vyb7"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_uwrxv"] + +[node name="Player" type="CharacterBody2D" unique_id=589925870] +scale = Vector2(1.8000001, 1.8000001) +script = ExtResource("1_3vyb7") +metadata/_edit_group_ = true + +[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1451670636] +shape = SubResource("CircleShape2D_uwrxv") +debug_color = Color(0.8463823, 0.0009889907, 0.9098306, 0.41960785) + +[node name="RightCast" type="RayCast2D" parent="." unique_id=434200409] +target_position = Vector2(17.222221, 0) + +[node name="LeftCast" type="RayCast2D" parent="." unique_id=194078728] +target_position = Vector2(-16.666666, 0) + +[node name="RightBulletSpawn" type="Marker2D" parent="." unique_id=713435511] +position = Vector2(12.777777, -7.777777) + +[node name="LeftBulletSpawn" type="Marker2D" parent="." unique_id=794453359] +position = Vector2(-13.333332, -7.7777777) diff --git a/scenes/trigger.tscn b/scenes/trigger.tscn new file mode 100644 index 0000000..b646138 --- /dev/null +++ b/scenes/trigger.tscn @@ -0,0 +1,17 @@ +[gd_scene format=3 uid="uid://cq5l3f6hna0kg"] + +[ext_resource type="Script" uid="uid://dy0ahrwhakd4d" path="res://scripts/trigger.gd" id="1_du5ex"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_8cj0n"] + +[node name="Trigger" type="Area2D" unique_id=1612521889] +script = ExtResource("1_du5ex") +metadata/_edit_group_ = true + +[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=887163202] +position = Vector2(817, 376.99997) +scale = Vector2(7.72, 7.72) +shape = SubResource("CircleShape2D_8cj0n") +debug_color = Color(0, 0.643742, 0.3001382, 0.41960785) + +[connection signal="body_entered" from="." to="." method="_on_body_entered"] diff --git a/scripts/bullet.gd b/scripts/bullet.gd new file mode 100644 index 0000000..0b8cd33 --- /dev/null +++ b/scripts/bullet.gd @@ -0,0 +1,15 @@ +class_name Bullet extends Area2D + +var speed:float = 700 +signal bulletHitSignal(body, bullet) + +func setSpeed(value) -> void: + speed = value + +func _process(delta: float) -> void: + position += transform.x * speed * delta + + +func _on_body_entered(body: Node2D) -> void: + if body is Crate: + bulletHitSignal.emit(body, self) diff --git a/scripts/bullet.gd.uid b/scripts/bullet.gd.uid new file mode 100644 index 0000000..8c37c34 --- /dev/null +++ b/scripts/bullet.gd.uid @@ -0,0 +1 @@ +uid://dcqok1j5pliwd diff --git a/scripts/crate.gd b/scripts/crate.gd index 7f42195..f2ceb07 100644 --- a/scripts/crate.gd +++ b/scripts/crate.gd @@ -1 +1,12 @@ class_name Crate extends RigidBody2D +var teleport_target:Vector2 = Vector2.ZERO +var should_teleport:bool = false + +func teleport_to(position:Vector2): + teleport_target = position + should_teleport = true + +func _integrate_forces(state: PhysicsDirectBodyState2D) -> void: + if should_teleport: + state.transform = Transform2D.IDENTITY.translated(teleport_target) + should_teleport = false diff --git a/scripts/game.gd b/scripts/game.gd index 85a2853..00e2e04 100644 --- a/scripts/game.gd +++ b/scripts/game.gd @@ -1,11 +1,15 @@ -extends Node2D +class_name GameController extends Node2D +signal destroySignal(body) +signal fanTriggerSignal(body, fanDirection) +signal teleportTriggerSignal(body) var numberOfCrates = 2 -var timeAvailable = 5 +var timeAvailable = 50 var timer = Timer.new() # Called when the node enters the scene tree for the first time. func _ready() -> void: + get_window().grab_focus() add_child(timer) timer.wait_time = 1 timer.one_shot = false @@ -24,14 +28,25 @@ func _process(delta: float) -> void: pass -func _on_trigger(body: Variant, trigger: Variant) -> void: +func _on_trigger(body: Variant, trigger: Variant, intent: String) -> void: print("Game controller knows trigger fired") - body.queue_free() - numberOfCrates -=1 - if numberOfCrates == 0: - print("WINNER!!!") - get_tree().reload_current_scene() + match intent: + "destroy": + destroySignal.emit(body) + "fan": + fanTriggerSignal.emit(body,Vector2(0,-1)*100*9) + print("fan effect") + "teleport": + print("teleport effect") + teleportTriggerSignal.emit(body) func crateTotal(numOfCrates): numberOfCrates = numOfCrates print("Game controller knows about crates - there are: " + str(numOfCrates)) + if numberOfCrates == 0: + print("WINNER!!!") + get_tree().reload_current_scene() + +func bulletDamage(body, _bullet) -> void: + if body.is_in_group("shootable"): + destroySignal.emit(body) diff --git a/scripts/player.gd b/scripts/player.gd index ecc0685..5af6538 100644 --- a/scripts/player.gd +++ b/scripts/player.gd @@ -1,9 +1,22 @@ -extends CharacterBody2D +class_name Player extends CharacterBody2D +signal pushSignal(body,direction) +signal shootSignal(markerPosition, speed) +@onready var right_cast: RayCast2D = $RightCast +@onready var left_cast: RayCast2D = $LeftCast +@onready var right_bullet_spawn: Marker2D = $RightBulletSpawn +@onready var left_bullet_spawn: Marker2D = $LeftBulletSpawn + +var pushRightEnabled = false +var pushLeftEnabled = false +var pushTarget +var pushTimer = 0 const SPEED = 300.0 const JUMP_VELOCITY = -400.0 +enum FaceDirection {RIGHT,LEFT} +var facing:FaceDirection = FaceDirection.RIGHT func _physics_process(delta: float) -> void: # Add the gravity. @@ -13,17 +26,63 @@ func _physics_process(delta: float) -> void: # Handle jump. if Input.is_action_just_pressed("ui_up") and is_on_floor(): velocity.y = JUMP_VELOCITY + + # Handle shove + if Input.is_action_just_pressed("shove"): + print("Shove pressed") + pushTimer = Time.get_ticks_msec() + if Input.is_action_just_released("shove"): + var pushForce = Time.get_ticks_msec() - pushTimer + if pushForce > 4000: + pushForce = 4000 + if pushRightEnabled and facing == FaceDirection.RIGHT: + pushSignal.emit(pushTarget, Vector2(1,0) * 100 * (pushForce/100)) + if pushLeftEnabled and facing == FaceDirection.LEFT: + pushSignal.emit(pushTarget, Vector2(-1,0) * 100 * (pushForce/100)) + + # Handle shoot + if Input.is_action_just_pressed("shoot"): + print("Shoot pressed") + match facing: + FaceDirection.RIGHT: + shootSignal.emit(right_bullet_spawn.global_transform, 800) + FaceDirection.LEFT: + shootSignal.emit(left_bullet_spawn.global_transform, 800) # 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: velocity.x = direction * SPEED + if direction > 0: + facing = FaceDirection.RIGHT + else: + facing = FaceDirection.LEFT else: velocity.x = move_toward(velocity.x, 0, SPEED) move_and_slide() + # Collision detection needs to happen AFTER move_and_slide calculation + if right_cast.is_colliding(): + print("Right raycast collision") + var collider = right_cast.get_collider() + if collider is Node: + pushRightEnabled = true + pushTarget = collider + else: + pushRightEnabled = false + + if left_cast.is_colliding(): + print("Left raycast collision") + var collider = left_cast.get_collider() + if collider is Node: + pushLeftEnabled = true + pushTarget = collider + else: + pushLeftEnabled = false + 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) + #c.get_collider().apply_central_impulse(-c.get_normal() * 100) + pushSignal.emit(c.get_collider(),-c.get_normal() * 100) diff --git a/scripts/scene_manager.gd b/scripts/scene_manager.gd index 56e6e49..a457692 100644 --- a/scripts/scene_manager.gd +++ b/scripts/scene_manager.gd @@ -1,9 +1,21 @@ extends Node2D -@onready var game: Node2D = $".." +@onready var game: GameController = $".." @onready var crates: Node2D = $"../Crates" +@onready var player: Player = $"../Player" + +var bullet = preload("res://scenes/bullet.tscn") +var bulletArray:Array[Bullet] = [] +var totalAllowedBullet:int = 7 # Called when the node enters the scene tree for the first time. func _ready() -> void: + # signals from the view + player.pushSignal.connect(pushTarget) + player.shootSignal.connect(makeBullet) + # signals from the game controller + game.destroySignal.connect(destroy) + game.fanTriggerSignal.connect(pushTarget) + game.teleportTriggerSignal.connect(teleportTarget) buildLevel() # Called every frame. 'delta' is the elapsed time since the previous frame. @@ -11,10 +23,47 @@ func _process(delta: float) -> void: pass func buildLevel() -> void: - # tell game controller how many crates + updateCrates() + +func updateCrates() -> void: var totalCrates = 0 if crates: for obj in crates.get_children(): if obj is Crate: + if not obj.tree_exited.is_connected(updateCrates): + obj.tree_exited.connect(updateCrates) totalCrates += 1 game.crateTotal(totalCrates) + +func pushTarget(body,pushDirection) -> void: + if body is RigidBody2D: + body.apply_central_impulse(pushDirection) + +func destroy(body) -> void: + body.queue_free() + +func teleportTarget(body) -> void: + if body is Crate: + var viewport_size = get_viewport().get_visible_rect().size + var random_x = randf_range(0,viewport_size.x) + var random_y = randf_range(0,viewport_size.y) + var newPosition = Vector2(random_x, random_y) + body.teleport_to(newPosition) + +func bulletFactory() -> Bullet: + var myBullet:Bullet + if bulletArray.size() <= totalAllowedBullet: + myBullet = bullet.instantiate() + add_sibling(myBullet) + if not myBullet.bulletHitSignal.is_connected(game.bulletDamage): + myBullet.bulletHitSignal.connect(game.bulletDamage) + else: + myBullet = bulletArray.pop_back() + bulletArray.push_front(myBullet) + return myBullet + +func makeBullet(spawnPosition, speed) -> void: + print("make bullet") + var myBullet:Bullet = bulletFactory() + myBullet.transform = spawnPosition + myBullet.setSpeed(speed) diff --git a/scripts/trigger.gd b/scripts/trigger.gd index 2e584af..a720dbd 100644 --- a/scripts/trigger.gd +++ b/scripts/trigger.gd @@ -1,5 +1,8 @@ -extends Area2D -signal areaTriggerSignal(body, trigger) +class_name Trigger extends Area2D + +@export var intent = "destroy" + +signal areaTriggerSignal(body, trigger, intent) # Called when the node enters the scene tree for the first time. func _ready() -> void: @@ -13,4 +16,4 @@ func _process(delta: float) -> void: func _on_body_entered(body: Node2D) -> void: print("Something has entered the trigger zone") - areaTriggerSignal.emit(body, self) + areaTriggerSignal.emit(body, self, intent)