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:
Simon Lübeß
2023-07-17 20:27:18 +02:00
parent 66d66d3308
commit e8505f6bdb
16 changed files with 492 additions and 61 deletions
@@ -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>