aboutsummaryrefslogtreecommitdiff
path: root/Assets/Scripts/DoorLogic.cs
blob: 5222bd156ea18b96a6fd1e9a84c726c894240483 (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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DoorLogic : MonoBehaviour {
  public string colour = "yellow";
  public bool open = false;

  public GameObject player;
  public GameObject exit;

  public AudioClip openNoise;
  public float openNoiseVolume = 1f;

  public float playerProximity = 0.6f;
  public float playerPlacedOffset = 2f;

  private Animator anim;
  private BoxCollider2D coll;
  private AudioSource audio;

  void OnDrawGizmos() {
    if (exit != null) {
      Gizmos.color = Color.white;
      Gizmos.DrawLine(transform.position, exit.transform.position);
    }
  }

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

    if (exit) {
      DoorLogic dl = exit.GetComponent<DoorLogic>();
      dl.exit = this.gameObject;
    }

    if (!player) {
      player = GameObject.FindGameObjectsWithTag("Player")[0];
    }

    SetAnimationColour();
    OpenOrClosed(open);
  }

  public void Open() {
    if (openNoise) audio.PlayOneShot(openNoise, openNoiseVolume);

    OpenOrClosed(true);
  }

  void Close() {
    OpenOrClosed(false);
  }

  void OpenOrClosed(bool which) {
    open = which;
    anim.SetBool("open", which);
    coll.isTrigger = which;
  }

  void SetAnimationColour() {
    switch (colour) {
      case "yellow":
        anim.SetInteger("colour", 0);
        break;
      case "blue":
        anim.SetInteger("colour", 1);
        break;
      case "green":
        anim.SetInteger("colour", 2);
        break;
      case "red":
        anim.SetInteger("colour", 3);
        break;
    }
  }

  void FixedUpdate() {
    if (exit) {
      if (Vector2.Distance(player.transform.position, this.transform.position)
          < playerProximity) {
        Teleport(player, exit);
      }
    }
  }

  void Teleport(GameObject player, GameObject exit) {
    float yDiff = this.transform.position.y - player.transform.position.y;

    player.transform.position = exit.transform.position +
      (Vector3.right * (exit.GetComponent<SpriteRenderer>().flipX ? -1 : 1) *
       playerPlacedOffset) - Vector3.up * yDiff;

    ResetAll();
  }

  public void Reset() {
    Close();
  }

  void ResetAll() {
    GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
    foreach (GameObject enemy in enemies) {
      Enemy script = enemy.GetComponent<Enemy>();
      script.Reset();
    }

    GameObject[] doors = GameObject.FindGameObjectsWithTag("Door");
    foreach (GameObject door in doors) {
      DoorLogic script = door.GetComponent<DoorLogic>();
      script.Reset();
    }
  }
}