World.gd 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. extends Spatial
  2. export (PackedScene) var lv #is null at the beginning
  3. export (PackedScene) var player
  4. #onready var camera_position = get_node("CameraPosition")
  5. var lv1
  6. var player1
  7. var has_game_started
  8. #FIXME: this is not the name, it is the path...
  9. var current_lv_name
  10. var current_spawn_name
  11. func _ready():
  12. pass
  13. func _process(delta):
  14. pass
  15. #print ("world is running")
  16. func set_level(name, spawn, save):
  17. #print ("world:set_level")
  18. current_lv_name = name
  19. current_spawn_name = spawn
  20. lv = load(name)
  21. if lv == null:
  22. #FIXME: better error handling, do this is global.gd - before entering the world scene
  23. print ("MapNotFoundError!")
  24. else:
  25. init_world(save)
  26. func init_world(save):
  27. has_game_started = false
  28. #FIXME: check if given spawn exists in the world BEFORE creating the world.
  29. create_world()
  30. spawn_player()
  31. #get_node("/root/Save").save_game()
  32. if save == true:
  33. print ("save!!!")
  34. get_node("/root/Save").save_node_data(self)
  35. func create_world():
  36. #lv1.queue_free()
  37. lv1 = lv.instance()
  38. add_child(lv1)
  39. var portals = get_tree().get_nodes_in_group("group_portal")
  40. for p in portals:
  41. #print (p)
  42. p.connect("on_level_change", self, "save_world_data")
  43. handle_spawners_on_load()
  44. handle_flowerspots_on_load()
  45. func spawn_player():
  46. var spawnName = "Spawn_01"
  47. if current_spawn_name != null:
  48. spawnName = current_spawn_name
  49. else:
  50. current_spawn_name = spawnName
  51. if lv1 != null:
  52. #print("spawn: " + str(spawnName))
  53. var spawnNode = lv1.find_node(spawnName, true, false)
  54. if spawnNode != null:
  55. player1 = player.instance()
  56. player1.translation = spawnNode.translation
  57. player1.rotate_y(spawnNode.rotation.y)
  58. #camera_position.translation = player1.translation
  59. add_child(player1)
  60. else:
  61. print("level has no spawn")
  62. else:
  63. print("lv1 not loaded")
  64. func save():
  65. var save_dict = {
  66. level = current_lv_name,
  67. spawn = current_spawn_name
  68. }
  69. return save_dict
  70. func clear_data():
  71. print ("clear_data")
  72. var spawners = get_tree().get_nodes_in_group("group_spawner")
  73. for s in spawners:
  74. s.queue_free ( )
  75. var spots = get_tree().get_nodes_in_group("group_flowerspot")
  76. for s in spots:
  77. s.queue_free ( )
  78. func reset_world():
  79. if lv1 != null:
  80. #print("respawn: " + str(current_spawn_name))
  81. var spawnNode = lv1.find_node(current_spawn_name, true, false)
  82. if spawnNode != null:
  83. player1.velocity = Vector3(0, 0, 0)
  84. player1.translation = spawnNode.translation
  85. player1.rotation_degrees = Vector3(90, 180, 180)
  86. player1.rotate_y(spawnNode.rotation.y)
  87. var collectorNode = player1.find_node("PlayerInventory", true, false)
  88. if collectorNode != null:
  89. collectorNode.reload()
  90. var spawners = get_tree().get_nodes_in_group("group_spawner")
  91. for s in spawners:
  92. if (s.spawned_object != null):
  93. var wr = weakref(s.spawned_object)
  94. if (wr.get_ref()):
  95. wr.get_ref().queue_free ( )
  96. handle_spawners_on_load()
  97. handle_flowerspots_on_load()
  98. func save_world_data():
  99. #print ("world.gd::save_world_data called")
  100. var wsdNode = find_node("WorldSaveData", true, false)
  101. if wsdNode != null:
  102. wsdNode.save_world_data(str(current_lv_name).get_base_dir ( ).replace("res://Scenes/Maps/", ""))
  103. #res://Scenes/Maps/
  104. #FIXME: get savedata: fix double / copied code
  105. #FIXME: why savedata_values[1] - better by name than by number
  106. func handle_spawners_on_load():
  107. var node_name = str(current_lv_name).get_base_dir ( ).replace("res://Scenes/Maps/", "") + "WorldSaveData"
  108. var savedata = get_node("/root/Save").get_node_data_by_name(node_name)
  109. if savedata != null:
  110. var savedata_values = savedata.values()[0].values()
  111. print (savedata_values[0])
  112. var spawners = get_tree().get_nodes_in_group("group_spawner")
  113. for s in spawners:
  114. if (savedata_values[0].find(str(s.name)) >= 0):
  115. s.is_active = false
  116. var spawners = get_tree().get_nodes_in_group("group_spawner")
  117. for s in spawners:
  118. s.spawn()
  119. func handle_flowerspots_on_load():
  120. print ("handle_flowerspots_on_load")
  121. var node_name = str(current_lv_name).get_base_dir ( ).replace("res://Scenes/Maps/", "") + "WorldSaveData"
  122. var savedata = get_node("/root/Save").get_node_data_by_name(node_name)
  123. #print (savedata)
  124. var spots = get_tree().get_nodes_in_group("group_flowerspot")
  125. if savedata != null:
  126. var savedata_values = savedata.values()[0].values()
  127. #print (savedata_values[1])
  128. for s in spots:
  129. for sv in savedata_values[1]:
  130. if s.name == sv[0] and not s.is_queued_for_deletion():
  131. #print (sv[0])
  132. #print (sv[1])
  133. s.set_status(sv[1])
  134. print (s.name)
  135. var fStatus = find_node("FlowerspotStatus", true, false)
  136. if fStatus != null:
  137. fStatus.on_load()