Merge pull request #197 from SHADE-DP/SP3-6-c-scripting

Modified how scripts are added and updated to support runtime adding of scripts
This commit is contained in:
XiaoQiDigipen 2022-11-13 16:38:32 +08:00 committed by GitHub
commit 57182f9715
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 49 additions and 39 deletions

View File

@ -226,4 +226,9 @@
Bounciness: 0
Density: 1
Position Offset: {x: 0, y: 0.5, z: 0}
Scripts: ~
Scripts:
- Type: Item
currCategory: 0
- Type: PickAndThrow
throwForce: [100, 200, 100]
item: 51000

View File

@ -51,7 +51,6 @@ public class AIPrototype : Script
private GameObject? player;
public AIPrototype(GameObject gameObj) : base(gameObj) { }
protected override void awake()
{

View File

@ -7,7 +7,6 @@ namespace SHADE_Scripting
{
public float turnSpeed = 0.5f;
public CameraControl(GameObject go) : base(go) { }
protected override void update()
{
//Camera

View File

@ -3,7 +3,6 @@ using System;
public class CameraFix : Script
{
public CameraFix(GameObject gameObj) : base(gameObj) { }
private Transform tranform;
public Vector3 pos = Vector3.Zero;

View File

@ -10,7 +10,6 @@ public class Item : Script
}
public ItemCategory currCategory;
public Item(GameObject gameObj) : base(gameObj) { }
protected override void awake()
{

View File

@ -8,7 +8,6 @@ public class PhysicsTest : Script
private Transform Transform;
private RigidBody RigidBody;
private Collider Collider;
public PhysicsTest(GameObject gameObj) : base(gameObj) { }
protected override void awake()
{

View File

@ -14,7 +14,6 @@ public class PickAndThrow : Script
private float lastXDir;
private float lastZDir;
private bool inRange = false;
public PickAndThrow(GameObject gameObj) : base(gameObj) { }
protected override void awake()
{

View File

@ -73,8 +73,6 @@ public class PlayerController : Script
public float mediumMultiper = 0.5f;
public float heavyMultiper = 0.25f;
public PlayerController(GameObject gameObj) : base(gameObj) { }
protected override void awake()
{
//default setup

View File

@ -2,8 +2,6 @@
public class PrintWhenActive : Script
{
public PrintWhenActive(GameObject gameObj) : base(gameObj) { }
protected override void update()
{
Debug.Log("Active!");

View File

@ -23,7 +23,6 @@ public class RaccoonShowcase : Script
[Range(-5, 5)]
public List<int> intList = new List<int>(new int[] { 2, 8, 2, 6, 8, 0, 1 });
public List<Light.Type> enumList = new List<Light.Type>(new Light.Type[] { Light.Type.Point, Light.Type.Directional, Light.Type.Ambient });
public RaccoonShowcase(GameObject gameObj) : base(gameObj) {}
protected override void awake()
{

View File

@ -14,8 +14,6 @@ public class RaccoonSpin : Script
[SerializeField]
private CallbackEvent<int, double, Vector3> testEvent3 = new CallbackEvent<int, double, Vector3>();
private Transform Transform;
public RaccoonSpin(GameObject gameObj) : base(gameObj) { }
protected override void awake()
{

View File

@ -15,7 +15,6 @@ namespace SHADE_Scripting
public float turnSpeedPitch = 0.3f;
public float turnSpeedYaw = 0.5f;
public float pitchClamp = 45.0f;
public ThirdPersonCamera(GameObject go) : base(go) { }
protected override void awake()
{

View File

@ -93,6 +93,11 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/
/* "All-time" Lifecycle Functions */
/*---------------------------------------------------------------------------------*/
void Script::Initialize(GameObject newOwner)
{
owner = newOwner;
}
void Script::OnAttached()
{
SAFE_NATIVE_CALL_BEGIN
@ -198,9 +203,8 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/
/* Constructors */
/*---------------------------------------------------------------------------------*/
Script::Script(GameObject gameObj)
: owner { gameObj }
, OnGizmosDrawOverriden { false }
Script::Script()
: OnGizmosDrawOverriden { false }
{}
/*---------------------------------------------------------------------------------*/

View File

@ -165,7 +165,7 @@ namespace SHADE
internal:
/*-----------------------------------------------------------------------------*/
/* Properties */
/* Fields */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// If true, the OnGizmosDraw function was overridden.
@ -176,6 +176,10 @@ namespace SHADE
/* "All-Time" Lifecycle Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Used to initialize a Script with a GameObject.
/// </summary>
void Initialize(GameObject newOwner);
/// <summary>
/// Used to call onAttached(). This is called immediately when this script is
/// attached to a GameObject.
/// </summary>
@ -272,13 +276,9 @@ namespace SHADE
/* Constructors */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Constructor for Script to tie it to a specific GameObject.
/// Constructors of derived Scripts should call this Constructor.
/// Default Constructor
/// </summary>
/// <param name="gameObj">
/// GameObject that this Script will be tied to.
/// </param>
Script(GameObject gameObj);
Script();
/*-----------------------------------------------------------------------------*/
/* Virtual "All-Time" Lifecycle Functions */

View File

@ -40,8 +40,7 @@ namespace SHADE
T ScriptStore::AddScript(Entity entity)
{
// Create the script and add it in
array<System::Object^>^ params = gcnew array<System::Object^>{GameObject(entity)};
Script^ script = safe_cast<Script^>(System::Activator::CreateInstance(T::typeid, params));
Script^ script = safe_cast<Script^>(System::Activator::CreateInstance(T::typeid));
return safe_cast<T>(AddScript(entity, script));
}
@ -72,9 +71,18 @@ namespace SHADE
}
// Add the script in
script->Initialize(GameObject(entity));
entityScriptList->Insert(System::Math::Clamp(index, 0, entityScriptList->Count), script);
awakeList.Add(script);
startList.Add(script);
if (Application::IsPlaying)
{
script->Awake();
script->Start();
}
else
{
awakeList.Add(script);
startList.Add(script);
}
script->OnAttached();
return script;
@ -122,6 +130,7 @@ namespace SHADE
std::ostringstream oss;
oss << "[ScriptStore] Failed to add Script named \"" << Convert::ToNative(scriptName)
<< "\" to Entity #" << entity << "! (" << Convert::ToNative(e->GetType()->Name) << ")";
oss << Convert::ToNative(e->ToString());
Debug::LogError(oss.str());
return false;
}
@ -451,9 +460,10 @@ namespace SHADE
continue;
// Update each script
for each (Script^ script in entity.Value)
ScriptList^ scripts = entity.Value;
for (int i = 0; i < scripts->Count; ++i)
{
script->FixedUpdate();
scripts[i]->FixedUpdate();
}
}
SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
@ -468,9 +478,10 @@ namespace SHADE
continue;
// Update each script
for each (Script^ script in entity.Value)
ScriptList^ scripts = entity.Value;
for (int i = 0; i < scripts->Count; ++i)
{
script->Update();
scripts[i]->Update();
}
}
SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
@ -485,9 +496,10 @@ namespace SHADE
continue;
// Update each script
for each (Script^ script in entity.Value)
ScriptList^ scripts = entity.Value;
for (int i = 0; i < scripts->Count; ++i)
{
script->LateUpdate();
scripts[i]->LateUpdate();
}
}
SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
@ -503,9 +515,10 @@ namespace SHADE
continue;
// Update each script
for each (Script^ script in entity.Value)
ScriptList^ scripts = entity.Value;
for (int i = 0; i < scripts->Count; ++i)
{
script->OnDrawGizmos();
scripts[i]->OnDrawGizmos();
}
}
SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore")
@ -537,8 +550,9 @@ namespace SHADE
auto entityScripts = scripts[entity.first];
if (entityScripts->Count > 0)
{
for each (Script ^ script in entityScripts)
for (int i = 0; i < entityScripts->Count; ++i)
{
Script^ script = entityScripts[i];
switch (collisionInfo.GetCollisionState())
{
case SHCollisionEvent::State::ENTER:
@ -578,8 +592,9 @@ namespace SHADE
auto entityScripts = scripts[entity.first];
if (entityScripts->Count > 0)
{
for each (Script ^ script in entityScripts)
for (int i = 0; i < entityScripts->Count; ++i)
{
Script^ script = entityScripts[i];
switch (triggerInfo.GetCollisionState())
{
case SHCollisionEvent::State::ENTER: