GameObject and Component
This commit is contained in:
16
src-csharp/Component.cs
Normal file
16
src-csharp/Component.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
|
||||
namespace Ougge;
|
||||
|
||||
public abstract class Component {
|
||||
private GameObject _gameObject;
|
||||
|
||||
internal Component(GameObject gameObject)
|
||||
{
|
||||
_gameObject = gameObject;
|
||||
}
|
||||
|
||||
public GameObject Owner { get => _gameObject.IsDestroyed ? throw new Exception("GameObject") : _gameObject; }
|
||||
|
||||
public virtual void Update(double deltaTime) {}
|
||||
}
|
||||
15
src-csharp/ExampleComponent.cs
Normal file
15
src-csharp/ExampleComponent.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
|
||||
namespace Ougge;
|
||||
|
||||
public class ExampleComponent : Component
|
||||
{
|
||||
public ExampleComponent(GameObject gameObject) : base(gameObject)
|
||||
{
|
||||
}
|
||||
|
||||
public override void Update(double deltaTime)
|
||||
{
|
||||
Console.WriteLine($"C# deltaTime {deltaTime} object id {Owner.Id}");
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Management.Instrumentation;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
@@ -7,18 +8,75 @@ namespace Ougge;
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct Transform
|
||||
{
|
||||
Vector2 scale;
|
||||
Vector2 scale;
|
||||
Vector2 position;
|
||||
float rotation;
|
||||
}
|
||||
|
||||
public class Component
|
||||
{
|
||||
GameObject owner;
|
||||
}
|
||||
|
||||
public class GameObject
|
||||
{
|
||||
Transform transform { get; }
|
||||
GameObject? parent;
|
||||
}
|
||||
// index in GameObjectPool
|
||||
private ulong _id;
|
||||
private uint _index;
|
||||
private bool _isDestroyed;
|
||||
|
||||
public ulong Id => _id;
|
||||
public bool IsDestroyed => _isDestroyed;
|
||||
|
||||
public Transform Transform { get; }
|
||||
public GameObject? Parent;
|
||||
|
||||
public readonly Dictionary<Type, Component> Components = new();
|
||||
|
||||
private GameObject()
|
||||
{
|
||||
}
|
||||
|
||||
private void Init(ulong id, uint nativePoolIndex)
|
||||
{
|
||||
_id = id;
|
||||
_index = nativePoolIndex;
|
||||
}
|
||||
|
||||
static public GameObject Create()
|
||||
{
|
||||
NativeMethods.createGameObject(out ulong id, out uint index);
|
||||
var o = new GameObject();
|
||||
o.Init(id, index);
|
||||
return o;
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
if(_isDestroyed)
|
||||
return;
|
||||
|
||||
_isDestroyed = NativeMethods.destroyGameObject(_index);
|
||||
if(!_isDestroyed)
|
||||
throw new Exception($"Can't destroy GameObject({_id})");
|
||||
}
|
||||
|
||||
public bool TryCreateComponent(Type t)
|
||||
{
|
||||
if(!t.IsSubclassOf(typeof(Component)))
|
||||
throw new Exception($"type {t.FullName} is not a derived class of {typeof(Component).FullName}");
|
||||
|
||||
if(Components.ContainsKey(t))
|
||||
return false;
|
||||
|
||||
Components.Add(t, (Component)Activator.CreateInstance(t, this));
|
||||
return true;
|
||||
}
|
||||
private bool TryCreateComponent(string fullName)
|
||||
{
|
||||
return TryCreateComponent(Type.GetType(fullName));
|
||||
}
|
||||
|
||||
private void UpdateComponents(double deltaTime)
|
||||
{
|
||||
foreach(var p in Components)
|
||||
{
|
||||
p.Value.Update(deltaTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
13
src-csharp/NativeMethods.cs
Normal file
13
src-csharp/NativeMethods.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Ougge;
|
||||
|
||||
internal static class NativeMethods
|
||||
{
|
||||
[DllImport("__Internal")]
|
||||
internal extern static bool destroyGameObject(uint index);
|
||||
|
||||
[DllImport("__Internal")]
|
||||
internal extern static void createGameObject(out ulong id, out uint index);
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Ougge;
|
||||
|
||||
public abstract class ScriptBase {
|
||||
public virtual void Update(double deltaTime) {}
|
||||
}
|
||||
|
||||
public class ExampleScript : ScriptBase
|
||||
{
|
||||
public override void Update(double deltaTime)
|
||||
{
|
||||
Console.WriteLine($"C# deltaTime {deltaTime}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user