Skip to content

Commit

Permalink
It finally works!
Browse files Browse the repository at this point in the history
  • Loading branch information
ObsessiveNerd committed Jan 20, 2024
1 parent 7fb938f commit 9c9875f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 232 deletions.
12 changes: 6 additions & 6 deletions Assets/Reclaim/Scenes/ContentCreation.unity
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ OcclusionCullingSettings:
--- !u!104 &2
RenderSettings:
m_ObjectHideFlags: 0
serializedVersion: 9
serializedVersion: 10
m_Fog: 0
m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1}
m_FogMode: 3
Expand All @@ -38,13 +38,12 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0.37311953, g: 0.38074014, b: 0.3587274, a: 1}
m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
m_ObjectHideFlags: 0
serializedVersion: 12
m_GIWorkflowMode: 1
m_GISettings:
serializedVersion: 2
m_BounceScale: 1
Expand All @@ -67,9 +66,6 @@ LightmapSettings:
m_LightmapParameters: {fileID: 0}
m_LightmapsBakeMode: 1
m_TextureCompression: 1
m_FinalGather: 0
m_FinalGatherFiltering: 1
m_FinalGatherRayCount: 256
m_ReflectionCompression: 2
m_MixedBakeMode: 2
m_BakeBackend: 1
Expand Down Expand Up @@ -323,6 +319,8 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
m_ProtocolType: 0
m_UseWebSockets: 0
m_UseEncryption: 0
m_MaxPacketQueueSize: 128
m_MaxPayloadSize: 6144
m_HeartbeatTimeoutMS: 500
Expand Down Expand Up @@ -445,6 +443,8 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 360c194efa2fbec4c88c177a66b660b7, type: 3}
m_Name:
m_EditorClassIdentifier:
SimpleNetworkPrefab: {fileID: 9129385938250492292, guid: bd7c1c498b223824eb464f34b960852d,
type: 3}
--- !u!114 &1123013525
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down
31 changes: 21 additions & 10 deletions Assets/Reclaim/Scripts/Components/Base Mechanics/Item.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using Unity.Netcode;
using UnityEngine;

Expand All @@ -19,8 +20,8 @@ public class ItemData : EntityComponent
[SerializeField]
public ItemRarity Rarity = ItemRarity.Uncommon;

public Action OnPickup;
public Action OnDrop;
public Action<ulong> OnPickup;
public Action<Entity> OnDrop;

[SerializeField]
public Type MonobehaviorType = typeof(Item);
Expand All @@ -35,12 +36,13 @@ public override void WakeUp()

void Pickup(GameEvent gameEvent)
{
Entity source = gameEvent.GetValue<GameObject>(EventParameter.Source).GetComponent<EntityBehavior>().Entity;
var sourceGo = gameEvent.GetValue<GameObject>(EventParameter.Source);
var source = sourceGo.GetComponent<EntityBehavior>().Entity;
var inventory = source.GetComponent<InventoryData>();
inventory.AddToInventory(Entity);

if(OnPickup != null)
OnPickup();
OnPickup(sourceGo.GetComponent<NetworkObject>().NetworkObjectId);
}

void Drop(GameEvent gameEvent)
Expand All @@ -57,8 +59,14 @@ void Drop(GameEvent gameEvent)
source.FireEvent(GameEventPool.Get(GameEventId.RemoveFromInventory)
.With(EventParameter.Item, Entity)).Release();

if(OnDrop != null)
OnDrop();
if (source.GameObject.GetComponent<NetworkObject>().IsOwner)
{
Point p = source.GetComponent<PositionData>().Point;
Services.Spawner.SpawnGameObject(Entity, p);
}

//if(OnDrop != null)
// OnDrop(source);
}

void GetContextMenuActions(GameEvent gameEvent)
Expand Down Expand Up @@ -99,15 +107,18 @@ public override void OnDestroy()
{
base.OnDestroy();
component.OnPickup -= PickedUp;
component.OnDrop -= Dropped;
}

void Dropped()
void Dropped(Entity source)
{
Point p = component.Entity.GetComponent<PositionData>().Point;
Services.Spawner.SpawnGameObject(component.Entity, p);
if (!IsOwner)
return;


}

void PickedUp()
void PickedUp(ulong ownerNetworkId)
{
PositionData pos = gameObject.GetComponent<Position>().component;
Services.Map.GetTile(pos.Point).RemoveObject(gameObject);
Expand Down
42 changes: 0 additions & 42 deletions Assets/Reclaim/Scripts/Networking/ActivatedNetworkObject.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Assets/Reclaim/Scripts/Networking/ActivatedNetworkObject.cs.meta

This file was deleted.

107 changes: 13 additions & 94 deletions Assets/Reclaim/Scripts/Services/Spawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,120 +7,43 @@
using UnityEngine;
using UnityEngine.UIElements;

public struct Test : INetworkSerializable, IEquatable<Test>
{
public ulong Key;
public FixedString4096Bytes Value;

public Test(ulong key, string value)
{
Key = key;
Value = value;
}

public bool Equals(Test other)
{
if (other.Key == Key)
return true;
return false;
}

public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
serializer.SerializeValue(ref Key);
serializer.SerializeValue(ref Value);
}
}

