aboutsummaryrefslogtreecommitdiff
path: root/Assets/Scripts/CharacterControl.cs
blob: cb447bf50f08ebe811161b804a74bcf83ede10ac (plain) (blame)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterControl : MonoBehaviour {
  public float moveForce = 365f;
  public AudioClip walkNoise;
  public float walkNoiseVolume = 1f;
  public float walkNoiseFrequency = 1f;

  public float jumpForce = 1000f;
  public float jumpSustainForce = 0f;
  public AudioClip jumpNoise;
  public float jumpNoiseVolume = 1f;
  public AudioClip landNoise;
  public float landNoiseVolume = 1f;

  public bool limitHorizontalSpeed = true;
  public float maxHorizontalSpeed = 5f;
  public bool limitHorizontalSpeedWhenDamaged = false;

  public bool limitVerticalSpeed = true;
  public float maxVerticalSpeed = 5f;
  public bool limitVerticalSpeedWhenDamaged = false;
  public bool limitJumpSpeed = false;

  public float damageDuration = 1f;
  public Vector2 knockbackForce = new Vector2(-400f, 200f);
  public AudioClip damageNoise;
  public float damageNoiseVolume = 1f;

  public GameObject bulletPrefab;
  public AudioClip shootNoise;
  public float shootNoiseVolume = 1f;
  public float bulletSpawnHeightOffset = 0f;
  public float bulletSpeed = 1f;
  public float shootCooldown;

  private bool grounded;
  private bool damaged;
  private bool facingRight;
  private bool shotRecently;

  private Rigidbody2D body;
  private BoxCollider2D coll;
  private Animator anim;
  private Transform trans;
  private AudioSource audio;

  private float distToGround;

	void Start() {
    body = GetComponent<Rigidbody2D>();
    coll = GetComponent<BoxCollider2D>();
    anim = GetComponent<Animator>();
    trans = GetComponent<Transform>();
    audio = GetComponent<AudioSource>();

    distToGround = coll.bounds.extents.y;
	}

  void Awake() {
    Invoke("WalkNoisily", 0.1f);
  }

  void WalkNoisily() {
    if (grounded && !damaged && (Input.GetButton("Move Left") || Input.GetButton("Move Right"))) {
      if (walkNoise) audio.PlayOneShot(walkNoise, walkNoiseVolume);
    }

    Invoke("WalkNoisily", walkNoiseFrequency);
  }

	void Update() {
    if (!damaged) {
      if (Input.GetButtonDown("Damage")) {
        Damaged();
      }
    }

    grounded = IsGrounded();
    anim.SetBool("grounded", grounded);
    if (grounded && body.velocity.y < 0) Undamaged();

    if (!damaged) {
      if (grounded && Input.GetButtonDown("Jump")) {
        Jump();
      }

      if (!shotRecently && Input.GetButtonDown("Shoot")) {
        Shoot();
      }

      anim.SetBool("looking_up", Input.GetButton("Look Up"));
    }
	}

  void FixedUpdate() {
    if (!damaged) {
      Walk();
    }

    LimitHorizontalSpeed();
    LimitVerticalSpeed();

    if (!damaged && !grounded && Input.GetButton("Jump") && body.velocity.y > 0) {
      JumpHigher();
    }

    FaceMovement();
  }

  void Shoot() {
    shotRecently = true;
    anim.SetBool("just_shot", true);

    if (shootNoise) audio.PlayOneShot(shootNoise, shootNoiseVolume);

    Vector3 spawnPosition = trans.position + new Vector3(0, bulletSpawnHeightOffset);

    GameObject bullet = Instantiate(bulletPrefab, spawnPosition, Quaternion.identity);
    Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();

    if (Input.GetButton("Look Up")) {
      rb.velocity = Vector3.up * bulletSpeed;
    } else if (facingRight) {
      rb.velocity = Vector3.right * bulletSpeed;
    } else {
      rb.velocity = Vector3.left * bulletSpeed;
    }

    Invoke("Unshoot", shootCooldown);
  }

  void Unshoot() {
    shotRecently = false;
    anim.SetBool("just_shot", false);
  }

  public void Damaged() {
    if (damaged) return;

    if (damageNoise) audio.PlayOneShot(damageNoise, damageNoiseVolume);

    // reset momentum
    body.velocity = Vector2.zero;

    body.AddForce(new Vector2(trans.localScale.x * knockbackForce.x, knockbackForce.y));

    damaged = true;
    anim.SetBool("hurt", true);

    Invoke("Undamaged", damageDuration);
  }

  void Undamaged() {
    damaged = false;
    anim.SetBool("hurt", false);
  }

  bool IsGrounded() {
    float xSize = coll.bounds.extents.x;

    Debug.DrawRay(
        transform.position + Vector3.left * xSize + Vector3.down * distToGround,
        Vector2.down * 0.1f, Color.red);
    Debug.DrawRay(
        transform.position + Vector3.right * xSize + Vector3.down * distToGround,
        Vector2.down * 0.1f, Color.red);

    bool ret = Physics2D.Raycast(
        transform.position + Vector3.left * xSize + Vector3.down * (distToGround - 0.1f),
        Vector2.down, 0.2f)
      || Physics2D.Raycast(
          transform.position + Vector3.right * xSize + Vector3.down * (distToGround - 0.1f),
          Vector2.down, 0.2f);

    if (ret && !grounded) {
      if (landNoise) audio.PlayOneShot(landNoise, landNoiseVolume);
    }

    return ret;
  }

  void Jump() {
    if (jumpNoise) audio.PlayOneShot(jumpNoise, jumpNoiseVolume);

    body.AddForce(Vector2.up * jumpForce);
  }

  void JumpHigher() {
    body.AddForce(Vector2.up * jumpSustainForce);
  }

  void Walk() {
    if (Input.GetButton("Move Right")) {
      anim.SetBool("walking", true);
      facingRight = true;
      body.AddForce(Vector2.right * moveForce);
    }

    if (Input.GetButton("Move Left")) {
      anim.SetBool("walking", true);
      facingRight = false;
      body.AddForce(Vector2.left * moveForce);
    }

    if (!Input.GetButton("Move Right") && !Input.GetButton("Move Left")) {
      anim.SetBool("walking", false);
      body.velocity = new Vector2(0, body.velocity.y);
    }
  }

  void LimitHorizontalSpeed() {
    if (limitHorizontalSpeed && (!damaged || limitHorizontalSpeedWhenDamaged)) {
      if (body.velocity.x > maxHorizontalSpeed) {
        body.velocity = new Vector2(maxHorizontalSpeed, body.velocity.y);
      } else if (body.velocity.x < -maxHorizontalSpeed) {
        body.velocity = new Vector2(-maxHorizontalSpeed, body.velocity.y);
      }
    }
  }

  void LimitVerticalSpeed() {
    if (limitVerticalSpeed && (!damaged || limitVerticalSpeedWhenDamaged)) {
      if (limitJumpSpeed && body.velocity.y > maxVerticalSpeed) {
        body.velocity = new Vector2(body.velocity.x, maxVerticalSpeed);
      } else if (body.velocity.y < -maxVerticalSpeed) {
        body.velocity = new Vector2(body.velocity.x, -maxVerticalSpeed);
      }
    }
  }

  void FaceMovement() {
    float scale = Mathf.Abs(trans.localScale.x);

    trans.localScale = new Vector2((facingRight ? 1 : -1) * scale, scale);
  }
}