From e3b2b02bd0331fe2cb84c248415901ee579f1768 Mon Sep 17 00:00:00 2001
From: Nekojimi <Jim@nekojimi.moe>
Date: Thu, 1 May 2025 17:47:42 +0100
Subject: [PATCH] Add initial version of citizen object.

---
 assets/images/lilguy.png.import      | 13 +++----
 assets/images/lilguy_arms.png.import | 13 +++----
 objects/units/citizen.gd             | 24 +++++++++++++
 objects/units/citizen.gd.uid         |  1 +
 objects/units/citizen.tscn           | 52 ++++++++++++++++++++++++++++
 objects/units/unit.tscn              |  2 +-
 scripts/unit.gd                      | 13 ++++++-
 7 files changed, 104 insertions(+), 14 deletions(-)
 create mode 100644 objects/units/citizen.gd
 create mode 100644 objects/units/citizen.gd.uid
 create mode 100644 objects/units/citizen.tscn

diff --git a/assets/images/lilguy.png.import b/assets/images/lilguy.png.import
index fc884a1..d762dbd 100644
--- a/assets/images/lilguy.png.import
+++ b/assets/images/lilguy.png.import
@@ -3,25 +3,26 @@
 importer="texture"
 type="CompressedTexture2D"
 uid="uid://brjswv5ryy8om"
-path="res://.godot/imported/lilguy.png-051ea7725e05924272fe99d4c4cd30d6.ctex"
+path.s3tc="res://.godot/imported/lilguy.png-051ea7725e05924272fe99d4c4cd30d6.s3tc.ctex"
 metadata={
-"vram_texture": false
+"imported_formats": ["s3tc_bptc"],
+"vram_texture": true
 }
 
 [deps]
 
 source_file="res://assets/images/lilguy.png"
-dest_files=["res://.godot/imported/lilguy.png-051ea7725e05924272fe99d4c4cd30d6.ctex"]
+dest_files=["res://.godot/imported/lilguy.png-051ea7725e05924272fe99d4c4cd30d6.s3tc.ctex"]
 
 [params]
 
-compress/mode=0
+compress/mode=2
 compress/high_quality=false
 compress/lossy_quality=0.7
 compress/hdr_compression=1
 compress/normal_map=0
 compress/channel_pack=0
-mipmaps/generate=false
+mipmaps/generate=true
 mipmaps/limit=-1
 roughness/mode=0
 roughness/src_normal=""
@@ -31,4 +32,4 @@ process/normal_map_invert_y=false
 process/hdr_as_srgb=false
 process/hdr_clamp_exposure=false
 process/size_limit=0
-detect_3d/compress_to=1
+detect_3d/compress_to=0
diff --git a/assets/images/lilguy_arms.png.import b/assets/images/lilguy_arms.png.import
index 8b0b7bd..feb6f17 100644
--- a/assets/images/lilguy_arms.png.import
+++ b/assets/images/lilguy_arms.png.import
@@ -3,25 +3,26 @@
 importer="texture"
 type="CompressedTexture2D"
 uid="uid://cpov32m0nxjvh"
-path="res://.godot/imported/lilguy_arms.png-579679cf4842d72a1b6c8bc34db4fa7f.ctex"
+path.s3tc="res://.godot/imported/lilguy_arms.png-579679cf4842d72a1b6c8bc34db4fa7f.s3tc.ctex"
 metadata={
-"vram_texture": false
+"imported_formats": ["s3tc_bptc"],
+"vram_texture": true
 }
 
 [deps]
 
 source_file="res://assets/images/lilguy_arms.png"
-dest_files=["res://.godot/imported/lilguy_arms.png-579679cf4842d72a1b6c8bc34db4fa7f.ctex"]
+dest_files=["res://.godot/imported/lilguy_arms.png-579679cf4842d72a1b6c8bc34db4fa7f.s3tc.ctex"]
 
 [params]
 
-compress/mode=0
+compress/mode=2
 compress/high_quality=false
 compress/lossy_quality=0.7
 compress/hdr_compression=1
 compress/normal_map=0
 compress/channel_pack=0
