adding a 3d version of lesson material

This commit is contained in:
OddlyTimbot committed 2025-06-30 14:24:48 -04:00
1 parent 4a8ebaf40d
commit 674677e617
13 files changed
+215 -32

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
extends Node
var bulletsFiredTotal := 0
var bulletsMadeTotal := 0
var bulletArray:Array = []
var bullet = preload("res://scenes/bullet.tscn")
@onready var gameController: Node3D = $".."
func _ready() -> void:
gameController.destroy_signal.connect(destroy)
#factory decides to issue new bullet, or
#recycle an old one
func bulletFactory():
var mybullet
if bulletArray.size() < 4:
mybullet = bullet.instantiate()
mybullet.connect("hit", gameController.onBulletHit)
owner.add_child(mybullet)
else:
mybullet = bulletArray.pop_back()
mybullet.setSpeed(700)
bulletArray.push_front(mybullet)
bulletsMadeTotal +=1
return mybullet
#order desk for new bullets
func makeBullet(position, speed):
var myBullet = bulletFactory()
myBullet.transform.origin = position
myBullet.setSpeed(speed)
return myBullet
func destroy(body):
if body is Crate:
body.queue_free()
if body is Bullet:
bulletArray.erase(body)
body.queue_free()
@@ -0,0 +1 @@
uid://cj0hc3gbvehgf
+22
View File
@@ -0,0 +1,22 @@
class_name Bullet extends Area3D
var speed = 2
signal hit(bullet, body)
func setSpeed(speedVal):
speed = speedVal
func _physics_process(delta):
#three approaches:
#1. update position with a vector3
#position += Vector3(speed * delta, 0, 0)
#2. update the transform
#global_transform.origin += transform.basis.x.normalized() * speed * delta
#3. calculate the vector of intended direction
var velocity_vector = transform.basis.x * speed
position += velocity_vector * delta
func _on_body_entered(body: Node3D) -> void:
print("bullet hit a thing")
hit.emit(self, body)
+1
View File
@@ -0,0 +1 @@
uid://b30inwwjypbrl
+48 -1
View File
@@ -3,7 +3,17 @@ extends CharacterBody3D
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
const SHOVE_POWER = 9
@onready var right_target: Node3D = $RightTarget
@onready var left_target: Node3D = $LeftTarget
@onready var right_cast: RayCast3D = $RightCast
@onready var left_cast: RayCast3D = $LeftCast
enum FaceDirection{LEFT, RIGHT}
var facing:FaceDirection = FaceDirection.RIGHT
var pushTarget:RigidBody3D
func _physics_process(delta: float) -> void:
# Add the gravity.
@@ -20,13 +30,50 @@ func _physics_process(delta: float) -> void:
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
if direction.x>0:
facing = FaceDirection.RIGHT
if direction.x<0:
facing = FaceDirection.LEFT
#velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
#velocity.z = move_toward(velocity.z, 0, SPEED)
if Input.is_action_just_pressed("shoot"):
match facing:
FaceDirection.RIGHT:
%SceneManager.makeBullet(right_target.global_position, 19)
FaceDirection.LEFT:
%SceneManager.makeBullet(left_target.global_position, -4)
if Input.is_action_just_pressed("shove"):
print("try to shove")
if pushTarget != null:
print("shove a crate")
var angle
if facing==FaceDirection.LEFT:
angle = -1
else:
angle = 1
pushTarget.apply_central_impulse(Vector3(angle,0,0) * SHOVE_POWER)
move_and_slide()
for i in get_slide_collision_count():
var collision = get_slide_collision(i)
if collision.get_collider() is RigidBody3D:
collision.get_collider().apply_central_impulse(-collision.get_normal() * 2)
match facing:
FaceDirection.LEFT:
if left_cast.is_colliding():
var collider = left_cast.get_collider()
if collider is Crate:
pushTarget = collider
else:
pushTarget = null
FaceDirection.RIGHT:
if right_cast.is_colliding():
var collider = right_cast.get_collider()
if collider is Crate:
pushTarget = collider
else:
pushTarget = null
+1
View File
@@ -0,0 +1 @@
class_name Crate extends RigidBody3D
+1
View File
@@ -0,0 +1 @@
uid://bspjn3hydupee
+8 -9
View File
@@ -1,14 +1,6 @@
extends Node3D
# 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:
pass
signal destroy_signal(body)
func onAreaEntered(effect, obj) -> void:
if effect == "destroy":
@@ -16,3 +8,10 @@ func onAreaEntered(effect, obj) -> void:
obj.queue_free()
elif effect == "powerUp":
print("power up the object")
func onBulletHit(bullet, body):
print("GC knows bullet hit")
if body is Crate:
print("hit a crate")
destroy_signal.emit(body)
destroy_signal.emit(bullet)