so the player goes room by room,the camera is static, if you exit the room the camera goes to that room,now i added a save feature,that saves the game everytime you enter a room.i did that by putting areas by every entrance/exit of the rooms,once you quit the game and go play it again,you will spawn on the last room you where,but the camera must know that you are in that room so it goes there with this code:
extends Camera2D
@onready var player: CharacterBody2D = $“…/Player”
Called when the node enters the scene tree for the first time.
func _ready() → void:
start()
Called every frame. ‘delta’ is the elapsed time since the previous frame.
func _process(delta: float) → void:
pass
func camera_panning() → void:
position = player.position
var x = floor(position.x / 960) * 960
var y = floor(position.y / 540) * 540
position = Vector2(x, y)
var tween = create_tween().set_trans(Tween.TRANS_CUBIC).set_ease(Tween.EASE_IN_OUT)
tween.tween_property(self, "position", Vector2(x, y), 0.15)
func start() → void:
for c in 2000:
position = player.position
var x = floor(position.x / 960) * 960
var y = floor(position.y / 540) * 540
position = Vector2(x, y)
var tween = create_tween().set_trans(Tween.TRANS_CUBIC).set_ease(Tween.EASE_IN_OUT)
tween.tween_property(self, "position", Vector2(x, y), 0)
await get_tree().create_timer(0).timeout
and here is the save system(main) and player code:
###main.gd
extends Node2D
@onready var player: CharacterBody2D = $Player
var config = ConfigFile.new()
Called when the node enters the scene tree for the first time.
func _ready() → void:
var saved_data = config.load(“res://savegame.cfg”)
if saved_data == OK:
$Player.position.x = config.get_value(“Player”, “x”)
$Player.position.y = config.get_value(“Player”, “y”)
Called every frame. ‘delta’ is the elapsed time since the previous frame.
func _process(delta: float) → void:
pass
func _input(event: InputEvent) → void:
if Input.is_action_just_pressed(“pause”):
get_tree().quit()
if Input.is_action_just_pressed(“restart”):
restart()
func restart() → void:
get_tree().reload_current_scene()
func save() → void:
config.set_value(“Player”, “x”, round($Player.position.x))
config.set_value(“Player”, “y”, round($Player.position.y))
config.save(“res://savegame.cfg”)
###player.gd
extends CharacterBody2D
const SPEED = 300
const DASH_SPEED = 700
const JUMP_VELOCITY = -350.0
const wall_jump = 1000
var alive = true
var dash_counter = 0
var dash_max = 2
var dashing = false
var can_dash = true
var jump_counter = 0
var gravity = 980
@export var jump_max = 2
@onready var anim: AnimatedSprite2D = $AnimatedSprite2D
@onready var ray_cast_2d: RayCast2D = $RayCast2D
func _ready() → void:
top_level = true
anim.show()
func _physics_process(delta: float) → void:
var direction := Input.get_axis(“left”, “right”)
if alive:
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
if velocity.y < 0:
anim.play(“Jump”)
if velocity.y > 0:
anim.play(“Fall”)
if ray_cast_2d.is_colliding():
anim.play(“Wall_Slide”)
if velocity.y > 0:
gravity = 200
else:
gravity = 1500
else:
gravity = 980
else:
jump_counter = 0
dash_counter = 0
# Handle jump.
if Input.is_action_just_pressed("jump") and jump_counter < jump_max:
jump_counter += 1
velocity.y = JUMP_VELOCITY
if ray_cast_2d.is_colliding():
if anim.flip_h == true:
velocity.x = 20
else:
velocity.x = -20
if Input.is_action_just_pressed("dash") and can_dash and !is_on_floor() and dash_counter < dash_max:
anim.play("Dash")
if direction == 0:
velocity.y = JUMP_VELOCITY - 170
$dashParticles.emitting = true
dashing = true
can_dash = false
dash_counter += 1
await get_tree().create_timer(0.15).timeout
can_dash = true
dashing = false
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
if is_on_floor():
if direction == 0:
anim.play("Idle")
if direction > 0 or direction < 0:
anim.play("Run")
if ray_cast_2d.is_colliding():
jump_counter = 0
dash_counter = 0
if direction:
if dashing:
velocity.x = direction * DASH_SPEED
else:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
func _input(event: InputEvent) → void:
if alive:
if Input.is_action_just_pressed(“left”):
ray_cast_2d.target_position = Vector2(-3, 0)
anim.flip_h = true
if Input.is_action_just_pressed(“right”):
ray_cast_2d.target_position = Vector2(3, 0)
anim.flip_h = false
func die() → void:
alive = false
anim.hide()
$CPUParticles2D.emitting = true
await get_tree().create_timer(1.3).timeout
restart()
func restart() → void:
$“…”.restart()
func _on_visible_on_screen_notifier_2d_screen_exited() → void:
for c in Engine.get_frames_per_second():
$“…/Camera2D”.camera_panning()
await get_tree().create_timer(0).timeout
func _on_hurt_box_body_entered(body: Node2D) → void:
die()
func _on_save_box_body_entered(body: Node2D) → void:
$“…”.save()