-mipmaps/generate=false
+mipmaps/generate=true
 mipmaps/limit=-1
 roughness/mode=0
 roughness/src_normal=""
@@ -31,4 +32,4 @@ process/normal_map_invert_y=false
 process/hdr_as_srgb=false
 process/hdr_clamp_exposure=false
 process/size_limit=0
-detect_3d/compress_to=1
+detect_3d/compress_to=0
diff --git a/objects/units/citizen.gd b/objects/units/citizen.gd
new file mode 100644
index 0000000..5c85937
--- /dev/null
+++ b/objects/units/citizen.gd
@@ -0,0 +1,24 @@
+@tool
+extends Unit
+class_name Citizen
+
+@onready var body: Sprite3D = $Body
+@onready var hands: Sprite3D = $Body/Hands
+
+func _process(delta: float) -> void:
+	super(delta)
+	var camera: Camera3D
+	if Engine.is_editor_hint():
+		camera = EditorInterface.get_editor_viewport_3d(0).get_camera_3d()
+	else:
+		camera = get_viewport().get_camera_3d()
+	var facing_direction: Vector3 = -global_basis.z
+	var camera_local_facing: Vector3 = facing_direction * camera.global_basis
+	
+	var facing_away: bool = camera_local_facing.z < 0
+	var facing_right: bool = camera_local_facing.x > 0
+	
+	body.frame = 1 if facing_away else 0
+	hands.frame = 1 if facing_away else 0
+	body.flip_h = facing_right
+	hands.flip_h = facing_right
diff --git a/objects/units/citizen.gd.uid b/objects/units/citizen.gd.uid
new file mode 100644
index 0000000..63547d9
--- /dev/null
+++ b/objects/units/citizen.gd.uid
@@ -0,0 +1 @@
+uid://b7xficxq807qd
diff --git a/objects/units/citizen.tscn b/objects/units/citizen.tscn
new file mode 100644
index 0000000..dad8249
--- /dev/null
+++ b/objects/units/citizen.tscn
@@ -0,0 +1,52 @@
+[gd_scene load_steps=8 format=3 uid="uid://skxli3htgn7"]
+
+[ext_resource type="PackedScene" uid="uid://1gcj3gixy6hs" path="res://objects/units/unit.tscn" id="1_6046h"]
+[ext_resource type="Script" uid="uid://b7xficxq807qd" path="res://objects/units/citizen.gd" id="2_dv62s"]
+[ext_resource type="Texture2D" uid="uid://brjswv5ryy8om" path="res://assets/images/lilguy.png" id="3_pedvu"]
+[ext_resource type="Texture2D" uid="uid://cpov32m0nxjvh" path="res://assets/images/lilguy_arms.png" id="4_i1unn"]
+[ext_resource type="ArrayMesh" uid="uid://gf0q5hs4wjva" path="res://assets/models/Ore.obj" id="5_dds8f"]
+
+[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_vpcy6"]
+albedo_texture = ExtResource("3_pedvu")
+billboard_mode = 2
+
+[sub_resource type="QuadMesh" id="QuadMesh_5xmhx"]
+material = SubResource("StandardMaterial3D_vpcy6")
+
+[node name="Citizen" instance=ExtResource("1_6046h")]
+collision_layer = 8
+collision_mask = 5
+script = ExtResource("2_dv62s")
+
+[node name="NavigationAgent3D" parent="." index="0"]
+target_position = Vector3(21.7818, 1, 12.7739)
+velocity = Vector3(0.811107, 0, -0.811107)
+
+[node name="MeshInstance3D" parent="." index="2"]
+visible = false
+mesh = SubResource("QuadMesh_5xmhx")
+
+[node name="Label3D" parent="." index="4"]
+text = "HP: 100"
+
+[node name="Body" type="Sprite3D" parent="." index="5"]
+transform = Transform3D(3, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0)
+billboard = 2
+shaded = true
+alpha_cut = 1
+texture_filter = 0
+texture = ExtResource("3_pedvu")
+hframes = 2
+
+[node name="Hands" type="Sprite3D" parent="Body" index="0"]
+transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.01)
+billboard = 2
+shaded = true
+alpha_cut = 1
+texture_filter = 0
+texture = ExtResource("4_i1unn")
+
+[node name="Held Item" type="MeshInstance3D" parent="Body/Hands" index="0"]
+transform = Transform3D(0.17, 0, 0, 0, 0.17, 0, 0, 0, 0.17, 5.96046e-08, -0.0580646, -0.0773078)
+mesh = ExtResource("5_dds8f")
+skeleton = NodePath("../../..")
diff --git a/objects/units/unit.tscn b/objects/units/unit.tscn
index f913d9c..e5ee26a 100644
--- a/objects/units/unit.tscn
+++ b/objects/units/unit.tscn
@@ -22,9 +22,9 @@ material = SubResource("StandardMaterial3D_jka67")
 
 [node name="Unit" type="RigidBody3D"]
 axis_lock_angular_x = true
