Skip to content

Commit

Permalink
Add player terrain modification, refactor namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
jeickhoff committed Jul 2, 2023
1 parent 833db7e commit 243b58c
Show file tree
Hide file tree
Showing 36 changed files with 759 additions and 356 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "Player",
"name": "PlayerInput",
"maps": [
{
"name": "Player Controls",
Expand Down Expand Up @@ -49,6 +49,24 @@
"processors": "",
"interactions": "Press",
"initialStateCheck": false
},
{
"name": "PrimaryAction",
"type": "Button",
"id": "3956cd52-8185-41fb-9912-de7c4f0f4020",
"expectedControlType": "Button",
"processors": "",
"interactions": "",
"initialStateCheck": false
},
{
"name": "SecondaryAction",
"type": "Button",
"id": "cded7730-b8f6-4797-9655-63d192ed9ee0",
"expectedControlType": "Button",
"processors": "",
"interactions": "",
"initialStateCheck": false
}
],
"bindings": [
Expand Down Expand Up @@ -249,6 +267,72 @@
"action": "Start",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "a24fee5f-2c51-49f3-be41-c09aa9e8ed71",
"path": "<Mouse>/leftButton",
"interactions": "Press",
"processors": "",
"groups": "Keyboard And Mouse",
"action": "PrimaryAction",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "a84268ce-bad2-40e7-ad9c-c80452eb88e1",
"path": "<Gamepad>/buttonEast",
"interactions": "Press",
"processors": "",
"groups": "Gamepad",
"action": "PrimaryAction",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "5ed49228-6ee5-44d1-ad90-f380c4d08248",
"path": "<Touchscreen>/touch1/tap",
"interactions": "Press",
"processors": "",
"groups": "Touchscreen",
"action": "PrimaryAction",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "477d91ba-8d21-4cf1-91d2-773df4d2eb77",
"path": "<Mouse>/rightButton",
"interactions": "Press",
"processors": "",
"groups": "Keyboard And Mouse",
"action": "SecondaryAction",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "7d60dc97-7615-402c-af85-c9d07ed6d266",
"path": "<Gamepad>/buttonWest",
"interactions": "Press",
"processors": "",
"groups": "Gamepad",
"action": "SecondaryAction",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "593c0891-f303-4df0-ae09-5ae5ebf49933",
"path": "<Touchscreen>/touch2/tap",
"interactions": "",
"processors": "",
"groups": "Touchscreen",
"action": "SecondaryAction",
"isComposite": false,
"isPartOfComposite": false
}
]
},
Expand Down
File renamed without changes.
36 changes: 36 additions & 0 deletions Assets/Prefabs/MultiplayPlayer.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,40 @@ MonoBehaviour:
m_CallState: 2
m_ActionId: f5459e2b-435f-4c28-808d-ce074d5df92d
m_ActionName: Player Controls/Start[/Keyboard/tab]
- m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 3403226978593572324}
m_TargetAssemblyTypeName: Opencraft.Player.Multiplay.MultiplayPlayerController,
Assembly-CSharp
m_MethodName: OnPrimaryAction
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
m_ActionId: 3956cd52-8185-41fb-9912-de7c4f0f4020
m_ActionName: Player Controls/PrimaryAction[/Mouse/leftButton,/XInputControllerWindows/buttonEast]
- m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 3403226978593572324}
m_TargetAssemblyTypeName: Opencraft.Player.Multiplay.MultiplayPlayerController,
Assembly-CSharp
m_MethodName: OnSecondaryAction
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
m_ActionId: cded7730-b8f6-4797-9655-63d192ed9ee0
m_ActionName: Player Controls/SecondaryAction[/Mouse/rightButton,/XInputControllerWindows/buttonWest]
m_DefaultActionMap: Player Controls
--- !u!20 &3141172503242693401
Camera:
Expand Down Expand Up @@ -288,6 +322,8 @@ MonoBehaviour:
inputLook: {x: 0, y: 0}
inputJump: 0
inputStart: 0
inputPrimaryAction: 0
inputSecondaryAction: 0
playerEntityExists: 0
playerEntityRequestSent: 0
debugText: {fileID: 6289289848544400366}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scenes/AuthoringScene/AuthoringScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!64 &382087691
MeshCollider:
m_ObjectHideFlags: 0
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/DebugStatsSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Opencraft.Terrain;
using Opencraft.Terrain.Authoring;
using Unity.Entities;

namespace Opencraft
Expand Down
6 changes: 1 addition & 5 deletions Assets/Scripts/Networking/NetCodeBootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,8 @@ public static World CreateStreamClientWorld(string name)
// so we use MultiplayInitSystem to start the relevant MonoBehaviours
var systems = new List<Type> { typeof(MultiplayInitSystem) };
DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(world, systems);

#if UNITY_DOTSRUNTIME
AppendWorldToClientTickWorld(world);
#else

ScriptBehaviourUpdateOrder.AppendWorldToCurrentPlayerLoop(world);
#endif

if (World.DefaultGameObjectInjectionWorld == null)
World.DefaultGameObjectInjectionWorld = world;
Expand Down
14 changes: 14 additions & 0 deletions Assets/Scripts/Player/Authoring/PlayerAuthoring.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Opencraft.Terrain.Utilities;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
Expand All @@ -22,6 +23,16 @@ public struct Player : IComponentData
[GhostField] public int Username;
public BlobAssetReference<BlobString> multiplayConnectionID;
}

// Component marking this entity as having a specific block selected.
// Neighbor block refers to the block neighbor of the selected block closest to this entity
public struct SelectedBlock : IComponentData
{
public int terrainAreaIndex;
public int3 blockLoc;
public int neighborTerrainAreaIndex;
public int3 neighborBlockLoc;
}


// Similar to NewSpawn, marks this player entity as freshly instantiated
Expand Down Expand Up @@ -52,12 +63,14 @@ public struct PlayerInput : IInputComponentData
readonly RefRW<PhysicsVelocity> m_Velocity;
readonly RefRO<PlayerInput> m_Input;
readonly RefRO<GhostOwner> m_Owner;
readonly RefRW<SelectedBlock> m_SelectedBlock;

public AutoCommandTarget AutoCommandTarget => m_AutoCommandTarget.ValueRO;
public PlayerInput Input => m_Input.ValueRO;
public int OwnerNetworkId => m_Owner.ValueRO.NetworkId;
public ref Player Player => ref m_Character.ValueRW;
public ref PhysicsVelocity Velocity => ref m_Velocity.ValueRW;
public ref SelectedBlock SelectedBlock => ref m_SelectedBlock.ValueRW;
}

[DisallowMultipleComponent]
Expand All @@ -76,6 +89,7 @@ public override void Bake(PlayerAuthoring authoring)
PlayerConfig = GetEntity(authoring.playerConfig.gameObject, TransformUsageFlags.Dynamic)
});
AddComponent(entity, new NewPlayer());
AddComponent(entity, new SelectedBlock());
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion Assets/Scripts/Player/Multiplay/Multiplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ IEnumerator ConnectGuest()

if (settings != null)
receiveVideoViewer.SetCodec(settings.ReceiverVideoCodec);


//todo hacky wait for the signalling server to connect
yield return new WaitForSeconds(1f);

handler.CreateConnection(connectionId);
yield return new WaitUntil(() => handler.IsConnected(connectionId));
}
Expand Down
19 changes: 18 additions & 1 deletion Assets/Scripts/Player/Multiplay/MultiplayPlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public class MultiplayPlayerController : MonoBehaviour
public Vector2 inputLook;
public bool inputJump;
public bool inputStart;

public bool inputPrimaryAction;
public bool inputSecondaryAction;
public bool playerEntityExists;
public bool playerEntityRequestSent;
public Entity playerEntity;
Expand Down Expand Up @@ -104,6 +105,22 @@ public void OnStart(InputAction.CallbackContext value)
inputStart = true;
}
}

public void OnPrimaryAction(InputAction.CallbackContext value)
{
if (value.performed)
{
inputPrimaryAction = true;
}
}

public void OnSecondaryAction(InputAction.CallbackContext value)
{
if (value.performed)
{
inputSecondaryAction = true;
}
}
}
}

75 changes: 75 additions & 0 deletions Assets/Scripts/Player/PlayerActionSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Opencraft.Player.Authoring;
using Opencraft.Terrain.Authoring;
using Opencraft.Terrain.Utilities;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Transforms;

