I am building a C# Game Engine for a College Project and I am facing some problems, I have a class called GameObject that contains a List of Component (Which is another Class), I created a class that saves a Vector3 Struct called Transform which Inherits from the Component class.
Now when I try to add the New Instance of Transform into the Component List in Gameobject it doesn't allow me to access the internal methods of the Transform class or do something like get the X coordinate:
// This is the List inside the class GameObject
public List<Component> Components = new List<Component>();
// This is the new Instance of Transform
Transform transform = new Transform();
// This is how I tried to add the new object to the list above
AddComponent(transform);
public void AddComponent(object component)
{
Components.Add((Component)component);
}
// This Works
Console.WriteLine(transform.Position.X);
// However, this line bellow does not allow me to use 'Position'
Console.WriteLine(Components[0]);
The Full Code is Listed Bellow for a Clearer Picture
GameObject Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BoBo3D_George.R_Dor.S
{
public class GameObject
{
// GameObject Class Fields
public List<Component> Components = new List<Component>();
private bool _isEnabled = true;
private string _name;
// ------------------------------
// GameObject Class Constructor
public GameObject(string name)
{
_name = name;
Transform transform = new Transform();
AddComponent(transform);
Rigidbody rigidBody = new Rigidbody();
AddComponent(rigidBody);
Console.WriteLine(transform.Position.X);
Console.WriteLine(Components[0]);
Console.WriteLine(Components[1]);
}
// ------------------------------
// GameObject Class Properties
public string Name
{
get
{
return "Object Name: " + _name;
}
set
{
_name = value;
}
}
public bool IsEnabled
{
get
{
return _isEnabled;
}
}
// ------------------------------
// GameObject Class Methods
public void Disable()
{
throw new System.NotImplementedException();
}
public void Enable()
{
throw new System.NotImplementedException();
}
public void Destroy()
{
throw new System.NotImplementedException();
}
public void AddComponent(object component)
{
Components.Add((Component)component);
}
public void RemoveComponent(object component)
{
throw new System.NotImplementedException();
}
public object GetComponent(int compNum)
{
throw new System.NotImplementedException();
}
}
}
Component, Transform & RigidBody Classes:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BoBo3D_George.R_Dor.S
{
public abstract class Component : IComponent
{
// IComponent Interface Implementation in Component Class
public void OnDisable()
{
throw new NotImplementedException();
}
public void OnEnable()
{
throw new NotImplementedException();
}
public void Start()
{
throw new NotImplementedException();
}
public void Update(float deltaTime)
{
throw new NotImplementedException();
}
}
// Transform Class Componnent
class Transform : Component
{
public Vector3 Position = new Vector3() { X = 0, Y = 0, Z = 0 };
public override string ToString()
{
string info;
info = Position.ToString();
return info;
}
}
// Rigidbody Class Componnent
class Rigidbody : Component
{
private bool _useGravity = false;
private float _g = Physics.Gravity();
private float _timeScale = 0;
private float _fallVelocity = 0;
private float _gravityScale = 0;
private string _info;
public bool UsingGravity
{
get
{
return _useGravity;
}
}
public override string ToString()
{
return $"Gravity:({_useGravity})\n" + _info;
}
public bool ToggleGravity()
{
if (_useGravity == true)
{
_useGravity = false;
}
else
{
_useGravity = true;
}
return _useGravity;
}
public float ActiveGravity()
{
if (_useGravity == true)
{
_timeScale++;
_fallVelocity = _g * _timeScale;
_gravityScale = (_g/2) * (_timeScale * _timeScale);
}
else
{
_timeScale = 0;
_fallVelocity = 0;
_gravityScale = 0;
}
_info = "Time:" + _timeScale + "\nVelocity:" + _fallVelocity + "\nHeight:" + _gravityScale;
return _gravityScale;
}
public void ApplyConstantForce(float incrementation, Vector3 directionInV3)
{
}
}
}