Skip to content

Commit

Permalink
#9
Browse files Browse the repository at this point in the history
+ changed workflow to more separation bewteen data and logic
#10
+ added lookDirectionInput system to turn player towards mouse position on screen
  • Loading branch information
Ammon committed May 26, 2020
1 parent 1a5ab92 commit 1445919
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Assets/PlayerInputAction/PlayerAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public @PlayerAction()
{
""name"": """",
""id"": ""8c8e490b-c610-4785-884f-f04217b23ca4"",
""path"": ""<Pointer>/delta"",
""path"": ""<Pointer>/position"",
""interactions"": """",
""processors"": """",
""groups"": "";Keyboard&Mouse;Touch"",
Expand Down
2 changes: 1 addition & 1 deletion Assets/PlayerInputAction/PlayerAction.inputactions
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
{
"name": "",
"id": "8c8e490b-c610-4785-884f-f04217b23ca4",
"path": "<Pointer>/delta",
"path": "<Pointer>/position",
"interactions": "",
"processors": "",
"groups": ";Keyboard&Mouse;Touch",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ public struct InputHoldComponent : IComponentData
// authoring the data in the Editor.

public float3 Value;
}
}

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,10 @@
using System;
using Unity.Entities;
using Unity.Mathematics;

[Serializable]
[GenerateAuthoringComponent]
public struct LookDirectionInputComponent : IComponentData
{
public float2 Value;
}

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,11 @@
using System;
using Unity.Entities;
using Unity.Mathematics;

[Serializable]
[GenerateAuthoringComponent]
public struct MovementDirectionInputComponent : IComponentData
{
public float2 NewValue;
public float2 OldValue;
}

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

35 changes: 35 additions & 0 deletions Assets/Scripts/PhysicsBasedMovement/Player 1.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ GameObject:
- component: {fileID: 4260431165245277472}
- component: {fileID: 4064267118410567286}
- component: {fileID: 4064267118410567305}
- component: {fileID: 1348250988532382530}
- component: {fileID: 1452994389441653916}
m_Layer: 0
m_Name: Player 1
m_TagString: Untagged
Expand Down Expand Up @@ -263,3 +265,36 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
ConversionMode: 0
--- !u!114 &1348250988532382530
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4064267118410567304}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: b36b8ab66d35eb04f9ec42e835094596, type: 3}
m_Name:
m_EditorClassIdentifier:
NewValue:
x: 0
y: 0
OldValue:
x: 0
y: 0
--- !u!114 &1452994389441653916
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4064267118410567304}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4a2fd68ef26ebb947986cf1598670dc8, type: 3}
m_Name:
m_EditorClassIdentifier:
Value:
x: 0
y: 0
56 changes: 40 additions & 16 deletions Assets/Scripts/PhysicsBasedMovement/System/InputSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,36 @@ public class InputSystem : SystemBase
{
private PlayerAction PlayerInput;
private EntityManager Manager;
private float2 OldMovementDirectionOrder;
public NativeArray<Vector2> MovementDirectionOrder;
private float2 OldMovementDirectionInput;
private Entity Player;

protected override void OnCreate()
{
PlayerInput = new PlayerAction();
MovementDirectionOrder = new NativeArray<Vector2>(1, Allocator.Persistent);

Manager = World
.DefaultGameObjectInjectionWorld
.EntityManager;

activateDodgeEvents();
activatingMovementEvents();
activateLookEvents();
}

protected override void OnDestroy()
{
MovementDirectionOrder.Dispose();
deactivateDodgeEvents();
deactivatingMovementEvents();
deactivateLookEvents();
}

#region Activation Behaviour
protected override void OnStartRunning()
{
//Debug.Log("width: " + Camera.main.scaledPixelWidth + ", height: " + Camera.main.scaledPixelHeight);
PlayerInput.Enable();

var Player = GetSingletonEntity<PlayerTag>();
Player = GetSingletonEntity<PlayerTag>();
Manager.AddComponentData(Player, new InputHoldComponent { Value = 0 });
var buffer = GetBufferFromEntity<LinkedEntityGroup>()[Player].Reinterpret<Entity>();
foreach (var entity in buffer)
Expand All @@ -59,6 +62,7 @@ protected override void OnStopRunning()

private void activateDodgeEvents()
{
// Dodge Input
PlayerInput.Player.DodgeForwardPressAsButton.performed += _ => readDodgeInput();
}

Expand All @@ -84,11 +88,26 @@ private void activatingMovementEvents()

}

private void activateLookEvents()
{
// look input
PlayerInput.Player.Look.performed +=
_ => readLookInput(PlayerInput.Player.Look.ReadValue<Vector2>());
}

private void deactivateDodgeEvents()
{
// DodgeInput
PlayerInput.Player.DodgeForwardPressAsButton.performed -= _ => readDodgeInput();
}

private void deactivateLookEvents()
{
// look input
PlayerInput.Player.Look.performed -=
_ => readLookInput(PlayerInput.Player.Look.ReadValue<Vector2>());
}

private void deactivatingMovementEvents()
{
// basic movement
Expand All @@ -114,34 +133,41 @@ private void deactivatingMovementEvents()
#endregion

#region Reading input
private void readMovementInput(Vector2 MovementDirection)
private void readMovementInput(float2 MovementDirection)
{
MovementDirectionOrder[0] = MovementDirection;
var movementInput = GetComponent<MovementDirectionInputComponent>(Player);
movementInput.NewValue = MovementDirection;
SetComponent<MovementDirectionInputComponent>(Player, movementInput);
}

private void readDodgeInput()
{

}

private void readLookInput(float2 Direction)
{
var lookInput = GetComponent<LookDirectionInputComponent>(Player);
lookInput.Value = Direction;
SetComponent<LookDirectionInputComponent>(Player, lookInput);
}

#endregion


protected override void OnUpdate()
{
var maxDurationValue = new float3(.8f, 0, .8f);
var nextMoveOrder = new float2(MovementDirectionOrder[0]);
var oldOrder = new NativeArray<float2>(1, Allocator.TempJob);
oldOrder[0] = OldMovementDirectionOrder;

var DeltaTime = Time.DeltaTime;
var handle = Entities.WithName("GetInputHoldDuration")
.WithAll<PlayerTag>()
.WithNone<Prefab>()
.ForEach(
(ref InputHoldComponent InputHoldComponent) =>
(ref InputHoldComponent InputHoldComponent, ref MovementDirectionInputComponent movementInput) =>
{
if (nextMoveOrder.Equals(oldOrder[0])
&& !(nextMoveOrder.Equals(float2.zero)))
if (movementInput.NewValue.Equals(movementInput.OldValue)
&& !(movementInput.NewValue.Equals(float2.zero)))
{
var holdValue = DeltaTime * 2; // increased value to make startup velocity a bit faster
Expand All @@ -152,11 +178,9 @@ protected override void OnUpdate()
else
{ InputHoldComponent.Value = float3.zero; }
oldOrder[0] = nextMoveOrder;
movementInput.OldValue = movementInput.NewValue;
}
).Schedule(Dependency);
handle.Complete();
OldMovementDirectionOrder = oldOrder[0];
oldOrder.Dispose();
}
}
7 changes: 4 additions & 3 deletions Assets/Scripts/PhysicsBasedMovement/System/MovementSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ protected override void OnCreate()

protected override void OnUpdate()
{
var ecb = endSimulationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent();
var Player = GetSingletonEntity<PlayerTag>();
var getHoldInputDuration = GetComponentDataFromEntity<InputHoldComponent>(true);
var nextOrderFromQueue = InputSystem.MovementDirectionOrder[0];
var getNextMovementInput = GetComponentDataFromEntity<MovementDirectionInputComponent>(true);
float3 maxVelocity = new float3(9.81f, 9.81f, 9.81f); // incorporating terminal velocity (no free fall)

Entities.WithName("Move_Player")
Expand All @@ -35,7 +34,8 @@ protected override void OnUpdate()
.ForEach(
(int entityInQueryIndex, ref PhysicsVelocity physicsVelocity) =>
{
var moveOrder = new float3(nextOrderFromQueue.x, 0, nextOrderFromQueue.y);
var movementInput = getNextMovementInput[Player];
var moveOrder = new float3(movementInput.NewValue.x, 0, movementInput.NewValue.y);
var holdDuration = getHoldInputDuration[Player];
if (holdDuration.Value.Equals(float3.zero) && moveOrder.Equals(float3.zero))
Expand All @@ -49,6 +49,7 @@ protected override void OnUpdate()
}
}
)
.WithReadOnly(getNextMovementInput)
.WithReadOnly(getHoldInputDuration)
.Schedule();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
using Unity.Physics;
using Unity.Physics.Extensions;

public class PlayerInputTurnSystem : SystemBase
{
protected override void OnUpdate()
{
Entities.WithName("TurnPlayerTowardsInput")
.WithAll<PlayerPhysicsTag>()
.WithNone<Prefab>()
.ForEach(
(ref PhysicsVelocity Veclocity, in PhysicsMass Mass) =>
{
//ComponentExtensions.ApplyAngularImpulse(ref Veclocity, Mass, float3.zero);
}
).Schedule();
}
}

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

0 comments on commit 1445919

Please sign in to comment.