Skip to content

Commit

Permalink
refactor LevelData logic and LevelUI
Browse files Browse the repository at this point in the history
  • Loading branch information
Lincoln-Ab authored and simone-lungarella committed Dec 15, 2024
1 parent 0717a02 commit d543a0a
Show file tree
Hide file tree
Showing 13 changed files with 1,400 additions and 1,519 deletions.
2,552 changes: 1,272 additions & 1,280 deletions assets/resources/levels/persistent_levels.tres

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions packed_scene/scene_2d/GameManager.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ script = ExtResource("1_tgd8e")
palette = ExtResource("2_wd6nv")
slider_collection = ExtResource("3_lx543")
tutorials = {
"01": ExtResource("4_m4vjt"),
"03": ExtResource("5_0l1wn"),
"07": ExtResource("6_n722h"),
"10": ExtResource("5_tb6dc"),
"17": ExtResource("8_rylgv"),
"27": ExtResource("9_kkyll")
0: ExtResource("4_m4vjt"),
2: ExtResource("5_0l1wn"),
6: ExtResource("6_n722h"),
9: ExtResource("5_tb6dc"),
16: ExtResource("8_rylgv"),
26: ExtResource("9_kkyll")
}
89 changes: 38 additions & 51 deletions scripts/scene_2d/game_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ var is_tutorial_visible: bool = true

var _player_save: PlayerSave
var _persistent_save: LevelContainer
var _active_level_name: String
var _next_level_name: String
var _active_level_id: int
var _next_level_id: int
var _context: GlobalConst.LevelGroup


Expand All @@ -57,8 +57,8 @@ func change_state(new_state: GlobalConst.GameState) -> void:
on_state_change.emit(new_state)

if new_state == GlobalConst.GameState.LEVEL_START:
if is_tutorial_visible and tutorials.has(_active_level_name):
var tutorial: TutorialData = tutorials.get(_active_level_name)
if is_tutorial_visible and tutorials.has(_active_level_id):
var tutorial: TutorialData = tutorials.get(_active_level_id)
var tutorial_ui: Tutorial = TUTORIAL.instantiate()
get_tree().root.add_child(tutorial_ui)
tutorial_ui.setup.call_deferred(tutorial)
Expand All @@ -78,10 +78,6 @@ func _set_and_check_save_integrity() -> bool:
# inconsistent counting between levels and progress
if _persistent_save.levels.size() != savegame.persistent_progress.size():
return false
# inconsistent naming between levels and progress
for level_name in _persistent_save.levels.keys():
if !savegame.persistent_progress.has(level_name):
return false
# set verified savegame
_player_save = savegame
return true
Expand All @@ -92,33 +88,32 @@ func set_levels_context(level_group: GlobalConst.LevelGroup) -> void:


func get_start_level_playable() -> LevelData:
_context = GlobalConst.LevelGroup.MAIN
if !_persistent_save.levels.is_empty():
set_levels_context(GlobalConst.LevelGroup.MAIN)
if !_persistent_save.is_empty():
# get the first level available
_active_level_name = _persistent_save.get_level_by_index(0)
_active_level_id = 0
# get the first level unlocked and not completed
for level_name in _persistent_save.levels_order:
var progress := _player_save.persistent_progress.get(level_name) as LevelProgress
for id in range(_persistent_save.levels.size()):
var progress := _player_save.persistent_progress[id] as LevelProgress
if progress.is_unlocked and !progress.is_completed:
_active_level_name = level_name
_active_level_id = id
break
set_levels_context(GlobalConst.LevelGroup.MAIN)
return get_active_level(_active_level_name)
return get_active_level(_active_level_id)

push_error("Nessun livello nel persistent save!")
return null


func save_persistent_level(level_name: String, level_data: LevelData) -> void:
_persistent_save.set_level(level_name, level_data.duplicate())
_player_save.reset_progress(GlobalConst.LevelGroup.MAIN, level_name)
func save_persistent_level(level_data: LevelData) -> void:
_persistent_save.add_level(level_data.duplicate())
_player_save.add_progress(GlobalConst.LevelGroup.MAIN, level_data.name)
ResourceSaver.save.call_deferred(_persistent_save, PERSISTENT_SAVE_PATH)
ResourceSaver.save.call_deferred(_player_save, PLAYER_SAVE_PATH)


