Untitled

 avatar
Kandif
plain_text
6 months ago
1.5 kB
5
Indexable
Never
extends Node2D

@onready var hud = $HUD
@onready var obstacle_spawner = $ObstacleSpawner
@onready var ground = $Ground
@onready var menu_layer = $MenuLayer

const SAVE_FILE_PAHT = "user://savedata.save"

var score = 0: set = set_score
var highscore = 0

func _ready():
	obstacle_spawner.connect("obstacle_created", Callable(self, "_on_obstacle_created"))
	load_highscore()

func new_game():
	#początkowa ilość punktów w grze
	self.score = 100
	obstacle_spawner.start()

func player_score():
	#zmiana punktów podczas zdobycia punktów
	self.score -= 1

func set_score(new_score):
	score = new_score
	hud.update_score(score)

func _on_obstacle_created(obs):
	obs.connect("score", Callable(self, "player_score"))


func _on_Player_died():
	game_over()

func game_over():
	obstacle_spawner.stop()
	ground.get_node("AnimationPlayer").stop()
	get_tree().call_group("obstacles", "set_physics_process", false)
	
	if score > highscore:
		highscore = score
		save_highscore()
	
	menu_layer.init_game_over_menu(score, highscore)


func _on_MenuLayer_start_game():
	new_game()

func save_highscore():
	var file = FileAccess.open(SAVE_FILE_PAHT, FileAccess.WRITE)
	file.store_var(highscore)

func load_highscore():
	if FileAccess.file_exists(SAVE_FILE_PAHT):
		var file = FileAccess.open(SAVE_FILE_PAHT, FileAccess.READ)
		highscore = file.get_var()
	else:
		highscore = 0


func _on_death_zone_body_entered(body):
	if body is Player:
		if body.has_method("die"):
			body.die()