-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFogOfWar.cs
156 lines (144 loc) · 4.59 KB
/
FogOfWar.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FogOfWar : MonoBehaviour
{
SpriteRenderer myRenderer;
GameObject player;
Vector2 myPos;
Vector2 playerPos;
int oldLayer;
int oldTurn;
int posy;
int posx;
// Start is called before the first frame update
void Start()
{
myRenderer = this.transform.GetComponent<SpriteRenderer>();
player = GameManager.instance.playerObj;
oldLayer = this.gameObject.layer;
ChangeColorToLayer();
//ShotRayCast();
posx = (int)gameObject.transform.position.x;
posy = (int)gameObject.transform.position.y;
}
// Update is called once per frame
void Update()
{
}
void ChangeColorToLayer()
{
if (myRenderer == null)
{
myRenderer = this.transform.GetComponent<SpriteRenderer>();
}
Vector2 Pos = new Vector2(posx, posy);
switch (this.gameObject.layer)
{
case 6: // UnSeen
if (this.transform.tag != "Monster")
{
myRenderer.color = Color.black;
MiniMapPanel.instance.CellColorChange(Pos, gameObject.layer);
}
else
{
Color newColor = new Color();
newColor = Color.white;
newColor.a = 0;
myRenderer.color = newColor;
}
break;
case 7:// Seen
if (this.transform.tag != "Monster")
{
myRenderer.color = Color.gray;
}
else
{
Color newColor = new Color();
newColor = Color.white;
newColor.a = 0;
myRenderer.color = newColor;
MiniMapPanel.instance.CellColorChange(Pos, gameObject.layer);
}
break;
case 8:// insight
{
myRenderer.color = Color.white;
MiniMapPanel.instance.CellColorChange(Pos, gameObject.layer);
}
break;
default:
break;
}
}
void ShotRayCast()
{
bool isBlock = false;
int originLayer = this.gameObject.layer;
myPos = this.transform.position;
playerPos = player.transform.position;
this.gameObject.layer = 2;
RaycastHit2D[] hit = Physics2D.RaycastAll(myPos,(playerPos-myPos),Vector2.Distance(myPos,playerPos));
this.gameObject.layer = originLayer;
for(int i = 0; i < hit.Length; i++)
{
if(hit[i].transform.tag == "Wall")
{
isBlock = true;
}
}
if (!isBlock)
{
this.gameObject.layer = LayerMask.NameToLayer("InSight");
}
if(isBlock && this.gameObject.layer == LayerMask.NameToLayer("InSight"))
{
this.gameObject.layer = LayerMask.NameToLayer("Seen");
}
oldLayer = this.gameObject.layer;
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject == player)
{
ShotRayCast();
ChangeColorToLayer();
oldLayer = this.gameObject.layer;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject == player && this.gameObject.layer != 6)
{
this.gameObject.layer = LayerMask.NameToLayer("Seen");
oldLayer = this.gameObject.layer;
ChangeColorToLayer();
}
}
private void OnTriggerStay2D(Collider2D collision)
{
if(collision.gameObject == player)
{
if (this.transform.tag == "Monster")
{
if (collision.gameObject == player && (myPos != new Vector2((int)this.transform.position.x, (int)this.transform.position.y) || TurnManager.instance.turn != oldTurn))
{
ShotRayCast();
ChangeColorToLayer();
oldTurn = TurnManager.instance.turn;
}
}
else
{
if (collision.gameObject == player && (TurnManager.instance.turn != oldTurn))
{
ShotRayCast();
ChangeColorToLayer();
oldTurn = TurnManager.instance.turn;
}
}
}
}
}