SamsonGame/Assets/Scripts/Game/Save/PlayerProgress.cs

106 lines
3.2 KiB
C#
Raw Permalink Normal View History

2021-12-29 20:50:11 +03:00
using game;
using UniRx;
using System.Collections.Generic;
namespace RND
{
public class PlayerProgress : IPersistent
{
public readonly ReactiveProperty<bool> TutorialIsOver;
public LevelReward LevelReward { get; private set; }
public int Level => (int) _completed;
public uint LevelId => _sequence[(int) _positionInSequence];
private uint _completed = 0;
private uint _positionInSequence = 0;
private List<uint> _sequence = null;
public PlayerProgress()
{
TutorialIsOver = new ReactiveProperty<bool>(GameSettings.Levels.HasTutorial == false);
}
public void IncreaseLevel()
{
#if UNITY_EDITOR
if (GameSettings.ProgressionIsFrozen)
return;
#endif
_completed++;
if (TutorialIsOver.Value)
{
bool sequenceIsOver = _positionInSequence + 1 >= _sequence.Count;
if (sequenceIsOver)
_sequence = SequenceBuilder.BuildByPattern(GameSettings.Levels.GetRandomPattern());
_positionInSequence = sequenceIsOver ? 0 : _positionInSequence + 1;
}
else
{
int tutorialLevelsCount = GameSettings.Levels.TutorialLenght;
bool nextLevelIsTutorial = _completed < tutorialLevelsCount;
if (nextLevelIsTutorial == false)
{
IList<LevelData> tutorial = GameSettings.Levels.GetBy(l => l.Type == EnumLevelType.TUTORIAL);
tutorial.ForEach(level => _sequence.Remove(level.ID));
_positionInSequence = 0;
TutorialIsOver.Value = true;
}
else
{
_positionInSequence++;
}
}
}
public void Load(DataGame data)
{
_completed = data.progress.completed;
_sequence = data.progress.sequence;
if (SequenceMustBeDefault())
{
_sequence = TutorialIsOver.Value
? SequenceBuilder.BuildDefault()
: SequenceBuilder.BuildDefaultWithTutorial();
_positionInSequence = (uint) ((_completed - GameSettings.Levels.TutorialLenght) % _sequence.Count);
}
else
{
if (data.progress.position >= _sequence.Count)
{
_positionInSequence = 0;
_sequence = SequenceBuilder.BuildByPattern(GameSettings.Levels.GetRandomPattern());
}
else
{
_positionInSequence = data.progress.position;
}
}
LevelReward = new LevelReward(data);
}
private bool SequenceMustBeDefault()
{
return _completed < GameSettings.Levels.DefaultSequence.Count
+ GameSettings.Levels.TutorialLenght;
}
public void Save(DataGame data)
{
data.progress.completed = _completed;
data.progress.sequence = _sequence;
data.progress.position = _positionInSequence;
LevelReward.Save(data);
}
}
}