-axis_lock_angular_y = true
 axis_lock_angular_z = true
 physics_material_override = SubResource("PhysicsMaterial_a0tk4")
+angular_damp = 20.0
 script = ExtResource("1_dberb")
 movement_force = 5.0
 max_speed = 5.0
diff --git a/scripts/unit.gd b/scripts/unit.gd
index c959777..cd9b569 100644
--- a/scripts/unit.gd
+++ b/scripts/unit.gd
@@ -1,9 +1,15 @@
 extends RigidBody3D
 class_name Unit
 
+@export_group("Defence")
 @export var max_hp: float = 100
+
+@export_group("Movement")
 @export var movement_force: float = 100
+@export var rotation_torque: float = 3
 @export var max_speed: float = 100
+
+@export_group("Navigation")
 @export var minimum_progress_rate: float = 1.0
 @export var stuck_time: float = 1.0
 
@@ -35,6 +41,7 @@ func _process(delta: float) -> void:
 	if nav_agent_3d.is_target_reached() \
 		or nav_agent_3d.target_position.is_zero_approx() \
 		or !nav_agent_3d.is_target_reachable():
+		#target_velocity = Vector3()
 		nav_agent_3d.target_position = move_target + Vector3(randfn(0, move_radius), 0, randfn(0, move_radius))
 		#nav_agent_3d.target_position = NavigationServer3D.map_get_random_point(NavigationServer3D.get_maps()[0], 1, true)
 		last_distance_to_target = nav_agent_3d.distance_to_target()
@@ -56,10 +63,11 @@ func _process(delta: float) -> void:
 		var next_point: Vector3 = nav_agent_3d.get_next_path_position()
 		#DebugDraw3D.draw_sphere(next_point, 0.1, Color.YELLOW)
 		var direction: Vector3 = (next_point - global_position).normalized()
+		#basis = Basis.looking_at(direction)
 		#DebugDraw3D.draw_line(global_position, global_position + linear_velocity, Color.BLUE)
 		target_velocity = direction * max_speed
 		nav_agent_3d.velocity = target_velocity
-		#DebugDraw3D.draw_line(global_position, global_position + target_velocity, Color.MAGENTA)
+		#DebugDraw3D.draw_line(global_position, global_positiaon + target_velocity, Color.MAGENTA)
 		#DebugDraw3D.draw_text(global_position + Vector3(0,1,0), "%f" % nav_agent_3d.distance_to_target())
 
 func unstuck() -> void:
@@ -90,3 +98,6 @@ func _physics_process(delta: float) -> void:
 		var force: Vector3 = (force_direction * movement_force).slide(normal)
 		#DebugDraw3D.draw_line(global_position, global_position + force, Color.GREEN)
 		apply_central_force(force)
+		apply_torque(Vector3(0,force_direction.normalized().signed_angle_to(-global_basis.z, Vector3.DOWN) * rotation_torque,0))
+		#DebugDraw3D.draw_line(global_position, global_position - global_basis.z, Color.BLUE)
+		#DebugDraw3D.draw_line(global_position, global_position + force_direction, Color.RED)