public class Spawner : NetworkBehaviour
{
NetworkList<Test> m_NetorkEntityMap; // = new NetworkList<Test>();
public GameObject SimpleNetworkPrefab;

void Awake()
{
Services.Register(this);
m_NetorkEntityMap = new NetworkList<Test>();
Services.Register(this);
}

public GameObject SpawnGameObject(GameObject go, Vector3 position)
{
var instance = Instantiate(go, position, Quaternion.identity);
instance.GetComponent<Position>().component.Point = new Point(position);

instance.GetComponent<NetworkObject>().Spawn();
var entity = instance.GetComponent<EntityBehavior>().Entity;
m_NetorkEntityMap.Add(new Test(instance.GetComponent<NetworkObject>().NetworkObjectId, Services.Serialization.SerializeEntity(entity)));

//instance.GetComponent<ActivatedNetworkObject>().Activate();
//instance.GetComponent<ActivatedNetworkObject>().ActivateOnSpawn.Value = true;
return instance;
}

public void GetEntityFromNetworkId(ulong networkId, out Entity entity)
{
foreach(var kvp in m_NetorkEntityMap)
{
if(kvp.Key == networkId)
{
entity = Services.Serialization.GetEntity(kvp.Value.ToString());
return;
}
}
entity = null;
}

public void RemoveFromNetworkIdMap(ulong networkId)
{
Test t = new Test(networkId, "");
m_NetorkEntityMap.Remove(t);
}

public void SpawnGameObject(Entity entity, Point p)
{
//if (entity.GameObject == null)
if (IsServer)
SpawnGameObject(Services.Serialization.SerializeEntity(entity), p);
else
SpawnGameObjectServerRpc(Services.Serialization.SerializeEntity(entity), p);
//else
// SpawnGameObject(entity.GameObject, p.ToVector());
}

[ServerRpc(RequireOwnership = false)]
public void SpawnGameObjectServerRpc(string entityJson, Point p)
public void SpawnGameObject(string entityJson, Point p)
{
GameObject temp = Resources.Load<GameObject>("SimpleNetworkObject");
var instance = Instantiate(temp, Services.Map.GetTile(p).transform.position, Quaternion.identity);
var instance = Instantiate(SimpleNetworkPrefab, Services.Map.GetTile(p).transform.position, Quaternion.identity);
instance.GetComponent<EntityBehavior>().EntityJson.Value = entityJson;
instance.GetComponent<NetworkObject>().Spawn();
ulong networkId = instance.GetComponent<NetworkObject>().NetworkObjectId;
m_NetorkEntityMap.Add(new Test(networkId, entityJson));

//instance.GetComponent<ActivatedNetworkObject>().ActivateOnSpawn.Value = true;
//SpawnGameObjectClientRpc(networkId);
//SpawnGameObjectClientRpc(networkId, entityJson);

}

//[ClientRpc]
//public void SpawnGameObjectClientRpc(string entityJson, Point p)
//{

//}

//[ClientRpc]
//void SpawnGameObjectClientRpc(ulong networkId)
//{
// NetworkObject netObject;
// GameObject instance;

// if (!NetworkManager.Singleton.SpawnManager.SpawnedObjects.TryGetValue(networkId, out netObject))
// {
// Debug.LogError($"No gameobject for networkId {networkId} found!");
// return;
// }
// else
// instance = netObject.gameObject;

// //instance.GetComponent<ActivatedNetworkObject>().Activate();
//}
[ServerRpc(RequireOwnership = false)]
public void SpawnGameObjectServerRpc(string entityJson, Point p)
{
SpawnGameObject(entityJson, p);
}

[ServerRpc(RequireOwnership = false)]
public void DestroyGameObjectServerRpc(ulong networkId)
Expand All @@ -135,13 +58,9 @@ public void DestroyGameObjectServerRpc(ulong networkId)
}
else
instance = netObject.gameObject;
//RemoveFromNetworkIdMap(networkId);
//m_NetorkEntityMap.Value.Remove(instance.GetComponent<NetworkObject>().NetworkObjectId);

instance.GetComponent<NetworkObject>().Despawn();
if (IsServer)
Destroy(instance);
//instance.GetComponent<Position>().component.Point = p;

//return instance;
}
}
Loading

0 comments on commit 9c9875f

Please sign in to comment.