-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacter.cs
More file actions
131 lines (102 loc) · 3.29 KB
/
Character.cs
File metadata and controls
131 lines (102 loc) · 3.29 KB
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
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class Character : Unit
{
[SerializeField]
private int lives = 5;
public int Lives
{
get { return lives; }
set
{
if (value <= 5) lives = value;
livesBar.Refresh();
}
}
private LivesBar livesBar;
[SerializeField]
private float speed = 5.0F;
[SerializeField]
private float jumpForce = 15.0F;
private bool isGrounded = false;
private Bullet bullet;
private CharState State
{
get { return (CharState)animator.GetInteger("State"); }
set { animator.SetInteger("State", (int)value); }
}
new private Rigidbody2D rigidbody;
private Animator animator;
private SpriteRenderer sprite;
private void Awake()
{
livesBar = FindObjectOfType<LivesBar>();
rigidbody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
sprite = GetComponentInChildren<SpriteRenderer>();
bullet = Resources.Load<Bullet>("Bullet");
}
private void FixedUpdate()
{
CheckGround();
}
private void Update()
{
if (isGrounded) State = CharState.Idle;
if (Input.GetButtonDown("Fire1")) Shoot();
if (Input.GetButton("Horizontal")) Run();
if (isGrounded && Input.GetButtonDown("Jump")) Jump();
if (transform.position.y < -10) SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 0);
if (Lives == 0) SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 0);
if (Input.GetKeyDown(KeyCode.Escape))
{
SceneManager.LoadScene("Levels");
}
}
private void Run()
{
Vector3 direction = transform.right * Input.GetAxis("Horizontal");
transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, speed * Time.deltaTime);
sprite.flipX = direction.x < 0.0F;
if (isGrounded) State = CharState.Run;
}
private void Jump()
{
rigidbody.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
}
private void Shoot()
{
Vector3 position = transform.position; position.y += 0.8F;
Bullet newBullet = Instantiate(bullet, position, bullet.transform.rotation) as Bullet;
newBullet.Parent = gameObject;
newBullet.Direction = newBullet.transform.right * (sprite.flipX ? -1.0F : 1.0F);
}
public override void ReceiveDamage()
{
Lives--;
rigidbody.velocity = Vector3.zero;
rigidbody.AddForce(transform.up * 8.0F, ForceMode2D.Impulse);
Debug.Log(lives);
}
private void CheckGround()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 0.3F);
isGrounded = colliders.Length > 1;
if (!isGrounded) State = CharState.Jump;
}
private void OnTriggerEnter2D(Collider2D collider)
{
Bullet bullet = collider.gameObject.GetComponent<Bullet>();
if (bullet && bullet.Parent != gameObject)
{
ReceiveDamage();
}
}
}
public enum CharState
{
Idle,
Run,
Jump
}