Revert to last week + recap: Static, Rigid, Character Bodies and Area Triggers.

This commit is contained in:
CatherineHoang committed 2026-07-06 14:27:17 -04:00
1 parent 616afe6dbb
commit 91dc632faf
7 files changed
+40 -145

No files matched your search

-12
View File
@@ -1,13 +1 @@
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
+8 -14
View File
@@ -1,13 +1,11 @@
class_name GameController extends Node2D
signal destroySignal(body)
extends Node2D
var numberOfCrates = 2
var timer = Timer.new()
var timeAvailable = 50
var timeAvailable = 5
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
get_window().grab_focus()
add_child(timer)
# time based on seconds
timer.wait_time = 1
@@ -28,18 +26,14 @@ func _process(delta: float) -> void:
pass
func _on_trigger(body: Variant, trigger: Variant, intent:String) -> void:
func _on_trigger(body: Variant, trigger: Variant) -> void:
print("Game controller knows trigger fired")
match intent:
"destroy":
destroySignal.emit(body)
body.queue_free()
numberOfCrates -= 1
if numberOfCrates == 0:
print("WINNER!")
get_tree().reload_current_scene()
func crateTotal(howMany):
numberOfCrates = howMany
print("GC knows crates: "+str(numberOfCrates))
if numberOfCrates == 0:
print("YOU WON!!!")
get_tree().reload_current_scene()
func bulletDamage(body, _bullet)->void:
if body.is_in_group("shootable"):
destroySignal.emit(body)
+2 -56
View File
@@ -1,21 +1,9 @@
class_name Player extends CharacterBody2D
signal pushSignal(body, direction)
signal shootSignal(markerPosition, speed)
extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
@onready var right_cast: RayCast2D = $RightCast
@onready var left_cast: RayCast2D = $LeftCast
@onready var right_spawn: Marker2D = $RightSpawn
@onready var left_spawn: Marker2D = $LeftSpawn
var pushRightEnabled = false
var pushLeftEnabled = false
var pushTarget
var pushTimer = 0
enum FaceDirection {RIGHT,LEFT}
var facing:FaceDirection = FaceDirection.RIGHT
func _physics_process(delta: float) -> void:
# Add the gravity.
@@ -25,59 +13,17 @@ func _physics_process(delta: float) -> void:
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
#Handle shove target
if Input.is_action_just_pressed("shove"):
print("I want to shove")
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:
#deliver the boom
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 Bullet
if Input.is_action_just_pressed("shoot"):
print("Ima shoot a bullet")
#marker postion, speed
match facing:
FaceDirection.RIGHT:
shootSignal.emit(right_spawn.global_transform, 800)
FaceDirection.LEFT:
shootSignal.emit(left_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()
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
#a loop
for i in get_slide_collision_count():
var c = get_slide_collision(i)
+4 -42
View File
@@ -1,20 +1,10 @@
extends Node2D
# hold control and drag + drop
@onready var game:GameController = $".."
@onready var game: Node2D = $".."
@onready var crates: Node2D = $"../Crates"
@onready var player: Player = $"../Player"
var bullet = preload("res://Scenes/bullet.tscn")
var bulletArray:Array[Bullet] = []
var totalAllowedBullets:int = 7
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
player.pushSignal.connect(pushTarget)
player.shootSignal.connect(makeBullet)
#signals from the GameController
game.destroySignal.connect(destroy)
buildLevel()
# Called every frame. 'delta' is the elapsed time since the previous frame.
@@ -22,39 +12,11 @@ func _process(delta: float) -> void:
pass
func buildLevel() -> void:
#tell GC how many crates...
updateCrates()
func updateCrates()->void:
# Tell GC how many crates
var totalCrates = 0
# check if any crates in scene
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
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 bulletFactory()->Bullet:
var myBullet:Bullet
#how many bullets have been produced?
if bulletArray.size() <= totalAllowedBullets:
#make a new bullet
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)
+3 -4
View File
@@ -1,6 +1,5 @@
class_name Trigger extends Area2D
signal areaTriggerSignal(body, trigger, intent)
@export var intent = "destroy"
extends Area2D
signal areaTriggerSignal(body, trigger)
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
@@ -14,4 +13,4 @@ func _process(delta: float) -> void:
func _on_body_entered(body: Node2D) -> void:
print("YEAHHHHH TRIGGERED")
areaTriggerSignal.emit(body, self, intent)
areaTriggerSignal.emit(body, self)