/**
* Copyright 2019 The Knights Of Unity, created by Pawel Stolarczyk
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System.Collections.Generic;
using DemoGame.Scripts.Utils;
using UnityEngine;
namespace DemoGame.Scripts.DataStorage
{
///
/// Stores references to all user avatars and clan emblems.
///
public class AvatarManager : Singleton
{
#region IconLists
///
/// List of user avatars.
///
[SerializeField] private List _avatars = null;
///
/// List of clan emblems.
///
[SerializeField] private List _emblems = null;
#endregion
#region IteratingMethods
///
/// Returns the name of next avatar based on supplied avatar name.
/// If name was not found in list,
/// returns the last icon available.
///
public string NextAvatar(string current)
{
return NextIcon(current, _avatars);
}
///
/// Returns the name of next emblem based on supplied emblem name.
/// If name was not found in list,
/// returns the last icon available.
///
public string NextEmblem(string current)
{
return NextIcon(current, _emblems);
}
///
/// Returns the name of next icon based on supplied name.
/// If name was not found in ,
/// returns the last icon available.
///
public string NextIcon(string current, List iconList)
{
if (iconList.Count == 0)
{
Debug.LogError("Couldn't get next icon: No icons found");
return null;
}
if (string.IsNullOrEmpty(current) == true)
{
return iconList[0].Name;
}
for (int i = 0; i < iconList.Count; i++)
{
if (iconList[i].Name == current)
{
return iconList[(i + 1) % iconList.Count].Name;
}
}
Debug.Log("Current icon [" + current + "] not found. Returning first icon.");
return iconList[0].Name;
}
#endregion
#region RetrievingMethods
///
/// Searches for the avatar with given
/// and returns its sprite.
///
public Sprite LoadAvatar(string name)
{
return LoadIcon(name, _avatars);
}
///
/// Searches for the emblem with given
/// and returns its sprite.
///
public Sprite LoadEmblem(string name)
{
return LoadIcon(name, _emblems);
}
///
/// Searches supplied for the icon with given
/// and returns its sprite.
///
private Sprite LoadIcon(string name, List iconList)
{
if (iconList.Count == 0)
{
Debug.LogError("Couldn't load icon: No icons found.");
return null;
}
if (string.IsNullOrEmpty(name) == true)
{
return iconList[iconList.Count - 1].Sprite;
}
else
{
foreach (Icon avatar in iconList)
{
if (avatar.Name == name)
{
return avatar.Sprite;
}
}
Debug.LogError("Icon with name " + name + " not found");
return iconList[iconList.Count - 1].Sprite;
}
}
#endregion
}
}