Level selection

This commit is contained in:
2023-02-05 19:14:04 +05:30
parent d2d1d49d48
commit d026008220
60 changed files with 36000 additions and 31 deletions

View File

@@ -2,13 +2,21 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Drawer : MonoBehaviour
{
public static Drawer instance {get;private set;}
void Awake(){
instance= this;
}
public LineRenderer lineRenderer;
public PolygonCollider2D edgeCollider2D;
List<Vector3> points = new List<Vector3>();
public Slider drawingFuel;
public float drawingFuelConsumption = 0.05f;
bool dragging = false;
public void OnMouseDown(BaseEventData e){
@@ -31,6 +39,7 @@ public class Drawer : MonoBehaviour
Vector3 startedPoint = new Vector3();
public void OnMouseDrag(BaseEventData e) {
if(drawingFuel.value <= 0){return;}
PointerEventData ped = (PointerEventData) e as PointerEventData;
Vector3 worldPos = Camera.main.ScreenToWorldPoint(ped.position);
if(points.Count> 0) {
@@ -43,6 +52,8 @@ public class Drawer : MonoBehaviour
points.Add(worldPos);
UpdateLine();
drawingFuel.value -= drawingFuelConsumption;
}
public void OnMouseUp(BaseEventData e)
@@ -75,6 +86,7 @@ public class Drawer : MonoBehaviour
//edgeCollider2D.SetPoints(points3);
edgeCollider2D.SetPath(0, points3);
edgeCollider2D.GetComponent<Rigidbody2D>().simulated = true;
GameManager.Player.GetComponent<Rigidbody2D>().simulated=true;
}

View File

@@ -7,6 +7,7 @@ public class Enemy : MonoBehaviour
public float moveSpeed = 0.1f;
public bool canSeePlayer = false;
public float distanceThreshold = 0.1f;
public float playerDistanceThreshold = 0.1f;
public float dodgeTime = 0.2f;
float dodgeTimer = 0;
public LayerMask linecastLayer;
@@ -32,7 +33,7 @@ public class Enemy : MonoBehaviour
{
canSeePlayer = true;
}
else
else if(hit.collider.tag == "Obstacle")
{
canSeePlayer = false;
float distanceToBarrier = Vector2.Distance(transform.position, hit.point);
@@ -45,7 +46,7 @@ public class Enemy : MonoBehaviour
if (canSeePlayer)
{
if (Vector2.Distance(GameManager.Player.position, transform.position) < distanceThreshold*5)
if (Vector2.Distance(GameManager.Player.position, transform.position) < playerDistanceThreshold)
{
GameManager.GameOver();
return;
@@ -53,20 +54,22 @@ public class Enemy : MonoBehaviour
}
Vector3 direction = (GameManager.Player.position - new Vector3(0,0.5f,0) - transform.position).normalized;
float rot_z = Mathf.Atan2(direction.y,direction.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0,0,rot_z+180);
if (dodgeTimer < dodgeTime)
{
dodgeTimer += Time.deltaTime;
if (dodgeTimer > dodgeTime/100f)
if (dodgeTimer > dodgeTime/100f || true)
{
debugColor = Color.red;
transform.Translate(new Vector3(-direction.x, direction.y) * moveSpeed * 1.2f);
transform.Translate(new Vector3(-direction.x, direction.y) * moveSpeed * 1.2f, Space.World);
return;
}
}
transform.Translate(direction * moveSpeed);
transform.Translate(direction * moveSpeed, Space.World);
debugColor = Color.white;
}

View File

@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
@@ -11,11 +12,15 @@ public class GameManager : MonoBehaviour
public static GameManager instance { get; private set; }
public Sprite normalFace, sadFace, pogFace;
public Text timerTxt;
public float game_start_timer = 5;
public float game_end_timer = 15;
public GameObject gameOverPanel;
public GameObject gameWonPanel;
public Image[] stars;
public GameObject[] bees;
void Awake()
{
@@ -46,21 +51,67 @@ public class GameManager : MonoBehaviour
ReleaseTheBees();
}
timerTxt.text = (game_end_timer - t).ToString("n0");
}else{
StopTheBees();
GameWon();
}
}
void ReleaseTheBees()
async void ReleaseTheBees()
{
foreach(GameObject enemy in bees)
{
enemy.SetActive(true);
await Task.Delay(200);
}
}
void StopTheBees()
{
foreach(GameObject enemy in bees)
{
enemy.GetComponent<Enemy>().enabled=false;
}
}
public static void GameOver()
{
instance.gameOverPanel.SetActive(true);
instance.gameStarted = false;
instance.player.GetComponent<SpriteRenderer>().sprite = instance.sadFace;
}
public static void GameWon(){
instance.gameStarted = false;
instance.gameWonPanel.SetActive(true);
instance.player.GetComponent<SpriteRenderer>().sprite = instance.pogFace;
instance.gameWon();
}
void gameWon(){
float fuelLeft = Drawer.instance.drawingFuel.value / Drawer.instance.drawingFuel.maxValue;
int level = 1;
if(fuelLeft > 0.5f){
level=2;
}else if(fuelLeft> 0.8f){
level =3;
}
for(int i =0;i < 3; i++){
stars[i].color = level-1 >= i ? Color.yellow : Color.gray;
}
int levelNumber = int.Parse(SceneManager.GetActiveScene().name.Replace("Level",""));
Debug.Log($"Level {levelNumber} won with {level} stars");
LevelSelect.SetLevel(levelNumber,level);
}
public void NextLevel(){
int levelNumber = int.Parse(SceneManager.GetActiveScene().name.Replace("Level",""));
SceneManager.LoadScene($"Level{levelNumber+1}");
}
public void Restart()

View File

@@ -0,0 +1,126 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Newtonsoft.Json;
public class LevelSelect : MonoBehaviour
{
public GameObject levelPrefab;
public int LevelCount = 25;
public static Dictionary<int,int> levelProgress;
// public static Dictionary<int,int> levelProgress {
// get{
// if(m_levelProgress == null){
// if(!PlayerPrefs.HasKey("progress") ){
// Debug.Log("No key for progress found, generating new");
// m_levelProgress=new Dictionary<int, int>();
// }else{
// try{
// m_levelProgress= JsonConvert.DeserializeObject<Dictionary<int,int>>(PlayerPrefs.GetString("progress"));
// }catch{
// Debug.LogError("Failed parsing progress from save, Creating new save");
// m_levelProgress = new Dictionary<int, int>();
// }
// }
// }
// return m_levelProgress;
// }set{
// m_levelProgress=value;
// // Debug.Log()
// PlayerPrefs.SetString("progress", JsonConvert.SerializeObject(value));
// PlayerPrefs.Save();
// Debug.Log("saving: " +JsonConvert.SerializeObject(value));
// }
// }
public static void SetLevel(int level, int stars){
if(levelProgress == null){
levelProgress = new Dictionary<int, int>();
}
if(levelProgress.ContainsKey(level)){
levelProgress[level] = stars;
}else{
levelProgress.Add(level,stars);
}
Debug.Log(JsonConvert.SerializeObject(levelProgress));
PlayerPrefs.SetString("progress", JsonConvert.SerializeObject(levelProgress));
PlayerPrefs.Save();
}
public Sprite locked,unlocked;
void Start()
{
// levelProgress.Add(0,2);
if(PlayerPrefs.HasKey("progress")){
string savedata = PlayerPrefs.GetString("progress");
levelProgress = JsonConvert.DeserializeObject<Dictionary<int,int>>(savedata);
Debug.Log(savedata);
}else{
levelProgress = new Dictionary<int, int>();
}
//Populate level selection
for(int i=0; i < LevelCount;i++){
int curIndex = i;
GameObject newLevel = Instantiate(levelPrefab, levelPrefab.transform.parent);
newLevel.transform.GetChild(0).GetComponent<Text>().text = $"Level {i+1}";
if(i==0){
newLevel.transform.GetChild(1).GetComponent<Image>().sprite = unlocked;
if(levelProgress.ContainsKey(0)){
newLevel.transform.GetChild(2).gameObject.SetActive(true);
for(int x =0; x < 3; x++){
newLevel.transform.GetChild(2).GetChild(x).GetComponent<Image>().color = (levelProgress[i] > x) ? Color.yellow : Color.gray;
}
}else{
}
}else{
if(levelProgress.ContainsKey(i-1)){
newLevel.transform.GetChild(2).gameObject.SetActive(true);
newLevel.transform.GetChild(1).GetComponent<Image>().sprite = unlocked;
if(levelProgress.ContainsKey(i)){
for(int x =0; x < 3; x++){
newLevel.transform.GetChild(2).GetChild(x).GetComponent<Image>().color = (levelProgress[i] > x) ? Color.yellow : Color.gray;
}
}
}else{
newLevel.transform.GetChild(1).GetComponent<Image>().sprite = locked;
newLevel.transform.GetChild(2).gameObject.SetActive(false);
}
}
newLevel.GetComponent<Button>().onClick.AddListener(()=>{SceneManager.LoadScene("Level"+curIndex);});
}
Destroy(levelPrefab);
}
// Update is called once per frame
void Update()
{
}
public static void SaveProgress(Dictionary<int,int> progress){
string val = JsonConvert.SerializeObject(progress);
PlayerPrefs.SetString("progress", val);
PlayerPrefs.Save();
Debug.Log("saving: " +val);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 130c563f78e2a1344ba835efe16e4adc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: