Skip to content

Commit

Permalink
Update NPCEditor with edit mode
Browse files Browse the repository at this point in the history
  • Loading branch information
The Dreamer committed Jan 18, 2021
1 parent b928793 commit 98a77f4
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 16 deletions.
3 changes: 2 additions & 1 deletion Assembly-CSharp-Editor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Assets\Editor\NPCEditor.cs" />
<Compile Include="Assets\Scripts\Editor\EditNPC.cs" />
<Compile Include="Assets\Scripts\Editor\NPCEditor.cs" />
<Reference Include="UnityEngine">
<HintPath>C:/Program Files/Unity/Hub/Editor/2019.4.18f1/Editor/Data/Managed/UnityEngine/UnityEngine.dll</HintPath>
</Reference>
Expand Down
3 changes: 2 additions & 1 deletion Assets/Resources/NPC SO AI/TestSpawn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
public class TestSpawn : MonoBehaviour
{
public NPCSO testing;

[Range(1,200)]
public int spawnCNT;
// Start is called before the first frame update
void Start()
{
Expand Down
File renamed without changes.
87 changes: 87 additions & 0 deletions Assets/Scripts/Editor/EditNPC.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using IAUS.ECS2.Component;
using IAUS.ECS2;
using Global.Component;
using SpawnerSystem.Editors;
using IAUS.SO.Interfaces;
using Components.MovementSystem;
namespace IAUS.SO.editor
{
public sealed partial class NPCEditor : EditorWindow {

void DisplayNPCSOForEditing(NPCSO SO) {
editorState = EditorState.EditExisting;
if (SO.Model)
{
GetModel = SO.Model;
createRandomCharacter = false;
}
else
createRandomCharacter = true;
GetPatrol = SO.GetPatrol;
GetRetreat = SO.GetRetreat;
GetTargetType = SO.Self.Type;

GetWait = SO.GetWait;
}
NPCSO GetNPCSO;
void DisplayListOfExistingSO() {
EditorGUILayout.BeginVertical("Box");
if (!NPCSODatabase.IsLoaded)
NPCSODatabase.LoadDatabase();
foreach (NPCSO SO in NPCSODatabase.NPCs)
{
EditorGUILayout.BeginHorizontal();
//add names
if (GUILayout.Button("testing"))
GetNPCSO = SO;
DisplayNPCSOForEditing(SO);
// delete SO
GUILayout.Button("X", GUILayout.Width(20));
EditorGUILayout.EndHorizontal();
}
if (GUILayout.Button("Create New SO"))
{
editorState = EditorState.CreateNew;
//reset to start values;
}

EditorGUILayout.EndVertical();
}
void SaveChanges() {

List<AIStates> StatesToAdd = new List<AIStates>();
for (int i = 0; i < showBtn.Length; i++)
{
if (showBtn[i])
{
switch ((AIStates)i)
{
case AIStates.Patrol:
StatesToAdd.Add(AIStates.Patrol);
break;
case AIStates.Wait:
StatesToAdd.Add(AIStates.Wait);
break;

}

}
}

GetNPCSO.Setup(GetModel, new AITarget() { Type = GetTargetType }, StatesToAdd, GetMove,
GetPatrol, GetWait, GetRetreat
);
SetStartValues();

}


enum EditorState {
CreateNew, EditExisting
}
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Editor/EditNPC.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 72 additions & 12 deletions Assets/Editor/NPCEditor.cs → Assets/Scripts/Editor/NPCEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,33 @@ static void Init()
window.minSize = new Vector2(600, 800);
window.Show();
}

private void Awake()
{
SetStartValues();
}
bool[] showBtn = new bool[System.Enum.GetNames(typeof(AIStates)).Length] ;
EditorState editorState;
TargetType GetTargetType;
Patrol GetPatrol;

Wait GetWait;
Retreat GetFlee;
GameObject model;
Retreat GetRetreat;
GameObject GetModel;
bool createRandomCharacter=false;
Vector2 scrollPos;
void OnGUI()
{
EditorGUILayout.BeginHorizontal();
DisplayListOfExistingSO();

EditorGUILayout.BeginVertical("Box");
GUILayout.Label("Base Settings", EditorStyles.boldLabel);
scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
EditorGUILayout.BeginVertical("Button");
createRandomCharacter = EditorGUILayout.Foldout(createRandomCharacter, "Create RNG GO To Be Implement Later");
if (!createRandomCharacter)
model = (GameObject)EditorGUILayout.ObjectField("Select Model", model, typeof(GameObject), false);
GetModel = (GameObject)EditorGUILayout.ObjectField("Select Model", GetModel, typeof(GameObject), false);

GetTargetType = (TargetType)EditorGUILayout.EnumPopup("AI Type", GetTargetType);
switch (GetTargetType)
{
Expand All @@ -52,12 +61,41 @@ void OnGUI()

if (GetTargetType == TargetType.Character)
GetMove = SetupMove(GetMove);
// add a switch here
EditorGUILayout.BeginHorizontal("Box");
switch (editorState)
{
case EditorState.CreateNew:

if (GUILayout.Button("Submit"))
{
CreateSO("Assets/Resources/NPC SO AI");
}
break;
case EditorState.EditExisting:

if(GUILayout.Button("Submit")){
CreateSO("Assets/Resources/NPC SO AI");
if (GUILayout.Button("Update"))
{
SaveChanges();
}
if (GUILayout.Button("Update"))
{
CreateSO("Assets/Resources/NPC SO AI");
}
break;
}
if (GUILayout.Button("Clear"))
{
// add nodal window to verfiy
SetStartValues();
editorState = EditorState.CreateNew;
}


EditorGUILayout.EndHorizontal();
EditorGUILayout.EndScrollView();

EditorGUILayout.EndVertical();
EditorGUILayout.EndHorizontal();
}
bool PatrolDistance = false;
bool PatrolHealthRatio = false;
Expand Down Expand Up @@ -130,6 +168,7 @@ void SetupFlee()


void CreateSO(string Path) {

List<AIStates> StatesToAdd= new List<AIStates>();
for (int i = 0; i < showBtn.Length; i++)
{
Expand All @@ -148,11 +187,32 @@ void SetupFlee()
}

ScriptableObjectUtility.CreateAsset<NPCSO>(Path, out NPCSO SO);
SO.Setup(model,new AITarget() { Type = GetTargetType },StatesToAdd, GetMove,
GetPatrol,GetWait,GetFlee
SO.Setup(GetModel,new AITarget() { Type = GetTargetType },StatesToAdd, GetMove,
GetPatrol,GetWait,GetRetreat
);


SetStartValues();
}

public void SetStartValues() {
GetModel = null;
GetTargetType = new TargetType();
GetPatrol = new Patrol()
{
BufferZone = .75f,
DistanceToPoint = new ConsiderationScoringData() { M = 50, K = -0.95f, B = .935f, C = .35f, responseType = ResponseType.Logistic },
HealthRatio = new ConsiderationScoringData() { M = 50, K = -1, B = .91f, C = .2f, responseType = ResponseType.Logistic },
_coolDownTime = 5

};
GetRetreat = new Retreat() { };
GetWait = new Wait() {
TimeLeft = new ConsiderationScoringData() { M=50, K=-1, B=.91f,C= .2f, responseType = ResponseType.Logistic, Inverse =false},
HealthRatio = new ConsiderationScoringData() { M = 50, K = -1, B = .91f, C = .2f, responseType = ResponseType.Logistic, Inverse = false },
StartTime =1,
_coolDownTime =5
};

}

}
}
File renamed without changes.
4 changes: 3 additions & 1 deletion Assets/Scripts/IAUS/Scripts/Archetypes Prefabs/NPCSO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ namespace IAUS.SO
{
public class NPCSO : ScriptableObject, INPCBasics
{
[SerializeField] uint spawnID;
public uint SpawnID { get { return spawnID; } }
[SerializeField] GameObject _model;
public GameObject Model { get { return _model; } }

public AITarget Self => GetSelf;
[SerializeField]AITarget GetSelf;
public List<AIStates> AIStatesAvailable => states;
Expand Down
53 changes: 53 additions & 0 deletions Assets/Scripts/IAUS/Scripts/Archetypes Prefabs/NPCSODatabase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace IAUS.SO
{
public static class NPCSODatabase
{
static public List<NPCSO> NPCs;
static public bool IsLoaded { get; private set; } = false;

static private void ValidateDatebase()
{
if (NPCs == null) NPCs = new List<NPCSO>();
}

static public void LoadDatabase()
{
if (IsLoaded)
return;
LoadDatabaseForce();
}
static public void LoadDatabaseForce()
{
ValidateDatebase();
IsLoaded = true;
NPCSO[] resources = Resources.LoadAll<NPCSO>(@"NPC SO AI");
foreach (NPCSO squad in resources)
{
if (!NPCs.Contains(squad))
NPCs.Add(squad);
}
}

static public void ClearDatabase()
{
IsLoaded = false;
NPCs.Clear();
}

static public NPCSO GetEnemy(int SpawnID)
{
ValidateDatebase();
LoadDatabase();
foreach (NPCSO squad in NPCs)
{
if (squad.SpawnID == SpawnID)
return ScriptableObject.Instantiate(squad) as NPCSO;
}
return null;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Unity.Mathematics;
using Unity.Entities;

using UnityEngine;
namespace InfluenceSystem.Component
{
[GenerateAuthoringComponent]
Expand Down
1 change: 1 addition & 0 deletions IAUS.Player.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@
<Compile Include="Assets\Scripts\IAUS\Scripts\AIStates.cs" />
<Compile Include="Assets\Scripts\IAUS\Scripts\Archetypes Prefabs\Interface\INPCBasics.cs" />
<Compile Include="Assets\Scripts\IAUS\Scripts\Archetypes Prefabs\NPCSO.cs" />
<Compile Include="Assets\Scripts\IAUS\Scripts\Archetypes Prefabs\NPCSODatabase.cs" />
<Compile Include="Assets\Scripts\IAUS\Scripts\Authoring\BaseAIAuthoring.cs" />
<Compile Include="Assets\Scripts\IAUS\Scripts\Components\BaseConsiderations.cs" />
<Compile Include="Assets\Scripts\IAUS\Scripts\Components\ConsiderationScoringData.cs" />
Expand Down
1 change: 1 addition & 0 deletions IAUS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<Compile Include="Assets\Scripts\IAUS\Scripts\AIStates.cs" />
<Compile Include="Assets\Scripts\IAUS\Scripts\Archetypes Prefabs\Interface\INPCBasics.cs" />
<Compile Include="Assets\Scripts\IAUS\Scripts\Archetypes Prefabs\NPCSO.cs" />
<Compile Include="Assets\Scripts\IAUS\Scripts\Archetypes Prefabs\NPCSODatabase.cs" />
<Compile Include="Assets\Scripts\IAUS\Scripts\Authoring\BaseAIAuthoring.cs" />
<Compile Include="Assets\Scripts\IAUS\Scripts\Components\BaseConsiderations.cs" />
<Compile Include="Assets\Scripts\IAUS\Scripts\Components\ConsiderationScoringData.cs" />
Expand Down

0 comments on commit 98a77f4

Please sign in to comment.