mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Scripting: FindEntityWithName and get script instance
+ Some C# UUID stuff + Added some Mono functions + ScriptEngine: + ID -> ScriptInstance map in ScriptEngine + GetManagedInstance + Fixed Scene not visible after loading a new scene + Renamed OnViewportResize to SetViewportSize for Scene and SceneRenderer
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using GlitchyEngine;
|
||||
using GlitchyEngine.Math;
|
||||
|
||||
namespace Sandbox;
|
||||
public class Camera : Entity
|
||||
{
|
||||
public float DistanceFromPlayer = 5.0f;
|
||||
|
||||
public float DontFollowRadius = 3.0f;
|
||||
|
||||
void OnUpdate(float deltaTime)
|
||||
{
|
||||
Entity player = FindEntityWithName("Player");
|
||||
|
||||
if (player != null)
|
||||
{
|
||||
Vector2 playerPosition = new Vector2(player.Transform.Translation.X, player.Transform.Translation.Y);
|
||||
|
||||
Vector2 cameraPosition = new Vector2(Transform.Translation.X, Transform.Translation.Y);
|
||||
|
||||
Vector2 distanceVector = playerPosition - cameraPosition;
|
||||
|
||||
float distance = distanceVector.Length();
|
||||
|
||||
Vector2 neededMovement = Vector2.Zero;
|
||||
|
||||
if (distance > DontFollowRadius)
|
||||
{
|
||||
neededMovement = distanceVector - (distanceVector / distance) * DontFollowRadius;
|
||||
}
|
||||
|
||||
Transform.Translation = new Vector3(cameraPosition + neededMovement, DistanceFromPlayer);
|
||||
|
||||
//Transform.Translation = new Vector3(player.Transform.Translation.X, player.Transform.Translation.Y, DistanceFromPlayer);
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.Error("Player not found!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -54,6 +54,8 @@ class MyTestEntity : Entity
|
||||
|
||||
public MyStruct AStruct;
|
||||
|
||||
public Camera Camera;
|
||||
|
||||
/// <summary>
|
||||
/// Called after the script component was created. (The entity might not be fully created yet)
|
||||
/// </summary>
|
||||
@@ -61,7 +63,16 @@ class MyTestEntity : Entity
|
||||
{
|
||||
Log.Info($"Create! {UUID}");
|
||||
|
||||
Log.Info($"Jump Force: {JumpForce}");
|
||||
|
||||
_rigidBody ??= GetComponent<RigidBody2D>() ?? AddComponent<RigidBody2D>();
|
||||
|
||||
Camera = FindEntityWithName("Camera").As<Camera>();
|
||||
|
||||
if (Camera == null)
|
||||
{
|
||||
Log.Error("Camera not found.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -82,6 +93,12 @@ class MyTestEntity : Entity
|
||||
force.X += MoveForce * deltaTime;
|
||||
}
|
||||
|
||||
if (Input.IsKeyPressed(Key.Q))
|
||||
Camera.DistanceFromPlayer -= deltaTime;
|
||||
|
||||
if (Input.IsKeyPressed(Key.E))
|
||||
Camera.DistanceFromPlayer += deltaTime;
|
||||
|
||||
if (Input.IsKeyPressing(Key.Space))
|
||||
{
|
||||
force.Y += JumpForce;
|
||||
|
||||
@@ -43,6 +43,7 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Camera.cs" />
|
||||
<Compile Include="MyTestEntity.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
},
|
||||
TransformComponent = {
|
||||
Position = {
|
||||
X = 0,
|
||||
X = -0.72789681,
|
||||
Y = 2,
|
||||
Z = 0
|
||||
},
|
||||
@@ -71,8 +71,8 @@
|
||||
SpriteRendererComponent = {
|
||||
Color = {
|
||||
R = 1,
|
||||
G = 0.941886365,
|
||||
B = 0.401484847,
|
||||
G = 0.941886961,
|
||||
B = 0.401484549,
|
||||
A = 1
|
||||
},
|
||||
Sprite = "",
|
||||
@@ -162,14 +162,20 @@
|
||||
OrthographicHeight = 10,
|
||||
OrthographicNearPlane = 0,
|
||||
OrthographicFarPlane = 10,
|
||||
AspectRatio = 3.22169805,
|
||||
AspectRatio = 0.884925187,
|
||||
FixedAspectRatio = false
|
||||
}
|
||||
},
|
||||
ScriptComponent = {
|
||||
ScriptClass = "Sandbox.Camera",
|
||||
Fields = [
|
||||
DistanceFromPlayer = (Float)-7,
|
||||
DontFollowRadius = (Float)3
|
||||
] }
|
||||
},
|
||||
{
|
||||
Id = 15710354273720487680,
|
||||
NameComponent = {
|
||||
Name = "Circle"
|
||||
Name = "Player"
|
||||
},
|
||||
CircleRendererComponent = {
|
||||
Color = {
|
||||
@@ -189,8 +195,8 @@
|
||||
},
|
||||
TransformComponent = {
|
||||
Position = {
|
||||
X = -0.12120308,
|
||||
Y = 0.660160363,
|
||||
X = -0.121203184,
|
||||
Y = 0.660160959,
|
||||
Z = 0
|
||||
},
|
||||
Rotation = {
|
||||
@@ -224,7 +230,42 @@
|
||||
Friction = 0.5,
|
||||
Restitution = 0,
|
||||
RestitutionThreshold = 0.5
|
||||
}
|
||||
},
|
||||
ScriptComponent = {
|
||||
ScriptClass = "Sandbox.MyTestEntity",
|
||||
Fields = [
|
||||
Bo = (Bool)false,
|
||||
By = (Byte)0,
|
||||
Us = (UShort)57,
|
||||
Ui = (UInt)54,
|
||||
Ul = (ULong)53,
|
||||
Sb = (SByte)-128,
|
||||
Sh = (Short)95,
|
||||
In = (Int)-149,
|
||||
Lo = (Long)82,
|
||||
Fl = (Float)-98.122963,
|
||||
Do = (Double)118,
|
||||
V2 = (float2){
|
||||
X = 11,
|
||||
Y = 12
|
||||
},
|
||||
V3 = (float3){
|
||||
X = 13,
|
||||
Y = 14,
|
||||
Z = 15
|
||||
},
|
||||
V4 = (float4){
|
||||
X = 14,
|
||||
Y = 17,
|
||||
Z = 18,
|
||||
W = 19
|
||||
},
|
||||
TheEntity = (Entity)0,
|
||||
JumpForce = (Float)200,
|
||||
MoveForce = (Float)148,
|
||||
MyNumber = (Int)22,
|
||||
MyDouble = (Double)23
|
||||
] }
|
||||
},
|
||||
{
|
||||
Id = 935640822766280888,
|
||||
@@ -249,9 +290,9 @@
|
||||
Z = 1
|
||||
},
|
||||
EditorEulerRotation = {
|
||||
X = 1.0908947,
|
||||
X = 1.09089541,
|
||||
Y = -0.340793997,
|
||||
Z = 0.160857916
|
||||
Z = 0.160857871
|
||||
}
|
||||
},
|
||||
LightComponent = {
|
||||
@@ -260,7 +301,7 @@
|
||||
Color = {
|
||||
R = 0.985467017,
|
||||
G = 1,
|
||||
B = 0.569978654
|
||||
B = 0.569978058
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -273,7 +314,7 @@
|
||||
Position = {
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Z = -1.47202551
|
||||
Z = 2.56930423
|
||||
},
|
||||
Rotation = {
|
||||
X = 0,
|
||||
@@ -298,6 +339,192 @@
|
||||
MeshRendererComponent = {
|
||||
Material = "Textures/TestMaterial.mat"
|
||||
}
|
||||
},
|
||||
{
|
||||
Id = 2241063100006479259,
|
||||
NameComponent = {
|
||||
Name = "Floor 2"
|
||||
},
|
||||
SpriteRendererComponent = {
|
||||
Color = {
|
||||
R = 0.425658971,
|
||||
G = 0.855207562,
|
||||
B = 0.0558161102,
|
||||
A = 1
|
||||
},
|
||||
Sprite = "",
|
||||
UvTransform = {
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Z = 1,
|
||||
W = 1
|
||||
}
|
||||
},
|
||||
TransformComponent = {
|
||||
Position = {
|
||||
X = 6.74610233,
|
||||
Y = -2.03225899,
|
||||
Z = 0
|
||||
},
|
||||
Rotation = {
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Z = -0.331792623,
|
||||
W = 0.943352342
|
||||
},
|
||||
Scale = {
|
||||
X = 5.29557419,
|
||||
Y = 1,
|
||||
Z = 1
|
||||
},
|
||||
EditorEulerRotation = {
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Z = -0.676406503
|
||||
}
|
||||
},
|
||||
Rigidbody2D = {
|
||||
BodyType = .Static,
|
||||
FixedRotation = false
|
||||
},
|
||||
BoxCollider2D = {
|
||||
Offset = {
|
||||
X = 0,
|
||||
Y = 0
|
||||
},
|
||||
Size = {
|
||||
X = 0.5,
|
||||
Y = 0.5
|
||||
},
|
||||
Density = 1,
|
||||
Friction = 0.5,
|
||||
Restitution = 0,
|
||||
RestitutionThreshold = 0.5
|
||||
}
|
||||
},
|
||||
{
|
||||
Id = 5126705881069187728,
|
||||
NameComponent = {
|
||||
Name = "Floor 3"
|
||||
},
|
||||
SpriteRendererComponent = {
|
||||
Color = {
|
||||
R = 0.666116953,
|
||||
G = 0.0600316115,
|
||||
B = 1,
|
||||
A = 1
|
||||
},
|
||||
Sprite = "",
|
||||
UvTransform = {
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Z = 1,
|
||||
W = 1
|
||||
}
|
||||
},
|
||||
TransformComponent = {
|
||||
Position = {
|
||||
X = 15.2321301,
|
||||
Y = -1.98709249,
|
||||
Z = 0
|
||||
},
|
||||
Rotation = {
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Z = 0.134986058,
|
||||
W = 0.990847468
|
||||
},
|
||||
Scale = {
|
||||
X = 13.9764566,
|
||||
Y = 1,
|
||||
Z = 1
|
||||
},
|
||||
EditorEulerRotation = {
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Z = 0.270798802
|
||||
}
|
||||
},
|
||||
Rigidbody2D = {
|
||||
BodyType = .Static,
|
||||
FixedRotation = false
|
||||
},
|
||||
BoxCollider2D = {
|
||||
Offset = {
|
||||
X = 0,
|
||||
Y = 0
|
||||
},
|
||||
Size = {
|
||||
X = 0.5,
|
||||
Y = 0.5
|
||||
},
|
||||
Density = 1,
|
||||
Friction = 0.5,
|
||||
Restitution = 0.800000012,
|
||||
RestitutionThreshold = 0.5
|
||||
}
|
||||
},
|
||||
{
|
||||
Id = 6861260438693901700,
|
||||
NameComponent = {
|
||||
Name = "Floor 4"
|
||||
},
|
||||
SpriteRendererComponent = {
|
||||
Color = {
|
||||
R = 0.176544324,
|
||||
G = 0.666713238,
|
||||
B = 0.687030852,
|
||||
A = 1
|
||||
},
|
||||
Sprite = "",
|
||||
UvTransform = {
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Z = 1,
|
||||
W = 1
|
||||
}
|
||||
},
|
||||
TransformComponent = {
|
||||
Position = {
|
||||
X = -6.27842855,
|
||||
Y = 2.62550211,
|
||||
Z = 0
|
||||
},
|
||||
Rotation = {
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Z = 0.187087834,
|
||||
W = 0.982343197
|
||||
},
|
||||
Scale = {
|
||||
X = 0.999999523,
|
||||
Y = 6.49634314,
|
||||
Z = 1
|
||||
},
|
||||
EditorEulerRotation = {
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Z = 0.376393586
|
||||
}
|
||||
},
|
||||
Rigidbody2D = {
|
||||
BodyType = .Static,
|
||||
FixedRotation = false
|
||||
},
|
||||
BoxCollider2D = {
|
||||
Offset = {
|
||||
X = 0,
|
||||
Y = 0
|
||||
},
|
||||
Size = {
|
||||
X = 0.5,
|
||||
Y = 0.5
|
||||
},
|
||||
Density = 1,
|
||||
Friction = 0.5,
|
||||
Restitution = 0.899999976,
|
||||
RestitutionThreshold = 0.5
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -206,6 +206,20 @@ namespace GlitchyEditor
|
||||
// Clear the swapchain-buffer
|
||||
RenderCommand.Clear(null, .Color | .Depth, .(0.2f, 0.2f, 0.2f), 1.0f, 0);
|
||||
|
||||
RenderCommand.SetBlendState(_alphaBlendState);
|
||||
RenderCommand.SetDepthStencilState(_depthStencilState);
|
||||
|
||||
if (_editor.GameViewportWindow.Visible)
|
||||
{
|
||||
_gameSceneRenderer.Scene = _activeScene;
|
||||
|
||||
_activeScene.SetViewportSize((uint32)_editor.GameViewportWindow.ViewportSize.X, (uint32)_editor.GameViewportWindow.ViewportSize.Y);
|
||||
_gameSceneRenderer.SetViewportSize((uint32)_editor.GameViewportWindow.ViewportSize.X, (uint32)_editor.GameViewportWindow.ViewportSize.Y);
|
||||
|
||||
RenderCommand.Clear(_gameViewportTarget, .Color | .Depth, .(0.2f, 0.2f, 0.2f), 1.0f, 0);
|
||||
_gameSceneRenderer.RenderRuntime(gameTime, _gameViewportTarget);
|
||||
}
|
||||
|
||||
RenderCommand.SetBlendState(_alphaBlendState);
|
||||
RenderCommand.SetDepthStencilState(_depthStencilState);
|
||||
|
||||
@@ -215,21 +229,16 @@ namespace GlitchyEditor
|
||||
|
||||
_editorSceneRenderer.Scene = _activeScene;
|
||||
|
||||
// Only use aspect ratio of editor-window, if game is not being rendered
|
||||
if (!_editor.GameViewportWindow.Visible)
|
||||
_activeScene.SetViewportSize((uint32)_editor.SceneViewportWindow.ViewportSize.X, (uint32)_editor.SceneViewportWindow.ViewportSize.Y);
|
||||
|
||||
_editorSceneRenderer.SetViewportSize((uint32)_editor.SceneViewportWindow.ViewportSize.X, (uint32)_editor.SceneViewportWindow.ViewportSize.Y);
|
||||
|
||||
RenderCommand.Clear(_editorViewportTarget, .Color | .Depth, .(0.2f, 0.2f, 0.2f), 1.0f, 0);
|
||||
_editorSceneRenderer.RenderEditor(gameTime, _camera, _editorViewportTarget, scope => DebugDraw3D, scope => DebugDraw2D);
|
||||
}
|
||||
|
||||
RenderCommand.SetBlendState(_alphaBlendState);
|
||||
RenderCommand.SetDepthStencilState(_depthStencilState);
|
||||
|
||||
if (_editor.GameViewportWindow.Visible)
|
||||
{
|
||||
_gameSceneRenderer.Scene = _activeScene;
|
||||
|
||||
RenderCommand.Clear(_gameViewportTarget, .Color | .Depth, .(0.2f, 0.2f, 0.2f), 1.0f, 0);
|
||||
_gameSceneRenderer.RenderRuntime(gameTime, _gameViewportTarget);
|
||||
}
|
||||
|
||||
RenderCommand.UnbindRenderTargets();
|
||||
RenderCommand.SetRenderTarget(null, 0, true);
|
||||
RenderCommand.BindRenderTargets();
|
||||
@@ -742,7 +751,7 @@ namespace GlitchyEditor
|
||||
|
||||
_camera.OnViewportResize(sizeX, sizeY);
|
||||
|
||||
_editorSceneRenderer.OnViewportResize(sizeX, sizeY);
|
||||
_activeScene.SetViewportSize(sizeX, sizeY);
|
||||
}
|
||||
|
||||
private void GameViewportSizeChanged(Object sender, float2 viewportSize)
|
||||
@@ -755,9 +764,7 @@ namespace GlitchyEditor
|
||||
|
||||
_gameViewportTarget.Resize(sizeX, sizeY);
|
||||
|
||||
_activeScene.OnViewportResize(sizeX, sizeY);
|
||||
|
||||
_gameSceneRenderer.OnViewportResize(sizeX, sizeY);
|
||||
_activeScene.SetViewportSize(sizeX, sizeY);
|
||||
}
|
||||
|
||||
private bool OnKeyPressed(KeyPressedEvent e)
|
||||
|
||||
Reference in New Issue
Block a user