-
Notifications
You must be signed in to change notification settings - Fork 1
/
game_state.gd
145 lines (120 loc) · 4.14 KB
/
game_state.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
extends Node
# all global game-state variables & methods go in here
# (things that can be messed with in dialogs and other game-mechanics)
# if it is specific to a map, put it in the map's script, instead.
# this is the data that is saved and makes up a game
var current = {
# quests that have been completed
"quests_completed": [],
# quests currently in profgress or taken-on
"quests_incommplete": [],
# current scene the player is in
"current_scene": "",
# position of player, in current_scene
"position": [0, 0],
# size of inventory for consumable/dropable items
"bag_size": 5,
# stuff limited by bag_size
"inventory": {},
# things that can be get/set in dialogs
"switches": {}
}
# save game
func save(dialog="d0ef38b0-8dec-4b25-aa0e-e94e36976341"):
var save_game = File.new()
save_game.open("user://savegame.save", File.WRITE)
current.current_scene = current_scene.name
var p = player.get_position()
current.position = [p.x, p.y]
var j = to_json(current)
print("save savegame: ", j)
save_game.store_line(j)
save_game.close()
show_dialog(dialog)
# load game
func load():
var save_game = File.new()
if not save_game.file_exists("user://savegame.save"):
return false
save_game.open("user://savegame.save", File.READ)
var j = save_game.get_line()
print("load savegame: ", j)
var o = parse_json(j)
o.position = Vector2(o.position[0], o.position[1])
return o
# track what object is the player
var player:KinematicBody2D
var camera:Camera2D
# this holds the last place before a scene-switch
var lastplace = Vector2.ZERO
onready var music = preload("res://sounds/ModPlayer.tscn").instance()
# can the player move?
var player_move = true
func set_player_move(p):
player_move = p
var wr = weakref(player)
if not p and wr.get_ref():
player.set_movement_vector(Vector2.ZERO)
# switch scenes
var current_scene = null
func goto_scene(path, position=Vector2.ZERO):
var p = weakref(player)
print("load map: %s" % path)
if not p.get_ref():
lastplace = Vector2.ZERO
else:
lastplace = player.get_position()
call_deferred("_deferred_goto_scene", path, position)
func _deferred_goto_scene(path, position=Vector2.ZERO):
current_scene.free()
var s = ResourceLoader.load(path)
current_scene = s.instance()
get_tree().get_root().add_child(current_scene)
get_tree().set_current_scene(current_scene)
if position != Vector2.ZERO:
player.set_position(position)
# show a SayWhat dialog
func show_dialog(id: String) -> void:
DialogueManager.game_state = current_scene
set_player_move(false)
var dialog = yield(DialogueManager.get_next_dialogue_line(id), "completed")
if dialog != null:
var balloon = ResourceLoader.load("res://Dialog.tscn").instance()
balloon.handle(dialog)
add_child(balloon)
show_dialog(yield(balloon, "dialog_actioned"))
else:
set_player_move(true)
# show a panel to the user
func show_panel(code, map_success):
var map_back = current_scene.name
goto_scene("res://maps/PanelEntry.tscn")
call_deferred("_deferred_show_panel", code, map_success, map_back)
func _deferred_show_panel(code, map_success, map_back):
current_scene.code = code
current_scene.map_success = map_success
current_scene.map_back = map_back
current_scene.position_back = lastplace
func _ready():
var root = get_tree().get_root()
current_scene = root.get_child(root.get_child_count() - 1)
DialogueManager.resource = preload("res://dialog.tres")
add_child(music)
func _input(event):
if event.is_action_pressed('ui_quicksave'):
save()
var wr = weakref(player)
if player_move and wr.get_ref():
var movement_vector = player.get_movement_vector()
movement_vector = Vector2.ZERO
movement_vector.x = Input.get_action_strength('ui_right') - Input.get_action_strength('ui_left')
movement_vector.y = Input.get_action_strength('ui_down') - Input.get_action_strength('ui_up')
player.set_movement_vector(movement_vector.normalized())
if event.is_action_pressed('ui_accept') and player.touching and funcref(player.touching, 'on_activate').is_valid():
player.touching.on_activate()
# B toggles zoom
if event.is_action_pressed('ui_cancel'):
if camera.zoom == Vector2(2, 2):
camera.zoom = Vector2(1, 1)
else:
camera.zoom = Vector2(2, 2)