CharacterSelect.gd 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. extends Control
  2. @onready var cursor: TextureRect = %Cursor
  3. var selected_index := 0
  4. signal selected
  5. signal cancelled
  6. var active := false
  7. var player_id := 0
  8. var character_sprite_jsons := [
  9. "res://Assets/Sprites/Players/Mario/Small.json",
  10. "res://Assets/Sprites/Players/Luigi/Small.json",
  11. "res://Assets/Sprites/Players/Toad/Small.json",
  12. "res://Assets/Sprites/Players/Toadette/Small.json"
  13. ]
  14. func _process(_delta: float) -> void:
  15. if active:
  16. handle_input()
  17. func _ready() -> void:
  18. update_sprites()
  19. func get_custom_characters() -> void:
  20. Player.CHARACTERS = ["Mario", "Luigi", "Toad", "Toadette"]
  21. Player.CHARACTER_NAMES = ["CHAR_MARIO", "CHAR_LUIGI", "CHAR_TOAD", "CHAR_TOADETTE"]
  22. AudioManager.character_sfx_map.clear()
  23. var idx := 0
  24. for i in Player.CHARACTERS:
  25. var path = ResourceSetter.get_pure_resource_path("res://Assets/Sprites/Players/" + i + "/CharacterInfo.json")
  26. print(path)
  27. if FileAccess.file_exists(path):
  28. var json = JSON.parse_string(FileAccess.open(path, FileAccess.READ).get_as_text())
  29. Player.CHARACTER_NAMES[idx] = json.name
  30. path = ResourceSetter.get_pure_resource_path("res://Assets/Sprites/Players/" + i + "/CharacterColour.json")
  31. if FileAccess.file_exists(path):
  32. Player.CHARACTER_COLOURS[idx] = load(path)
  33. idx += 1
  34. print(Player.CHARACTER_NAMES)
  35. DirAccess.make_dir_recursive_absolute("user://custom_characters")
  36. for i in DirAccess.get_directories_at("user://custom_characters"):
  37. if FileAccess.file_exists("user://custom_characters/" + i + "/CharacterInfo.json"):
  38. var char_path = "user://custom_characters/" + i + "/"
  39. var json = JSON.parse_string(FileAccess.open(char_path + "CharacterInfo.json", FileAccess.READ).get_as_text())
  40. Player.CHARACTERS.append(i)
  41. Player.CHARACTER_NAMES.append(json.name)
  42. if FileAccess.file_exists(char_path + "CharacterColour.json"):
  43. Player.CHARACTER_COLOURS.append(load(char_path + "CharacterColour.json"))
  44. if FileAccess.file_exists(char_path + "LifeIcon.json"):
  45. GameHUD.character_icons.append(load(char_path + "LifeIcon.json"))
  46. if FileAccess.file_exists(char_path + "ColourPalette.json"):
  47. Player.CHARACTER_PALETTES.append(load(char_path + "ColourPalette.json"))
  48. if FileAccess.file_exists(char_path + "SFX.json"):
  49. AudioManager.character_sfx_map[i] = JSON.parse_string(FileAccess.open(char_path + "SFX.json", FileAccess.READ).get_as_text())
  50. func open() -> void:
  51. get_custom_characters()
  52. show()
  53. grab_focus()
  54. selected_index = int(Global.player_characters[player_id])
  55. update_sprites()
  56. await get_tree().physics_frame
  57. active = true
  58. func handle_input() -> void:
  59. if Input.is_action_just_pressed("ui_left"):
  60. selected_index = wrap(selected_index - 1, 0, Player.CHARACTERS.size())
  61. update_sprites()
  62. elif Input.is_action_just_pressed("ui_right"):
  63. selected_index = wrap(selected_index + 1, 0, Player.CHARACTERS.size())
  64. update_sprites()
  65. if Input.is_action_just_pressed("ui_accept"):
  66. Global.player_characters[player_id] = str(selected_index)
  67. var characters := Global.player_characters
  68. for i in characters:
  69. if int(i) > 3:
  70. characters = "0000"
  71. Settings.file.game.characters = characters
  72. Settings.save_settings()
  73. selected.emit()
  74. close()
  75. elif Input.is_action_just_pressed("ui_back"):
  76. close()
  77. cancelled.emit()
  78. func update_sprites() -> void:
  79. %Left.force_character = Player.CHARACTERS[wrap(selected_index - 1, 0, Player.CHARACTERS.size())]
  80. %Selected.force_character = Player.CHARACTERS[wrap(selected_index, 0, Player.CHARACTERS.size())]
  81. %Right.force_character = Player.CHARACTERS[wrap(selected_index + 1, 0, Player.CHARACTERS.size())]
  82. for i in [%Left, %Selected, %Right]:
  83. i.update()
  84. i.play("Pose" if i == %Selected else "FaceForward")
  85. %PlayerColourTexture.resource_json = Player.CHARACTER_COLOURS[selected_index]
  86. %CharacterName.text = tr(Player.CHARACTER_NAMES[selected_index])
  87. $Panel/MarginContainer/VBoxContainer/CharacterName/TextShadowColourChanger/ColourPaletteSampler.texture = %ColourPaletteSampler.texture
  88. $Panel/MarginContainer/VBoxContainer/CharacterName/TextShadowColourChanger.handle_shadow_colours()
  89. func select() -> void:
  90. selected.emit()
  91. hide()
  92. active = false
  93. func close() -> void:
  94. active = false
  95. hide()