This commit is contained in:
Jdevadas 2026-03-09 21:04:23 -04:00
parent fa2677d014
commit 84e1af69e3
13 changed files with 250 additions and 49 deletions

View File

@ -23,6 +23,14 @@ folder_colors={
"res://scripts/": "green" "res://scripts/": "green"
} }
[input]
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] [physics]
3d/physics_engine="Jolt Physics" 3d/physics_engine="Jolt Physics"

12
scenes/bullet.tscn Normal file
View File

@ -0,0 +1,12 @@
[gd_scene format=3 uid="uid://cp6dv18imsngd"]
[ext_resource type="Script" uid="uid://bfuhpow7x2xr1" path="res://scripts/bullet.gd" id="1_mkf8s"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_rtl8c"]
[node name="Bullet" type="Area2D" unique_id=1567847193]
script = ExtResource("1_mkf8s")
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=492684522]
shape = SubResource("RectangleShape2D_rtl8c")
debug_color = Color(0.5263991, 0.42294088, 0.98291093, 0.41960785)

View File

@ -1,62 +1,71 @@
[gd_scene format=3 uid="uid://c2f5v136bbqod"] [gd_scene format=3 uid="uid://c2f5v136bbqod"]
[ext_resource type="Script" uid="uid://dxubdt7nh5s1j" path="res://scripts/gameController.gd" id="1_lnu2h"] [ext_resource type="Script" uid="uid://dxubdt7nh5s1j" path="res://scripts/gameController.gd" id="1_lnu2h"]
[ext_resource type="Script" uid="uid://qa1q5a2swlfc" path="res://scripts/scene_manager.gd" id="2_iywne"]
[ext_resource type="PackedScene" uid="uid://b6lw2go5mwk3h" path="res://scenes/crate.tscn" id="2_lbhrr"] [ext_resource type="PackedScene" uid="uid://b6lw2go5mwk3h" path="res://scenes/crate.tscn" id="2_lbhrr"]
[ext_resource type="Script" uid="uid://c17de7vvtri1e" path="res://scripts/player.gd" id="2_yqjtg"] [ext_resource type="PackedScene" uid="uid://s0utas3jmhjk" path="res://scenes/player.tscn" id="3_iywne"]
[ext_resource type="PackedScene" uid="uid://bf50mxdma8wq4" path="res://scenes/trigger.tscn" id="3_lbhrr"] [ext_resource type="PackedScene" uid="uid://bf50mxdma8wq4" path="res://scenes/trigger.tscn" id="3_lbhrr"]
[ext_resource type="PackedScene" uid="uid://cp6dv18imsngd" path="res://scenes/bullet.tscn" id="6_p57ef"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_8cj0n"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_8cj0n"]
size = Vector2(46, 20) size = Vector2(46, 20)
[sub_resource type="CircleShape2D" id="CircleShape2D_lnu2h"]
[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_iywne"] [sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_iywne"]
[node name="Game" type="Node2D" unique_id=863075634] [node name="Game" type="Node2D" unique_id=863075634]
script = ExtResource("1_lnu2h") script = ExtResource("1_lnu2h")
[node name="StaticBody2D" type="StaticBody2D" parent="." unique_id=793440430] [node name="SceneManager" type="Node2D" parent="." unique_id=1880010886]
unique_name_in_owner = true
script = ExtResource("2_iywne")
[node name="Player" parent="." unique_id=1109630338 instance=ExtResource("3_iywne")]
position = Vector2(326, 174)
[node name="Triggers" type="Node2D" parent="." unique_id=824433282]
[node name="Trigger" parent="Triggers" unique_id=1432397495 instance=ExtResource("3_lbhrr")]
position = Vector2(500, 295)
[node name="Trigger2" parent="Triggers" unique_id=528038205 instance=ExtResource("3_lbhrr")]
position = Vector2(196, 297)
effect = "teleport"
[node name="Trigger3" parent="Triggers" unique_id=516097243 instance=ExtResource("3_lbhrr")]
position = Vector2(287, 54)
effect = "powerup"
[node name="Crates" type="Node2D" parent="." unique_id=670229137]
[node name="Crate" parent="Crates" unique_id=289536100 instance=ExtResource("2_lbhrr")]
position = Vector2(400.00003, 171)
scale = Vector2(0.99999994, 0.99999994)
[node name="Crate2" parent="Crates" unique_id=1758294520 instance=ExtResource("2_lbhrr")]
position = Vector2(382, 96.999985)
scale = Vector2(0.99999994, 0.99999994)
[node name="Crate3" parent="Crates" unique_id=161286893 instance=ExtResource("2_lbhrr")]
position = Vector2(354, 114)
scale = Vector2(0.99999994, 0.99999994)
[node name="Level" type="Node2D" parent="." unique_id=2000617151]
[node name="StaticBody2D" type="StaticBody2D" parent="Level" unique_id=793440430]
position = Vector2(340.99997, 211) position = Vector2(340.99997, 211)
scale = Vector2(3.2297606, 1.0433427) scale = Vector2(3.2297606, 1.0433427)
metadata/_edit_group_ = true metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D" unique_id=21019430] [node name="CollisionShape2D" type="CollisionShape2D" parent="Level/StaticBody2D" unique_id=21019430]
shape = SubResource("RectangleShape2D_8cj0n") shape = SubResource("RectangleShape2D_8cj0n")
[node name="Crate" parent="." unique_id=289536100 instance=ExtResource("2_lbhrr")] [node name="StaticBody2D2" type="StaticBody2D" parent="Level" unique_id=531572601]
position = Vector2(400.00003, 171)
[node name="Crate2" parent="." unique_id=1758294520 instance=ExtResource("2_lbhrr")]
position = Vector2(284.99994, 159)
[node name="Trigger" parent="." unique_id=1432397495 instance=ExtResource("3_lbhrr")]
position = Vector2(500, 295)
effect = "destroy"
[node name="Trigger2" parent="." unique_id=528038205 instance=ExtResource("3_lbhrr")]
position = Vector2(238, 286)
effect = "bounce"
[node name="Player" type="CharacterBody2D" parent="." unique_id=1104847114]
position = Vector2(326, 174)
scale = Vector2(2, 2)
script = ExtResource("2_yqjtg")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="Player" unique_id=823407697]
shape = SubResource("CircleShape2D_lnu2h")
debug_color = Color(0.89751947, 0.22656804, 0.5594576, 0.41960785)
[node name="StaticBody2D2" type="StaticBody2D" parent="." unique_id=531572601]
position = Vector2(363, 399) position = Vector2(363, 399)
scale = Vector2(1.4, 2.2) scale = Vector2(1.4, 2.2)
metadata/_edit_group_ = true metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="StaticBody2D2" unique_id=237256227] [node name="CollisionShape2D" type="CollisionShape2D" parent="Level/StaticBody2D2" unique_id=237256227]
shape = SubResource("WorldBoundaryShape2D_iywne") shape = SubResource("WorldBoundaryShape2D_iywne")
[node name="Crate3" parent="." unique_id=161286893 instance=ExtResource("2_lbhrr")] [node name="Bullet" parent="." unique_id=1567847193 instance=ExtResource("6_p57ef")]
position = Vector2(354, 114) position = Vector2(256, 130)
[connection signal="AreaTrigger" from="Trigger" to="." method="_on_trigger"]
[connection signal="AreaTrigger" from="Trigger2" to="." method="_on_trigger"]

