MovingPlattform.cs 5.03 KB
Newer Older
Unknown's avatar
Unknown committed
1
2
3
4
5
6
7
8
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent (typeof (BoxCollider2D))]
public class MovingPlattform : MonoBehaviour {

    public List<Vector2> m_Points;
Unknown's avatar
Unknown committed
9
    public List<string> m_Tags;
Unknown's avatar
Unknown committed
10
11
    [SerializeField] private float m_Speed = 0.3f; //plattform geschwindigkeit
    [SerializeField] private float m_Wait = 2f; //zeit in Sekunden wie lange die Plattform wartet
12
    [SerializeField] private int m_StartIndex = 0; //Index in der Liste an der die Plattform starten soll
Unknown's avatar
Unknown committed
13
14
15
16
17
18
19
    [SerializeField] private bool m_MoveingByTouching = false; //plattform bewegt sich nur bei Spieler Kontakt
    [SerializeField] private bool m_Loop = false; //Bool zur festlegung ob plattform sich loopen soll

    private bool isMoving = false; //Bool zu Checken ob Plattform sich bewegt kann auch genutzt werden um festzustellen ob die Coroutine Move(Vector2 a, Vector2 b) läuft
    private bool isFacingForward = true; //sagt aus in welche Richtung sich die Plattform bewegt
    private int isAt = 0; //Index an welcher stelle sich die Plattform in der List m_Points befindet

20
21
    private Vector2 origin = Vector2.zero;

Unknown's avatar
Unknown committed
22
    void Awake () {
23
24
        origin = transform.position;
        SetStart (m_StartIndex);
Unknown's avatar
Unknown committed
25
26
27
    }

    void Update () {
Unknown's avatar
Unknown committed
28
        if (m_Points.Count <= 1 || isMoving) return;
Unknown's avatar
Unknown committed
29
30
31
32
33
34
35
36
37

        if (!m_MoveingByTouching)
            Patrol ();
    }

    //Setze die Startposition der Plattform.
    public void SetStart (int index) {
        if (index >= 0 && index < m_Points.Count) {
            this.isAt = index;
Unknown's avatar
Unknown committed
38
            transform.position = origin + m_Points[isAt];
Unknown's avatar
Unknown committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
        }
    }

    //entscheidet ob die Plattformpatrolie läuft in einer Schleife oder die Plattform bewegt sich vor und zurück
    private void Patrol () {
        if (!m_Loop) {
            //Controls movement back an forth
            if (isFacingForward) {
                MoveForward ();
            } else {
                MoveBackward ();
            }
        } else {
            //Loops plattform motion forward. Can be Backwards
            MoveForward ();
        }
    }

    private void MoveForward () {
        if ((isAt + 1) < m_Points.Count) {
            //normale vor und zurück bewegung
60
            StartCoroutine (Move (origin + m_Points[isAt], origin + m_Points[isAt + 1]));
Unknown's avatar
Unknown committed
61
62
            ++isAt;
        } else if ((isAt + 1) > m_Points.Count && m_Loop) {
Unknown's avatar
Unknown committed
63
            //Loop sprung der Plattform
64
            StartCoroutine (Move (origin + m_Points[isAt], origin + m_Points[0]));
Unknown's avatar
Unknown committed
65
66
67
68
69
70
71
72
73
74
            isAt = 0;
        } else {
            //setzten in welche richtung die Plattform sich bewegt
            isFacingForward = false;
        }
    }

    //selbe wie bei MoveForward nur Backwards
    private void MoveBackward () {
        if ((isAt - 1) >= 0) {
75
            StartCoroutine (Move (origin + m_Points[isAt], origin + m_Points[isAt - 1]));
Unknown's avatar
Unknown committed
76
77
            isAt--;
        } else if ((isAt - 1) < 0 && m_Loop) {
78
            StartCoroutine (Move (origin + m_Points[isAt], origin + m_Points[m_Points.Count - 1]));
Unknown's avatar
Unknown committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
            isAt = m_Points.Count - 1;
        } else {
            isFacingForward = true;
        }
    }

    //Coroutine zur bewegung der Plattform
    private IEnumerator Move (Vector2 a, Vector2 b) {
        isMoving = true;
        for (float i = 0; i <= 1f; i += (m_Speed * Time.deltaTime)) {
            transform.position = Vector2.Lerp (a, b, i);
            yield return new WaitForEndOfFrame ();
        }
        yield return new WaitForSeconds (m_Wait);
        isMoving = false;
    }

    //CollisionEnter mit Spieler kann um Gegner erweitert werden
Unknown's avatar
Unknown committed
97
98
    void OnCollisionEnter2D (Collision2D col2D)
    {
Unknown's avatar
Unknown committed
99
	    bool found = false;
Unknown's avatar
Unknown committed
100
101
102
103
104
105
	    for (int i = 0; i < m_Tags.Count; i++)
	    {
		    if (col2D.gameObject.tag.Equals(m_Tags[i]) || col2D.gameObject.tag.Equals("Player"))
		    {
			    //Setzt parent object des Spielers zu dieser Plattform somit wird die transformation der Plattform auf den Spieler Übertragen
			    col2D.transform.SetParent (gameObject.transform);
Unknown's avatar
Unknown committed
106
			    found |= true;
Unknown's avatar
Unknown committed
107
108
		    }
	    }
Unknown's avatar
Unknown committed
109
110
111

	    if (m_MoveingByTouching && m_Points.Count > 1 && found)
		    Patrol ();
Unknown's avatar
Unknown committed
112
113
114
115
    }

    //CollisionExit mit Spieler kann um Gegner erweitert werden
    void OnCollisionExit2D (Collision2D col2D) {
Unknown's avatar
Unknown committed
116
117
118
119
120
121
122
123
124

	    for (int i = 0; i < m_Tags.Count; i++)
	    {
		    if (col2D.gameObject.tag.Equals(m_Tags[i]) || col2D.gameObject.tag.Equals("Player"))
		    {
			    //Sets Spieler Parent zu niemand
			    col2D.transform.SetParent (null);
		    }
	    }
Unknown's avatar
Unknown committed
125
126
127
    }

    void OnDrawGizmosSelected () {
128
        Vector2 pos = transform.position;
Unknown's avatar
Unknown committed
129
130
131
        if (m_Points != null) {
            for (int i = 0; i < m_Points.Count; i++) {
                Gizmos.color = Color.green;
132
                Gizmos.DrawSphere (pos + m_Points[i], 0.1f);
Unknown's avatar
Unknown committed
133
134
135

                Gizmos.color = Color.blue;
                if (i + 1 < m_Points.Count) {
136
                    Gizmos.DrawLine (pos + m_Points[i], pos + m_Points[i + 1]);
Unknown's avatar
Unknown committed
137
                } else if (i + 1 >= m_Points.Count && m_Loop) {
138
                    Gizmos.DrawLine (pos + m_Points[i], pos + m_Points[0]);
Unknown's avatar
Unknown committed
139
140
141
142
143
                }

            }
        }
    }
Unknown's avatar
Unknown committed
144
}