func save_custom_level(level_name: String, level_data: LevelData) -> void:
_player_save.custom_levels.set_level(level_name, level_data.duplicate())
_player_save.reset_progress(GlobalConst.LevelGroup.CUSTOM, level_name)
func save_custom_level(level_data: LevelData) -> void:
_player_save.custom_levels.add_level(level_data.duplicate())
_player_save.add_progress(GlobalConst.LevelGroup.CUSTOM, level_data.name)
ResourceSaver.save.call_deferred(_player_save, PLAYER_SAVE_PATH)


Expand All @@ -131,29 +126,29 @@ func _get_levels() -> LevelContainer:
return null


func _get_progress() -> Dictionary:
func _get_progress() -> Array[LevelProgress]:
match _context:
GlobalConst.LevelGroup.CUSTOM:
return _player_save.custom_progress
GlobalConst.LevelGroup.MAIN:
return _player_save.persistent_progress
return {}
return []


func set_next_level() -> bool:
var is_valid_level: bool
_next_level_name = _get_levels().get_next_level(_active_level_name)
is_valid_level = _next_level_name != ""
_next_level_id = _active_level_id + 1
is_valid_level = _get_levels().levels.size() > _next_level_id
if is_valid_level:
_player_save.unlock_level(_context, _next_level_name)
_player_save.unlock_level(_context, _next_level_id)
return is_valid_level


func get_active_level(level_name: String = "") -> LevelData:
if level_name != "":
_active_level_name = level_name
var data := _get_levels().levels.get(_active_level_name) as LevelData
return data
func get_active_level(level_id: int = -1) -> LevelData:
if level_id > -1:
_active_level_id = level_id
return _get_levels().levels[_active_level_id] as LevelData



func set_level_scale(level_width: int, level_height: int) -> void:
Expand All @@ -165,14 +160,14 @@ func set_level_scale(level_width: int, level_height: int) -> void:


func get_next_level() -> LevelData:
_active_level_name = _next_level_name
_active_level_id = _next_level_id
return get_active_level()


func update_level_progress(move_left: int) -> bool:
var active_progress: LevelProgress
var is_record: bool
active_progress = _get_progress().get(_active_level_name)
active_progress = _get_progress()[_active_level_id]
if !active_progress.is_completed != (move_left > active_progress.move_left):
active_progress.move_left = move_left
is_record = true
Expand All @@ -182,21 +177,13 @@ func update_level_progress(move_left: int) -> bool:
return is_record


func get_page_levels(group: GlobalConst.LevelGroup, first: int, last: int) -> Dictionary:
var levels_in_page: Dictionary
var temp_levels: LevelContainer
var temp_progress: Dictionary
func get_page_levels(group: GlobalConst.LevelGroup, first: int, last: int) -> Array[LevelProgress]:
match group:
GlobalConst.LevelGroup.CUSTOM:
temp_levels = _player_save.custom_levels
temp_progress = _player_save.custom_progress
return _player_save.custom_progress.slice(first - 1, last + 1)
GlobalConst.LevelGroup.MAIN:
temp_levels = _persistent_save
temp_progress = _player_save.persistent_progress
if temp_levels != null:
for level_name in temp_levels.get_levels_group_by_index(first - 1, last):
levels_in_page[level_name] = temp_progress.get(level_name)
return levels_in_page
return _player_save.persistent_progress.slice(first - 1, last + 1)
return []


func get_num_levels(group: GlobalConst.LevelGroup) -> int:
Expand All @@ -210,16 +197,16 @@ func get_num_levels(group: GlobalConst.LevelGroup) -> int:


func is_level_completed() -> bool:
return _get_progress().get(_active_level_name).is_completed
return _get_progress()[_active_level_id].is_completed


func unlock_level(group: GlobalConst.LevelGroup, level_name: String) -> void:
_player_save.unlock_level(group, level_name)
func unlock_level(group: GlobalConst.LevelGroup, level_id: int) -> void:
_player_save.unlock_level(group, level_id)
ResourceSaver.save.call_deferred(_player_save, PLAYER_SAVE_PATH)


func delete_level(level_name: String) -> void:
_player_save.delete_level(level_name)
func delete_level(level_id: int) -> void:
_player_save.delete_level(level_id)
ResourceSaver.save.call_deferred(_player_save, PLAYER_SAVE_PATH)


