-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.cs
executable file
·179 lines (156 loc) · 5.09 KB
/
Controller.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Controller : MonoBehaviour
{
public float maxSpeed;
bool facingRight = false;
float move;
float ijumpForce;
public float jumpforce;
public bool grounded = true;
float extraspeed = 1f;
public Transform groundCheck;
float groundRadius = 0.05f;
public LayerMask whatIsGround;
bool muerto;
public Vector3 spawnPoint;
Talisman[] habilities;
public GameObject[] talismans;
public Slider healthSlider;
public Slider experienceSlider;
public Image talismanSimbol;
public bool dashing = false;
Animator anim;
public Talisman curTalisman;
int selectedTalisman = 0;
public Text levelTag;
public float health;
public float maxhealth;
public Image gameOverScreen;
void Awake() {
habilities = new Talisman[talismans.Length];
}
void Start()
{
gameOverScreen.CrossFadeAlpha(0f, 0f, false);
spawnPoint = transform.position;
anim = GetComponent<Animator>();
ijumpForce = jumpforce;
for (int i = 0; i < talismans.Length; i++)
{
GameObject go = Instantiate(talismans[i]) as GameObject;
go.transform.parent = transform;
habilities[i] = go.GetComponent<Talisman>();
}
}
void Update()
{
healthSlider.value = health;
move = Input.GetAxis("Horizontal");
anim.SetFloat("Speed", Mathf.Abs(move));
curTalisman = habilities[selectedTalisman];
levelTag.text = "level " + curTalisman.level;
talismanSimbol.sprite = curTalisman.simbol;
experienceSlider.value = curTalisman.energy;
if (curTalisman.level != 1)
experienceSlider.minValue = curTalisman.energytresholds[curTalisman.level - 2];
else
experienceSlider.minValue = 0;
experienceSlider.maxValue = curTalisman.energytresholds[curTalisman.level - 1];
if (!dashing)
{
if (Input.GetButton("Run"))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed * 1.7f, GetComponent<Rigidbody2D>().velocity.y);
}
else
{
GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
if (move > 0 && !facingRight)
Flip();
else if (move < 0 && facingRight)
Flip();
}
if (Input.GetButtonDown("Jump") && (grounded))
{
grounded = false;
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, GetComponent<Rigidbody2D>().velocity.y + jumpforce);
}
if (Input.GetButton("Jump") && GetComponent<Rigidbody2D>().velocity.y > 0 && !(grounded))
{
grounded = false;
extraspeed -= Time.deltaTime * 50;
jumpforce += Time.deltaTime * extraspeed;
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpforce);
}
if (!grounded && !Input.GetButton("Jump"))
jumpforce = 0f;
if (Input.GetButtonDown("SwitchUp")) {
if (selectedTalisman != 3)
selectedTalisman++;
else
selectedTalisman = 0;
}
if (Input.GetButtonDown("SwitchDown"))
{
if (selectedTalisman != 0)
selectedTalisman--;
else
selectedTalisman = 3;
}
if (Input.GetButtonDown("Action")) {
if (selectedTalisman != 0)
curTalisman.activate();
}
if (Input.GetButton("Action")) {
if (selectedTalisman == 2)
{
curTalisman.activate();
}
}
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
if (grounded)
{
jumpforce = ijumpForce;
extraspeed = 1f;
}
if (transform.position.y < -2f && !muerto)
{
die();
}
}
void die() {
gameOverScreen.CrossFadeAlpha(1f, 0.7f, false);
muerto = true;
Invoke("respawn", 2f);
}
public void getHit(float amount) {
health -= amount;
curTalisman.looseEnergy(amount * 0.5f);
if (health <= 0) {
die();
}
}
void respawn()
{
gameOverScreen.CrossFadeAlpha(0, 0.5f, false);
Application.LoadLevel(Application.loadedLevel);
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
public void heal(float healz) {
health += healz;
if (health > maxhealth)
health = maxhealth;
}
}