PJ/Assets/scripts/Move.cs

286 lines
7.6 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Move : MonoBehaviour
{
public float speed;
public float NormalSpeed;
public string numlvl { get; set; }
public float MinY;
public float jumpforce = 15f;
public bool isGrounded = false;
public bool IsKiller = false;
public float RayDistance;
public AudioSource CoinColect;
//public GameObject[] control;
//int ButtonSize;
private bool CanTryJump;
private bool IsLadder;
private int TJ;
private Rigidbody2D rb;
public AudioClip LVLmusic;
public GameObject LvlMusic;
public GameObject LvlMusic2;
public AudioSource LM;
public Slider Healt;
public int ThisScene;
public int Lives;
int MaxLives;
public BubbleBlock BB;
public GameObject Body;
public GameObject EndScreen;
public bool JumpChangeGravity;
List<RaycastHit2D> hit;
int checker;
public bool rp;
public bool Finished;
public string LName;
public GameObject TS;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Money")
{
int Money = PlayerPrefs.GetInt("Moneys");
int MaxMoney = PlayerPrefs.GetInt("MaxMoney");
MaxMoney++;
Money++;
Console.WriteLine("Money Colect");
PlayerPrefs.SetInt("MaxMoney", MaxMoney);
PlayerPrefs.SetInt("Moneys", Money);
Destroy(collision.gameObject);
CoinColect.Play();
}
if (collision.tag == "Metall")
{
int Money = PlayerPrefs.GetInt("Metall");
int MaxMoney = PlayerPrefs.GetInt("MaxMetall");
MaxMoney++;
Money++;
Console.WriteLine("Money Colect");
PlayerPrefs.SetInt("MaxMetall", MaxMoney);
PlayerPrefs.SetInt("Metall", Money);
Destroy(collision.gameObject);
CoinColect.Play();
}
if (collision.tag == "Crystal")
{
int Money = PlayerPrefs.GetInt("Moneys");
int MaxMoney = PlayerPrefs.GetInt("MaxMoney");
MaxMoney += 50;
Money += 50;
Console.WriteLine("Money Colect");
PlayerPrefs.SetInt("MaxMoney", MaxMoney);
PlayerPrefs.SetInt("Moneys", Money);
Destroy(collision.gameObject);
CoinColect.Play();
}
if (collision.tag == "Ladder")
{
IsLadder = true;
}
else
{
IsLadder = false;
}
CanTryJump = true;
}
private void Awake()
{
this.enabled = true;
if (GameObject.Find("LvlMusic(Clone)") == null)
{
Instantiate(LvlMusic);
LvlMusic2 = GameObject.Find("LvlMusic(Clone)");
LM = LvlMusic2.GetComponent<AudioSource>();
if (LM.clip != LVLmusic && LVLmusic != null) { LM.clip = LVLmusic; }
LM.Play();
DontDestroyOnLoad(LM);
UnityEngine.Debug.Log(LM);
LM.volume = PlayerPrefs.GetFloat("MV");
}
else
{
LvlMusic2 = GameObject.Find("LvlMusic(Clone)");
LM = LvlMusic2.GetComponent<AudioSource>();
if (LM.clip != LVLmusic && LVLmusic != null)
{
LM.volume = PlayerPrefs.GetFloat("MV");
LM.clip = LVLmusic;
LM.Play();
}
}
}
private void Start()
{
ThisScene = SceneManager.loadedSceneCount;
this.enabled = true;
//ButtonSize = PlayerPrefs.GetInt("ButtonSize");
//Physics2D.gravity = new Vector2(0, -9.81f);
rb = GetComponent<Rigidbody2D>();
speed = 0f;
MaxLives = Lives;
}
private void Update()
{
rb.linearVelocity = new Vector2(speed, rb.linearVelocity.y);
if (Input.GetKeyDown(KeyCode.A))
{
OnLeftButtonDown();
}
if (Input.GetKeyDown(KeyCode.D))
{
OnRightButtonDown();
}
if (Input.GetKey(KeyCode.Space) || Input.GetKeyDown(KeyCode.W))
{
if (JumpChangeGravity)
{
ChangeGravity();
}
else
{
BJump();
}
}
if ((Input.GetKeyUp(KeyCode.A) && speed < 0)|| (Input.GetKeyUp(KeyCode.D) && speed > 0))
{
OnButtonUp();
}
for(float i = -20; i <= 20; i++)
{
Debug.DrawRay(new Vector2(rb.position.x + i / 20, rb.position.y), Vector2.down, Color.red ,RayDistance);
if (Physics2D.Raycast(new Vector2(rb.position.x + i/20, rb.position.y), Vector2.down, RayDistance).collider)
{
checker++;
}
}
if (rp)
{if
(JumpChangeGravity)
{
ChangeGravity();
}
else
{
BJump();
}
}
RaycastHit2D hdit = Physics2D.Raycast(rb.position, Vector2.down, RayDistance);
isGrounded = checker > 0;
checker = 0;
if(Lives > MaxLives){Lives = MaxLives;}
}
private void FixedUpdate()
{
this.enabled = true;
if (Healt != null)
{
Healt.value = Lives;
}
if (Lives <= 0)
{
Destroy(this.gameObject);
print("Lives");
}
if (MinY != 0)
{
if (transform.position.y < -8)
{
Lives = 0;
}
}
}
public void SJ(bool J)
{
rp = J;
}
public void BJump()
{
if (IsLadder && isGrounded)
{
rb.linearVelocity = Vector2.up * 20;
}
if (isGrounded && CanTryJump)
{
rb.linearVelocity = Vector2.up * jumpforce;
}
if (BB != null)
{
BB.Stop();
}
IsLadder = false;
}
public void ChangeGravity()
{
Physics2D.gravity = -Physics2D.gravity;
}
public void OnLeftButtonDown()
{
Body.GetComponent<Transform>().transform.localScale = new Vector2(-2.5f, 2.5f);
GetComponent<Transform>().transform.localScale = new Vector2(-1, 1);
if (speed >= -NormalSpeed)
{
speed = -NormalSpeed;
}
}
public void OnRightButtonDown()
{
Body.GetComponent<Transform>().transform.localScale = new Vector2(2.5f, 2.5f);
GetComponent<Transform>().transform.localScale = new Vector2(1, 1);
if (speed <= NormalSpeed)
{
speed = NormalSpeed;
}
}
public void OnButtonUp()
{
speed = 0;
}
private void OnDestroy()
{
if (ThisScene == SceneManager.loadedSceneCount && Lives <= 0 && !Finished)
{
Buttons ES = Instantiate(EndScreen).GetComponent<Buttons>();
ES.string1 = numlvl;
}
else { print(ThisScene); }
}
public void Retry()
{
TransferScript ts = Instantiate(TS).GetComponent<TransferScript>();
ts.string1 = numlvl;
ts.bool1 = true;
ts.gameObject.SetActive(true);
ts.gameObject.name = "Tobj";
DontDestroyOnLoad(ts.gameObject);
Buttons.End("ImportLeverl");
}
}