Skip to content

Commit

Permalink
drop items from Inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
7ubi committed Jul 3, 2021
1 parent e537e9d commit 8e438ee
Show file tree
Hide file tree
Showing 10 changed files with 521 additions and 34 deletions.
9 changes: 5 additions & 4 deletions Assets/Scenes/Game.unity
Original file line number Diff line number Diff line change
Expand Up @@ -2790,6 +2790,7 @@ MonoBehaviour:
inventory: {fileID: 1743702931}
image: {fileID: 1446842824}
text: {fileID: 101661990}
worldCreation: {fileID: 1731320882}
--- !u!222 &477995640
CanvasRenderer:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -4122,13 +4123,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 914612302}
m_LocalRotation: {x: 0.7071068, y: 0, z: -0.7071068, w: 0}
m_LocalPosition: {x: -0, y: 0.00893, z: 0.00439}
m_LocalRotation: {x: 0.5, y: -0.5, z: -0.5, w: 0.5}
m_LocalPosition: {x: 0, y: 0.00893, z: -0.00025}
m_LocalScale: {x: 0.00027, y: 0.00027, z: 0.00027}
m_Children: []
m_Father: {fileID: 260724419}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 180, y: 90, z: 0}
m_LocalEulerAnglesHint: {x: 180, y: 90, z: 90}
--- !u!23 &914612304
MeshRenderer:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -8416,7 +8417,7 @@ MonoBehaviour:
- {fileID: 92023475}
- {fileID: 92023475}
- {fileID: 92023475}
destroyedBlockReach: 2
destroyedBlockReach: 1
itemHandel: {fileID: 914612302}
blockHandel: {fileID: 1400752257}
voxelizer: {fileID: 1743702926}
Expand Down
44 changes: 26 additions & 18 deletions Assets/Scripts/Player/PlayerInventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class PlayerInventory : MonoBehaviour

[Header("ItemHandel")]
[SerializeField] private GameObject itemHandel;

public Transform ItemHandelTransform => itemHandel.transform;
[SerializeField] private GameObject blockHandel;
[SerializeField] private Voxelizer voxelizer;
private MeshFilter _itemHandelMesh;
Expand Down Expand Up @@ -101,7 +103,10 @@ private void Update()

selector.localPosition = new Vector2(-400 + Current * 100, selector.localPosition.y);


if (!InInventory && Input.GetKeyDown(KeyCode.Q))
{
Drop();
}

if (!Input.GetKeyDown(KeyCode.E)) return;
cellParent.gameObject.SetActive(!cellParent.gameObject.activeInHierarchy);
Expand Down Expand Up @@ -499,23 +504,7 @@ private void CreateHandel()
}
}

public void Drop(int index)
{
if (ItemIds[index] == 0) return;

worldCreation.CreateDestroyedBlock(ItemIds[index], transform.position + transform.forward * 3f);
ItemCount[index] -= 1;


if (ItemCount[index] == 0)
{
ItemIds[index] = 0;
itemImages[index].sprite = null;
itemImages[index].color = new Color(255, 255, 255, 0);
}

UpdateText(index);
}


private void OpenCrafting()
{
Expand All @@ -532,4 +521,23 @@ public void OpenFurnace()
crafting.SetActive(false);
furnace.SetActive(true);
}

private void Drop()
{
if (ItemIds[Current] == 0) return;
AddDestroyedBlock(worldCreation.CreateDestroyedBlock(ItemIds[Current],
ItemHandelTransform.position + transform.forward * 2f));
ItemCount[Current] -= 1;


if (ItemCount[Current] == 0)
{
ItemCount[Current] = 0;
ItemIds[Current] = 0;
itemImages[Current].sprite = null;
itemImages[Current].color = new Color(255, 255, 255, 0);
}

UpdateText(Current);
}
}
21 changes: 21 additions & 0 deletions Assets/Scripts/UI/InventoryCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class InventoryCell : MonoBehaviour
[SerializeField] private PlayerInventory inventory;
[SerializeField] private Image image;
[SerializeField] private TMP_Text text;
[SerializeField] private worldCreation worldCreation;

public int Id { get; set; }
public int Count { get; set; }
Expand All @@ -32,6 +33,11 @@ private void Update()

_rectTransform.anchoredPosition = new Vector2(Input.mousePosition.x * _scaler.referenceResolution.x / Screen.width,
Input.mousePosition.y * _scaler.referenceResolution.y / Screen.height);

if (Input.GetKeyDown(KeyCode.Q))
{
Drop();
}
}

