Week 2 Learnings: Week 1 recap (bodies, area trigger), raycasts, dynamic instantiation (spawning things)

This commit is contained in:
CatherineHoang committed 2026-07-06 16:06:59 -04:00
1 parent 91dc632faf
commit 68a092aa68
8 files changed
+114 -47

No files matched your search

+11 -10
View File
@@ -1,13 +1,14 @@
class_name Bullet extends Area2D
var speed:float = 700
signal bulletHitSignal(body, bullet)
func setSpeed(value)->void:
speed = value
func _process(delta: float) -> void:
extends Area2D
var speed := 700
signal hit(bullet, body)
func setSpeed(speedValue):
speed = speedValue
func _physics_process(delta: float) -> void:
# delta - time passed between physics process being called, think FPS
position += transform.x * speed * delta
func _on_body_entered(body: Node2D) -> void:
if body is Crate:
bulletHitSignal.emit(body,self)
print("I HIT SMTHING")
+1 -1
View File
@@ -1 +1 @@
uid://u1ubij2eih4r
uid://lt4ob78t6q2a
+1 -2
View File
@@ -25,7 +25,6 @@ func countdown() -> void:
func _process(delta: float) -> void:
pass
func _on_trigger(body: Variant, trigger: Variant) -> void:
print("Game controller knows trigger fired")
body.queue_free()
@@ -33,7 +32,7 @@ func _on_trigger(body: Variant, trigger: Variant) -> void:
if numberOfCrates == 0:
print("WINNER!")
get_tree().reload_current_scene()
func crateTotal(howMany):
numberOfCrates = howMany
print("GC knows crates: "+str(numberOfCrates))
+60
View File
@@ -3,7 +3,21 @@ extends CharacterBody2D
const SPEED = 300.0
const JUMP_VELOCITY = -400.0
const PUSH_FORCE = 700
var faceLeft = false
# Can I push right / left
var pushLeftEnabled = false
var pushRightEnabled = false
@onready var right_ray: RayCast2D = $RightRay
@onready var left_ray: RayCast2D = $LeftRay
@onready var marker_right: Node2D = $MarkerRight
@onready var marker_left: Node2D = $MarkerLeft
var pushTarget
var bullet = preload("res://Scenes/bullet.tscn")
func _physics_process(delta: float) -> void:
# Add the gravity.
@@ -14,9 +28,35 @@ func _physics_process(delta: float) -> void:
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
if Input.is_action_just_pressed("Shove") && pushLeftEnabled && faceLeft:
print("shove box on the left")
pushTarget.apply_central_impulse(Vector2(-1,0) * PUSH_FORCE * 1.5)
pushLeftEnabled = false
if Input.is_action_just_pressed("Shove") && pushRightEnabled && not faceLeft:
print("shove box on the right")
pushTarget.apply_central_impulse(Vector2(1,0) * PUSH_FORCE * 1.5)
pushRightEnabled = false
if Input.is_action_just_pressed("Shoot"):
print("will shoot")
var myBullet = bullet.instantiate()
if faceLeft:
print("shoot left")
myBullet.setSpeed(-700)
myBullet.transform = marker_left.global_transform
else:
print("shoot right")
myBullet.transform = marker_right.global_transform
owner.add_child(myBullet)
# 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 <0:
faceLeft = true
if direction >0:
faceLeft = false
if direction:
velocity.x = direction * SPEED
else:
@@ -30,3 +70,23 @@ func _physics_process(delta: float) -> void:
#is this bonkable? reverse normal
if c.get_collider() is RigidBody2D:
c.get_collider().apply_central_impulse(-c.get_normal() * 100)
if left_ray.is_colliding():
print("left ray collision")
var collider = left_ray.get_collider()
if collider is Node:
if collider.is_in_group("pushables"):
pushLeftEnabled = true
pushTarget = collider
else:
pushLeftEnabled = false
if right_ray.is_colliding():
print("right ray collision")
var collider = right_ray.get_collider()
if collider is Node:
if collider.is_in_group("pushables"):
pushRightEnabled = true
pushTarget = collider
else:
pushRightEnabled = false