2022-01-20 19:45:19 +03:00
using System.Collections ;
using System.Collections.Generic ;
using UnityEngine ;
2022-01-25 14:31:55 +03:00
using UnityEngine.SceneManagement ;
2022-01-20 19:45:19 +03:00
using UnityEngine.XR.ARFoundation ;
using UnityEngine.XR.ARSubsystems ;
using UnityEngine.UI ;
using System.Linq ;
2022-01-25 14:31:55 +03:00
using System ;
2022-01-20 19:45:19 +03:00
public class main : MonoBehaviour
{
[SerializeField] List < GameObject > chests ;
[SerializeField] GameObject chestWidthLamp ;
2022-01-26 22:10:40 +03:00
[SerializeField] GameObject openChestButton ;
[SerializeField] GameObject getLampButton ;
2022-01-20 19:45:19 +03:00
ARRaycastManager raycastManager ;
Text status ;
bool spawnedchest = false ;
bool search = true ;
public List < ARRaycastHit > hits ;
bool isTracking = false ; //<2F> <> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> state <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
GameObject rayRecObj ;
void Start ( )
{
raycastManager = gameObject . GetComponent < ARRaycastManager > ( ) ;
2022-01-28 19:01:54 +03:00
status = GameObject . FindGameObjectWithTag ( "status" ) . GetComponent < Text > ( ) ;
2022-01-20 19:45:19 +03:00
}
void Update ( )
{
detectPLanes ( ) ;
openChest ( ) ;
}
private void spawnChests ( Vector3 hitPos )
{
2022-01-26 22:10:40 +03:00
GameObject obj ;
2022-01-20 19:45:19 +03:00
List < Vector3 > cords = new List < Vector3 > {
new Vector3 ( hitPos . x + 0.7f , hitPos . y , hitPos . z + 0.2f ) ,
new Vector3 ( hitPos . x - 0.7f , hitPos . y , hitPos . z + 0.2f ) ,
new Vector3 ( hitPos . x + 0.3f , hitPos . y , hitPos . z + 0.1f ) ,
new Vector3 ( hitPos . x - 0.3f , hitPos . y , hitPos . z + 0.1f ) ,
new Vector3 ( hitPos . x , hitPos . y , hitPos . z )
} ;
for ( int i = 0 ; i < = cords . Count + 1 ; i + + ) {
2022-01-25 14:31:55 +03:00
GameObject chest = chests [ UnityEngine . Random . Range ( 0 , chests . Count ) ] ;
2022-01-20 19:45:19 +03:00
//GameObject chest = chests[0];
2022-01-25 14:31:55 +03:00
int index = UnityEngine . Random . Range ( 0 , cords . Count - 1 ) ;
obj = Instantiate ( chest , position : cords [ index ] , rotation : new Quaternion ( ) ) ;
obj . name = "chest" + i . ToString ( ) ;
2022-01-20 19:45:19 +03:00
cords . RemoveAt ( index ) ;
}
2022-01-25 14:31:55 +03:00
obj = Instantiate ( chestWidthLamp , position : cords [ 0 ] , rotation : new Quaternion ( ) ) ;
2022-01-28 19:01:54 +03:00
obj . name = "chest4" ;
2022-01-20 19:45:19 +03:00
}
private void detectPLanes ( )
{
hits = new List < ARRaycastHit > ( ) ;
raycastManager . Raycast ( new Vector2 ( Screen . width / 2 , Screen . height / 2 ) , hits , TrackableType . Planes ) ;
if ( hits . Count = = 0 & & search = = true )
{
2022-01-25 14:31:55 +03:00
status . text = "<22> <> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> " + string . Concat ( Enumerable . Repeat ( "." , UnityEngine . Random . RandomRange ( 1 , 3 ) ) ) ;
2022-01-20 19:45:19 +03:00
}
else
{
search = false ;
if ( spawnedchest = = false & & hits . Count > 0 )
{
2022-01-26 22:10:40 +03:00
status . gameObject . SetActive ( false ) ;
2022-01-20 19:45:19 +03:00
spawnedchest = true ;
spawnChests ( hits [ 0 ] . pose . position ) ;
}
}
}
private void openChest ( )
{
Ray ray = new Ray ( gameObject . transform . GetChild ( 0 ) . transform . position , gameObject . transform . GetChild ( 0 ) . transform . forward ) ;
RaycastHit raycastHit ;
2022-01-26 22:10:40 +03:00
if ( Physics . Raycast ( ray , out raycastHit ) & & GameObject . FindGameObjectsWithTag ( "aim" ) . Length = = 0 )
2022-01-20 19:45:19 +03:00
{
status . text = "ray" ;
2022-01-26 22:10:40 +03:00
if ( raycastHit . collider . gameObject . tag = = "lamp" )
{
rayRecObj = raycastHit . collider . gameObject ;
rayRecObj . GetComponent < Outline > ( ) . enabled = true ;
getLampButton . SetActive ( true ) ;
}
else if ( raycastHit . collider . gameObject . tag = = "chest" )
2022-01-20 19:45:19 +03:00
{
if ( isTracking = = false )
{
isTracking = true ;
rayRecObj = raycastHit . collider . gameObject ;
//<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> + <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
2022-01-25 14:31:55 +03:00
PlayerPrefs . SetInt ( "chestNumber" , Convert . ToInt32 ( rayRecObj . name . Substring ( 5 ) ) ) ;
2022-01-20 19:45:19 +03:00
rayRecObj . GetComponent < Outline > ( ) . enabled = true ;
2022-01-26 22:10:40 +03:00
openChestButton . SetActive ( true ) ;
2022-01-20 19:45:19 +03:00
}
//raycastHit.collider.gameObject.transform.GetChild(0).GetComponent<Renderer>().material.color = Color.green;
}
else
{
resetIsTracking ( ) ;
}
}
else {
resetIsTracking ( ) ;
}
}
private void resetIsTracking ( )
{
if ( isTracking = = true )
{
isTracking = false ;
//<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> + <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
rayRecObj . GetComponent < Outline > ( ) . enabled = false ;
2022-01-26 22:10:40 +03:00
openChestButton . SetActive ( false ) ;
getLampButton . SetActive ( false ) ;
2022-01-20 19:45:19 +03:00
}
}
2022-01-25 14:31:55 +03:00
public void openBloha ( )
{
SceneManager . LoadScene ( "BlohaGame" , LoadSceneMode . Additive ) ;
}
2022-01-20 19:45:19 +03:00
}