This repository has been archived by the owner on Apr 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLevel.cs
173 lines (146 loc) · 4.63 KB
/
Level.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Level : MonoBehaviour
{
public LevelLoaderJSON levelLoaderJSON;
[SerializeField]
public int levelIndex;
public float timeBetweenBlockSpawn;
[SerializeField]
public int spawnX,spawnY;
[SerializeField]
Block levelBlockPrefab;
[SerializeField]
IntBlockDictionary levelBlockPrefabs;
public void Awake()
{
levelLoaderJSON = GetComponent<LevelLoaderJSON>();
}
[ContextMenu("LoadThisLevel")]
public List<Block> LoadThisLevel()
{
return levelLoaderJSON.LoadThisLvL();
}
public Block GetTileAt(Vector2 pos, bool skipVerification = false)
{
if (!skipVerification && IsPlayerPosOutOfBound(pos))
{
return null;
}
return levelLoaderJSON.blocksPos[pos];
}
public Block GetTileFromIndex(int index)
{
return levelLoaderJSON.blocksIndex[index];
}
public BlockType GetTileType(Vector2 pos)
{
if (IsPlayerPosOutOfBound(pos))
{
return 0;
}
return levelLoaderJSON.blocksPos[pos].type;
}
IEnumerator RevealLevel(){
for (int i = 0; i < levelLoaderJSON.blockList.Count; i++)
{
if(levelLoaderJSON.blockList[i] != null){
if(levelLoaderJSON.blockList[i].type == BlockType.Bridge)
{
BridgeBlock bridge = (BridgeBlock)levelLoaderJSON.blockList[i];
if (bridge.isFirstTileOfBridge)
{
Vector3 pos = levelLoaderJSON.blocksIndex[bridge.bridgeAnchorTileId].transform.position;
bridge.transform.position = new Vector3(pos.x, -0.25f, pos.z);
bridge.Show(true);
}
}
else
{
levelLoaderJSON.blockList[i].Show();
}
yield return new WaitForSeconds(timeBetweenBlockSpawn);
}
}
}
public Coroutine StartLevelCoroutine()
{
return StartCoroutine(StartLevel());
}
public IEnumerator StartLevel(){
LoadThisLevel();
yield return StartCoroutine(RevealLevel());
yield return LevelManager.Instance.StartSpawnPlayer((spawnX,spawnY));
}
public bool IsPlayerPosOutOfBound(Vector2 pos)
{
bool outOfBound = !levelLoaderJSON.blocksPos.ContainsKey(pos);
if (!outOfBound)
{
Block block = GetTileAt(pos, true);
if(block is BridgeBlock)
{
return !((BridgeBlock)block).GetIsOpen();
}
}
return outOfBound;
}
public bool HasPlayerWin(Vector2 pos1, Vector2 pos2)
{
if(pos1 != pos2 || IsPlayerPosOutOfBound(pos1))
{
return false;
}
return (levelLoaderJSON.blocksPos[pos1].type == BlockType.Goal);
}
public bool IsPlayerOnTeleporter(Vector2 pos1, Vector2 pos2)
{
if (pos1 != pos2 || IsPlayerPosOutOfBound(pos1))
{
return false;
}
return (levelLoaderJSON.blocksPos[pos1].type == BlockType.Teleporter);
}
public bool IsPlayerOnSoftSwitch(Vector2 pos1, Vector2 pos2)
{
if (IsPlayerPosOutOfBound(pos2) || IsPlayerPosOutOfBound(pos1))
{
return false;
}
return (levelLoaderJSON.blocksPos[pos1].type == BlockType.SoftButton || levelLoaderJSON.blocksPos[pos2].type == BlockType.SoftButton);
}
public bool IsPlayerOnHardSwitch(Vector2 pos1, Vector2 pos2)
{
if (pos1 != pos2 || IsPlayerPosOutOfBound(pos1))
{
return false;
}
return (levelLoaderJSON.blocksPos[pos1].type == BlockType.HardButton);
}
public bool IsOnUnstableTile(Vector2 pos1, Vector2 pos2)
{
if (pos1 != pos2 || IsPlayerPosOutOfBound(pos1))
{
return false;
}
return (levelLoaderJSON.blocksPos[pos1].type == BlockType.Unstable);
}
public Vector2 GetOtherTeleporter(TeleporterBlock myTeleporter)
{
TeleporterBlock other = (TeleporterBlock)levelLoaderJSON.blocksIndex[myTeleporter.linkedTeleporterId];
if(other == null)
{
Debug.Log("no teleporter");
return Vector2.one * -1;
}
return new Vector2(other.transform.position.x, other.transform.position.z);
}
public bool spawnOnStart;
void Start()
{
if(spawnOnStart){
StartCoroutine(StartLevel());
}
}
}