public void SetSprite(Sprite icon, int id, int count, int lastIndex)
Expand Down Expand Up @@ -65,4 +71,19 @@ public void ResetToOriginalPos()
inventory.SetItem(LastIndex, Id, Count);
Reset();
}

// ReSharper disable Unity.PerformanceAnalysis
private void Drop()
{
if (Id == 0) return;
inventory.AddDestroyedBlock(worldCreation.CreateDestroyedBlock(Id,
inventory.ItemHandelTransform.position + inventory.transform.forward * 2f));
Count -= 1;


if (Count == 0)
Reset();

UpdateText();
}
}
4 changes: 3 additions & 1 deletion Assets/Scripts/World/Blocks/TNT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ private void Explode()
if (chunk.BlockIDs[bix, biy, biz] == 0)
continue;

playerInventory.AddDestroyedBlock(worldCreation.CreateDestroyedBlock(chunk.BlockIDs[bix, biy, biz], pos + new Vector3(0.375f, 0.1f, 0.375f)));
playerInventory.AddDestroyedBlock(worldCreation.CreateDestroyedBlock(
worldCreation.Blocks[chunk.BlockIDs[bix, biy, biz]].DropID,
pos + new Vector3(0.375f, 0.1f, 0.375f)));

chunk.BlockIDs[bix, biy, biz] = 0;

Expand Down
22 changes: 13 additions & 9 deletions Assets/Scripts/World/worldCreation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,16 @@ public void DestroyBlock(Vector3 block)
{

var chunck = GetChunck(block);

var bix = Mathf.FloorToInt(block.x) - (int)chunck.transform.position.x;

var position = chunck.transform.position;
var bix = Mathf.FloorToInt(block.x) - (int)position.x;
var biy = Mathf.FloorToInt(block.y);
var biz = Mathf.FloorToInt(block.z) - (int)chunck.transform.position.z;
var biz = Mathf.FloorToInt(block.z) - (int)position.z;
var c = chunck.GetComponent<Chunck>();

_playerInventory.AddDestroyedBlock(CreateDestroyedBlock(c.BlockIDs[bix, biy, biz], block + new Vector3(0.375f, 0.1f, 0.375f)));
_playerInventory.AddDestroyedBlock(
CreateDestroyedBlock(Blocks[c.BlockIDs[bix, biy, biz]].DropID,
block + new Vector3(0.375f, 0.1f, 0.375f)));

c.BlockIDs[bix, biy, biz] = 0;

Expand Down Expand Up @@ -516,13 +519,14 @@ public int GetHeight(float x, float z)
return height;
}

