clean up file structure

This commit is contained in:
Michael Campbell 2026-02-18 15:30:41 -05:00
parent 1318fdd9fc
commit 324e7d437b
28 changed files with 102 additions and 125 deletions

135
player/player.gd Normal file
View file

@ -0,0 +1,135 @@
class_name Player
extends CharacterBody3D
enum State {
NORMAL,
DASHING,
LOCKED,
}
const MOVE_SPEED := 5.
const DASH_SPEED := 25.
static var instance: Player
@export var shake_noise: FastNoiseLite
var shake_duration := 0.
@onready var camera_transform: Transform3D = %Camera3D.transform
var damage_clock := 0.
var aim_angle: float
var health := 3:
set(v):
health = v
if is_node_ready():
%HealthLabel.text = "Health: %d" % v
var state := State.NORMAL
var dash_direction: Vector2
var stamina := 1.0
func _init() -> void:
instance = self
func _ready() -> void:
health = health
func _process_stamina(delta: float) -> void:
stamina = move_toward(stamina, 1., delta * 0.2)
%StaminaBar.value = stamina
func _process_movement() -> void:
var input = Input.get_vector("move_left", "move_right", "move_up", "move_down")
input.normalized()
velocity = Vector3(input.x, 0., input.y) * MOVE_SPEED
move_and_slide()
if Input.is_action_just_pressed("dash") and stamina >= 1.:
stamina = 0.
dash_direction = input
state = State.DASHING
await get_tree().create_timer(.25, false).timeout
state = State.LOCKED
await get_tree().create_timer(.5, false).timeout
state = State.NORMAL
func _process_dash() -> void:
velocity = Vector3(dash_direction.x, 0., dash_direction.y) * DASH_SPEED
move_and_slide()
func _process_aim() -> void:
var viewport_mouse_pos := get_viewport().get_mouse_position()
var r_origin: Vector3 = %Camera3D.project_ray_origin(viewport_mouse_pos)
var r_dir: Vector3 = %Camera3D.project_ray_normal(viewport_mouse_pos)
# y = mx + b
# 0 = mx + b
# -b / m = x
var t := -r_origin.y / r_dir.y
var world_mouse_pos = r_origin + r_dir * t
var to_mouse_pos = world_mouse_pos - global_position
var angle = Vector2(to_mouse_pos.x, -to_mouse_pos.z).angle()
%Cannon.rotation.y = angle
aim_angle = angle
%Reticle.position = to_mouse_pos
var fire_clock := 0.
func _process_shoot(delta: float) -> void:
var clock_mul := 1.
match state:
State.DASHING: clock_mul = 2.5
State.LOCKED: clock_mul = 2.5
fire_clock -= delta * clock_mul
if Input.is_action_pressed("fire") and fire_clock <= 0.:
var dir := Vector3.RIGHT.rotated(Vector3.UP, aim_angle)
var player_projectile: PlayerProjectile = preload("player_projectile.tscn").instantiate()
player_projectile.init(dir)
player_projectile.global_position = global_position + Vector3.UP * 0.5
add_sibling(player_projectile)
fire_clock = 60. / 125.
func _process_cam_shake(delta: float) -> void:
shake_duration -= delta
%Camera3D.transform = camera_transform
if shake_duration <= 0:
return
var x := shake_noise.get_noise_1d(shake_duration * 10000)
var y := shake_noise.get_noise_1d(-shake_duration * 10000)
var v := \
camera_transform.basis.x * x + \
camera_transform.basis.y * y
print("v = ", v)
%Camera3D.position += v * .5
func _process(delta: float) -> void:
match state:
State.NORMAL: _process_movement()
State.DASHING: _process_dash()
State.LOCKED: pass
_process_aim()
_process_shoot(delta)
_process_cam_shake(delta)
_process_stamina(delta)
damage_clock -= delta
func damage(damager: Node3D) -> void:
if state == State.DASHING:
damager.queue_free()
else:
if damage_clock <= 0.:
health -= 1
damage_clock = 3
shake_duration = .25
if health == 0:
get_tree().reload_current_scene()

1
player/player.gd.uid Normal file
View file

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

65
player/player.tscn Normal file
View file

