-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cs
157 lines (130 loc) · 3.32 KB
/
Player.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public float Speed;
public float JumpForce;
public int Life;
private int TemporaryLife;
private bool Dead;
private Rigidbody2D rig;
private Animator anim;
public bool isJumping;
public bool isWalking;
public GameObject[] hearts;
void Start()
{
Life = hearts.Length;
rig = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
void Update()
{
if (!Dead)
{
Move();
Jump();
}
}
void Move()
{
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.position += movement * Time.deltaTime * Speed;
if (Input.GetAxis("Horizontal") > 0f)
{
isWalking = true;
anim.SetBool("walk", true);
transform.eulerAngles = new Vector3(0f, 0f, 0f);
}
if (Input.GetAxis("Horizontal") < 0f)
{
isWalking = true;
anim.SetBool("walk", true);
transform.eulerAngles = new Vector3(0f, 180f, 0f);
}
if (Input.GetAxis("Horizontal") == 0f)
{
anim.SetBool("walk", false);
isWalking = false;
}
}
void Jump()
{
if (Input.GetButtonDown("Jump"))
{
if (!isJumping)
{
rig.AddForce(new Vector2(0f, JumpForce), ForceMode2D.Impulse);
anim.SetBool("jump", true);
}
}
}
void FreezeAnimation()
{
if (Dead)
{
anim.enabled = false;
}
}
void PlayerDamage()
{
if (Life > 0)
{
Life--;
Destroy(hearts[Life].gameObject);
if (Life < 1)
{
Dead = true;
anim.SetBool("damage", true);
Invoke("CallGameOver", 0.5f);
}
}
}
void CallGameOver()
{
GameController.instance.ShowGameOver();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Enemy")
{
rig.AddForce(new Vector2(0f, JumpForce), ForceMode2D.Impulse);
}
if (collision.gameObject.tag == "Explosion")
{
PlayerDamage();
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.layer == 8)
{
isJumping = false;
anim.SetBool("jump", false);
}
if (collision.gameObject.tag == "Enemy" && collision.collider.GetType() == typeof(BoxCollider2D))
{
PlayerDamage();
}
if (collision.gameObject.tag == "Spike" || collision.gameObject.tag == "Saw")
{
for (int i = 0; i < 2; i++)
{
PlayerDamage();
}
}
if (collision.gameObject.tag == "Trampoline")
{
rig.AddForce(new Vector2(0f, JumpForce +8), ForceMode2D.Impulse);
anim.SetBool("jump", true);
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.layer == 8)
{
isJumping = true;
}
}
}