// ReSharper disable Unity.PerformanceAnalysis
public GameObject CreateDestroyedBlock(int id, Vector3 pos)
{
if (Blocks[id].DropID < _playerInventory.itemIndexStart)
if (id < _playerInventory.itemIndexStart)
{
var block = Instantiate(destroyedBlock, pos, Quaternion.identity, GetChunck(pos).transform);

block.GetComponent<DestroyedBlock>().ID = Blocks[id].DropID;
block.GetComponent<DestroyedBlock>().ID = id;

var newMesh = new Mesh();
var vertices = new List<Vector3>();
Expand All @@ -532,7 +536,7 @@ public GameObject CreateDestroyedBlock(int id, Vector3 pos)

var currentIndex = 0;

var b = Blocks[Blocks[id].DropID];
var b = Blocks[id];

var offset = new Vector3Int(0, 0, 0);

Expand Down Expand Up @@ -563,8 +567,8 @@ public GameObject CreateDestroyedBlock(int id, Vector3 pos)
else
{
var item = Instantiate(destroyedItem, pos, Quaternion.identity, GetChunck(pos).transform);
item.GetComponent<DestroyedBlock>().ID = Blocks[id].DropID;
var mesh = voxelizer.SpriteToVoxel(_playerInventory.Items[Blocks[id].DropID].texture2d,
item.GetComponent<DestroyedBlock>().ID = id;
var mesh = voxelizer.SpriteToVoxel(_playerInventory.Items[id].texture2d,
standardBlockShape, blockCreation);

item.GetComponent<MeshFilter>().mesh = mesh;
Expand Down
2 changes: 1 addition & 1 deletion Assets/UniversalRenderPipelineAsset.asset
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ MonoBehaviour:
m_OpaqueDownsampling: 1
m_SupportsTerrainHoles: 1
m_SupportsHDR: 1
m_MSAA: 2
m_MSAA: 1
m_RenderScale: 1.41
m_MainLightRenderingMode: 1
m_MainLightShadowsSupported: 1
Expand Down
77 changes: 77 additions & 0 deletions Logs/AssetImportWorker0.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
Using pre-set license
Built from '2021.1/release' branch; Version is '2021.1.10f1 (b15f561b2cef) revision 11624278'; Using compiler version '192528614'; Build Type 'Release'
OS: 'Windows 10 Home; OS build 19041.1052; Version 2004; 64bit' Language: 'de' Physical Memory: 16324 MB
BatchMode: 1, IsHumanControllingUs: 0, StartBugReporterOnCrash: 0, Is64bit: 1, IsPro: 0

COMMAND LINE ARGUMENTS:
C:\Program Files\2021.1.10f1\Editor\Unity.exe
-adb2
-batchMode
-noUpm
-name
AssetImportWorker0
-projectPath
D:/Code/Unity/ourcraft
-logFile
Logs/AssetImportWorker0.log
-srvPort
55620
Successfully changed project path to: D:/Code/Unity/ourcraft
D:/Code/Unity/ourcraft
Using Asset Import Pipeline V2.
Refreshing native plugins compatible for Editor in 88.07 ms, found 2 plugins.
Preloading 0 native plugins for Editor in 0.00 ms.
Initialize engine version: 2021.1.10f1 (b15f561b2cef)
[Subsystems] Discovering subsystems at path C:/Program Files/2021.1.10f1/Editor/Data/Resources/UnitySubsystems
[Subsystems] Discovering subsystems at path D:/Code/Unity/ourcraft/Assets
GfxDevice: creating device client; threaded=0; jobified=0
Direct3D:
Version: Direct3D 11.0 [level 11.1]
Renderer: NVIDIA GeForce GTX 1070 (ID=0x1b81)
Vendor: NVIDIA
VRAM: 8088 MB
Driver: 27.21.14.6089
Shader 'Universal Render Pipeline/Particles/Lit': fallback shader 'Universal Render Pipeline/Particles/SimpleLit' not found
Initialize mono
Mono path[0] = 'C:/Program Files/2021.1.10f1/Editor/Data/Managed'
Mono path[1] = 'C:/Program Files/2021.1.10f1/Editor/Data/MonoBleedingEdge/lib/mono/unityjit'
Mono config path = 'C:/Program Files/2021.1.10f1/Editor/Data/MonoBleedingEdge/etc'
Using monoOptions --debugger-agent=transport=dt_socket,embedding=1,server=y,suspend=n,address=127.0.0.1:56964
Begin MonoManager ReloadAssembly
Registering precompiled unity dll's ...
Register platform support module: C:/Program Files/2021.1.10f1/Editor/Data/PlaybackEngines/WindowsStandaloneSupport/UnityEditor.WindowsStandalone.Extensions.dll
Registered in 0.002424 seconds.
Native extension for WindowsStandalone target not found
Refreshing native plugins compatible for Editor in 61.39 ms, found 2 plugins.
Preloading 0 native plugins for Editor in 0.00 ms.
Mono: successfully reloaded assembly
- Completed reload, in 3.659 seconds
Domain Reload Profiling:
ReloadAssembly (3659ms)
BeginReloadAssembly (58ms)
ExecutionOrderSort (0ms)
DisableScriptedObjects (0ms)
BackupInstance (0ms)
ReleaseScriptingObjects (0ms)
CreateAndSetChildDomain (1ms)
EndReloadAssembly (487ms)
LoadAssemblies (55ms)
RebuildTransferFunctionScriptingTraits (0ms)
SetupTypeCache (186ms)
ReleaseScriptCaches (0ms)
RebuildScriptCaches (31ms)
SetupLoadedEditorAssemblies (220ms)
LogAssemblyErrors (0ms)
InitializePlatformSupportModulesInManaged (6ms)
SetLoadedEditorAssemblies (0ms)
RefreshPlugins (61ms)
BeforeProcessingInitializeOnLoad (1ms)
ProcessInitializeOnLoadAttributes (100ms)
ProcessInitializeOnLoadMethodAttributes (51ms)
AfterProcessingInitializeOnLoad (0ms)
EditorAssembliesLoaded (0ms)
ExecutionOrderSort2 (0ms)
AwakeInstancesAfterBackupRestoration (0ms)
Platform modules already initialized, skipping
Registering precompiled user dll's ...
Registered in 0.028623 seconds.
Loading

0 comments on commit 8e438ee

Please sign in to comment.