@ -0,0 +1,65 @@
[gd_scene format=3 uid="uid://cv5p3w87nk0wn"]
[ext_resource type="Script" uid="uid://dhin7ux2njfja" path="res://player/player.gd" id="1_4flbx"]
[ext_resource type="Script" uid="uid://cjdxibg3pnans" path="res://player/score_label.gd" id="2_onrkg"]
[sub_resource type="FastNoiseLite" id="FastNoiseLite_onrkg"]
[sub_resource type="BoxMesh" id="BoxMesh_onrkg"]
[sub_resource type="BoxShape3D" id="BoxShape3D_i3pqv"]
[sub_resource type="BoxMesh" id="BoxMesh_hqtel"]
size = Vector3(2, 0.1, 0.1)
[sub_resource type="SphereMesh" id="SphereMesh_4flbx"]
[node name="Player" type="CharacterBody3D" unique_id=1904432250]
collision_layer = 2
collision_mask = 13
script = ExtResource("1_4flbx")
shake_noise = SubResource("FastNoiseLite_onrkg")
[node name="MeshInstance3D" type="MeshInstance3D" parent="." unique_id=204505475]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, 0)
mesh = SubResource("BoxMesh_onrkg")
[node name="CollisionShape3D" type="CollisionShape3D" parent="." unique_id=918462062]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.5, 0)
shape = SubResource("BoxShape3D_i3pqv")
[node name="Camera3D" type="Camera3D" parent="." unique_id=253280781]
unique_name_in_owner = true
transform = Transform3D(1, 0, 0, 0, 0.53349644, 0.84580237, 0, -0.84580237, 0.53349644, 0, 20.150661, 11.874705)
[node name="Cannon" type="Node3D" parent="." unique_id=985210791]
unique_name_in_owner = true
[node name="MeshInstance3D" type="MeshInstance3D" parent="Cannon" unique_id=1149215568]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0.75, 0)
mesh = SubResource("BoxMesh_hqtel")
[node name="Reticle" type="MeshInstance3D" parent="." unique_id=242722222]
unique_name_in_owner = true
visible = false
mesh = SubResource("SphereMesh_4flbx")
[node name="CanvasLayer" type="CanvasLayer" parent="." unique_id=1676328708]
[node name="ScoreLabel" type="Label" parent="CanvasLayer" unique_id=1108751125]
offset_right = 40.0
offset_bottom = 23.0
theme_override_font_sizes/font_size = 48
text = "Score: %s"
script = ExtResource("2_onrkg")
[node name="HealthLabel" type="Label" parent="CanvasLayer" unique_id=1653322479]
unique_name_in_owner = true
anchors_preset = 10
anchor_right = 1.0
offset_bottom = 67.0
grow_horizontal = 2
size_flags_horizontal = 10
theme_override_font_sizes/font_size = 48
text = "Health: 2"
horizontal_alignment = 2

View file

@ -0,0 +1,28 @@
class_name PlayerProjectile
extends Area3D
const SPEED := 20.
var _initialized := false
var velocity: Vector3
var health: int = 1:
set(v):
health = v
if v == 0:
queue_free()
func _ready() -> void:
body_entered.connect(func(body: Node3D):
if body.has_method("hit"):
body.hit(self)
health -= 1
)
func init(dir: Vector3) -> void:
_initialized = true
velocity = dir * SPEED
func _physics_process(delta: float) -> void:
position += velocity * delta
func _process(_delta: float) -> void:
assert(_initialized)

View file

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

View file

@ -0,0 +1,24 @@
[gd_scene format=3 uid="uid://beruy087nlwfu"]
[ext_resource type="Script" uid="uid://c1owlhp4be7wi" path="res://player/projectile/player_projectile.gd" id="1_0gc0p"]
[ext_resource type="PackedScene" uid="uid://yri7p15m8l1u" path="res://utils/lifetime/lifetime.tscn" id="2_73r22"]
[sub_resource type="SphereMesh" id="SphereMesh_wqvto"]
radius = 0.25
height = 0.5
[sub_resource type="SphereShape3D" id="SphereShape3D_0gc0p"]
radius = 0.25
[node name="PlayerProjectile" type="Area3D" unique_id=966789634]
collision_layer = 0
collision_mask = 12
script = ExtResource("1_0gc0p")
[node name="MeshInstance3D" type="MeshInstance3D" parent="." unique_id=2116862847]
mesh = SubResource("SphereMesh_wqvto")
[node name="CollisionShape3D" type="CollisionShape3D" parent="." unique_id=908451103]
shape = SubResource("SphereShape3D_0gc0p")
[node name="Lifetime" parent="." unique_id=631221358 instance=ExtResource("2_73r22")]

12
player/score_label.gd Normal file
View file

@ -0,0 +1,12 @@
extends Label
var score := 0:
set(v):
score = v
text = "Score: %d" % v
func _ready() -> void:
score = score
SignalBus.building_destroyed.connect(func(_building):
score += 10
)

View file

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