namespace Opencraft.Player
{
// Handle the terrain modification events specifically
[UpdateInGroup(typeof(SimulationSystemGroup))]
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
[BurstCompile]
public partial struct PlayerActionSystem : ISystem
{
private BufferLookup<TerrainBlocks> _terrainBlockLookup;
private NativeArray<Entity> terrainAreasEntities;

[BurstCompile]
public void OnCreate(ref SystemState state)
{

state.RequireForUpdate<Authoring.Player>();
state.RequireForUpdate<TerrainArea>();
state.RequireForUpdate<TerrainSpawner>();
_terrainBlockLookup = state.GetBufferLookup<TerrainBlocks>(false);

}

[BurstCompile]
public void OnUpdate(ref SystemState state)
{
state.CompleteDependency();
_terrainBlockLookup.Update(ref state);
var terrainAreasQuery = SystemAPI.QueryBuilder().WithAll<TerrainArea, LocalTransform>().Build();
terrainAreasEntities = terrainAreasQuery.ToEntityArray(state.WorldUpdateAllocator);

foreach (var player in SystemAPI.Query<PlayerAspect>().WithAll<Simulate>())
{
// Destroy block action
if (player.Input.PrimaryAction.IsSet && player.SelectedBlock.terrainAreaIndex != -1)
{

Entity terrainAreaEntity = terrainAreasEntities[player.SelectedBlock.terrainAreaIndex];
if(_terrainBlockLookup.TryGetBuffer(terrainAreaEntity, out DynamicBuffer<TerrainBlocks> terrainBlocks))
{
int blockIndex = TerrainUtilities.BlockLocationToIndex(ref player.SelectedBlock.blockLoc);
DynamicBuffer<int> blocks = terrainBlocks.Reinterpret<int>();
if (blocks[blockIndex] != -1)
{
blocks[blockIndex] = -1;
}
}

}
// Place block action, using the neighbor of selected block
if (player.Input.SecondaryAction.IsSet && player.SelectedBlock.neighborTerrainAreaIndex != -1)
{
Entity terrainAreaEntity = terrainAreasEntities[player.SelectedBlock.neighborTerrainAreaIndex];
if(_terrainBlockLookup.TryGetBuffer(terrainAreaEntity, out DynamicBuffer<TerrainBlocks> terrainBlocks))
{
int blockIndex = TerrainUtilities.BlockLocationToIndex(ref player.SelectedBlock.neighborBlockLoc);
DynamicBuffer<int> blocks = terrainBlocks.Reinterpret<int>();
if (blocks[blockIndex] == -1)
{
blocks[blockIndex] = 1;
}
}
}
}

}
}
}
3 changes: 3 additions & 0 deletions Assets/Scripts/Player/PlayerActionSystem.cs.meta

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

19 changes: 17 additions & 2 deletions Assets/Scripts/Player/PlayerInputSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ public void OnUpdate(ref SystemState state)

input.ValueRW.Movement = default;
input.ValueRW.Jump = default;
input.ValueRW.PrimaryAction= default;
input.ValueRW.SecondaryAction= default;


// Movement
input.ValueRW.Movement.x = playerController.inputMovement.x;
input.ValueRW.Movement.y = playerController.inputMovement.y;
if (playerController.inputJump)
Expand All @@ -52,15 +54,28 @@ public void OnUpdate(ref SystemState state)
playerController.inputJump = false;
}

// Look
input.ValueRW.Pitch = math.clamp(input.ValueRW.Pitch + playerController.inputLook.y, -math.PI / 2,
math.PI / 2);
input.ValueRW.Yaw = math.fmod(input.ValueRW.Yaw + playerController.inputLook.x, 2 * math.PI);


// Sync camera to look
playerObj.transform.rotation = math.mul(quaternion.RotateY(input.ValueRO.Yaw),
quaternion.RotateX(-input.ValueRO.Pitch));
var offset = math.rotate(playerObj.transform.rotation, _cameraOffset);
playerObj.transform.position = localToWorld.ValueRO.Position + offset;

// Action buttons
if (playerController.inputPrimaryAction)
{
input.ValueRW.PrimaryAction.Set();
playerController.inputPrimaryAction= false;
}
if (playerController.inputSecondaryAction)
{
input.ValueRW.SecondaryAction.Set();
playerController.inputSecondaryAction= false;
}
}
}
}
Expand Down
Loading

0 comments on commit 243b58c

Please sign in to comment.