Skip to content

Commit

Permalink
refactor: 3d camera logic
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed May 12, 2024
1 parent e35285f commit fc01ef5
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 20 deletions.
Binary file added Project/Assets/icon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions Project/Assets/icon.png.import
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions Project/Game3D/Game3D.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
32 changes: 22 additions & 10 deletions Project/Game3D/Game3D.tscn
Original file line number Diff line number Diff line change
@@ -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 )
Expand All @@ -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 )
Expand All @@ -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 )
Expand All @@ -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 )
Expand All @@ -70,19 +82,19 @@ 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 )

[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 )
11 changes: 2 additions & 9 deletions Project/Game3D/Player/Player.gd
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
extends Area

class_name Player

signal food_eated
signal dead

Expand All @@ -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"):
Expand Down
8 changes: 7 additions & 1 deletion Project/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit fc01ef5

Please sign in to comment.