20
scenes/player.tscn Normal file
View File

@ -0,0 +1,20 @@
[gd_scene format=3 uid="uid://s0utas3jmhjk"]
[ext_resource type="Script" uid="uid://c17de7vvtri1e" path="res://scripts/player.gd" id="1_3vyb7"]
[sub_resource type="CircleShape2D" id="CircleShape2D_lnu2h"]
[node name="Player" type="CharacterBody2D" unique_id=1109630338]
scale = Vector2(2, 2)
script = ExtResource("1_3vyb7")
metadata/_edit_group_ = true
[node name="CollisionShape2D" type="CollisionShape2D" parent="." unique_id=1851921509]
shape = SubResource("CircleShape2D_lnu2h")
debug_color = Color(0.89751947, 0.22656804, 0.5594576, 0.41960785)
[node name="RightTarget" type="Marker2D" parent="." unique_id=1700751856]
position = Vector2(23.5, -5)
[node name="LeftTarget" type="Marker2D" parent="." unique_id=2004390104]
position = Vector2(-23.5, -11)

View File

@ -13,4 +13,5 @@ metadata/_edit_group_ = true
shape = SubResource("CircleShape2D_8cj0n") shape = SubResource("CircleShape2D_8cj0n")
debug_color = Color(0.3766567, 0.61053944, 0.19980416, 0.41960785) debug_color = Color(0.3766567, 0.61053944, 0.19980416, 0.41960785)
[connection signal="body_entered" from="." to="." method="_on_body_entered"]
[connection signal="body_exited" from="." to="." method="_on_body_exited"] [connection signal="body_exited" from="." to="." method="_on_body_exited"]

11
scripts/bullet.gd Normal file
View File

@ -0,0 +1,11 @@
class_name Bullet extends Area2D
var speed:float=700
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
position +=transform.x*speed *delta

1
scripts/bullet.gd.uid Normal file
View File

@ -0,0 +1 @@
uid://bfuhpow7x2xr1

View File

@ -1 +1,13 @@
class_name Crate extends RigidBody2D 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

View File

