Skip to content

Commit 978f349

Browse files
committed
Remove Enemy SearchSpace script
1 parent 58254c5 commit 978f349

File tree

10 files changed

+62
-154
lines changed

10 files changed

+62
-154
lines changed

Assets/Scripts/Game/EnemyGenerator/EnemyGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private void Evolution()
4949
while (pop.Count() < _parameters.initialPopulationSize)
5050
{
5151
Individual ind = Individual.GetRandom(_searchSpace);
52-
//Difficulty.Calculate(ref ind);
52+
_fitnessFunction.SetSearchSpace(_searchSpace);
5353
_fitnessFunction.Calculate(ref ind, _parameters.difficulty);
5454
pop.PlaceIndividual(ind);
5555
}

Assets/Scripts/Game/EnemyGenerator/EnemyGeneratorManager.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ private void SetGeneticAlgorithmSettings(DifficultyLevels difficultyLevels)
5656
_geneticSettings.numberOfWeapons = _searchSpaceConfig.WeaponSet.GetEnemyWeaponCount();
5757
_geneticSettings.difficulty = EnemyDifficultyFactor.GetDifficultyFactor(difficultyLevels);
5858
_fitnessFunction = new TopdownGame.Overlord.Inheritance.RulesGenerator.TopdownFitness();
59-
6059
}
6160

6261
private void EvolveEnemies()

Assets/Scripts/Game/EnemyGenerator/IEnemyFitness.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
public interface IEnemyFitness
77
{
8+
void SetSearchSpace(SearchSpaceConfig searchSpace);
89
void Calculate(ref Individual _individual, float goal);
910
bool IsBest(Individual _i1, Individual _i2);
1011
}

Assets/Scripts/Game/EnemyGenerator/Mutation.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Util;
22
using System;
3+
using Codice.Client.Common;
34

