Reworked scripts to no longer need definition of constructors

This commit is contained in:
Kah Wei 2022-11-13 16:29:25 +08:00
parent 39e26f1b6e
commit 4dc8527395
15 changed files with 23 additions and 27 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,6 +71,7 @@ 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);
if (Application::IsPlaying) if (Application::IsPlaying)
{ {
@ -130,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;
} }