-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move characters back if they go outside of the world boundary
- Loading branch information
1 parent
02999cc
commit 4ed6577
Showing
5 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
Assets/Code/Systems.Initialization/InitializeGroup/InitializeWorldBoundarySystem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Code/Systems.Initialization/InitializeGroup/InitializeWorldBoundarySystem.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
Assets/Code/Systems.Simulation/MoveBackFromOutsideWorldBoundarySystem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Assets/Code/Systems.Simulation/MoveBackFromOutsideWorldBoundarySystem.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.