-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHero.cs
141 lines (136 loc) · 3.36 KB
/
Hero.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Hero : MonoBehaviour
{
[SerializeField] float speed;
[SerializeField] float jumpForce;
[SerializeField] Slider HP;
[SerializeField] int BorderHitPoints;
[SerializeField] int MeteorHitPoints;
private Rigidbody2D rb;
private Animator anim;
private SpriteRenderer sprite;
private bool isGrounded = false;
public Vector3 respawn ;
private States State
{
get { return (States)anim.GetInteger("state"); }
set { anim.SetInteger("state", (int)value); }
}
void Update()
{
if (isGrounded)
{
State = States.idle;
}
if (Input.GetAxis("Horizontal") != 0)
{
Run();
}
if (isGrounded && Input.GetButtonDown("Jump"))
{
Jump();
}
}
private void FixedUpdate()
{
CheckGround();
}
private void Awake()
{
HP = GameObject.Find("HPShow1").GetComponent<Slider>();
HP.value = 100;
rb = GetComponent<Rigidbody2D>();
sprite = GetComponentInChildren<SpriteRenderer>();
anim = GetComponent<Animator>();
}
private void Run()
{
if (isGrounded)
{
State = States.run;
}
Vector3 dir = transform.right * Input.GetAxis("Horizontal");
transform.position = Vector3.MoveTowards(transform.position, transform.position + dir, speed * Time.deltaTime);
sprite.flipX = dir.x < 0;
}
private void Jump()
{
rb.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
}
private void CheckGround()
{
Collider2D[] collider = Physics2D.OverlapCircleAll(transform.position, 0.3f);
isGrounded = collider.Length > 1;
if (!isGrounded)
{
State = States.jump;
}
}
public enum States
{
idle,
run,
jump
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Border"))
{
if (HP.value >= BorderHitPoints)
{
HP.value -= BorderHitPoints;
transform.position = respawn;
}
else
{
GameOverScreen1();
}
}
if (collision.gameObject.CompareTag("NextLvlPass"))
{
LoadNextLevel();
}
if (collision.gameObject.CompareTag("Finish."))
{
FinishScreen1();
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Enemy"))
{
if (HP.value >= MeteorHitPoints)
{
HP.value -= MeteorHitPoints;
}
else
{
GameOverScreen1();
}
}
}
public void QuitGame()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#endif
Application.Quit();
}
private void LoadNextLevel()
{
HP.value = 100;
SceneManager.LoadScene("Level2");
}
private void FinishScreen1()
{
SceneManager.LoadScene("Finish1");
}
private void GameOverScreen1()
{
SceneManager.LoadScene("GameOverS");
}
}