04vini

Untitled

Jul 9th, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.12 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6.  
  7. public class Person : MonoBehaviour
  8. {
  9. public float speed = 5.0f;
  10. public int hp;
  11. public int maxHp = 100;
  12. public float mana;
  13. public float maxMana = 200;
  14. public int dmg = 10;
  15. public bool shield;
  16. public float attackRangeXPunch = 3;
  17. public float attackRangeYPunch = 3;
  18. public float attackRangeXStun = 3;
  19. public float attackRangeYStun= 3;
  20.  
  21. private Animator anim;
  22. public Vector3 targetPosition;
  23. public Image hpBar;
  24. public Image manaBar;
  25. public int level = 1;
  26. public int bossSave;
  27. public bool isPaused;
  28. public bool isPausedSt;
  29. public Image heathIcon;
  30. public Sprite[] heathIconS;
  31. Vector3 movement;
  32. bool inverControls;
  33. public GameObject panelPause;
  34. public GameObject panelGameOver;
  35. public GameObject panelDecisão;
  36. public GameObject panelSkillTree;
  37.  
  38. //public Transform attackPosDef;
  39. //times cogumelo
  40. private float TimeDecreaseHp;
  41. private float TimeDecreaseSpeed;
  42. private float TimeStun;
  43. private float TimeInverControl;
  44. private float TimeDmg;
  45. private float TimeDef;
  46. public LayerMask whatIsEnemies;
  47.  
  48.  
  49.  
  50. public PlayerSkills skiltree;
  51.  
  52. public int skillPoints;
  53.  
  54. private static Person playerInstance;
  55. Rigidbody2D rigidbody;
  56. // Start is called before the first frame update
  57.  
  58. private void Awake()
  59. {
  60. if (playerInstance == null)
  61. {
  62. playerInstance = this;
  63. }
  64. else
  65. {
  66. Destroy(gameObject);
  67. }
  68.  
  69. }
  70. void Start()
  71. {
  72.  
  73. anim = GetComponent<Animator>();
  74. speed = 5.0f;
  75. hp = maxHp;
  76. mana = maxMana;
  77. rigidbody = GetComponent<Rigidbody2D>();
  78. rigidbody.velocity = Vector3.zero;
  79. skiltree = new PlayerSkills();
  80. LoadPlayer();
  81.  
  82. }
  83.  
  84.  
  85. // Update is called once per frame
  86. void Update()
  87. {
  88.  
  89. hpBar.fillAmount = (float) hp / (float) maxHp;
  90. manaBar.fillAmount = mana / maxMana;
  91.  
  92.  
  93.  
  94. if (hp > 85)
  95. {
  96. heathIcon.sprite = heathIconS[0];
  97. }
  98. else if(hp > 70)
  99. {
  100. heathIcon.sprite = heathIconS[1];
  101. }
  102. else if(hp > 55)
  103. {
  104. heathIcon.sprite = heathIconS[2];
  105. }
  106. else if(hp > 40)
  107. {
  108. heathIcon.sprite = heathIconS[3];
  109. }
  110. else if(hp > 25)
  111. {
  112. heathIcon.sprite = heathIconS[4];
  113. }
  114. else if(hp > 15)
  115. {
  116. heathIcon.sprite = heathIconS[5];
  117. }
  118. else if(hp > 10)
  119. {
  120. heathIcon.sprite = heathIconS[6];
  121. }
  122.  
  123.  
  124. if (FindObjectOfType<SkillThree>().CanUseFireball())
  125. {
  126. dmg = 20;
  127. }
  128. if (FindObjectOfType<SkillThree>().CanUseFireballNC())
  129. {
  130. dmg = 30;
  131. }
  132. if (FindObjectOfType<SkillThree>().CanUseFireballC())
  133. {
  134. dmg = 1;
  135. }
  136. if (isPaused == true)
  137. {
  138. Time.timeScale = 0;
  139. panelPause.SetActive(true);
  140. }
  141. if(Time.timeScale > 0)
  142. {
  143. Time.timeScale = 1;
  144. panelPause.SetActive(false);
  145. movement = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0f);
  146. targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  147. //seta animação do personagem para lado em que ele esta olhando
  148. Vector3 deltaPosition = targetPosition - transform.position;
  149. anim.SetFloat("Magnitude", deltaPosition.magnitude);
  150. Vector3 targetDirection = deltaPosition.normalized;
  151. if (hp > 0)
  152. {
  153. anim.SetFloat("HorizontalIdle", targetDirection.x);
  154. anim.SetFloat("VerticalIdle", targetDirection.y);
  155. anim.SetFloat("Horizontal", targetDirection.x);
  156. anim.SetFloat("Vertical", targetDirection.y);
  157. anim.SetFloat("Speed", movement.magnitude);
  158. if (inverControls == false)
  159. {
  160. transform.position = transform.position + movement * speed * Time.deltaTime;
  161. }
  162. else
  163. {
  164. transform.position = transform.position + -movement * speed * Time.deltaTime;
  165. }
  166. }
  167.  
  168.  
  169. }
  170.  
  171. if (Input.GetKeyDown(KeyCode.Escape) && isPausedSt == false)
  172. {
  173. PauseGame();
  174. }
  175.  
  176. if (Input.GetKeyDown(KeyCode.K) && isPaused == false)
  177. {
  178. OpenSkillTree();
  179. }
  180.  
  181. if (isPausedSt)
  182. {
  183. panelSkillTree.SetActive(true);
  184. Time.timeScale = 0;
  185. }
  186. else
  187. {
  188. panelSkillTree.SetActive(false);
  189. Time.timeScale = 1;
  190. }
  191.  
  192. if (hp <= 0)
  193. {
  194. anim.SetBool("Death", true);
  195. panelGameOver.SetActive(true);
  196. }
  197. else if (hp >= maxHp)
  198. {
  199. hp = maxHp;
  200. }
  201. else
  202. {
  203. panelGameOver.SetActive(false);
  204. }
  205.  
  206. if (mana < maxMana)
  207. {
  208. mana += Time.deltaTime * 1;
  209. }
  210. else if (mana >= maxMana)
  211. {
  212. mana = maxMana;
  213. }
  214.  
  215.  
  216. if (mana <= 0)
  217. {
  218. mana = 0;
  219. }
  220. if(speed > 5)
  221. {
  222. speed -= Time.deltaTime;
  223. }
  224.  
  225. //DMG COGUMELOS
  226. if (TimeDecreaseHp > 0)
  227. {
  228.  
  229. hp --;
  230. TimeDecreaseHp -= Time.deltaTime;
  231. }
  232. else if(TimeDecreaseHp < 0)
  233. {
  234. TimeDecreaseHp = 0;
  235. }
  236.  
  237. if(TimeDef > 0)
  238. {
  239. shield = true;
  240. TimeDef -= Time.deltaTime;
  241. }
  242. else
  243. {
  244. shield = false;
  245. TimeDef = 0;
  246. }
  247.  
  248. if (TimeStun > 0)
  249. {
  250.  
  251. speed = 0;
  252. TimeStun -= Time.deltaTime;
  253. }
  254. else if (TimeStun < 0)
  255. {
  256. TimeStun = 0;
  257. }
  258. else if (TimeDecreaseSpeed > 0)
  259. {
  260.  
  261. speed = 2;
  262. TimeDecreaseSpeed -= Time.deltaTime;
  263. }
  264. else if (TimeDecreaseSpeed < 0)
  265. {
  266. TimeDecreaseSpeed = 0;
  267. }
  268. else
  269. {
  270. speed = 5.0f;
  271.  
  272. }
  273. if (TimeInverControl > 0)
  274. {
  275.  
  276. inverControls = true;
  277. TimeInverControl -= Time.deltaTime;
  278. }
  279. else if (TimeInverControl < 0)
  280. {
  281. TimeInverControl = 0;
  282. }
  283. else
  284. {
  285. inverControls = false;
  286. }
  287.  
  288. if(TimeDmg > 0)
  289. {
  290. TimeDmg -= Time.deltaTime;
  291. }
  292. else
  293. {
  294. GetComponent<SpriteRenderer>().color = Color.white;
  295. }
  296.  
  297. if(FindObjectOfType<CutscenePass>().isPaused == true)
  298. {
  299. Time.timeScale = 0;
  300. }
  301. else
  302. {
  303. Time.timeScale = 1;
  304. }
  305.  
  306. }
  307.  
  308.  
  309. public void TakeHeal(int amount) {
  310. hp += amount;
  311. }
  312.  
  313. public void DecreaseHp()
  314. {
  315. TimeDecreaseHp = 0.4f;
  316. GetComponent<SpriteRenderer>().color = Color.red;
  317. TimeDmg = 0.4f;
  318. }
  319.  
  320. public void Stun()
  321. {
  322. TimeStun = 2.0f;
  323. GetComponent<SpriteRenderer>().color = Color.magenta;
  324. TimeDmg = 2.0f;
  325. }
  326.  
  327. public void DecraseSpeed()
  328. {
  329. GetComponent<SpriteRenderer>().color = Color.yellow;
  330. TimeDmg = 3.0f;
  331. TimeDecreaseSpeed = 3.0f;
  332. }
  333. public void InvertControls()
  334. {
  335. GetComponent<SpriteRenderer>().color = Color.blue;
  336. TimeInverControl = 3.0f;
  337. TimeDmg = 3.0f;
  338.  
  339. }
  340.  
  341.  
  342. public void ActiveShield()
  343. {
  344. TimeDef = 0.3f;
  345. }
  346.  
  347.  
  348.  
  349. public void PauseGame()
  350. {
  351. isPaused = !isPaused;
  352. }
  353. public void OpenSkillTree()
  354. {
  355. isPausedSt = !isPausedSt;
  356. }
  357.  
  358.  
  359.  
  360. public void TakeDamage(int amount)
  361. {
  362. if (shield)
  363. {
  364. shield = false;
  365.  
  366. }
  367. else
  368. {
  369. hp -= amount;
  370. }
  371. GetComponent<SpriteRenderer>().color = Color.red;
  372. TimeDmg = 1.0f;
  373. }
  374.  
  375.  
  376.  
  377. public void SavePlayer()
  378. {
  379. SaveSystem.SavePlayer(this);
  380. CaptureState();
  381.  
  382. }
  383.  
  384. public void LoadPlayer()
  385. {
  386. PlayerData data = SaveSystem.LoadPlayer();
  387.  
  388. level = data.level;
  389. hp = data.health;
  390. maxHp = data.Maxhealth;
  391. maxMana = data.MaxMana;
  392. mana = data.Mana;
  393. bossSave = data.SaveBoss;
  394. dmg = data.Dmg;
  395. RestoreState(skiltree);
  396. }
  397.  
  398. public object CaptureState()
  399. {
  400. return skiltree.CaptureState();
  401. }
  402.  
  403. public void RestoreState(object state)
  404. {
  405. skiltree.RestoreState(state);
  406. }
  407.  
  408. public void Load()
  409. {
  410. LoadPlayer();
  411. switch (level)
  412. {
  413. case 1:
  414. SceneManager.LoadScene("Level1");
  415. break;
  416. case 2:
  417. SceneManager.LoadScene("Level2");
  418. break;
  419. }
  420. }
  421.  
  422. public void NewGame()
  423. {
  424.  
  425. SceneManager.LoadScene("Level1");
  426. level = 1;
  427. hp = 100;
  428. maxHp = 100;
  429. maxMana = 200;
  430. mana = 200;
  431. bossSave = 0;
  432. dmg = 10;
  433. SavePlayer();
  434.  
  435. }
  436.  
  437. public void HelpFade()
  438. {
  439. hp += 50;
  440. mana += 50;
  441. if (FindObjectOfType<SkillThree>().CanUsePleh())
  442. {
  443. hp += 70;
  444. mana += 70;
  445. }
  446.  
  447. if (FindObjectOfType<SkillThree>().CanUsePlehUp())
  448. {
  449. hp += 90;
  450. mana += 90;
  451. }
  452. }
  453.  
  454. public void TrocaCena()
  455. {
  456.  
  457. SavePlayer();
  458.  
  459. SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
  460. }
  461.  
  462.  
  463.  
  464. private void OnTriggerEnter2D(Collider2D collision)
  465. {
  466. if (collision.gameObject.tag == "Final")
  467. {
  468. level += 1;
  469.  
  470. TrocaCena();
  471.  
  472. }
  473.  
  474. if(collision.gameObject.tag == "DecisionBoss")
  475. {
  476. panelDecisão.SetActive(true);
  477. }
  478.  
  479. if(collision.gameObject.tag == "Boss3")
  480. {
  481. if (!shield)
  482. {
  483. hp = 0;
  484. }
  485. else
  486. {
  487. FindObjectOfType<BossRun>().DazedTime();
  488. }
  489. }
  490.  
  491. }
  492.  
  493. private void OnTriggerExit2D(Collider2D collision)
  494. {
  495. if (collision.gameObject.tag == "DecisionBoss")
  496. {
  497. panelDecisão.SetActive(false);
  498. }
  499. }
  500. }
Add Comment
Please, Sign In to add comment