@ -1,9 +1,28 @@
extends Node2D class_name GameController extends Node2D
var crateTotal=0
signal destroySignal(body)
signal teleportSignal(body)
signal levelChangeSignal(level)
var currentScene:String="res://scenes/game.tscn"
var timer := Timer.new()
var timeAvailable := 10
var crateTotal=3
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready() -> void: func _ready() -> void:
pass # Replace with function body. add_child(timer)
timer.wait_time =1
timer.one_shot=false
timer.connect("timeout", secondCounter)
timer.start()
func secondCounter()-> void:
timeAvailable -=1
if timeAvailable <=0:
print("YOU LOSE!!")
levelChangeSignal.emit(currentScene)
# Called every frame. 'delta' is the elapsed time since the previous frame. # Called every frame. 'delta' is the elapsed time since the previous frame.
@ -11,7 +30,7 @@ func _process(delta: float) -> void:
pass pass
func _on_trigger(body: Variant,effect) -> void: func _on_trigger(body: Variant,effect,trigger) -> void:
print("GC knows trigger..."+effect) print("GC knows trigger..."+effect)
if body is Crate: if body is Crate:
match effect: match effect:
@ -19,6 +38,25 @@ func _on_trigger(body: Variant,effect) -> void:
crateTotal -=1 crateTotal -=1
if crateTotal <=0: if crateTotal <=0:
print("you win") print("you win")
body.queue_free() levelChangeSignal.emit(currentScene)
destroySignal.emit(body)
"teleport":
print("GC teleport an object")
teleportSignal.emit(body)
"powerup":
print("GC knows powerup")
timeAvailable +=5
if body is Player:
match effect:
"powerup":
print("GC knows powerup")
timeAvailable +=5
destroySignal.emit(trigger)
func crateUpdate(cratesAmount) ->void:
crateTotal=cratesAmount
print("GC updated crates: " +str(crateTotal))

View File

@ -1,9 +1,14 @@
extends CharacterBody2D class_name Player extends CharacterBody2D
@onready var right_target: Marker2D = $RightTarget
@onready var left_target: Marker2D = $LeftTarget
const SPEED = 300.0 const SPEED = 300.0
const JUMP_VELOCITY = -400.0 const JUMP_VELOCITY = -400.0
const BUMP_POWER=50 const BUMP_POWER=50
enum FaceDirection{LEFT, RIGHT}
var facing:FaceDirection =FaceDirection.RIGHT
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
@ -11,6 +16,18 @@ func _physics_process(delta: float) -> void:
if not is_on_floor(): if not is_on_floor():
velocity += get_gravity() * delta velocity += get_gravity() * delta
# handle shoot
if Input.is_action_just_pressed("shoot"):
print("Player1 wants to shoot")
match facing:
FaceDirection.RIGHT:
print("shoot to the right")
%SceneManager.makebullet(right_target.global_transform,700)
FaceDirection.LEFT:
print("shoot to the left")
%SceneManager.makebullet(left_target.global_transform,-700)
# Handle jump. # Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor(): if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY velocity.y = JUMP_VELOCITY
@ -20,6 +37,10 @@ func _physics_process(delta: float) -> void:
var direction := Input.get_axis("ui_left", "ui_right") var direction := Input.get_axis("ui_left", "ui_right")
if direction: if direction:
velocity.x = direction * SPEED velocity.x = direction * SPEED
if direction <0:
facing =FaceDirection.LEFT
if direction >0:
facing =FaceDirection.RIGHT
else: else:
velocity.x = move_toward(velocity.x, 0, SPEED) velocity.x = move_toward(velocity.x, 0, SPEED)

66
scripts/scene_manager.gd Normal file
View File

@ -0,0 +1,66 @@
class_name SceneManager extends Node2D
@onready var triggers: Node2D = $"../Triggers"
@onready var level: Node2D = $"../Level"
@onready var crates: Node2D = $"../Crates"
@onready var game: GameController = $".."
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
buildLevel()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func buildLevel() -> void:
print("building level")
updatesCrates()
updateTriggers()
#wire up signals from gamecontrollerd
game.destroySignal.connect(destroy)
game.teleportSignal.connect(teleport)
game.levelChangeSignal.connect(loadLevel)
func updateTriggers()->void:
if triggers:
for obj in triggers.get_children():
if obj is Trigger:
if not obj.AreaTrigger.is_connected(game._on_trigger):
obj.AreaTrigger.connect(game._on_trigger)
func updatesCrates() -> void:
#check that there is a crates node
if crates:
var totalCrates=0
for obj in crates.get_children():
if obj is Crate:
if not obj.tree_exited.is_connected(updatesCrates):
obj.tree_exited.connect(updatesCrates)
totalCrates+=1
print("Total crates: "+str(totalCrates))
game.crateUpdate(totalCrates)
func destroy(body)->void:
body.queue_free()
func teleport(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 loadLevel(level:String) -> void:
get_tree().call_deferred("change_scene_to_file",level)
func makeBullet(targetPosition,speed) -> void:
print("make a bullet")

View File

@ -0,0 +1 @@
uid://qa1q5a2swlfc

View File

@ -1,7 +1,7 @@
extends Area2D class_name Trigger extends Area2D
signal AreaTrigger(body,effect) signal AreaTrigger(body,effect,trigger)
@export var effect="destory" @export var effect="destroy"
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready() -> void: func _ready() -> void:
pass # Replace with function body. pass # Replace with function body.
@ -12,6 +12,7 @@ func _process(delta: float) -> void:
pass pass
func _on_body_exited(body: Node2D) -> void: func _on_body_entered(body: Node2D) -> void:
print("Trigger activated") print("Trigger activated")
AreaTrigger.emit(body,effect) AreaTrigger.emit(body,effect,self)