Skip to content

Commit

Permalink
Move characters back if they go outside of the world boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
laicasaane committed Oct 10, 2024
1 parent 02999cc commit 4ed6577
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Assets/Code/Components/Components.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public struct SpawnRange : IComponentData
public float3 max;
}

public struct WorldBoundary : IComponentData
{
public float3 min;
public float3 max;
}

public struct Randomizer : IComponentData
{
public Unity.Mathematics.Random value;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Unity.Entities;
using UnityEngine;

namespace SimpleSetupEcs2d
{
[UpdateInGroup(typeof(InitializeSystemGroup))]
public sealed partial class InitializeWorldBoundarySystem : SystemBase
{
protected override void OnCreate()
{
var query = SystemAPI.QueryBuilder()
.WithNone<WorldBoundary>()
.Build();

RequireForUpdate(query);
}

protected override void OnUpdate()
{
if (Camera.main == false)
{
return;
}

var cam = Camera.main;
var camTrans = cam.transform;

#if UNITY_EDITOR
UnityEditor.PlayModeWindow.GetRenderingResolution(out var resolutionWidth, out var resolutionHeight);
#else
var resolution = Screen.currentResolution;
var resolutionWidth = resolution.width;
var resolutionHeight = resolution.height;
#endif

var scaleWidthFactor = resolutionWidth / (float)resolutionHeight;
var height = cam.orthographicSize * 2f;
var width = height * scaleWidthFactor;

var rect = new Rect(
new(camTrans.position.x - (width * 0.5f), camTrans.position.y - (height * 0.5f))
, new(width, height)
);

var boundary = new WorldBoundary {
min = new(rect.min, 0f),
max = new(rect.max, 0f),
};

EntityManager.CreateSingleton(boundary, nameof(WorldBoundary));
CheckedStateRef.Enabled = false;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;

namespace SimpleSetupEcs2d
{
[BurstCompile]
[UpdateInGroup(typeof(SimulationSystemGroup))]
[UpdateAfter(typeof(MoveSystem))]
public partial struct MoveBackFromOutsideWorldBoundarySystem : ISystem
{
private EntityQuery _boundaryQuery;
private EntityQuery _entityQuery;

[BurstCompile]
public void OnCreate(ref SystemState state)
{
_boundaryQuery = SystemAPI.QueryBuilder()
.WithAll<WorldBoundary>()
.Build();

_entityQuery = SystemAPI.QueryBuilder()
.WithAll<FaceDirection>()
.WithAllRW<LocalTransform>()
.WithAll<CanMoveTag>()
.WithDisabled<NeedsInitComponentsTag>()
.WithDisabled<NeedsDestroyTag>()
.Build();

state.RequireForUpdate(_boundaryQuery);
state.RequireForUpdate(_entityQuery);
}

[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var boundary = _boundaryQuery.GetSingleton<WorldBoundary>();

var job = new MoveBackFromOutsideWorldBoundaryJob {
boundary = boundary,
padding = 1f,
};

state.Dependency = job.ScheduleParallel(_entityQuery, state.Dependency);
}

[BurstCompile]
private partial struct MoveBackFromOutsideWorldBoundaryJob : IJobEntity
{
public WorldBoundary boundary;
public float padding;

private void Execute(in FaceDirection faceDirection, ref LocalTransform transform)
{
var position = transform.Position;
var min = boundary.min;
var max = boundary.max;
var xToTheRight = math.select(position.x, min.x - padding, position.x > max.x + padding);
var xToTheLeft = math.select(position.x, max.x + padding, position.x < min.x - padding);
position.x = math.select(xToTheLeft, xToTheRight, faceDirection.value > 0);

transform.Position = position;
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4ed6577

Please sign in to comment.