45
namespace Game.EnemyGenerator
56
{
@@ -48,9 +49,13 @@ public static Individual Apply(Individual parent, int chance, SearchSpaceConfig
4849
}
4950
// Apply mutation on weapon attributes
5051
var weapon = individual.Weapon;
51-
if (chance > RandomSingleton.GetInstance().RandomPercent())
52+
var test = RandomSingleton.GetInstance().RandomPercent();
53+
if (chance > test)
54+
{
55+
}
56+
if (chance > test)
5257
{
53-
weapon.Weapon = RandomSingleton.GetInstance().RandomElementFromArray(SearchSpace.Instance.rWeaponType);
58+
weapon.Weapon = RandomSingleton.GetInstance().RandomElementFromList<Enum>(searchSpace.WeaponSet.GetAllWeaponTypes());
5459
}
5560
if (chance > RandomSingleton.GetInstance().RandomPercent())
5661
{

Assets/Scripts/Game/EnemyGenerator/Population.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ public List<Individual> ToList()
107107
}
108108

109109
/// Print all the individuals of the MAP-Elites population.
110+
/*
111+
public enum WeaponType
112+
{
113+
Barehand, // Enemy attacks the player with barehands (Melee).
114+
Sword, // Enemy uses a short sword to damage the player (Melee).
115+
Bow, // Enemy shots projectiles towards the player (Range).
116+
BombThrower, // Enemy shots bombs towards the player (Range).
117+
Shield, // Enemy uses a shield to defend itself (Defense).
118+
CureSpell, // Enemy uses magic to cure other enemies (Defense).
119+
}
110120
public void Debug()
111121
{
112122
for (int m = 0; m < dimension.movement; m++)
@@ -129,7 +139,7 @@ public void Debug()
129139
}
130140
}
131141
}
132-
142+
*/
133143
public int NIndividualsBetterThan(int amount, float acceptableFitness)
134144
{
135145
var betterThanNCounter = 0;

Assets/Scripts/Game/EnemyGenerator/Representation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static Individual GetRandom(SearchSpaceConfig searchSpace)
8484
var restTime = RandomSingleton.GetInstance().Next(minFloat, maxFloat);
8585
EnemyData e = new EnemyData(health, strength, attackSpeed, movementType, movementSpeed, activeTime, restTime);
8686
// Create a random weapon
87-
var weaponType = RandomSingleton.GetInstance().RandomElementFromArray(SearchSpace.Instance.rWeaponType);
87+
var weaponType = RandomSingleton.GetInstance().RandomElementFromList<Enum>(searchSpace.WeaponSet.GetAllWeaponTypes());
8888
(minFloat, maxFloat) = (searchSpace.WeaponStatus1.Min, searchSpace.WeaponStatus1.Max);
8989
var projectileSpeed = RandomSingleton.GetInstance().Next(minFloat, maxFloat);
9090
WeaponData w = new WeaponData(weaponType, projectileSpeed);
@@ -134,12 +134,12 @@ float restTime
134134
[Serializable]
135135
public struct WeaponData
136136
{
137-
public WeaponType Weapon { get; set; }
137+
public Enum Weapon { get; set; }
138138
public float WeaponStatus1 { get; set; } // Old name: ProjectileSpeed
139139

140140
/// Weapon constructor.
141141
public WeaponData(
142-
WeaponType weapon,
142+
Enum weapon,
143143
float projectileSpeed
144144
)
145145
{

Assets/Scripts/Game/EnemyGenerator/SearchSpace.cs

Lines changed: 0 additions & 122 deletions
This file was deleted.

Assets/Scripts/Game/EnemyGenerator/SearchSpace.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

Assets/Scripts/Game/EnemyGenerator/TopdownFitness.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using Game.EnemyGenerator;
22
using Overlord.GenerationController.Facade;
33
using System;
4+
using System.Linq;
45
using UnityEngine;
6+
using static Codice.Client.Common.Connection.AskCredentialsToUser;
57

68
namespace TopdownGame.Overlord.Inheritance.RulesGenerator
79
{
@@ -12,6 +14,13 @@ public class TopdownFitness: IEnemyFitness
1214
public readonly string CANNOT_COMPARE_INDIVIDUALS =
1315
"There is no way of comparing two null individuals.";
1416

17+
private SearchSpaceConfig _searchSpace;
18+
19+
public void SetSearchSpace(SearchSpaceConfig searchSpace)
20+
{
21+
_searchSpace = searchSpace;
22+
}
23+
1524
/// Calculate the fitness value of the entered individual.
1625
///
1726
/// An individual's fitness is defined by the distance of the
@@ -93,16 +102,19 @@ private float CalculateStrengthFactor(Individual _individual)
93102
float fS = 1;
94103
// Melee enemies attack by touching the player, therefore, the
95104
// movement speed increase their strenght
96-
fS *= SearchSpace.MeleeWeaponList().Contains(w.Weapon) ?
105+
fS *= _searchSpace.WeaponSet.GetMeleeWeaponTypes().Contains(w.Weapon) ?
97106
e.Status2 * e.Status4 : 1;
98107
// Shooter enemies attack by throwing projectiles, then we count
99108
// both attack speed (shooting frequency) and projectile speed
100109
// Besides, the projectiles have the same damage
101-
fS *= SearchSpace.RangedWeaponList().Contains(w.Weapon) ?
102-
(e.Status3 * w.WeaponStatus1) * 3 : 1;
110+
fS *= _searchSpace.WeaponSet.GetRangedWeaponTypes().Contains(w.Weapon) ?
111+
(e.Status2 * e.Status3) * w.WeaponStatus1 : 1;
103112
// The cooldown of healer enemies follows the attack speed
104-
fS *= w.Weapon == WeaponType.CureSpell ?
113+
if (_searchSpace.WeaponSet is TopdownEnemyWeaponsSO topdownWeaponsSO)
114+
fS *= topdownWeaponsSO.IsHealerWeapon(w.Weapon) ?
105115
e.Status3 * 2 : 1;
116+
//fS *= w.Weapon == WeaponType.CureSpell ?
117+
// e.Status3 * 2 : 1;
106118
return fS;
107119
}
108120

@@ -115,15 +127,16 @@ private float CalculateGameplayFactor(Individual individual)
115127
var enemy = individual.Enemy;
116128
var weapon = individual.Weapon;
117129
var gameplayFactor = 1f;
118-
if (SearchSpace.MeleeWeaponList().Contains(weapon.Weapon))
130+
if (_searchSpace.WeaponSet.GetMeleeWeaponTypes().Contains(weapon.Weapon))
119131
{
120132
gameplayFactor = CalculateMeleeWeaponGameplayFactor(enemy, gameplayFactor);
121133
}
122-
else if (SearchSpace.RangedWeaponList().Contains(weapon.Weapon))
134+
else if (_searchSpace.WeaponSet.GetRangedWeaponTypes().Contains(weapon.Weapon))
123135
{
124136
gameplayFactor = CalculateRangedWeaponGameplayFactor(enemy, gameplayFactor);
125137
}
126-
if (weapon.Weapon == WeaponType.CureSpell)
138+
if (_searchSpace.WeaponSet is TopdownEnemyWeaponsSO topdownWeaponsSO &&
139+
topdownWeaponsSO.IsHealerWeapon(weapon.Weapon))
127140
{
128141
gameplayFactor = CalculateHealerGameplayFactor(enemy, gameplayFactor);
129142
}

Assets/Scripts/OverlordData/RulesGenerator/TopdownEnemyWeaponsSO.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ public class TopdownEnemyWeaponsSO : EnemyWeaponsSO<TopdownEnemyWeaponsSO.Weapon
88
{
99
public enum WeaponTypeEnums
1010
{
11-
None, // No weapon
12-
Bow, // Ranged weapon
13-
BombThrower,// Ranged weapon
14-
Barehand, // Melee weapon
15-
Sword, // Melee weapon
16-
Shield // Melee weapon
11+
Barehand, // Enemy attacks the player with barehands (Melee).
12+
Sword, // Enemy uses a short sword to damage the player (Melee).
13+
Bow, // Enemy shots projectiles towards the player (Range).
14+
BombThrower, // Enemy shots bombs towards the player (Range).
15+
Shield, // Enemy uses a shield to defend itself (Defense).
16+
CureSpell, // Enemy uses magic to cure other enemies (Defense).
1717
}
1818
public override List<Enum> GetRangedWeaponTypes()
1919
{
@@ -32,4 +32,17 @@ public override List<Enum> GetMeleeWeaponTypes()
3232
WeaponTypeEnums.Shield,
3333
};
3434
}
35+
36+
public List<Enum> GetHealerWeaponTypes()
37+
{
38+
return new List<Enum>
39+
{
40+
WeaponTypeEnums.CureSpell,
41+
};
42+
}
43+
44+
public bool IsHealerWeapon(Enum weapon)
45+
{
46+
return weapon.Equals(WeaponTypeEnums.CureSpell);
47+
}
3548
}

0 commit comments

Comments
 (0)