-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.gd
390 lines (295 loc) · 11.1 KB
/
Main.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
extends Node2D
var rng = RandomNumberGenerator.new()
const Giko = preload("res://Giko/Giko.gd")
const ActionQueueGiko = preload("res://Giko/ActionQueueGiko.gd")
const PlayerGiko = preload("res://Giko/PlayerGiko.gd")
var gikoPrefab = preload("res://Giko/Giko.tscn")
var lightPrefab = preload("res://Quests/Misc/Flashlight.tscn")
var messages: Dictionary = {}
var totalMessages = 0
var playerGiko: Node
var activeItems: Dictionary = {}
var activeNPCs: Dictionary = {}
var environmentItems: Dictionary = {}
var timeElapsed = 0
var currentRandomGikos: Dictionary = {}
const DEFAULT_NEXT_GIKO_TIME = 5
var nextGikoTime = Utils.rng.randfn(DEFAULT_NEXT_GIKO_TIME, 5)
var adjacentRoomsThread
var adjacentRoomsCache = []
onready var camera = $"%Camera2D"
onready var dialogueManager = $"%Dialogue"
var loadedSave = {
"ROOM": "admin_st",
"PLAYER_POSITION": Vector2(2, 4),
"PLAYER_DIRECTION": Constants.Directions.DIR_DOWN
}
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
# Called when the node enters the scene tree for the first time.
func _ready():
Quests.initQuests()
gameContinue()
# dialogueManager.setLineSet(Utils.makeSimpleDialogue([
# "Welcome to Giko Story!",
# "This server is an amalgamation of the international and Japanese servers.",
# "Go explore, talk to everyone, pick up items and have fun!"
# ]), "")
# dialogueManager.show()
func gameContinue():
SceneTransition.titleScreen = false
changeRoom(loadedSave["ROOM"])
spawnRandomGikos()
spawnPlayerGiko(loadedSave["PLAYER_POSITION"], loadedSave["PLAYER_DIRECTION"])
func loadRandomRoom() -> void:
#loadRoom("admin")
loadRoom(Rooms.ROOMS.keys()[Utils.rng.randi() % Rooms.ROOMS.keys().size()])
func cleanRoom() -> void:
currentRandomGikos.clear()
for n in $"%zObjects".get_children():
$"%zObjects".remove_child(n)
n.queue_free()
func spawnRandomGikos(number: int = Utils.rng.randi() % 10) -> void:
for _i in range(number):
var randomGiko = getAvailableRandomGikoPair()
spawnRandomGiko(randomGiko[0], randomGiko[1])
func getAvailableRandomGikoPair() -> Array:
if Utils.rng.randf() > 0.6:
return [
"Anonymous",
Constants.Character.values()[Utils.rng.randi() % Constants.Character.values().size()]
]
else:
var randomGiko = Constants.RANDOM_GIKOS[Utils.rng.randi() % Constants.RANDOM_GIKOS.size()]
return [randomGiko["name"], randomGiko["character"]]
func spawnRandomGikoAtDoor() -> void:
var randomGiko = getAvailableRandomGikoPair()
var randomRoom = Rooms.getRandomDoorInRoom()
spawnRandomGiko(randomGiko[0], randomGiko[1], randomRoom, true)
func changeRoom(newRoom: String):
$"%zObjects".remove_child(playerGiko)
cleanRoom()
loadRoom(newRoom)
spawnRandomGikos()
$"%zObjects".add_child(playerGiko)
func doorChangeRoom(target):
changeRoom(target["roomId"])
playerGiko.place(
Vector2(
Rooms.currentRoomData["doors"][target["doorId"]]["x"],
Rooms.currentRoomData["doors"][target["doorId"]]["y"]
),
Utils.roomDirectionToEnum(Rooms.currentRoomData["doors"][target["doorId"]]["direction"])
)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
timeElapsed += delta
if timeElapsed > nextGikoTime:
nextGikoTime = Utils.rng.randfn(DEFAULT_NEXT_GIKO_TIME, 5)
timeElapsed = 0
if Utils.rng.randf() > 0.5:
spawnRandomGikoAtDoor()
else:
popRandomGiko()
func popRandomGiko() -> void:
if currentRandomGikos.size() > 0:
var key = currentRandomGikos.keys()[Utils.rng.randi() % currentRandomGikos.keys().size()]
if !removeRandomGiko(key):
print("pop failed")
print(key)
func removeRandomGiko(key : int) -> bool:
if is_instance_valid(currentRandomGikos[key]):
currentRandomGikos[key].disappear(true)
currentRandomGikos.erase(key)
return true
return false
func adjacentRoomsLoaded():
adjacentRoomsThread.wait_to_finish()
func loadAdjacentRooms(adjacentRooms):
for room in adjacentRooms:
adjacentRoomsCache.push_back(load(room))
call_deferred("adjacentRoomsLoaded")
func loadRoom(roomName: String) -> void:
Rooms.loaded = false
Rooms.updateRoomData(roomName)
if Rooms.currentRoomData.has("exterior") && Rooms.currentRoomData["exterior"] == true:
$"%Snow".visible = true
else:
$"%Snow".visible = false
var newRoomTexture = load("res://" + Rooms.ROOMS[roomName]["backgroundImageUrl"])
$Background.scale = Vector2(Rooms.currentRoomScale / 4, Rooms.currentRoomScale / 4)
$Background.texture = newRoomTexture
$Background.position = Rooms.getCurrentRoomOffset()
$"%RoomName".setNewRoom(Rooms.DISPLAY_NAMES[roomName])
timeElapsed = 0
loadObjects()
loadItems()
loadNPCs()
loadEnvironment()
$"%Grid".draw_grid()
## Room specific:
if roomName == "bar_giko_square":
if State.BLACKOUT:
var newLight = lightPrefab.instance()
$"%zObjects".add_child(newLight)
newLight.position = Utils.getTilePosAtCoords(Vector2(1, 10))
newLight.color = Color("ffc4c4")
newLight.texture_scale = 1.0
#adjacentRoomsThread = Thread.new()
#adjacentRoomsCache.clear()
var adjacentRooms = []
for door in Rooms.currentRoomData["doors"].values():
if door["target"] != null:
adjacentRooms.push_back(
"res://" + Rooms.ROOMS[door["target"]["roomId"]]["backgroundImageUrl"]
)
#adjacentRoomsThread.start(self, "loadAdjacentRooms", adjacentRooms)
Rooms.loaded = true
Quests.checkConditions(Quests.QuestConditionType.Room)
return
func loadObjects() -> void:
for object in Rooms.currentRoomData["objects"]:
var objectSprite = Sprite.new()
objectSprite.centered = false
objectSprite.texture = load("res://rooms/" + Rooms.currentRoomId + "/" + object["url"])
objectSprite.scale = Vector2(0.25, 0.25) * (object["scale"] if object.has("scale") else 1)
$"%zObjects".add_child(objectSprite)
var roomOffset: Vector2 = Rooms.getCurrentRoomOffset()
objectSprite.position = Vector2(
(
roomOffset.x
+ (object["offset"]["x"] * (object["scale"] if object.has("scale") else 1))
),
roomOffset.y + (object["offset"]["y"] * (object["scale"] if object.has("scale") else 1))
)
var coords = Vector2(object["x"], object["y"])
objectSprite.z_index = Utils.getTilePosAtCoords(coords).y
func loadEnvironment() -> void:
environmentItems.clear()
if Items.BACKGROUND_ENVIRONMENT.has(Rooms.currentRoomId):
for itemPosition in Items.BACKGROUND_ENVIRONMENT[Rooms.currentRoomId]:
var itemData = Items.BACKGROUND_ENVIRONMENT[Rooms.currentRoomId][itemPosition]
loadEnvironmentItem(itemPosition, itemData)
func loadEnvironmentItem(itemPosition: Vector2, itemData: Dictionary) -> void:
var itemSprite = Sprite.new()
itemSprite.texture = load(itemData["url"])
itemSprite.scale = Vector2(itemData["world_scale"], itemData["world_scale"])
itemSprite.rotation_degrees = itemData["rotation"]
$"%zObjects".add_child(itemSprite)
itemSprite.position = Utils.getTilePosAtCoords(itemPosition) + itemData["offset"]
itemSprite.z_index = itemSprite.position.y
environmentItems[itemPosition] = itemSprite
func removeEnvironmentItem(position: Vector2) -> void:
if environmentItems.has(position):
environmentItems[position].queue_free()
environmentItems.erase(position)
func replaceEnvironmentItem(position: Vector2, itemData: Dictionary) -> void:
removeEnvironmentItem(position)
loadEnvironmentItem(position, itemData)
func loadItems() -> void:
activeItems.clear()
if Items.ACTIVE_ITEMS.has(Rooms.currentRoomId):
for item in Items.ACTIVE_ITEMS[Rooms.currentRoomId]:
loadItem(item)
func loadItem(item: Dictionary) -> void:
var itemSprite = Sprite.new()
var itemData = Items.ITEMS[item["id"]]
#itemSprite.centered = false
itemSprite.texture = load(itemData["url"])
itemSprite.scale = Vector2(item["world_scale"], item["world_scale"])
itemSprite.rotation_degrees = item["rotation"]
$"%zObjects".add_child(itemSprite)
var coords = Vector2(item["x"], item["y"])
itemSprite.position = Utils.getTilePosAtCoords(coords)
itemSprite.z_index = itemSprite.position.y
activeItems[coords] = itemSprite
func loadNPCs() -> void:
activeNPCs.clear()
if NPCs.ACTIVE_NPCs.has(Rooms.currentRoomId):
for npcData in NPCs.ACTIVE_NPCs[Rooms.currentRoomId].values():
spawnNPCGiko(npcData)
func removeActiveItem(item: Dictionary) -> void:
var tile = Vector2(item["x"], item["y"])
if activeItems.has(tile):
activeItems[tile].queue_free()
activeItems.erase(tile)
func canInteract() -> bool:
if dialogueManager.isDialoguing:
dialogueManager.skipToNext()
return false
else:
return true
func talkToNPC(NPCId: String) -> void:
if (
NPCs.ACTIVE_NPCs[Rooms.currentRoomId][NPCId].has("lines")
&& NPCs.ACTIVE_NPCs[Rooms.currentRoomId][NPCId]["lines"].size() > 0
):
var qLine = NPCs.ACTIVE_NPCs[Rooms.currentRoomId][NPCId]["lines"].pop_front()
dialogueManager.setLineSet(qLine, NPCId)
dialogueManager.show()
if qLine["info"]["requeue"]:
NPCs.ACTIVE_NPCs[Rooms.currentRoomId][NPCId]["lines"].push_back(qLine)
elif NPCs.NPCs[NPCId].lines.size() > 0:
var dialogueLine = NPCs.NPCs[NPCId].lines.pop_front()
NPCs.NPCs[NPCId].lines.push_back(dialogueLine)
dialogueManager.setLineSet(dialogueLine, NPCId)
dialogueManager.show()
func talkToEnvironment(tile: Vector2) -> void:
if (
Items.ACTIVE_ENVIRONMENT.has(Rooms.currentRoomId)
&& Items.ACTIVE_ENVIRONMENT[Rooms.currentRoomId].has(tile)
):
dialogueManager.setLineSet(Items.ACTIVE_ENVIRONMENT[Rooms.currentRoomId][tile], "")
dialogueManager.show()
func spawnPlayerGiko(position, direction) -> void:
var newGiko = gikoPrefab.instance()
newGiko.set_script(PlayerGiko)
newGiko.character = Constants.Character.Giko
newGiko.displayName = State.NAME
## prepare callbacks
newGiko.changeRoom = funcref(self, "doorChangeRoom")
newGiko.removeActiveItem = funcref(self, "removeActiveItem")
newGiko.talkToNPC = funcref(self, "talkToNPC")
newGiko.talkToEnvironment = funcref(self, "talkToEnvironment")
newGiko.canInteract = funcref(self, "canInteract")
newGiko.place(position, direction)
$"%zObjects".add_child(newGiko)
playerGiko = newGiko
$Camera2D.player = playerGiko
#playerGiko.message("I miss Mona.")
func spawnNPCGiko(NPCData: Dictionary) -> Giko:
var newGiko = gikoPrefab.instance()
newGiko.set_script(Giko)
newGiko.initializeNPC(NPCData)
$"%zObjects".add_child(newGiko)
return newGiko
func spawnActionQueueGiko(NPCData: Dictionary) -> ActionQueueGiko:
var newGiko = gikoPrefab.instance()
newGiko.set_script(ActionQueueGiko)
newGiko.initializeNPC(NPCData)
$"%zObjects".add_child(newGiko)
return newGiko
func spawnRandomGiko(name: String, character: int, door = null, playSound = false) -> void:
var newGiko = gikoPrefab.instance()
newGiko.set_script(Giko)
newGiko.initializeRandom(name, character)
$"%zObjects".add_child(newGiko)
if door != null:
newGiko.place(
Vector2(
Rooms.currentRoomData["doors"][door]["x"], Rooms.currentRoomData["doors"][door]["y"]
),
Utils.roomDirectionToEnum(Rooms.currentRoomData["doors"][door]["direction"])
)
if playSound:
Audio.playFX(Audio.FX.Login)
currentRandomGikos[newGiko.get_instance_id()] = newGiko
func save() -> Dictionary:
return {
"ROOM": Rooms.currentRoomId,
"PLAYER_POSITION": Vector2(playerGiko.currentTile),
"PLAYER_DIRECTION": playerGiko.currentDirection
}
func load(save: Dictionary) -> void:
loadedSave = save