169 lines
5.9 KiB
C#
169 lines
5.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
public class GroundItemManager : MonoBehaviour
|
|
{
|
|
public static GroundItemManager Instance { get; private set; }
|
|
private Material outlineMaterial;
|
|
private readonly Material[] materialTemp = new Material[2];
|
|
private TMP_Text interactText;
|
|
public static bool InteractingWithItem = false;
|
|
|
|
private void Awake()
|
|
{
|
|
Instance = this;
|
|
outlineMaterial = OnDemandLoader.Load<Material>("Materials/Shaders/Weapon_Outline/OutlineMaterial");
|
|
interactText = UIManager.Instance.itemInteractMessage;
|
|
}
|
|
|
|
public void UpdateGroundItems(int count, int[] ids, int[] x, int[] z)
|
|
{
|
|
//clear the whole array
|
|
foreach (GameObject go in groundItems)
|
|
{
|
|
if (go != null)
|
|
Destroy(go);
|
|
}
|
|
|
|
if (count >= 64)
|
|
{
|
|
count = 64;
|
|
}
|
|
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
GameObject g = OnDemandLoader.Load<GameObject>("items/" + ids[i]);
|
|
if (g == null) continue;
|
|
|
|
GameObject groundItem = Instantiate(g);
|
|
Weapon itemData = OnDemandLoader.Load<Weapon>("config/" + ids[i]);
|
|
ItemType itemType = itemData.ItemType;
|
|
|
|
AssignOutlineMaterial(groundItem);
|
|
|
|
//Crypto coins
|
|
if (IsCryptoCoin(itemType))
|
|
{
|
|
CreateCryptoCoin(groundItem, i, x, z);
|
|
}
|
|
|
|
CreateItemGlow(itemType, groundItem, i, x, z);
|
|
|
|
groundItem.name = g.name;
|
|
groundItems[i] = groundItem;
|
|
}
|
|
}
|
|
|
|
private static bool IsCryptoCoin(ItemType type) => type == ItemType.COIN;
|
|
|
|
private void CreateItemGlow(ItemType type, GameObject groundItem, int currIndex, IReadOnlyList<int> x,
|
|
IReadOnlyList<int> z)
|
|
{
|
|
GameObject droppedItemGlow = OnDemandLoader.Load<GameObject>("gfx/DroppedItemGlow");
|
|
groundItem.transform.localScale = Vector3.one * 1.1f;
|
|
Vector3 spawnPosition = new Vector3(x[currIndex], 0, z[currIndex]);
|
|
groundItem.transform.position =
|
|
new Vector3(x[currIndex], World.Instance.getHeight(spawnPosition) + 1f, z[currIndex]);
|
|
GameObject glow = Instantiate(droppedItemGlow, groundItem.transform);
|
|
ParticleSystem glowSystem = glow.GetComponent<ParticleSystem>();
|
|
ParticleSystem.MainModule main = glowSystem.main;
|
|
|
|
Color glowColor = type switch
|
|
{
|
|
ItemType.GUN => WeaponColor,
|
|
ItemType.EXPLOSIVE => ExplosiveColor,
|
|
ItemType.MELEE => MeleeColor,
|
|
ItemType.CONSUMABLE => ConsumableColor,
|
|
ItemType.COIN => CoinColor,
|
|
ItemType.MISC => MiscColor,
|
|
_ => MiscColor
|
|
};
|
|
|
|
foreach (MeshRenderer rend in groundItem.GetComponentsInChildren<MeshRenderer>())
|
|
{
|
|
rend.materials[1].SetColor(OutlineColor, glowColor);
|
|
}
|
|
|
|
main.startColor = glowColor;
|
|
}
|
|
|
|
private void CreateCryptoCoin(GameObject groundItem, int currIndex, IReadOnlyList<int> x, IReadOnlyList<int> z)
|
|
{
|
|
groundItem.transform.localScale = Vector3.one * 0.245f;
|
|
|
|
Animator anim = groundItem.AddComponent<Animator>();
|
|
|
|
anim.transform.eulerAngles = new Vector3(90, 0, 0);
|
|
|
|
RuntimeAnimatorController controller =
|
|
OnDemandLoader.Load<RuntimeAnimatorController>("Crypto_Coins/CryptoSpin_Controller");
|
|
anim.runtimeAnimatorController = controller;
|
|
anim.applyRootMotion = true;
|
|
|
|
Vector3 pos = new Vector3(x[currIndex], 10.35f, z[currIndex]);
|
|
anim.transform.position = pos;
|
|
|
|
//Add Particle
|
|
GameObject coinFountain = OnDemandLoader.Load<GameObject>("gfx/Coin_Fountain");
|
|
Vector3 floorHeight =
|
|
new Vector3(x[currIndex], World.Instance.getHeight(new Vector3(x[currIndex], 0, z[currIndex])) + 1f,
|
|
z[currIndex]);
|
|
Instantiate(coinFountain, floorHeight, Quaternion.identity, groundItem.transform);
|
|
}
|
|
|
|
|
|
private void AssignOutlineMaterial(GameObject groundItem)
|
|
{
|
|
foreach (MeshRenderer rend in groundItem.GetComponentsInChildren<MeshRenderer>())
|
|
{
|
|
materialTemp[0] = rend.material;
|
|
materialTemp[1] = outlineMaterial;
|
|
|
|
rend.materials = materialTemp;
|
|
}
|
|
}
|
|
public void Update()
|
|
{
|
|
if (interactText.gameObject != null)
|
|
interactText.text = "";
|
|
|
|
InteractingWithItem = false;
|
|
|
|
foreach (GameObject g in groundItems)
|
|
{
|
|
if (g == null) continue;
|
|
|
|
if (!(Vector3.Distance(Player.Instance.transform.position, g.transform.position) < 2.0f)) continue;
|
|
|
|
if (interactText.gameObject != null)
|
|
{
|
|
if (interactText != null)
|
|
{
|
|
InteractingWithItem = true;
|
|
ItemDef itemDef = ItemDef.GetInstance();
|
|
string objectName = itemDef.getItemName(int.Parse(g.name));
|
|
string objectLevel = itemDef.getItemLevel(int.Parse(g.name));
|
|
interactText.text =
|
|
InputMessageHandler.Instance.Interpolate(
|
|
$"Press [[Interact]] to pick up: {objectLevel} [<color=#FF7600><b>{objectName}</b></color>]");
|
|
}
|
|
}
|
|
|
|
Player.Instance.interactableGroundItem = g;
|
|
return;
|
|
}
|
|
}
|
|
|
|
private readonly GameObject[] groundItems = new GameObject[64];
|
|
|
|
|
|
private static readonly Color WeaponColor = Color.yellow;
|
|
private static readonly Color ExplosiveColor = Color.red;
|
|
private static readonly Color MeleeColor = Color.magenta;
|
|
private static readonly Color ConsumableColor = Color.green;
|
|
private static readonly Color CoinColor = Color.blue;
|
|
private static readonly Color MiscColor = Color.white;
|
|
private static readonly int OutlineColor = Shader.PropertyToID("_OutlineColor");
|
|
} |