Godot Version
4
Question
I have in my game a house. The player can enter in the house and exit it, but when he do, the camera go to the start position.
Here is the world script
extends Node2D
class_name GameManager
signal toogle_game_paused(is_paused : bool)
var game_paused : bool = false:
get:
return game_paused
set(value):
game_paused = value
get_tree().paused = game_paused
emit_signal("toogle_game_paused", game_paused)
func _ready():
if Global.game_first_load_in == true:
$player.position.x = Global.player_start_position_x
$player.position.y = Global.player_start_position_y
$CanvasLayer/pause_menu.hide()
$player/pause_button.hide()
$Mob_spawner.position.x = randi_range(-430, 2290)
$Mob_spawner2.position.x = randi_range(-430, 2290)
$Mob_spawner3.position.x = randi_range(-430, 2290)
$Mob_spawner.position.y = randi_range(-310, 730)
$Mob_spawner2.position.y = randi_range(-310, 730)
$Mob_spawner3.position.y = randi_range(-310, 730)
$CanvasLayer/death_menu.hide()
else:
$UI.hide()
$player.position.x = Global.player_exit_house_position_x
$player.position.y = Global.player_exit_house_position_y
$player/world_camera.position.x = Global.player_exit_house_position_x
$player/world_camera.position.y = Global.player_exit_house_position_y
$player/house_camera.position.x = Global.player_exit_house_position_x
$player/house_camera.position.y = Global.player_exit_house_position_y
$player.current_camera()
func _process(delta):
changescene()
func _on_hud_start_game():
$player/pause_button.show()
Global.game_started = true
func _on_player_death():
$CanvasLayer/death_menu.show()
func _on_pause_button_pressed():
game_paused = !game_paused
func _on_door_entrance_body_entered(body):
if body.has_method("player") and Global.house_door_opened == true:
Global.transition_scene = true
func changescene():
if Global.transition_scene == true:
if Global.current_scene == "world":
get_tree().change_scene_to_file("res://Scenes/inside_house.tscn")
Global.game_first_load_in = false
Global.finish_changescenes()
here is the house script :
extends Node2D
func _ready():
Global.current_scene = "inside_house"
$AnimationPlayer.play("new_animation")
await $AnimationPlayer.animation_finished
Global.game_started = true
func _process(delta):
change_scene()
func _on_exit_house_area_body_entered(body):
if body.has_method("player"):
Global.transition_scene = true
func change_scene():
if Global.transition_scene == true:
if Global.current_scene == "inside_house":
get_tree().change_scene_to_file("res://Scenes/world.tscn")
Global.finish_changescenes()
here is the player script:
extends CharacterBody2D
signal death
var enemy_in_attack_range = false
var enemy_attack_cooldown = true
var enemy = null
var health = 100
@onready var healthbar = $health_bar
var player_alive = true
var attack_in_progress = false
var current_direction
var speed = normal_speed
const normal_speed = 300
const dash_speed = 600
const dash_length = .1
@onready var dash = $Dash
func _ready():
current_direction = "down"
player_anim(0)
func _process(delta):
speed = dash_speed if dash.is_dashing() and $Dash/dash_timer.timeout else normal_speed
player_movement(delta)
attack()
enemy_attack()
update_health()
current_camera()
if Global.player_current_attack == true and enemy_in_attack_range == true:
if enemy != null:
enemy.position -= (enemy.position - position).normalized() * 0.1
if health <= 0:
player_alive = false
Global.player_alive = false
health = 0
self.hide()
$CollisionShape2D.disabled = true
death.emit()
$AnimatedSprite2D.play("Death animation")
Global.player_alive = player_alive
func player_movement(delta):
if Global.game_started == true and attack_in_progress == false:
$Pseudo.text = Global.player_name
if player_alive == true:
if Input.is_action_pressed("Right"):
current_direction = "right"
velocity.x = speed
velocity.y = 0
player_anim(1)
elif Input.is_action_pressed("Left"):
current_direction = "left"
velocity.x = -speed
velocity.y = 0
player_anim(1)
elif Input.is_action_pressed("Up"):
current_direction = "up"
velocity.x = 0
velocity.y = -speed
player_anim(1)
elif Input.is_action_pressed("Down"):
current_direction = "down"
velocity.x = 0
velocity.y = speed
player_anim(1)
else :
velocity.x = 0
velocity.y = 0
player_anim(0)
if Input.is_action_just_pressed("A"):
dash.start_dash(dash_length)
move_and_slide()
func player_anim(movement):
var direction = current_direction
var anim = $AnimatedSprite2D
if direction == "right":
anim.flip_h = false
if movement == 0:
if attack_in_progress == false :
anim.play("Idle side")
elif movement == 1:
anim.play("Walk side")
if direction == "left":
anim.flip_h = true
if movement == 0:
if attack_in_progress == false :
anim.play("Idle side")
elif movement == 1:
anim.play("Walk side")
if direction == "down":
if movement == 0:
if attack_in_progress == false :
anim.play("Idle front")
elif movement == 1:
anim.play("Walk front")
if direction == "up":
if movement == 0:
if attack_in_progress == false :
anim.play("Idle back")
elif movement == 1:
anim.play("Walk back")
func current_camera():
if Global.current_scene == "world":
$world_camera.enabled = true
$house_camera.enabled = false
if Global.current_scene == "inside_house":
$world_camera.enabled = false
$house_camera.enabled = true
func player():
pass
func _on_player_hitbox_body_entered(body):
if body.has_method("enemy"):
enemy = body
enemy_in_attack_range = true
func _on_player_hitbox_body_exited(body):
if body.has_method("enemy"):
enemy = body
enemy_in_attack_range = false
func enemy_attack():
if enemy_in_attack_range and enemy_attack_cooldown == true and player_alive == true:
health -= 20
$regin_timer.start()
enemy_attack_cooldown = false
print("player -20 health")
$attack_cooldown.start()
func _on_attack_cooldown_timeout():
enemy_attack_cooldown = true
func attack():
var direction = current_direction
if Input.is_action_just_pressed("mouse left") and Global.player_alive == true:
Global.player_current_attack = true
attack_in_progress = true
if direction == "right":
$AnimatedSprite2D.flip_h = false
$AnimatedSprite2D.play("Attack side")
$deal_attack_timer.start()
if direction == "left":
$AnimatedSprite2D.flip_h = true
$AnimatedSprite2D.play("Attack side")
$deal_attack_timer.start()
if direction == "down":
$AnimatedSprite2D.play("Attack front")
$deal_attack_timer.start()
if direction == "up":
$AnimatedSprite2D.play("Attack back")
$deal_attack_timer.start()
func _on_deal_attack_timer_timeout():
$deal_attack_timer.stop()
Global.player_current_attack = false
attack_in_progress = false
func update_health():
if health != 0:
$health_bar.value = health
$health_bar.visible = true
if health >= 100:
$health_bar.visible = false
elif health == 80:
$health_bar.modulate = Color(105/255, 255/255, 102/255) # Vert
elif health == 60:
$health_bar.modulate = Color(255/255, 255/255, 0/255) # Jaune
elif health == 40:
$health_bar.modulate = Color(255/255, 166/255, 61/255) # Orange
elif health == 20:
$health_bar.modulate = Color(255/255, 0/255, 0/255) # Rouge
func _on_regin_timer_timeout():
if health < 100:
health += 20
if health > 100:
health = 100
if health <= 0:
health = 0
and here is the global script:
extends Node
var player_current_attack = false
var current_scene = "world"
var transition_scene = false
var game_first_load_in = true
var player_exit_house_position_x = 1821
var player_exit_house_position_y = 514
var player_start_position_x = 554
var player_start_position_y = 329
var game_started = false
var player_name = "You"
var player_alive = true
var house_door_opened = false
func finish_changescenes():
if transition_scene == true:
transition_scene = false
if current_scene == "world":
current_scene == "inside_house"
else:
current_scene == "world"