Make camera offset to player's direction

This commit is contained in:
Marco 2023-10-02 15:54:10 +02:00
parent 26480fca74
commit c404f0197b
5 changed files with 15 additions and 9 deletions

View File

@ -111,10 +111,6 @@ position = Vector2(449, -466)
[node name="Player" parent="." instance=ExtResource("3_blhoc")] [node name="Player" parent="." instance=ExtResource("3_blhoc")]
position = Vector2(535, 571) position = Vector2(535, 571)
[node name="Camera2D" type="Camera2D" parent="Player"]
zoom = Vector2(0.6, 0.6)
position_smoothing_enabled = true
[node name="Projectiles" type="Node2D" parent="."] [node name="Projectiles" type="Node2D" parent="."]
[connection signal="player_entered_gate" from="Gate" to="." method="_on_gate_player_entered_gate"] [connection signal="player_entered_gate" from="Gate" to="." method="_on_gate_player_entered_gate"]

5
scenes/player/camera.gd Normal file
View File

@ -0,0 +1,5 @@
extends Camera2D
func handle_offset(dir: Vector2, delta: float):
var desired_offset = dir*500
offset = offset.lerp(desired_offset, delta*2)

View File

@ -13,6 +13,9 @@ func _process(_delta):
look_at(get_global_mouse_position()) look_at(get_global_mouse_position())
var player_direction = (get_global_mouse_position() - position).normalized()
$Camera.handle_offset(player_direction, _delta)
if Input.is_action_just_pressed("primary action") and can_laser: if Input.is_action_just_pressed("primary action") and can_laser:
can_laser = false can_laser = false
$LaserTimer.start() $LaserTimer.start()
@ -21,15 +24,12 @@ func _process(_delta):
var laser_markers = $LaserStartPositions.get_children() var laser_markers = $LaserStartPositions.get_children()
var selected_laser = laser_markers[randi() % laser_markers.size()] var selected_laser = laser_markers[randi() % laser_markers.size()]
var player_direction = (get_global_mouse_position() - position).normalized()
laser_fired.emit(selected_laser.global_position, player_direction) laser_fired.emit(selected_laser.global_position, player_direction)
if Input.is_action_just_pressed("secondary action") and can_grenade: if Input.is_action_just_pressed("secondary action") and can_grenade:
can_grenade = false can_grenade = false
$GrenadeTimer.start() $GrenadeTimer.start()
var grenade_marker = $GrenadeStartPositions/GrenadeMarker1 var grenade_marker = $GrenadeStartPositions/GrenadeMarker1
var player_direction = (get_global_mouse_position() - position).normalized()
grenade_thrown.emit(grenade_marker.global_position, player_direction) grenade_thrown.emit(grenade_marker.global_position, player_direction)

View File

@ -1,7 +1,8 @@
[gd_scene load_steps=4 format=3 uid="uid://cdo8mdy41ooe8"] [gd_scene load_steps=5 format=3 uid="uid://cdo8mdy41ooe8"]
[ext_resource type="Script" path="res://scenes/player/player.gd" id="1_v2hsv"] [ext_resource type="Script" path="res://scenes/player/player.gd" id="1_v2hsv"]
[ext_resource type="Texture2D" uid="uid://dqo0m7ok23wh6" path="res://graphics/player/player.png" id="1_x6dmn"] [ext_resource type="Texture2D" uid="uid://dqo0m7ok23wh6" path="res://graphics/player/player.png" id="1_x6dmn"]
[ext_resource type="Script" path="res://scenes/player/camera.gd" id="3_uf66x"]
[sub_resource type="CircleShape2D" id="CircleShape2D_aph52"] [sub_resource type="CircleShape2D" id="CircleShape2D_aph52"]
radius = 12.641 radius = 12.641
@ -44,5 +45,10 @@ position = Vector2(115, 3)
[node name="GrenadeMarker1" type="Marker2D" parent="GrenadeStartPositions"] [node name="GrenadeMarker1" type="Marker2D" parent="GrenadeStartPositions"]
position = Vector2(115, 0) position = Vector2(115, 0)
[node name="Camera" type="Camera2D" parent="."]
zoom = Vector2(0.75, 0.75)
position_smoothing_enabled = true
script = ExtResource("3_uf66x")
[connection signal="timeout" from="LaserTimer" to="." method="_on_laser_timeout"] [connection signal="timeout" from="LaserTimer" to="." method="_on_laser_timeout"]
[connection signal="timeout" from="GrenadeTimer" to="." method="_on_grenade_timer_timeout"] [connection signal="timeout" from="GrenadeTimer" to="." method="_on_grenade_timer_timeout"]

View File

@ -4,5 +4,4 @@ extends Area2D
var direction: Vector2 = Vector2.UP var direction: Vector2 = Vector2.UP
func _process(delta): func _process(delta):
position += direction * speed * delta position += direction * speed * delta