done
This commit is contained in:
30
Assets/Scripts/Chest.cs
Normal file
30
Assets/Scripts/Chest.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Assertions;
|
||||
|
||||
public class Chest : MonoBehaviour
|
||||
{
|
||||
private GameManager _gameManager;
|
||||
private void Awake()
|
||||
{
|
||||
_gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
|
||||
Assert.IsNotNull(_gameManager);
|
||||
}
|
||||
|
||||
private void OnTriggerStay(Collider other)
|
||||
{
|
||||
Debug.Log("Triggered");
|
||||
if (!Input.GetKeyDown(KeyCode.Space))
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (!other.name.Equals("Player"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_gameManager.OpenChest();
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Chest.cs.meta
Normal file
11
Assets/Scripts/Chest.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b2fcf21fe940c17999a76bd98b4fb687
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
129
Assets/Scripts/GameManager.cs
Normal file
129
Assets/Scripts/GameManager.cs
Normal file
@@ -0,0 +1,129 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Assertions;
|
||||
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Player _player;
|
||||
|
||||
private float _lastTimeof2Seconds;
|
||||
[SerializeField] private TMPro.TextMeshProUGUI _timer;
|
||||
|
||||
[SerializeField] private TMPro.TextMeshProUGUI _gameOver;
|
||||
|
||||
[SerializeField] private GameObject _potionPrefab;
|
||||
private Vector2 _potionGroundSpawnRange = new Vector2(-48f, 48f);
|
||||
private int _healthDecrease = -2;
|
||||
|
||||
[SerializeField] private GameObject _keyPrefab;
|
||||
private HiddenObject _keyInstanceScript;
|
||||
private GameObject _keyInstance;
|
||||
private bool isKeyCollected = false;
|
||||
|
||||
[SerializeField] private GameObject _chestClosedPrefab;
|
||||
[SerializeField] private GameObject _chestOpenPrefab;
|
||||
private GameObject _chestInstance;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Assert.IsNotNull(_player);
|
||||
_lastTimeof2Seconds = Time.time;
|
||||
_gameOver.enabled = false;
|
||||
|
||||
SpawnPotion();
|
||||
|
||||
Assert.IsNotNull(_keyPrefab);
|
||||
_keyInstance = Instantiate(_keyPrefab,
|
||||
new Vector3(
|
||||
Random.Range(_potionGroundSpawnRange.x, _potionGroundSpawnRange.y),
|
||||
0.5f,
|
||||
Random.Range(_potionGroundSpawnRange.x, _potionGroundSpawnRange.y)
|
||||
),
|
||||
Quaternion.identity);
|
||||
|
||||
_keyInstanceScript = _keyInstance.transform.GetChild(0).gameObject.GetComponent<HiddenObject>();
|
||||
Assert.IsNotNull(_keyInstance);
|
||||
|
||||
Assert.IsNotNull(_chestClosedPrefab);
|
||||
_chestInstance = Instantiate(_chestClosedPrefab,
|
||||
new Vector3(
|
||||
Random.Range(_potionGroundSpawnRange.x, _potionGroundSpawnRange.y),
|
||||
0.5f,
|
||||
Random.Range(_potionGroundSpawnRange.x, _potionGroundSpawnRange.y)
|
||||
),
|
||||
Quaternion.LookRotation(Vector3.up));
|
||||
}
|
||||
|
||||
public void CollectKey()
|
||||
{
|
||||
isKeyCollected = true;
|
||||
Debug.Log("Collected Key");
|
||||
}
|
||||
|
||||
public void OpenChest()
|
||||
{
|
||||
if (!isKeyCollected)
|
||||
{
|
||||
Debug.LogWarning("Key not collected");
|
||||
return;
|
||||
}
|
||||
|
||||
Instantiate(
|
||||
_chestOpenPrefab,
|
||||
new Vector3(
|
||||
_chestInstance.transform.position.x,
|
||||
0.5f,
|
||||
_chestInstance.transform.position.z
|
||||
),
|
||||
Quaternion.LookRotation(Vector3.up)
|
||||
);
|
||||
GameOver(true);
|
||||
}
|
||||
public void SpawnPotion()
|
||||
{
|
||||
Assert.IsNotNull(_potionPrefab);
|
||||
Instantiate(_potionPrefab,
|
||||
new Vector3(
|
||||
Random.Range(_potionGroundSpawnRange.x, _potionGroundSpawnRange.y),
|
||||
0.5f,
|
||||
Random.Range(_potionGroundSpawnRange.x, _potionGroundSpawnRange.y)),
|
||||
Quaternion.identity);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
_timer.SetText(((int)(Time.time)).ToString());
|
||||
if (Time.time - _lastTimeof2Seconds > 2)
|
||||
{
|
||||
Debug.Log("Decreasing health");
|
||||
_lastTimeof2Seconds = Time.time;
|
||||
_player.AddHealth(_healthDecrease);
|
||||
}
|
||||
|
||||
if (_player.GetHealth() <= 0)
|
||||
{
|
||||
GameOver();
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.R))
|
||||
{
|
||||
_keyInstanceScript.ShowObjectForSeconds(3);
|
||||
}
|
||||
}
|
||||
|
||||
private void GameOver(bool isWon = false)
|
||||
{
|
||||
_player.SetIsMovementEnabled(false);
|
||||
_healthDecrease = 0;
|
||||
if (isWon)
|
||||
{
|
||||
_gameOver.SetText("You won!");
|
||||
}
|
||||
else
|
||||
{
|
||||
_gameOver.SetText("You lost!");
|
||||
}
|
||||
_gameOver.enabled = true;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/GameManager.cs.meta
Normal file
11
Assets/Scripts/GameManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a696e5fdd07208c0bbf549e6ce6e4435
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
49
Assets/Scripts/HiddenObject.cs
Normal file
49
Assets/Scripts/HiddenObject.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Assertions;
|
||||
|
||||
public class HiddenObject : MonoBehaviour
|
||||
{
|
||||
private MeshRenderer _renderer;
|
||||
private GameManager _gameManager;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_renderer = GetComponent<MeshRenderer>();
|
||||
Assert.IsNotNull(_renderer);
|
||||
_renderer.enabled = false;
|
||||
|
||||
_gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
|
||||
Assert.IsNotNull(_gameManager);
|
||||
}
|
||||
|
||||
public void ShowObjectForSeconds(int seconds)
|
||||
{
|
||||
if (_renderer.enabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
StartCoroutine(ShowObjectForSecondsEnumerator(seconds));
|
||||
}
|
||||
|
||||
|
||||
private IEnumerator ShowObjectForSecondsEnumerator(int seconds)
|
||||
{
|
||||
_renderer.enabled = true;
|
||||
yield return new WaitForSeconds(seconds);
|
||||
_renderer.enabled = false;
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (!other.name.Equals("Player"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_gameManager.CollectKey();
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/HiddenObject.cs.meta
Normal file
11
Assets/Scripts/HiddenObject.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b697b5cfaf970c80e98324e353ddc5e6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
42
Assets/Scripts/Player.cs
Normal file
42
Assets/Scripts/Player.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Player : MonoBehaviour
|
||||
{
|
||||
private Vector3 _input;
|
||||
[Range(1f,100f)][SerializeField] private float _speed;
|
||||
[SerializeField] private int _health = 10;
|
||||
[SerializeField] private TMPro.TextMeshProUGUI _healthText;
|
||||
private bool _isMovementEnabled = true;
|
||||
|
||||
public bool GetIsMovementEnabled() => _isMovementEnabled;
|
||||
public void SetIsMovementEnabled(bool isMovementEnabled) => _isMovementEnabled = isMovementEnabled;
|
||||
public int GetHealth() => _health;
|
||||
|
||||
public void AddHealth(int health)
|
||||
{
|
||||
if (_health <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_health += health;
|
||||
_healthText.text = _health.ToString();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_isMovementEnabled)
|
||||
{
|
||||
_input = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
|
||||
if (Input.GetAxisRaw("Vertical") > 0)
|
||||
{
|
||||
gameObject.transform.Translate(Vector3.forward * (_speed * Time.deltaTime));
|
||||
}else if (Input.GetAxisRaw("Vertical") < 0)
|
||||
{
|
||||
gameObject.transform.Translate(Vector3.back * (_speed * Time.deltaTime));
|
||||
}
|
||||
gameObject.transform.Rotate(0, Input.GetAxisRaw("Horizontal") * 100f * Time.deltaTime, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Player.cs.meta
Normal file
11
Assets/Scripts/Player.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4ff9d150b39a1e3b1a244c60268927cf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
33
Assets/Scripts/Potion.cs
Normal file
33
Assets/Scripts/Potion.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Assertions;
|
||||
|
||||
public class Potion : MonoBehaviour
|
||||
{
|
||||
private Player _player;
|
||||
private GameManager _gameManager;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_player = GameObject.Find("Player").GetComponent<Player>();
|
||||
Assert.IsNotNull(_player);
|
||||
|
||||
_gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
|
||||
Assert.IsNotNull(_gameManager);
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (!other.name.Equals("Player"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_player.AddHealth(5);
|
||||
Debug.Log("Potion collected increased health by 5");
|
||||
Destroy(gameObject);
|
||||
_gameManager.SpawnPotion();
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Potion.cs.meta
Normal file
11
Assets/Scripts/Potion.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bd300e30ff0e77de6823067d62881e9c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
32
Assets/Scripts/ShowHelp.cs
Normal file
32
Assets/Scripts/ShowHelp.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Assertions;
|
||||
|
||||
public class ShowHelp : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private String _helpText;
|
||||
[SerializeField] private TMPro.TextMeshProUGUI _helpTextUI;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Assert.IsFalse(String.IsNullOrEmpty(_helpText));
|
||||
_helpTextUI.SetText(_helpText);
|
||||
_helpTextUI.enabled = false;
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (!other.name.Equals("Player"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
_helpTextUI.enabled = true;
|
||||
}
|
||||
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
_helpTextUI.enabled = false;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/ShowHelp.cs.meta
Normal file
11
Assets/Scripts/ShowHelp.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0695f192e512f4e28bdf02672332659d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user