using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LovecraftianPuzzle : MonoBehaviour
{
public enum EldritchSymbols { Nyarlathotep, Azathoth, Cthulhu, ShubNiggurath, YogSothoth }
public Transform[] symbolSlots;
public GameObject[] eldritchSymbolPrefabs;
public GameObject[] glyphsGameObject;
public List glyphs;
public List books;
public GameObject portal;
public Animator portalAnimator;
public AudioSource gatewayAudio;
public AudioClip placeSymbolSound;
public AudioClip puzzleSolvedSound;
public GameObject bookParticlePrefab;
public GameObject shadowPeoplePrefab;
private GameObject player;
private AudioSource playerAudioSource;
private InventoryManager InvManager;
private AudioSource puzzleAudio;
private FearSystem fearSystem;
private QuestSystem questSystem;
private void Start()
{
player = GameObject.Find("Player");
playerAudioSource = player.GetComponent<AudioSource>();
puzzleAudio = GetComponent<AudioSource>();
InvManager = FindAnyObjectByType(typeof(InventoryManager)) as InventoryManager;
fearSystem = FindAnyObjectByType(typeof(FearSystem)) as FearSystem;
questSystem = FindAnyObjectByType(typeof(QuestSystem)) as QuestSystem;
glyphs = new List<Glyphs>
{
new Glyphs { symbolType = EldritchSymbols.Nyarlathotep, symbolGlyph = glyphsGameObject[0]},
new Glyphs { symbolType = EldritchSymbols.Azathoth,symbolGlyph = glyphsGameObject[1]},
new Glyphs { symbolType = EldritchSymbols.Cthulhu, symbolGlyph = glyphsGameObject[2]},
new Glyphs { symbolType = EldritchSymbols.ShubNiggurath, symbolGlyph = glyphsGameObject[3]},
new Glyphs { symbolType = EldritchSymbols.YogSothoth, symbolGlyph = glyphsGameObject[4]}
};
books = new List<Book>
{
new Book { title = "Invocations of The Cosmic Horror", symbolType = EldritchSymbols.Nyarlathotep, symbolPrefab = eldritchSymbolPrefabs[0] },
new Book { title = "Mark Of The Old Gods", symbolType = EldritchSymbols.Azathoth, symbolPrefab = eldritchSymbolPrefabs[1] },
new Book { title = "The House Of R'lyeh", symbolType = EldritchSymbols.Cthulhu, symbolPrefab = eldritchSymbolPrefabs[2] },
new Book { title = "The Shadow People", symbolType = EldritchSymbols.ShubNiggurath, symbolPrefab = eldritchSymbolPrefabs[3] },
new Book { title = "The Unknown and The Deep Void", symbolType = EldritchSymbols.YogSothoth, symbolPrefab = eldritchSymbolPrefabs[4] }
};
}
private List<EldritchSymbols> correctOrder = new List<EldritchSymbols>()
{
EldritchSymbols.Nyarlathotep,
EldritchSymbols.Azathoth,
EldritchSymbols.Cthulhu,
EldritchSymbols.ShubNiggurath,
EldritchSymbols.YogSothoth
};
private List<EldritchSymbols> currentOrder = new List<EldritchSymbols>();
private Dictionary<EldritchSymbols, int> symbolToSlotIndex = new Dictionary<EldritchSymbols, int>()
{
{ EldritchSymbols.Nyarlathotep, 0 },
{ EldritchSymbols.Azathoth, 1 },
{ EldritchSymbols.Cthulhu, 2 },
{ EldritchSymbols.ShubNiggurath, 3 },
{ EldritchSymbols.YogSothoth, 4 }
};
[System.Serializable]
public class Glyphs
{
public EldritchSymbols symbolType;
public GameObject symbolGlyph;
}
[System.Serializable]
public class Book
{
public string title;
public EldritchSymbols symbolType;
public GameObject symbolPrefab;
}
public void PlaceSymbol(EldritchSymbols symbol)
{
int bookIndex = books.FindIndex(b => b.symbolType == symbol);
Book book = books.Find(b => b.symbolType == symbol);
string title = book?.title;
// Check if the book index is valid and the corresponding slot is empty
if (bookIndex >= 0 && bookIndex < books.Count && symbolSlots[bookIndex].childCount == 0)
{
bool itemFound = false; // Flag variable to check if the item was found in the inventory
for (int t = 0; t < InvManager.MaxItems; t++) //Starting a loop in the slots of the inventory:
{
if (InvManager.Slots[t].IsTaken == true) //Checking if there's an item in this slot.
{
Item ItemScript = InvManager.Slots[t].Item.GetComponent<Item>(); //Getting the item script from the items inside the bag.
if (ItemScript.Name == title)
{
itemFound = true; // Set the flag to true
ItemScript.ItemDropType = Item.ItemDropTypes.Destroy;
InvManager.RemoveItem(ItemScript.gameObject.transform, 1);
break; // Break out of the loop since the item was found
}
}
}
if (itemFound) // Check if the item was found in the inventory
{
GameObject symbolObject = Instantiate(
books[bookIndex].symbolPrefab,
symbolSlots[bookIndex].position,
Quaternion.Euler(0, 24, -90), // Rotate 90 degrees around the Z axis
symbolSlots[bookIndex]);
Item itemScript = symbolObject.GetComponent<Item>();
itemScript.name = itemScript.Name;
if (!currentOrder.Contains(symbol)) // Check if the symbol is already in the list
{
currentOrder.Add(symbol); // Add it only if it's not present
// Check if the newly placed symbol is in the correct position
if (currentOrder.Count <= correctOrder.Count && currentOrder[currentOrder.Count - 1] == correctOrder[currentOrder.Count - 1])
{
// Find the Glyph that has the same symbol
Glyphs glyph = glyphs.Find(g => g.symbolType == symbol);
if (glyph != null)
{
// Define the emissive color
Color emissiveColor = Color.red; // replace with the desired color
// Get all children of the Glyph
foreach (Transform child in glyph.symbolGlyph.transform)
{
// Get the SpriteRenderer of the child
SpriteRenderer spriteRenderer = child.GetComponent<SpriteRenderer>();
if (spriteRenderer != null)
{
// Apply the emissive color to the SpriteRenderer
spriteRenderer.material.SetColor("_EmissionColor", emissiveColor);
}
}
}
}
}
playerAudioSource.PlayOneShot(placeSymbolSound);
}
else
{
NotificationUI notificationUI = FindObjectOfType(typeof(NotificationUI)) as NotificationUI;
notificationUI.SendMsg("<color=white>The book " + "<color=red>" + books[bookIndex].title + "</color> is missing");
}
}
NarrativeUI narrative = FindObjectOfType(typeof(NarrativeUI)) as NarrativeUI;
narrative.SendMsg("Symbol of " + books[bookIndex].symbolType, 4);
StartCoroutine(player.GetComponent<PlayerInteraction>().InteractionDelay());
CheckPuzzleSolved();
}