Expand Down
15 changes: 6 additions & 9 deletions scripts/scene_2d/level_builder.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const BUILDER_SAVE = preload("res://packed_scene/user_interface/BuilderSave.tscn
const PANEL = preload("res://assets/resources/themes/panel.tres")
const BUILDER_SELECTION = preload("res://packed_scene/user_interface/BuilderSelection.tscn")

var _level_name: String
var _level_data: LevelData
var _cell_collection: Dictionary
var _slider_collection: Dictionary
Expand Down Expand Up @@ -49,9 +48,9 @@ func _on_state_change(new_state: GlobalConst.GameState) -> void:
get_tree().root.add_child.call_deferred(builder_save)
builder_save.on_query_close.connect(_on_save_query_received)
GameManager.builder_save = builder_save
if _level_name != "":
if _level_data.name != "":
var moves := String.num_int64(_level_data.moves_left)
GameManager.builder_save.initialize_info.call_deferred(_level_name, moves)
GameManager.builder_save.initialize_info.call_deferred(_level_data.name, moves)

GlobalConst.GameState.BUILDER_RESIZE:
self.visible = true
Expand Down Expand Up @@ -101,11 +100,9 @@ func _on_state_change(new_state: GlobalConst.GameState) -> void:
self.visible = false


func construct_level(level_data: LevelData = null, level_name := "") -> void:
func construct_level(level_data: LevelData = null) -> void:
if level_data != null:
_level_data = level_data
if level_name != "":
_level_name = level_name

GameManager.set_level_scale(_level_data.width, _level_data.height)
var half_grid := Vector2(_level_data.width, _level_data.height) * GlobalConst.CELL_SIZE / 2
Expand Down Expand Up @@ -255,11 +252,11 @@ func _on_slider_change(ref: BuilderSlider, data: SliderData) -> void:

func _on_save_query_received(is_persistent_level: bool, level_name: String, level_moves: int):
_level_data.moves_left = level_moves
_level_name = level_name
_level_data.name = level_name
if is_persistent_level:
GameManager.save_persistent_level(_level_name, _level_data)
GameManager.save_persistent_level(_level_data)
else:
GameManager.save_custom_level(_level_name, _level_data)
GameManager.save_custom_level(_level_data)
_reset_builder_grid.call_deferred()


Expand Down
18 changes: 9 additions & 9 deletions scripts/user_interface/custom_level_inspect.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const LEVEL_MANAGER = preload("res://packed_scene/scene_2d/LevelManager.tscn")
const GAME_UI = preload("res://packed_scene/user_interface/GameUI.tscn")
const PASTE_CHECK_ICON = preload("res://assets/ui/paste_check_icon.png")

var _level_name: String
var _level_id: int
var _level_code: String

@onready var label: Label = %LevelName
Expand All @@ -23,9 +23,9 @@ func _ready() -> void:
GameManager.on_state_change.connect(_on_state_change)


func init_inspector(level_name: String, progress: LevelProgress):
_level_name = level_name
label.text = _level_name
func init_inspector(level_id: int, progress: LevelProgress):
label.text = progress.name
_level_id = level_id

var percentage: float
if progress.is_completed:
Expand All @@ -36,7 +36,7 @@ func init_inspector(level_name: String, progress: LevelProgress):
level_score_img.material.set_shader_parameter("percentage", percentage)

GameManager.set_levels_context(GlobalConst.LevelGroup.CUSTOM)
var level_data: LevelData = GameManager.get_active_level(_level_name)
var level_data: LevelData = GameManager.get_active_level(_level_id)
_level_code = Encoder.encode(level_data)
copy_btn.text = " " + _level_code

Expand All @@ -54,8 +54,8 @@ func _on_build_btn_pressed() -> void:
GameManager.level_builder = level_builder

GameManager.set_levels_context(GlobalConst.LevelGroup.CUSTOM)
var level_data: LevelData = GameManager.get_active_level(_level_name)
level_builder.construct_level.call_deferred(level_data.duplicate(), _level_name)
var level_data: LevelData = GameManager.get_active_level(_level_id)
level_builder.construct_level.call_deferred(level_data.duplicate())

GameManager.change_state.call_deferred(GlobalConst.GameState.BUILDER_IDLE)

Expand All @@ -74,7 +74,7 @@ func _on_play_btn_pressed() -> void:
GameManager.level_manager = level_manager

GameManager.set_levels_context(GlobalConst.LevelGroup.CUSTOM)
var level_data: LevelData = GameManager.get_active_level(_level_name)
var level_data: LevelData = GameManager.get_active_level(_level_id)
level_manager.init_level.call_deferred(level_data)


Expand Down Expand Up @@ -107,7 +107,7 @@ func _on_copy_btn_pressed() -> void:

func _on_delete_btn_pressed() -> void:
AudioManager.play_click_sound()
GameManager.delete_level(_level_name)
GameManager.delete_level(_level_id)
level_deleted.emit()
GameManager.change_state(GlobalConst.GameState.LEVEL_PICK)

Expand Down
28 changes: 23 additions & 5 deletions scripts/user_interface/level_button.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const NORMAL_STYLE: StyleBoxFlat = preload("res://assets/resources/themes/level_
const HOVER_STYLE: StyleBoxFlat = preload("res://assets/resources/themes/level_button_hover.tres")
const LEVEL_INSPECT = preload("res://packed_scene/user_interface/LevelInspect.tscn")
const CUSTOM_LEVEL_INSPECT = preload("res://packed_scene/user_interface/CustomLevelInspect.tscn")
const THEME = preload("res://assets/resources/themes/default.tres")

# Icons
const LOCK_ICON: CompressedTexture2D = preload("res://assets/ui/lock_level_icon.png")
Expand All @@ -15,7 +16,7 @@ const ONE_STAR = preload("res://assets/ui/one_star.png")
const TWO_STARS = preload("res://assets/ui/two_stars.png")
const THREE_STARS = preload("res://assets/ui/three_stars.png")

var _level_name: String
var _level_id: int
var _progress: LevelProgress
var _level_group: GlobalConst.LevelGroup

Expand Down Expand Up @@ -45,7 +46,7 @@ func _pressed() -> void:
GameManager.level_inspect = inspect_instance

_toggle_connection.call_deferred(true)
GameManager.level_inspect.init_inspector.call_deferred(_level_name, _progress)
GameManager.level_inspect.init_inspector.call_deferred(_level_id, _progress)
GameManager.change_state.call_deferred(GlobalConst.GameState.LEVEL_INSPECT)

GlobalConst.LevelGroup.CUSTOM:
Expand All @@ -55,15 +56,15 @@ func _pressed() -> void:
GameManager.custom_inspect = inspect_instance

_toggle_connection.call_deferred(true)
GameManager.custom_inspect.init_inspector.call_deferred(_level_name, _progress)
GameManager.custom_inspect.init_inspector.call_deferred(_level_id, _progress)
GameManager.change_state.call_deferred(GlobalConst.GameState.CUSTOM_LEVEL_INSPECT)


func _get_minimum_size() -> Vector2:
return Vector2(128, 128)


func construct(level_name: String, progress: LevelProgress, group: GlobalConst.LevelGroup) -> void:
func construct(level_id: int, progress: LevelProgress, group: GlobalConst.LevelGroup) -> void:
if !progress.is_unlocked:
icon = LOCK_ICON
elif progress.is_completed:
Expand All @@ -72,10 +73,26 @@ func construct(level_name: String, progress: LevelProgress, group: GlobalConst.L
else:
icon = ZERO_STARS

_level_name = level_name
_level_id = level_id
_progress = progress
_level_group = group
if get_child_count() > 0:
get_child(0).text = str(level_id + 1)
else:
add_child(_create_label(level_id + 1))
get_child(0).visible = progress.is_unlocked


func _create_label(index: int) -> Label:
var lab: Label = Label.new()
lab.text = str(index)

lab.vertical_alignment = VerticalAlignment.VERTICAL_ALIGNMENT_CENTER
lab.horizontal_alignment = HorizontalAlignment.HORIZONTAL_ALIGNMENT_CENTER
lab.theme = THEME
lab.set_anchors_preset(PRESET_FULL_RECT)
return lab


func _count_stars(moves_left: int) -> int:
# Less than -2 moves is equal to 0 stars. Zero or more moves are equivalent to 3 stars
Expand All @@ -84,6 +101,7 @@ func _count_stars(moves_left: int) -> int:

func _unlock_level() -> void:
icon = ZERO_STARS
get_child(0).show()


func _delete_level_button() -> void:
Expand Down
Loading

0 comments on commit d543a0a

Please sign in to comment.