Skip to content

Commit

Permalink
Fitness function selection
Browse files Browse the repository at this point in the history
The fitness function can now be selected from the options menu. Also,
any settings on the options menu are automatically filled in with the
currently used value.
  • Loading branch information
Thomas Fisher committed May 11, 2018
1 parent 7a8eacc commit 19b907a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 14 deletions.
Binary file modified .vs/EvolutionaryGame/v15/Server/sqlite3/storage.ide
Binary file not shown.
Binary file modified Assets/Scenes/Options.unity
Binary file not shown.
11 changes: 10 additions & 1 deletion Assets/Scripts/EvolutionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,16 @@ void UpdateFitnessScores(List<Individual> population)
{
Individual currentIndividual = population[i];

float currentFitness = currentIndividual.CalculateFitness(Individual.ACCURATE_LONG_LIFE_FUNC);
int selectedFitnessFunc = (int)PlayerPrefs.GetFloat("FitnessFunc");

if (selectedFitnessFunc < 0 || selectedFitnessFunc > (Individual.NB_OF_FUNCTIONS - 1))
{
selectedFitnessFunc = Individual.DEFAULT_FUNC;
}

Debug.Log("Updating fitness scores using fitness function #" + selectedFitnessFunc);

float currentFitness = currentIndividual.CalculateFitness(selectedFitnessFunc);

if (currentFitness > bestFitnessScore)
{
Expand Down
3 changes: 3 additions & 0 deletions Assets/Scripts/GameSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ public class GameSettings {
public int vSync;
public int resolutionIndex;
public bool fullscreen;

public float globalVolume;

public int fitnessFunc;
}
2 changes: 2 additions & 0 deletions Assets/Scripts/Individual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class Individual
public const float MUTATION_MULT = 0.1f;

public const int LIFETIME_FUNC = 0, SURVIVAL_FUNC = 1, ACCURACY_FUNC = 2, NB_ON_TARGET_FUNC = 3, ACCURATE_LONG_LIFE_FUNC = 4;
public const int DEFAULT_FUNC = ACCURATE_LONG_LIFE_FUNC;
public const int NB_OF_FUNCTIONS = 5;
#endregion
#region Properties
private DodgeAI dodgeAI;
Expand Down
11 changes: 9 additions & 2 deletions Assets/Scripts/SceneLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
using UnityEngine.SceneManagement;

public class SceneLoader : MonoBehaviour {
public const int GAME = 1;
public const int MAIN_MENU = 0, GAME = 1, CREDITS = 2, OPTIONS = 3;

private int currentIdx;
private static int prevIdx = 0;

Expand All @@ -24,6 +25,9 @@ private void Start()
}
}

/// <summary>
/// Check for navigation button input
/// </summary>
private void Update()
{
if (Input.GetButton("Cancel"))
Expand All @@ -33,7 +37,10 @@ private void Update()

if (Input.GetButton("Submit"))
{
LoadScene(GAME);
if (currentIdx == MAIN_MENU)
{
LoadScene(GAME);
}
}
}

Expand Down
30 changes: 19 additions & 11 deletions Assets/Scripts/SettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,14 @@ public class SettingsManager : MonoBehaviour {
public Dropdown resolutionDropdown;
public Dropdown antiAliasingDropdown;
public Dropdown vSyncDropdown;
public Dropdown fitnessFuncDropdown;
public Slider volumeSlider;

public Resolution[] resolutions;
public GameSettings gameSettings;

public AudioSource audioSrc;

/// <summary>
/// Initialize properties that should be set during run-time.
/// Set the default values of UI components.
/// </summary>
private void Start()
{
volumeSlider.value = PlayerPrefs.GetFloat("MasterVol");
}

/// <summary>
/// Initialize the values of options UI components.
/// Sets up listeners for UI components.
Expand All @@ -38,15 +30,30 @@ void OnEnable () {
antiAliasingDropdown.onValueChanged.AddListener(delegate { OnAntiAliasingChange(); });
vSyncDropdown.onValueChanged.AddListener(delegate { OnVSyncChange(); });
volumeSlider.onValueChanged.AddListener(delegate { OnVolumeChange(); });

fitnessFuncDropdown.onValueChanged.AddListener(delegate { OnFitnessFuncChange(); });
resolutions = Screen.resolutions;

foreach(Resolution resolution in resolutions)
{
resolutionDropdown.options.Add(new Dropdown.OptionData(resolution.ToString()));
}
}

volumeSlider.value = PlayerPrefs.GetFloat("MasterVol");
fitnessFuncDropdown.value = (int)PlayerPrefs.GetFloat("FitnessFunc");

fullscreenToggle.isOn = Screen.fullScreen;
textureQualityDropdown.value = QualitySettings.masterTextureLimit;
antiAliasingDropdown.value = QualitySettings.antiAliasing;
vSyncDropdown.value = QualitySettings.vSyncCount;
}

public void OnFitnessFuncChange()
{
gameSettings.fitnessFunc = fitnessFuncDropdown.value;
PlayerPrefs.SetFloat("FitnessFunc", gameSettings.fitnessFunc);
PlayerPrefs.Save();
}

/// <summary>
/// Toggle fullscreen mode.
/// </summary>
Expand All @@ -60,6 +67,7 @@ public void OnFullscreenToggle()
/// </summary>
public void OnResolutionChange()
{
gameSettings.resolutionIndex = resolutionDropdown.value;
Screen.SetResolution(resolutions[resolutionDropdown.value].width, resolutions[resolutionDropdown.value].height, Screen.fullScreen);
}

Expand Down

0 comments on commit 19b907a

Please sign in to comment.