adding a 3d version of lesson material
This commit is contained in:
parent
4a8ebaf40d
commit
674677e617
@ -14,3 +14,16 @@ config/name="3dbasic"
|
||||
run/main_scene="res://scenes/game.tscn"
|
||||
config/features=PackedStringArray("4.4", "Forward Plus")
|
||||
config/icon="res://icon.svg"
|
||||
|
||||
[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":88,"key_label":0,"unicode":120,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
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":90,"key_label":0,"unicode":122,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
|
21
week1/3dVersion/scenes/bullet.tscn
Normal file
21
week1/3dVersion/scenes/bullet.tscn
Normal file
@ -0,0 +1,21 @@
|
||||
[gd_scene load_steps=4 format=3 uid="uid://c43nh4oilmrsg"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://b30inwwjypbrl" path="res://scripts/bullet.gd" id="1_mkf8s"]
|
||||
|
||||
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_y25gk"]
|
||||
|
||||
[sub_resource type="CapsuleMesh" id="CapsuleMesh_mkf8s"]
|
||||
|
||||
[node name="Bullet" type="Area3D"]
|
||||
transform = Transform3D(0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0, 0, 0)
|
||||
script = ExtResource("1_mkf8s")
|
||||
|
||||
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
|
||||
transform = Transform3D(0.0105416, 0.999944, 0, -0.999944, 0.0105416, 0, 0, 0, 1, 0, 0, 0)
|
||||
shape = SubResource("CapsuleShape3D_y25gk")
|
||||
|
||||
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
|
||||
transform = Transform3D(0.00249584, 0.999997, 0, -0.999997, 0.00249584, 0, 0, 0, 1, 0, 0, 0)
|
||||
mesh = SubResource("CapsuleMesh_mkf8s")
|
||||
|
||||
[connection signal="body_entered" from="." to="." method="_on_body_entered"]
|
@ -1,11 +1,14 @@
|
||||
[gd_scene load_steps=3 format=3 uid="uid://50e6ihpajc71"]
|
||||
[gd_scene load_steps=4 format=3 uid="uid://50e6ihpajc71"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://bspjn3hydupee" path="res://scripts/crate.gd" id="1_b66cd"]
|
||||
|
||||
[sub_resource type="BoxShape3D" id="BoxShape3D_uwrxv"]
|
||||
|
||||
[sub_resource type="BoxMesh" id="BoxMesh_yqjtg"]
|
||||
|
||||
[node name="RigidBody3D" type="RigidBody3D"]
|
||||
[node name="Crate" type="RigidBody3D"]
|
||||
transform = Transform3D(0.809222, 0.587503, 0, -0.587503, 0.809222, 0, 0, 0, 1, 0, 0, 0)
|
||||
script = ExtResource("1_b66cd")
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
|
||||
|
@ -1,9 +1,10 @@
|
||||
[gd_scene load_steps=12 format=3 uid="uid://d35inb2m6x8t2"]
|
||||
[gd_scene load_steps=11 format=3 uid="uid://d35inb2m6x8t2"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://0p7ncwmk6d86" path="res://scripts/gamecontroller.gd" id="1_lbhrr"]
|
||||
[ext_resource type="PackedScene" uid="uid://50e6ihpajc71" path="res://scenes/crate.tscn" id="1_lnu2h"]
|
||||
[ext_resource type="Script" uid="uid://bqp5ks7j080sc" path="res://scripts/character_body_3d.gd" id="2_yqjtg"]
|
||||
[ext_resource type="Script" uid="uid://cj0hc3gbvehgf" path="res://scripts/SceneManager.gd" id="2_p57ef"]
|
||||
[ext_resource type="PackedScene" uid="uid://dg7tfjcefup1a" path="res://scenes/trigger.tscn" id="3_iywne"]
|
||||
[ext_resource type="PackedScene" uid="uid://ctsjljm7dv6us" path="res://scenes/player.tscn" id="4_iywne"]
|
||||
|
||||
[sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_8cj0n"]
|
||||
sky_horizon_color = Color(0.662243, 0.671743, 0.686743, 1)
|
||||
@ -24,13 +25,13 @@ size = Vector3(6, 1, 1)
|
||||
[sub_resource type="BoxMesh" id="BoxMesh_8cj0n"]
|
||||
size = Vector3(6, 1, 1)
|
||||
|
||||
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_lnu2h"]
|
||||
|
||||
[sub_resource type="CapsuleMesh" id="CapsuleMesh_lbhrr"]
|
||||
|
||||
[node name="Node3D" type="Node3D"]
|
||||
script = ExtResource("1_lbhrr")
|
||||
|
||||
[node name="SceneManager" type="Node" parent="."]
|
||||
unique_name_in_owner = true
|
||||
script = ExtResource("2_p57ef")
|
||||
|
||||
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
|
||||
environment = SubResource("Environment_yqjtg")
|
||||
|
||||
@ -48,27 +49,30 @@ debug_color = Color(0.892607, 0.253495, 0.511318, 0.42)
|
||||
[node name="MeshInstance3D" type="MeshInstance3D" parent="StaticBody3D"]
|
||||
mesh = SubResource("BoxMesh_8cj0n")
|
||||
|
||||
[node name="StaticBody3D2" type="StaticBody3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 7.22537, 1.49153, 0)
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
[node name="CollisionShape3D" type="CollisionShape3D" parent="StaticBody3D2"]
|
||||
shape = SubResource("BoxShape3D_8cj0n")
|
||||
debug_color = Color(0.892607, 0.253495, 0.511318, 0.42)
|
||||
|
||||
[node name="MeshInstance3D" type="MeshInstance3D" parent="StaticBody3D2"]
|
||||
mesh = SubResource("BoxMesh_8cj0n")
|
||||
|
||||
[node name="Camera3D" type="Camera3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 0.957662, 0.287895, 0, -0.287895, 0.957662, 0, 0.811349, 8.59259)
|
||||
|
||||
[node name="RigidBody3D" parent="." instance=ExtResource("1_lnu2h")]
|
||||
transform = Transform3D(0.809222, 0.587503, 0, -0.587503, 0.809222, 0, 0, 0, 1, -2.71974, 1.74283, 0.0672417)
|
||||
|
||||
[node name="RigidBody3D2" parent="." instance=ExtResource("1_lnu2h")]
|
||||
transform = Transform3D(0.809222, 0.587503, 0, -0.587503, 0.809222, 0, 0, 0, 1, -0.761535, 3.57451, 0.0672417)
|
||||
|
||||
[node name="Area3D" parent="." instance=ExtResource("3_iywne")]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2.49989, -1.52771, 0)
|
||||
|
||||
[node name="CharacterBody3D" type="CharacterBody3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.989455, 1.55096, 0)
|
||||
script = ExtResource("2_yqjtg")
|
||||
metadata/_edit_group_ = true
|
||||
[node name="player" parent="." instance=ExtResource("4_iywne")]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.221856, 1.55096, 0)
|
||||
|
||||
[node name="CollisionShape3D" type="CollisionShape3D" parent="CharacterBody3D"]
|
||||
shape = SubResource("CapsuleShape3D_lnu2h")
|
||||
[node name="Crate" parent="." instance=ExtResource("1_lnu2h")]
|
||||
transform = Transform3D(0.809222, 0.587503, 0, -0.587503, 0.809222, 0, 0, 0, 1, -1.88754, 2.3762, 0)
|
||||
|
||||
[node name="MeshInstance3D" type="MeshInstance3D" parent="CharacterBody3D"]
|
||||
mesh = SubResource("CapsuleMesh_lbhrr")
|
||||
[node name="Crate2" parent="." instance=ExtResource("1_lnu2h")]
|
||||
transform = Transform3D(0.809222, 0.587503, 0, -0.587503, 0.809222, 0, 0, 0, 1, 2.31779, 2.3762, 0)
|
||||
|
||||
[connection signal="onAreaEntered" from="Area3D" to="." method="onAreaEntered"]
|
||||
|
30
week1/3dVersion/scenes/player.tscn
Normal file
30
week1/3dVersion/scenes/player.tscn
Normal file
@ -0,0 +1,30 @@
|
||||
[gd_scene load_steps=4 format=3 uid="uid://ctsjljm7dv6us"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://bqp5ks7j080sc" path="res://scripts/character_body_3d.gd" id="1_3vyb7"]
|
||||
|
||||
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_lnu2h"]
|
||||
|
||||
[sub_resource type="CapsuleMesh" id="CapsuleMesh_lbhrr"]
|
||||
|
||||
[node name="CharacterBody3D" type="CharacterBody3D"]
|
||||
script = ExtResource("1_3vyb7")
|
||||
metadata/_edit_group_ = true
|
||||
|
||||
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
|
||||
visible = false
|
||||
shape = SubResource("CapsuleShape3D_lnu2h")
|
||||
|
||||
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
|
||||
mesh = SubResource("CapsuleMesh_lbhrr")
|
||||
|
||||
[node name="RightTarget" type="Node3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.809181, 0, 0)
|
||||
|
||||
[node name="LeftTarget" type="Node3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.813077, 0, 0)
|
||||
|
||||
[node name="RightCast" type="RayCast3D" parent="."]
|
||||
transform = Transform3D(0.00829023, -0.999966, 0, 0.999966, 0.00829023, 0, 0, 0, 1, 0, -0.312465, 0)
|
||||
|
||||
[node name="LeftCast" type="RayCast3D" parent="."]
|
||||
transform = Transform3D(-0.00111711, 0.999999, 0, -0.999999, -0.00111711, 0, 0, 0, 1, 0, -0.312465, 0)
|
40
week1/3dVersion/scripts/SceneManager.gd
Normal file
40
week1/3dVersion/scripts/SceneManager.gd
Normal 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()
|
1
week1/3dVersion/scripts/SceneManager.gd.uid
Normal file
1
week1/3dVersion/scripts/SceneManager.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://cj0hc3gbvehgf
|
22
week1/3dVersion/scripts/bullet.gd
Normal file
22
week1/3dVersion/scripts/bullet.gd
Normal 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
week1/3dVersion/scripts/bullet.gd.uid
Normal file
1
week1/3dVersion/scripts/bullet.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://b30inwwjypbrl
|
@ -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
week1/3dVersion/scripts/crate.gd
Normal file
1
week1/3dVersion/scripts/crate.gd
Normal file
@ -0,0 +1 @@
|
||||
class_name Crate extends RigidBody3D
|
1
week1/3dVersion/scripts/crate.gd.uid
Normal file
1
week1/3dVersion/scripts/crate.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://bspjn3hydupee
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user