i have theses two codes. one deals with the gridsquare prefab which are generated when the puzzle starts, and we have a wordchecker scripts which basically deals with the seelection logic. please help me modify the following scripts. keep in mind that raycasting is the core of the word selection logic. heres the grid square script using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GridSquare : MonoBehaviour
{
public int SquareIndex { get; set; }
private AlphabetData.LetterData _normalLetterData;
private AlphabetData.LetterData _selectedLetterData;
private AlphabetData.LetterData _correctLetterData;
private SpriteRenderer _displayedImage;
private bool _selected;
private bool _clicked;
private int _index = -1;
private bool _correct;
private AudioSource _source;
public void SetIndex(int index)
{
_index = index;
}
public int GetIndex()
{
return _index;
}
// Start is called before the first frame update
void Start()
{
_selected = false;
_clicked = false;
_correct = false;
_displayedImage = GetComponent<SpriteRenderer>();
_source=GetComponent<AudioSource>();
}
public void OnEnable()
{
// Subscribe to various game events
GameEvents.OnEnableSquareSelection += OnEnableSquareSelection;
GameEvents.OnDisableSquareSelection += OnDisableSquareSelection;
GameEvents.OnSelectSquare += SelectSquare;
GameEvents.OnCorrectWord += CorrectWord;
}
private void OnDisable()
{
// Unsubscribe from game events to prevent memory leaks
GameEvents.OnEnableSquareSelection -= OnEnableSquareSelection;
GameEvents.OnDisableSquareSelection -= OnDisableSquareSelection;
GameEvents.OnSelectSquare -= SelectSquare;
GameEvents.OnCorrectWord -= CorrectWord;
}
private void CorrectWord(string word, List<int> squareIndexes)
{
if (_selected && squareIndexes.Contains(_index))
{
_correct = true;
_displayedImage.sprite = _correctLetterData.image;
}
_selected = false;
_clicked = false;
}
public void OnEnableSquareSelection()
{
_clicked = true;
_selected = false;
}
public void OnDisableSquareSelection()
{
_selected = false;
_clicked = false;
if (_correct == true)
_displayedImage.sprite = _correctLetterData.image;
else
_displayedImage.sprite = _normalLetterData.image;
}
private void SelectSquare(Vector3 position)
{
if (this.gameObject.transform.position == position)
{
_displayedImage.sprite = _selectedLetterData.image;
}
}
public void SetSprite(AlphabetData.LetterData normalLetterData, AlphabetData.LetterData selectedLetterData, AlphabetData.LetterData correctLetterData)
{
_normalLetterData = normalLetterData;
_selectedLetterData = selectedLetterData;
_correctLetterData = correctLetterData;
GetComponent<SpriteRenderer>().sprite = _normalLetterData.image;
}
private void OnMouseDown()
{
OnEnableSquareSelection(); // Trigger the event if there are subscribers
GameEvents.EnableSquareSelectionMethod(); // Assuming this is a method or property
CheckSquare();
_displayedImage.sprite = _selectedLetterData.image;
}
private void OnMouseEnter()
{
CheckSquare();
}
private void OnMouseUp()
{
GameEvents.ClearSelectionMethod();
GameEvents.DisableSquareSelectionMethod();
}
public void CheckSquare()
{
if (_selected == false && _clicked == true)
{
if(SoundManager.instance.IsSoundFXMuted()==false)
_source.Play();
_selected = true;
GameEvents.CheckSquareMethod(_normalLetterData.letter, gameObject.transform.position, _index);
}
}
}
heres the wordchecker script using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class WordChecker : MonoBehaviour
{
public GameData currentGameData;
public GameLevelData gameLevelData;
private string _word;
private int _assignedPoints = 0;
private int _completedWords = 0;
private Ray _rayUp, _rayDown;
private Ray _rayLeft, _rayRight;
private Ray _rayDiagonalLeftUp, _rayDiagonalLeftDown;
private Ray _rayDiagonalRightUp, _rayDiagonalRightDown;
private Ray _currentRay = new Ray();
private Vector3 _rayStartPosition;
private List<int> _correctSquareList = new List<int>();
private void OnEnable()
{
GameEvents.OnCheckSquare += SquareSelected;
GameEvents.OnClearSelection += ClearSelection;
GameEvents.OnLoadNextLevel += LoadNextGameLevel;
}
private void OnDisable()
{
GameEvents.OnCheckSquare -= SquareSelected;
GameEvents.OnClearSelection -= ClearSelection;
GameEvents.OnLoadNextLevel -= LoadNextGameLevel;
}
private void LoadNextGameLevel()
{
Debug.Log("Loading next game level...");
SceneManager.LoadScene("GameScene");
}
void Start()
{
currentGameData.selectedBoardData.ClearData();
_assignedPoints = 0;
_completedWords = 0;
AdManager.Instance.LoadBannerAd();
Debug.Log("Game started. Board data cleared.");
}
void Update()
{
if (_assignedPoints > 0 && Application.isEditor)
{
Debug.DrawRay(_rayUp.origin, _rayUp.direction * 4);
Debug.DrawRay(_rayDown.origin, _rayDown.direction * 4);
Debug.DrawRay(_rayLeft.origin, _rayLeft.direction * 4);
Debug.DrawRay(_rayRight.origin, _rayRight.direction * 4);
Debug.DrawRay(_rayDiagonalLeftUp.origin, _rayDiagonalLeftUp.direction * 4);
Debug.DrawRay(_rayDiagonalLeftDown.origin, _rayDiagonalLeftDown.direction * 4);
Debug.DrawRay(_rayDiagonalRightUp.origin, _rayDiagonalRightUp.direction * 4);
Debug.DrawRay(_rayDiagonalRightDown.origin, _rayDiagonalRightDown.direction * 4);
}
}
private void SquareSelected(string letter, Vector3 squarePosition, int squareIndex)
{
if (_assignedPoints == 0)
{
Debug.Log($"Square selected: Letter: {letter}, Position: {squarePosition}, Index: {squareIndex}");
_rayStartPosition = squarePosition;
_correctSquareList.Add(squareIndex);
_word += letter;
_rayUp = new Ray(new Vector2(squarePosition.x, squarePosition.y), Vector3.up);
_rayDown = new Ray(new Vector2(squarePosition.x, squarePosition.y), Vector3.down);
_rayLeft = new Ray(new Vector2(squarePosition.x, squarePosition.y), Vector3.left);
_rayRight = new Ray(new Vector2(squarePosition.x, squarePosition.y), Vector3.right);
_rayDiagonalLeftUp = new Ray(new Vector2(squarePosition.x, squarePosition.y), Vector3.left + Vector3.up);
_rayDiagonalLeftDown = new Ray(new Vector2(squarePosition.x, squarePosition.y), Vector3.left + Vector3.down);
_rayDiagonalRightUp = new Ray(new Vector2(squarePosition.x, squarePosition.y), Vector3.right + Vector3.up);
_rayDiagonalRightDown = new Ray(new Vector2(squarePosition.x, squarePosition.y), Vector3.right + Vector3.down);
}
else if (_assignedPoints == 1)
{
_correctSquareList.Add(squareIndex);
_currentRay = SelectRay(_rayStartPosition, squarePosition);
GameEvents.SelectSquareMethod(squarePosition);
_word += letter;
CheckWord();
}
else
{
if (IsPointOnTheRay(_currentRay, squarePosition))
{
_correctSquareList.Add(squareIndex);
GameEvents.SelectSquareMethod(squarePosition);
_word += letter;
CheckWord();
}
}
_assignedPoints++;
}
private void CheckWord()
{
foreach (var searchingWord in currentGameData.selectedBoardData.SearchWords)
{
if (_word == searchingWord.Word && searchingWord.Found == false)
{
searchingWord.Found = true;
GameEvents.CorrectWordMethod(_word, _correctSquareList);
Debug.Log($"Word found: {_word}");
// Access the translation of the correct word
string translation = searchingWord.Translation;
// Use the translation in your gameplay logic
Debug.Log($"Translation: {translation}");
_completedWords++;
_word = string.Empty;
_correctSquareList.Clear();
CheckBoardCompleted();
return;
}
}
}
private bool IsPointOnTheRay(Ray currentRay, Vector3 point)
{
RaycastHit[] hits = Physics.RaycastAll(currentRay, 100.0f);
for (int i = 0; i < hits.Length; i++)
{
if (hits[i].transform.position == point)
return true;
}
return false;
}
private Ray SelectRay(Vector2 firstPosition, Vector2 secondPosition)
{
Vector3 direction = (secondPosition - firstPosition).normalized;
float tolerance = 0.01f;
if (Mathf.Abs(direction.x) < tolerance && Mathf.Abs(direction.y - 1f) < tolerance)
{
return _rayUp;
}
if (Mathf.Abs(direction.x) < tolerance && Mathf.Abs(direction.y - (-1f)) < tolerance)
{
return _rayDown;
}
if (Mathf.Abs(direction.x - (-1f)) < tolerance && Mathf.Abs(direction.y) < tolerance)
{
return _rayLeft;
}
if (Mathf.Abs(direction.x - 1f) < tolerance && Mathf.Abs(direction.y) < tolerance)
{
return _rayRight;
}
if (direction.x < 0f && direction.y > 0f)
{
return _rayDiagonalLeftUp;
}
if (direction.x < 0f && direction.y < 0f)
{
return _rayDiagonalLeftDown;
}
if (direction.x > 0f && direction.y > 0f)
{
return _rayDiagonalRightUp;
}
if (direction.x > 0f && direction.y < 0f)
{
return _rayDiagonalRightDown;
}
return _rayDown;
}
private void ClearSelection()
{
_assignedPoints = 0;
_correctSquareList.Clear();
_word = string.Empty;
}
private void CheckBoardCompleted()
{
Debug.Log("Checking if board completed...");
bool loadNextCategory = false;
if (currentGameData.selectedBoardData.SearchWords.Count == _completedWords)
{
//save current level data
var categoryName = currentGameData.selectedCategoryName;
var currentBoardIndex = DataSaver.ReadCategoryCurrentIndexValues(categoryName);
var nextBoardIndex = -1;
var currentCategoryIndex = 0;
bool readNextLevelName = false;
for (int index = 0; index < gameLevelData.data.Count; index++)
{
if (readNextLevelName)
{
nextBoardIndex = DataSaver.ReadCategoryCurrentIndexValues(gameLevelData.data[index].categoryName);
readNextLevelName = false;
}
if (gameLevelData.data[index].categoryName == categoryName)
{
readNextLevelName = true;
currentCategoryIndex = index;
}
}
var currentLevelSize = gameLevelData.data[currentCategoryIndex].boardData.Count;
if (currentBoardIndex < currentLevelSize)
currentBoardIndex += 1;
DataSaver.SavedCategoryData(categoryName, currentBoardIndex);
//Unlock next category
if (currentBoardIndex >= currentLevelSize)
{
currentCategoryIndex++;
if (currentCategoryIndex < gameLevelData.data.Count)//if this is not the last category
{
categoryName = gameLevelData.data[currentCategoryIndex].categoryName;
currentBoardIndex = 0;
loadNextCategory = true;
if (nextBoardIndex <= 0)
{
DataSaver.SavedCategoryData(categoryName, currentBoardIndex);
}
}
else
{
SceneManager.LoadScene("SelectCategory");
}
}
else
{
GameEvents.BoardCompletedMethod();
}
if (loadNextCategory)
GameEvents.UnlockNextCategoryMethod();
}
}
}
the codes are working. But i am having problems building the game due to use of the old input system.