From fc01ef5ac21a36d362afa23cd22338254399c474 Mon Sep 17 00:00:00 2001 From: 201flaviosilva Date: Sun, 12 May 2024 23:39:43 +0100 Subject: [PATCH] refactor: 3d camera logic --- Project/Assets/icon.png | Bin 0 -> 3433 bytes Project/Assets/icon.png.import | 37 ++++++++++++++++++++++++++++++++ Project/Game3D/Game3D.gd | 15 +++++++++++++ Project/Game3D/Game3D.tscn | 32 ++++++++++++++++++--------- Project/Game3D/Player/Player.gd | 11 ++-------- Project/project.godot | 8 ++++++- 6 files changed, 83 insertions(+), 20 deletions(-) create mode 100644 Project/Assets/icon.png create mode 100644 Project/Assets/icon.png.import diff --git a/Project/Assets/icon.png b/Project/Assets/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..2b658158b6a2211b656d83fee35e3941e5ad376a GIT binary patch literal 3433 zcmV-v4VLnWP)% zeQ;D&mcW1Sz5Yz6)9KDvha`kRNCH6;fh8hl5CLJL#94P0kSWL2L6_M@XV|G7W)`Zf z-5OY?7-!3|)YS!a1yKQ)!O=x=(IEnYd`o~pLJ~+wI)QXPx;y>-_K(mpA>EyB(m~K) zRabZQz3<$6&b#m2bI-j`pbv58v80WgSS*XelX0SY9Qv79?$|+vESUkXyRANTrsK zA_$^ZZ$y+7-*%se6~E6*cb64p+3$7#t++VPfbds{Js)OCl^wXliAzo_TBqf`}($^*Q{9b2oKM>8|az7&ek?i zak!G5pB<#-YWuMCQy4${F&RK-OpS@a!m-)>*`^)czE z-P!^`Oh2i8` z|HvgAZXf3@U2OTXp40>l4@?}v+&c=mbKDpn`S}_;y6iFQb4(2|``WX+=#~9g9qs|; z4!4(WUtQwK(n3m#@@Z}B03gL=;GRk2*-&u-huhchnwCx{8^5@~j-w51S$a3KrxmmP zlYKGkb4(53=yn01RY_PK!Rzci*2u$?Gx_7c{C&Uw+Z}Fp9%~FKTdR`#wGk^~YJd(~ z&(PFsmC)WDw2sr`W8JQ^m=iQ~Iz0d+Xp}T}I&pe@L9gkR*{6+I5mN(n+Int#f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WX@^cuKnMR7ya6__M+d8Oi zv~c2VEhnpLIZ;)M(-k@hiiTk`KrqxQCFT?(=_w`*@o~gy)JT$uF`*~4wRU#l^Z9W& zU3AzSG_|(T+-k)Y#dH=eeGf_5fOuJ!hwYsKm)k>qV+-|-EyE%|TI6qnIGYGG0m#1S z9m(xVA6+ktcBf|-fY%e-L&&!>0bT7b+sC2^8vwu8)s6pqFbe!WFJ4zSfL{{D^^(i& zZC2@%vXlyGlF#EtQmPPwBtE_i_*Cns*DjGx5C)#+!3}iXm z>vn?Qk06L3h`0UQqk&gvJx&L%ZX3vc{0J4ctBs4=FC6!M?_ZwoO|{rt>%AV2_XBlO z@=lN7_b5e$3D5wy&+)L}*b#(!hwOga>-R2gId}5)_S$2;&hGyK3X(st#K?P)00000 LNkvXXu0mjfd922> literal 0 HcmV?d00001 diff --git a/Project/Assets/icon.png.import b/Project/Assets/icon.png.import new file mode 100644 index 0000000..53047c0 --- /dev/null +++ b/Project/Assets/icon.png.import @@ -0,0 +1,37 @@ +[remap] + +importer="texture" +type="StreamTexture" +path.s3tc="res://.import/icon.png-ec880de02d5dab0aa15458af9d6c53ed.s3tc.stex" +path.etc2="res://.import/icon.png-ec880de02d5dab0aa15458af9d6c53ed.etc2.stex" +metadata={ +"imported_formats": [ "s3tc", "etc2" ], +"vram_texture": true +} + +[deps] + +source_file="res://Assets/icon.png" +dest_files=[ "res://.import/icon.png-ec880de02d5dab0aa15458af9d6c53ed.s3tc.stex", "res://.import/icon.png-ec880de02d5dab0aa15458af9d6c53ed.etc2.stex" ] + +[params] + +compress/mode=2 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=true +flags/filter=true +flags/mipmaps=true +flags/anisotropic=false +flags/srgb=1 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +process/normal_map_invert_y=false +stream=false +size_limit=0 +detect_3d=false +svg/scale=1.0 diff --git a/Project/Game3D/Game3D.gd b/Project/Game3D/Game3D.gd index 53ecc18..34b7892 100644 --- a/Project/Game3D/Game3D.gd +++ b/Project/Game3D/Game3D.gd @@ -3,6 +3,21 @@ extends Spatial var score: int = 0 var is_game_over: bool = false +onready var CAMERA: Camera = $Camera +onready var CAMERA_TARGET: Position3D = $"Player/Camera Target" +onready var PLAYER: Player = $Player + +func _ready(): + CAMERA.look_at(CAMERA_TARGET.global_transform.origin, Vector3.UP) + CAMERA.translation = CAMERA.translation.rotated(Vector3.UP, deg2rad(-45)) + +func _process(delta: float) -> void: + if get_tree().paused: return; + + var camera_position = CAMERA_TARGET.global_transform.origin - PLAYER.direction * 10 + CAMERA.translation = camera_position + CAMERA.look_at(CAMERA_TARGET.global_transform.origin, Vector3.UP) + func _input(event: InputEvent) -> void: if event.is_action_pressed("ui_cancel"): if is_game_over: diff --git a/Project/Game3D/Game3D.tscn b/Project/Game3D/Game3D.tscn index b028aa5..cb9d3b1 100644 --- a/Project/Game3D/Game3D.tscn +++ b/Project/Game3D/Game3D.tscn @@ -1,8 +1,17 @@ -[gd_scene load_steps=13 format=2] +[gd_scene load_steps=16 format=2] [ext_resource path="res://Game3D/Player/Player.tscn" type="PackedScene" id=1] [ext_resource path="res://UI/Pause.tscn" type="PackedScene" id=2] [ext_resource path="res://Game3D/Game3D.gd" type="Script" id=3] +[ext_resource path="res://Assets/icon.png" type="Texture" id=4] + +[sub_resource type="SpatialMaterial" id=10] +albedo_texture = ExtResource( 4 ) +uv1_scale = Vector3( 25, 25, 1 ) + +[sub_resource type="PlaneMesh" id=11] +material = SubResource( 10 ) +size = Vector2( 60, 60 ) [sub_resource type="CubeMesh" id=1] size = Vector3( 2, 2, 50 ) @@ -21,13 +30,13 @@ albedo_color = Color( 0, 1, 0.243137, 1 ) [sub_resource type="CubeMesh" id=3] material = SubResource( 7 ) -size = Vector3( 100, 2, 2 ) +size = Vector3( 104, 2, 2 ) [sub_resource type="ConcavePolygonShape" id=4] data = PoolVector3Array( -50, 1, 1, 50, 1, 1, -50, -1, 1, 50, 1, 1, 50, -1, 1, -50, -1, 1, 50, 1, -1, -50, 1, -1, 50, -1, -1, -50, 1, -1, -50, -1, -1, 50, -1, -1, 50, 1, 1, 50, 1, -1, 50, -1, 1, 50, 1, -1, 50, -1, -1, 50, -1, 1, -50, 1, -1, -50, 1, 1, -50, -1, -1, -50, 1, 1, -50, -1, 1, -50, -1, -1, 50, 1, 1, -50, 1, 1, 50, 1, -1, -50, 1, 1, -50, 1, -1, 50, 1, -1, -50, -1, 1, 50, -1, 1, -50, -1, -1, 50, -1, 1, 50, -1, -1, -50, -1, -1 ) [sub_resource type="CubeMesh" id=8] -size = Vector3( 100, 2, 2 ) +size = Vector3( 104, 2, 2 ) [sub_resource type="ConcavePolygonShape" id=9] data = PoolVector3Array( -50, 1, 1, 50, 1, 1, -50, -1, 1, 50, 1, 1, 50, -1, 1, -50, -1, 1, 50, 1, -1, -50, 1, -1, 50, -1, -1, -50, 1, -1, -50, -1, -1, 50, -1, -1, 50, 1, 1, 50, 1, -1, 50, -1, 1, 50, 1, -1, 50, -1, -1, 50, -1, 1, -50, 1, -1, -50, 1, 1, -50, -1, -1, -50, 1, 1, -50, -1, 1, -50, -1, -1, 50, 1, 1, -50, 1, 1, 50, 1, -1, -50, 1, 1, -50, 1, -1, 50, 1, -1, -50, -1, 1, 50, -1, 1, -50, -1, -1, 50, -1, 1, 50, -1, -1, -50, -1, -1 ) @@ -47,10 +56,13 @@ far = 150.0 [node name="Camera Target" type="Position3D" parent="Player"] transform = Transform( 1, 0, 0, 0, 0.707107, 0.707107, 0, -0.707107, 0.707107, 0, 6.5, 7 ) +[node name="Floor" type="MeshInstance" parent="."] +mesh = SubResource( 11 ) + [node name="Walls" type="Spatial" parent="."] [node name="Left" type="Area" parent="Walls"] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -50, 0, 0 ) +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -25, 0, 0 ) [node name="MeshInstance" type="MeshInstance" parent="Walls/Left"] mesh = SubResource( 1 ) @@ -60,7 +72,7 @@ material/0 = SubResource( 5 ) shape = SubResource( 2 ) [node name="Right" type="Area" parent="Walls"] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 50, 0, 0 ) +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 25, 0, 0 ) [node name="MeshInstance" type="MeshInstance" parent="Walls/Right"] mesh = SubResource( 1 ) @@ -70,7 +82,7 @@ material/0 = SubResource( 6 ) shape = SubResource( 2 ) [node name="Top" type="Area" parent="Walls"] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -25 ) +transform = Transform( 0.5, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -25 ) [node name="MeshInstance" type="MeshInstance" parent="Walls/Top"] mesh = SubResource( 3 ) @@ -78,11 +90,11 @@ mesh = SubResource( 3 ) [node name="CollisionShape" type="CollisionShape" parent="Walls/Top"] shape = SubResource( 4 ) -[node name="Area" type="Area" parent="Walls"] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 25 ) +[node name="Down" type="Area" parent="Walls"] +transform = Transform( 0.5, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 25 ) -[node name="MeshInstance" type="MeshInstance" parent="Walls/Area"] +[node name="MeshInstance" type="MeshInstance" parent="Walls/Down"] mesh = SubResource( 8 ) -[node name="CollisionShape" type="CollisionShape" parent="Walls/Area"] +[node name="CollisionShape" type="CollisionShape" parent="Walls/Down"] shape = SubResource( 9 ) diff --git a/Project/Game3D/Player/Player.gd b/Project/Game3D/Player/Player.gd index c90619f..f96f94d 100644 --- a/Project/Game3D/Player/Player.gd +++ b/Project/Game3D/Player/Player.gd @@ -1,5 +1,7 @@ extends Area +class_name Player + signal food_eated signal dead @@ -8,23 +10,14 @@ export (float, 0.05, 1) var normal_speed: float = 0.1 var direction: Vector3 = Vector3(0, 0, -1) var speed: float = normal_speed -onready var CAMERA: Camera = get_parent().get_node("Camera") -onready var CAMERA_TARGET: Position3D = get_node("Camera Target") onready var MOVE_TIMER: Timer = $Move func _ready(): _set_speed(normal_speed) - CAMERA.look_at(CAMERA_TARGET.global_transform.origin, Vector3.UP) - CAMERA.translation = CAMERA.translation.rotated(Vector3.UP, deg2rad(-45)) - func _process(delta: float) -> void: if get_tree().paused: return; look_at(global_transform.origin + direction, Vector3.UP) - - var camera_position = CAMERA_TARGET.global_transform.origin - direction * 10 - CAMERA.translation = camera_position - CAMERA.look_at(CAMERA_TARGET.global_transform.origin, Vector3.UP) func _input(event): if event.is_action_pressed("left"): diff --git a/Project/project.godot b/Project/project.godot index 79f4ff1..0493e76 100644 --- a/Project/project.godot +++ b/Project/project.godot @@ -8,8 +8,14 @@ config_version=4 -_global_script_classes=[ ] +_global_script_classes=[ { +"base": "Area", +"class": "Player", +"language": "GDScript", +"path": "res://Game3D/Player/Player.gd" +} ] _global_script_class_icons={ +"Player": "" } [application]