Basic entity scripting

This commit is contained in:
Simon Lübeß
2023-03-29 13:33:34 +02:00
parent 5e5134a607
commit e60d74fccd
23 changed files with 1193 additions and 89 deletions
+35 -27
View File
@@ -1,36 +1,44 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using GlitchyEngine.Math;
namespace GlitchyEngine
namespace GlitchyEngine;
public class CSharpTesting
{
public class CSharpTesting
{
public float MyPublicFloatVar = 5.0f;
public CSharpTesting()
{
Console.WriteLine("Hallo von C#!");
DoSomething();
Console.WriteLine(Sample());
}
[DllImport ("__Internal", EntryPoint="DoSomething")]
static extern void DoSomething();
public float MyPublicFloatVar = 5.0f;
public CSharpTesting()
{
Console.WriteLine("Hallo von C#!");
DoSomething();
Console.WriteLine(Sample());
[MethodImpl(MethodImplOptions.InternalCall)]
static extern string Sample();
Vector3 a = new Vector3(1, 2, 3);
Vector3 b = new Vector3(3, 2, 1);
public void PrintFloatVar()
{
Console.WriteLine("MyPublicFloatVar = {0:F}", MyPublicFloatVar);
}
Vector3 result;
//Vector3.Add_Internal(a, b, out result);
private float IncrementFloatVar(float value)
{
MyPublicFloatVar += value;
return MyPublicFloatVar;
}
//Console.WriteLine(result);
}
}
[DllImport ("__Internal", EntryPoint="DoSomething")]
static extern void DoSomething();
[MethodImpl(MethodImplOptions.InternalCall)]
static extern string Sample();
public void PrintFloatVar()
{
Console.WriteLine("MyPublicFloatVar = {0:F}", MyPublicFloatVar);
}
private float IncrementFloatVar(float value)
{
MyPublicFloatVar += value;
return MyPublicFloatVar;
}
}
+8
View File
@@ -0,0 +1,8 @@
using GlitchyEngine.Core;
namespace GlitchyEngine;
public class Component : EngineObject
{
}
+8
View File
@@ -0,0 +1,8 @@
using GlitchyEngine.Core;
namespace GlitchyEngine;
public class Transform : EngineObject
{
}
+26
View File
@@ -0,0 +1,26 @@
namespace GlitchyEngine.Core;
public class EngineObject
{
protected internal UUID _uuid;
/// <summary>
/// UUID used for identifying the object in the engine.
/// </summary>
public UUID UUID => _uuid;
/// <summary>
/// Empty constructor not used. Do NOT USE!
/// </summary>
protected EngineObject()
{}
/// <summary>
/// Creates a new EngineObject with the given ID.
/// </summary>
/// <param name="uuid">UUID of the object.</param>
internal EngineObject(UUID uuid)
{
_uuid = uuid;
}
}
+6
View File
@@ -0,0 +1,6 @@
namespace GlitchyEngine.Core;
public struct UUID
{
private ulong _uuid;
}
+56
View File
@@ -0,0 +1,56 @@
using System;
using GlitchyEngine.Core;
using GlitchyEngine.Math;
namespace GlitchyEngine;
internal struct EntityHandle
{
public uint Version;
public uint Index;
}
public abstract class Entity : EngineObject
{
//private UUID _uuid;
//public UUID UUID => _uuid;
///// <summary>
///// Empty constructor not used. Do NOT USE!
///// </summary>
//protected Entity()
//{}
//internal Entity(UUID uuid)
//{
// _uuid = uuid;
//}
public T GetComponent<T>() where T : Component
{
return null;
}
//public Transform Transform => new (){_uuid = _uuid};
public Vector3 Translation
{
get
{
ScriptGlue.Entity_GetTranslation(_uuid, out Vector3 translation);
return translation;
}
set => ScriptGlue.Entity_SetTranslation(_uuid, value);
}
// Will be executed once after the entity as be created.
// void OnCreate();
// Will be executed every frame.
// void OnUpdate(GameTime);
// Will be executed once when the entity is being destroyed.
// void OnDestroy();
}
+27
View File
@@ -0,0 +1,27 @@
using System.Runtime.CompilerServices;
namespace GlitchyEngine;
public static class Input
{
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern bool IsKeyPressed(Key key);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern bool IsKeyReleased(Key key);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern bool IsKeyToggled(Key key);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern bool IsKeyPressing(Key key);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern bool IsKeyReleasing(Key key);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern bool IsMouseButtonPressed(MouseButton mouseButton);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern bool IsMouseButtonReleased(MouseButton mouseButton);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern bool IsMouseButtonPressing(MouseButton mouseButton);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern bool IsMouseButtonReleasing(MouseButton mouseButton);
}
+120
View File
@@ -0,0 +1,120 @@
namespace GlitchyEngine;
public enum Key
{
// Based on Win32 Virtual Keys
LeftButton = 1, // Left mouse button
RightRutton = 2, // Right mouse button
MiddleButton = 4, // Middle mouse button(three - button mouse)
XButton1 = 5, // X1 mouse button
XButton2 = 6, // X2 mouse button
Backspace = 8, // Backspace key
Tab = 9, // Tab key
Return = 13, // Enter key
Shift = 16, // Shift key
Control = 17, // Ctrl key
Alt = 18, // Alt key
Pause = 19, // Pause key
CapsLock = 20, // Caps Lock key
Escape = 27, // ESC key
Space = 32, // Spacebar
Prior = 33, // Page Up key
Next = 34, // Page Down key
End = 35, // END key
Home = 36, // HOME key
Left = 37, // LEFT ARROW key
Up = 38, // UP ARROW key
Right = 39, // RIGHT ARROW key
Down = 40, // DOWN ARROW key
Print = 42, // PRINT key
Insert = 45, // INS key
Delete = 46, // DEL key
Help = 47, // HELP key
Zero = 48, // 0 key
One = 49, // 1 key
Two = 50, // 2 key
Three = 51, // 3 key
Four = 52, // 4 key
Five = 53, // 5 key
Six = 54, // 6 key
Seven = 55, // 7 key
Eight = 56, // 8 key
Nine = 57, // 9 key
A = 65, // A key
B = 66, // B key
C = 67, // C key
D = 68, // D key
E = 69, // E key
F = 70, // F key
G = 71, // G key
H = 72, // H key
I = 73, // I key
J = 74, // J key
K = 75, // K key
L = 76, // L key
M = 77, // M key
N = 78, // N key
O = 79, // O key
P = 80, // P key
Q = 81, // Q key
R = 82, // R key
S = 83, // S key
T = 84, // T key
U = 85, // U key
V = 86, // V key
W = 87, // W key
X = 88, // X key
Y = 89, // Y key
Z = 90, // Z key
LeftSuper = 91, // Left Windows key(Microsoft® Natural® keyboard)
RightSuper = 92, // Right Windows key(Natural keyboard)
ContextMenu = 93, // Context Menu key (VK_APPS)
Numpad0 = 96, // Numeric keypad 0 key
Numpad1 = 97, // Numeric keypad 1 key
Numpad2 = 98, // Numeric keypad 2 key
Numpad3 = 99, // Numeric keypad 3 key
Numpad4 = 100, // Numeric keypad 4 key
Numpad5 = 101, // Numeric keypad 5 key
Numpad6 = 102, // Numeric keypad 6 key
Numpad7 = 103, // Numeric keypad 7 key
Numpad8 = 104, // Numeric keypad 8 key
Numpad9 = 105, // Numeric keypad 9 key
Multiply = 106, // Multiply key
Add = 107, // Add key
Separator = 108, // Separator key
Subtract = 109, // Subtract key
Decimal = 110, // Decimal key
Divide = 111, // Divide key
F1 = 112, // F1 key
F2 = 113, // F2 key
F3 = 114, // F3 key
F4 = 115, // F4 key
F5 = 116, // F5 key
F6 = 117, // F6 key
F7 = 118, // F7 key
F8 = 119, // F8 key
F9 = 120, // F9 key
F10 = 121, // F10 key
F11 = 122, // F11 key
F12 = 123, // F12 key
F13 = 124, // F13 key
F14 = 125, // F14 key
F15 = 126, // F15 key
F16 = 127, // F16 key
F17 = 128, // F17 key
F18 = 129, // F18 key
F19 = 130, // F19 key
F20 = 131, // F20 key
F21 = 132, // F21 key
F22 = 133, // F22 key
F23 = 134, // F23 key
F24 = 135, // F24 key
Numlock = 144, // NUM LOCK key
Scroll = 145, // SCROLL LOCK key
LeftShift = 160, // Left SHIFT key
RightShift = 161, // Right SHIFT key
LeftControl = 162, // Left CONTROL key
RightControl = 163, // Right CONTROL key
LeftAlt = 164, // Left Alt key
RightAlt = 165 // Right Alt key
}
+50
View File
@@ -0,0 +1,50 @@
using System.Runtime.CompilerServices;
namespace GlitchyEngine;
public class Log
{
public enum LogLevel
{
Trace = 0,
Debug,
Info,
Warning,
Error,
Critical,
Off
}
public static void Trace(string message)
{
LogMessage_Impl(LogLevel.Trace, message);
}
//public static void LogDebug(string message)
//{
// LogMessage_Impl(LogLevel.Info, message);
//}
public static void Info(string message)
{
LogMessage_Impl(LogLevel.Info, message);
}
public static void Warning(string message)
{
LogMessage_Impl(LogLevel.Warning, message);
}
public static void Error(string message)
{
LogMessage_Impl(LogLevel.Error, message);
}
public static void Critical(string message)
{
LogMessage_Impl(LogLevel.Critical, message);
}
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern string LogMessage_Impl(LogLevel logLevel, string message);
}
+53
View File
@@ -0,0 +1,53 @@
using System;
namespace GlitchyEngine.Math;
public struct Vector2
{
public static readonly Vector2 Zero = new(0.0f, 0.0f);
public static readonly Vector2 UnitX = new(1.0f, 0.0f);
public static readonly Vector2 UnitY = new(0.0f, 1.0f);
public static readonly Vector2 One = new(0.0f, 0.0f);
public const int ComponentCount = 2;
public float X, Y;
public Vector2()
{
X = Y = 0.0f;
}
public Vector2(float x, float y)
{
X = x;
Y = y;
}
public float this[int index]
{
get
{
switch(index)
{
case 0: return X;
case 1: return Y;
default: throw new IndexOutOfRangeException();
}
}
set
{
switch(index)
{
case 0:
X = value;
break;
case 1:
Y = value;
break;
default: throw new IndexOutOfRangeException();
}
}
}
}
+128
View File
@@ -0,0 +1,128 @@
using System;
using System.CodeDom;
using System.Runtime.CompilerServices;
namespace GlitchyEngine.Math;
public struct Vector3
{
public static readonly Vector3 Zero = new(0.0f, 0.0f, 0.0f);
public static readonly Vector3 UnitX = new(1.0f, 0.0f, 0.0f);
public static readonly Vector3 UnitY = new(0.0f, 1.0f, 0.0f);
public static readonly Vector3 UnitZ = new(0.0f, 0.0f, 1.0f);
public static readonly Vector3 One = new(0.0f, 0.0f, 0.0f);
public static readonly Vector3 Forward = new(0.0f, 0.0f, 1.0f);
public static readonly Vector3 Backward = new(0.0f, 0.0f, -1.0f);
public static readonly Vector3 Left = new(-1.0f, 0.0f, 0.0f);
public static readonly Vector3 Right = new(1.0f, 0.0f, 0.0f);
public static readonly Vector3 Up = new(0.0f, 1.0f, 0.0f);
public static readonly Vector3 Down = new(0.0f, -1.0f, 0.0f);
public const int ComponentCount = 3;
public float X, Y, Z;
public Vector3()
{
X = Y = Z = 0.0f;
}
public Vector3(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
public Vector3(Vector2 xy, float z)
{
X = xy.X;
Y = xy.Y;
Z = z;
}
public float this[int index]
{
get
{
switch(index)
{
case 0: return X;
case 1: return Y;
case 2: return Z;
default: throw new IndexOutOfRangeException();
}
}
set
{
switch(index)
{
case 0:
X = value;
break;
case 1:
Y = value;
break;
case 2:
Z = value;
break;
default: throw new IndexOutOfRangeException();
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator +(in Vector3 a, in Vector3 b) => new(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator +(float a, in Vector3 b) => new(a + b.X, a + b.Y, a + b.Z);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator +(in Vector3 a, float b) => new(a.X + b, a.Y + b, a.Z + b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator +(in Vector3 a) => a;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator -(in Vector3 a, in Vector3 b) => new(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator -(float a, in Vector3 b) => new(a - b.X, a - b.Y, a - b.Z);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator -(in Vector3 a, float b) => new(a.X - b, a.Y - b, a.Z - b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator -(in Vector3 a) => new Vector3(-a.X, -a.Y, -a.Z);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator *(in Vector3 a, in Vector3 b) => new(a.X * b.X, a.Y * b.Y, a.Z * b.Z);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator *(float a, in Vector3 b) => new(a * b.X, a * b.Y, a * b.Z);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator *(in Vector3 a, float b) => new(a.X * b, a.Y * b, a.Z * b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator /(in Vector3 a, in Vector3 b) => new(a.X / b.X, a.Y / b.Y, a.Z / b.Z);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator /(float a, in Vector3 b) => new(a / b.X, a / b.Y, a / b.Z);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator /(in Vector3 a, float b) => new(a.X / b, a.Y / b, a.Z / b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Vector3 a, Vector3 b)
{
float diffX = a.X - b.X;
float diffY = a.Y - b.Y;
float diffZ = a.Z - b.Z;
return diffX * diffX + diffY * diffY + diffZ * diffZ < 0.00001f;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Vector3 a, Vector3 b)
{
return !(a == b);
}
public override string ToString()
{
return $"X:{X}, Y:{Y}, Z:{Z}";
}
}
+11
View File
@@ -0,0 +1,11 @@
namespace GlitchyEngine;
public enum MouseButton : byte
{
None = 0,
LeftButton,
RightButton,
MiddleButton,
XButton1,
XButton2,
}
+59
View File
@@ -0,0 +1,59 @@
using System;
using GlitchyEngine.Math;
namespace GlitchyEngine;
class MyTestEntity : Entity
{
//Rigidbody2D _rigidBody;
/// <summary>
/// Called after the script component was created. (The entity might not be fully created yet)
/// </summary>
void OnCreate()
{
Log.Info($"Create! {UUID}");
//_rigidBody = GetComponent<Rigidbody2D>();
}
///// <summary>
///// Called after the entity was created completely.
///// </summary>
//void OnInstantiate()
//{
// Log.Warning($"Instantiated!");
//}
/// <summary>
/// Called every frame.
/// </summary>
/// <param name="deltaTime"></param>
void OnUpdate(float deltaTime)
{
if (Input.IsKeyPressed(Key.A))
{
Log.Info($"HALLO!");
Vector3 translation = Translation;
translation.Y += 1.0f * deltaTime;
Translation = translation;
//_rigidBody.ApplyImpulse(new Vector2(0, 10));
}
if (Input.IsMouseButtonReleasing(MouseButton.LeftButton))
{
Log.Info("Ouha!");
}
}
/// <summary>
/// Called before the component is being destroyed.
/// </summary>
void OnDestroy()
{
Log.Trace("Destroy");
}
}
+13
View File
@@ -43,8 +43,21 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Components\Component.cs" />
<Compile Include="Components\Transform.cs" />
<Compile Include="Core\EngineObject.cs" />
<Compile Include="Core\UUID.cs" />
<Compile Include="CSharpTesting.cs" />
<Compile Include="Entity.cs" />
<Compile Include="Input.cs" />
<Compile Include="Key.cs" />
<Compile Include="Log.cs" />
<Compile Include="Math\Vector2.cs" />
<Compile Include="Math\Vector3.cs" />
<Compile Include="MouseButton.cs" />
<Compile Include="MyTestEntity.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ScriptGlue.cs" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+16
View File
@@ -0,0 +1,16 @@
using System.Runtime.CompilerServices;
using GlitchyEngine.Core;
using GlitchyEngine.Math;
namespace GlitchyEngine;
/// <summary>
/// All methods in here are glued to the ScriptGlue.bf in the engine.
/// </summary>
internal static class ScriptGlue
{
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Entity_GetTranslation(UUID entityId, out Vector3 translation);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Entity_SetTranslation(UUID entityId, in Vector3 translation);
}