-
Notifications
You must be signed in to change notification settings - Fork 1
/
World.gd
64 lines (48 loc) · 1.37 KB
/
World.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
extends Node2D
onready var hud = $HUD
onready var obstacle_spawner = $ObstacleSpawner
onready var ground = $Ground
onready var menu_layer = $MenuLayer
const SAVE_FILE_PATH = "user://savedata.save"
var score = 0 setget set_score
var highscore = 0
func _ready():
obstacle_spawner.connect("obstacle_created", self, "_on_obstacle_created")
load_highscore()
func new_game():
self.score = 0
obstacle_spawner.start()
func player_score():
self.score += 1
func set_score(new_score):
score = new_score
hud.update_score(score)
func _on_obstacle_created(obs):
obs.connect("score", self, "player_score")
func _on_DeathZone_body_entered(body):
if body is Player:
if body.has_method("die"):
body.die()
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 save_data = File.new()
save_data.open(SAVE_FILE_PATH, File.WRITE)
save_data.store_var(highscore)
save_data.close()
func load_highscore():
var load_data = File.new()
if load_data.file_exists(SAVE_FILE_PATH):
load_data.open(SAVE_FILE_PATH, File.READ)
highscore = load_data.get_var()
load_data.close()