Skip to content

Commit

Permalink
fix: player movimentation
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed Apr 17, 2023
1 parent e548f5d commit c69a3ad
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 32 deletions.
80 changes: 76 additions & 4 deletions Project/Game3D/Game3D.tscn
Original file line number Diff line number Diff line change
@@ -1,16 +1,88 @@
[gd_scene load_steps=4 format=2]
[gd_scene load_steps=13 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]

[sub_resource type="CubeMesh" id=1]
size = Vector3( 2, 2, 50 )

[sub_resource type="SpatialMaterial" id=5]
albedo_color = Color( 0, 0.14902, 1, 1 )

[sub_resource type="ConcavePolygonShape" id=2]
data = PoolVector3Array( -1, 1, 25, 1, 1, 25, -1, -1, 25, 1, 1, 25, 1, -1, 25, -1, -1, 25, 1, 1, -25, -1, 1, -25, 1, -1, -25, -1, 1, -25, -1, -1, -25, 1, -1, -25, 1, 1, 25, 1, 1, -25, 1, -1, 25, 1, 1, -25, 1, -1, -25, 1, -1, 25, -1, 1, -25, -1, 1, 25, -1, -1, -25, -1, 1, 25, -1, -1, 25, -1, -1, -25, 1, 1, 25, -1, 1, 25, 1, 1, -25, -1, 1, 25, -1, 1, -25, 1, 1, -25, -1, -1, 25, 1, -1, 25, -1, -1, -25, 1, -1, 25, 1, -1, -25, -1, -1, -25 )

[sub_resource type="SpatialMaterial" id=6]
albedo_color = Color( 1, 0, 0, 1 )

[sub_resource type="SpatialMaterial" id=7]
albedo_color = Color( 0, 1, 0.243137, 1 )

[sub_resource type="CubeMesh" id=3]
material = SubResource( 7 )
size = Vector3( 100, 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 )

[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 )

[node name="Game3D" type="Spatial"]
pause_mode = 2
script = ExtResource( 3 )

[node name="Camera" type="Camera" parent="."]
transform = Transform( 1, 0, 0, 0, 0.724268, 0.689518, 0, -0.689518, 0.724268, 0, 37.5787, 45.8828 )

[node name="Pause" parent="." instance=ExtResource( 2 )]

[node name="Camera" type="Camera" parent="."]
transform = Transform( 1, 0, 0, 0, 0.642788, 0.766044, 0, -0.766044, 0.642788, 0, 30, 15 )
far = 150.0

[node name="Player" parent="." instance=ExtResource( 1 )]

[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="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 )

[node name="MeshInstance" type="MeshInstance" parent="Walls/Left"]
mesh = SubResource( 1 )
material/0 = SubResource( 5 )

[node name="CollisionShape" type="CollisionShape" parent="Walls/Left"]
shape = SubResource( 2 )

[node name="Right" type="Area" parent="Walls"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 50, 0, 0 )

[node name="MeshInstance" type="MeshInstance" parent="Walls/Right"]
mesh = SubResource( 1 )
material/0 = SubResource( 6 )

[node name="CollisionShape" type="CollisionShape" parent="Walls/Right"]
shape = SubResource( 2 )

[node name="Top" type="Area" parent="Walls"]
transform = Transform( 1, 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="MeshInstance" type="MeshInstance" parent="Walls/Area"]
mesh = SubResource( 8 )

[node name="CollisionShape" type="CollisionShape" parent="Walls/Area"]
shape = SubResource( 9 )
60 changes: 33 additions & 27 deletions Project/Game3D/Player/Player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,50 @@ extends Area
signal food_eated
signal dead

export (float, 0.1, 1) var normal_speed: float = 0.2
export (float, 0.05, 1) var normal_speed: float = 0.1

var direction: Vector3 = Vector3.FORWARD
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() -> void:
func _ready():
_set_speed(normal_speed)
pass

func _process(delta: float) -> void:
if get_tree().paused: return
CAMERA.look_at(CAMERA_TARGET.global_transform.origin, Vector3.UP)
CAMERA.translation = CAMERA.translation.rotated(Vector3.UP, deg2rad(-45))

if Input.is_action_just_pressed("up"):
direction = Vector3.FORWARD
rotation_degrees.y = 0

if Input.is_action_just_pressed("down"):
direction = Vector3.BACK
rotation_degrees.y = 180

if Input.is_action_just_pressed("left"):
direction = Vector3.LEFT
rotation_degrees.y = 90
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"):
if direction.x == -1: direction = Vector3.BACK # Down
elif direction.x == 1: direction = Vector3.FORWARD # Up
elif direction.z == -1: direction = Vector3.LEFT # Left
elif direction.z == 1: direction = Vector3.RIGHT # Right

if event.is_action_pressed("right"):
if direction.x == -1: direction = Vector3.FORWARD # Up
elif direction.x == 1: direction = Vector3.BACK # Down
elif direction.z == -1: direction = Vector3.RIGHT # Right
elif direction.z == 1: direction = Vector3.LEFT # Left

if Input.is_action_just_pressed("right"):
direction = Vector3.RIGHT
rotation_degrees.y = -90

if Input.is_action_just_pressed("run"): _set_speed(normal_speed / 2)
elif Input.is_action_just_released("run"): _set_speed(normal_speed)
if event.is_action_pressed("run"): _set_speed(normal_speed / 2)
elif event.is_action_released("run"): _set_speed(normal_speed)

func _on_Move_timeout() -> void:
if get_tree().paused: return
global_translation += direction
global_translate(direction)

func _set_speed(value: float) -> void:
var move_timer = $Move
speed = value
move_timer.wait_time = speed
move_timer.start()
MOVE_TIMER.wait_time = speed
MOVE_TIMER.start()
2 changes: 1 addition & 1 deletion Project/Game3D/Player/Player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ script = ExtResource( 1 )
transform = Transform( 1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0 )
mesh = SubResource( 1 )

[node name="MeshInstance2" type="MeshInstance" parent="."]
[node name="Direction" type="MeshInstance" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -1.03733 )
mesh = SubResource( 3 )
material/0 = SubResource( 4 )
Expand Down
1 change: 1 addition & 0 deletions Project/UI/Pause.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ func _ready() -> void:
hide()

func _on_HomeButton_pressed() -> void:
get_tree().paused = false
get_tree().change_scene("res://UI/Menu.tscn")

0 comments on commit c69a3ad

Please sign in to comment.