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 Bounciness: 0
Density: 1 Density: 1
Position Offset: {x: 0, y: 0.5, z: 0} 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; private GameObject? player;
public AIPrototype(GameObject gameObj) : base(gameObj) { }
protected override void awake() protected override void awake()
{ {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -23,7 +23,6 @@ public class RaccoonShowcase : Script
[Range(-5, 5)] [Range(-5, 5)]
public List<int> intList = new List<int>(new int[] { 2, 8, 2, 6, 8, 0, 1 }); 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 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() protected override void awake()
{ {

View File

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

View File

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

View File

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

View File

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

View File

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