Commit af576cf5 authored by 18신대성's avatar 18신대성 Committed by 18손재민

Pair를 Generic이 아니게 바꿈

parent b4394627
...@@ -2,19 +2,24 @@ ...@@ -2,19 +2,24 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class Pair<L, R> public class Pair
{ {
public L l; public float l;
public R r; public float r;
public Pair(L _l, R _r) public Pair(float _l, float _r)
{ {
this.l = _l; this.l = _l;
this.r = _r; this.r = _r;
} }
public Pair<R, L> Swap() public Pair Swap()
{ {
return new Pair<R, L>(r, l); return new Pair(r, l);
}
public Pair ApplyMargin(float margin)
{
return new Pair(l - margin, r + margin);
} }
} }
...@@ -9,6 +9,7 @@ public class Floor : MonoBehaviour ...@@ -9,6 +9,7 @@ public class Floor : MonoBehaviour
/// </summary> /// </summary>
public Vector2Int mapPos; public Vector2Int mapPos;
public bool isGoalFloor = false; public bool isGoalFloor = false;
public IObject objOnFloor = null;
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
......
...@@ -28,9 +28,9 @@ public class Mirror : Wall, IBulletInteractor, IBreakable ...@@ -28,9 +28,9 @@ public class Mirror : Wall, IBulletInteractor, IBreakable
{ {
Vector2 stPos = _shooter.pos; // position of shooter's cell Vector2 stPos = _shooter.pos; // position of shooter's cell
//Debug.Log("stPos: " + stPos); //Debug.Log("stPos: " + stPos);
List<Pair<float, float>> parRay = new List<Pair<float, float>> List<Pair> parRay = new List<Pair>
{ {
new Pair<float, float>(0, 1) new Pair(0, 1)
}; };
int side, i, iBack, stCheck; int side, i, iBack, stCheck;
...@@ -58,7 +58,7 @@ public class Mirror : Wall, IBulletInteractor, IBreakable ...@@ -58,7 +58,7 @@ public class Mirror : Wall, IBulletInteractor, IBreakable
float wallPos = (dir ? wall.Key.y : wall.Key.x); float wallPos = (dir ? wall.Key.y : wall.Key.x);
if (wall.Value.GetInstanceID() != GetInstanceID() && (side < 0 ? wallPos < mirrorPos && wallPos > stPosFix : wallPos > mirrorPos && wallPos < stPosFix)) if (wall.Value.GetInstanceID() != GetInstanceID() && (side < 0 ? wallPos < mirrorPos && wallPos > stPosFix : wallPos > mirrorPos && wallPos < stPosFix))
{ {
Pair<float, float> pair = new Pair<float, float>(PointToParRay(stPos, wall.Value.ldPos, false), PointToParRay(stPos, wall.Value.rdPos, false)); Pair pair = new Pair(PointToParRay(stPos, wall.Value.ldPos, false), PointToParRay(stPos, wall.Value.rdPos, false));
if (pair.l > pair.r) pair = pair.Swap(); if (pair.l > pair.r) pair = pair.Swap();
SubtractRay(parRay, pair); SubtractRay(parRay, pair);
yield return null; yield return null;
...@@ -73,12 +73,12 @@ public class Mirror : Wall, IBulletInteractor, IBreakable ...@@ -73,12 +73,12 @@ public class Mirror : Wall, IBulletInteractor, IBreakable
// remove backside of mirror // remove backside of mirror
for (int j = iBack; Mathf.Abs(j) < MapManager.inst.currentMap.maxMapSize; j -= side) for (int j = iBack; Mathf.Abs(j) < MapManager.inst.currentMap.maxMapSize; j -= side)
{ {
Debug.Log(j); //Debug.Log(j);
foreach (var obj in copyObjGrid) foreach (var obj in copyObjGrid)
{ {
if ((dir ? obj.Key.y : obj.Key.x) == j) if ((dir ? obj.Key.y : obj.Key.x) == j)
{ {
if (IsInRay(parRay, PointToParRay(stPos, obj.Key, false))) if (IsInRay(parRay, PointToParRay(stPos, obj.Key, false), -0.1f))
{ {
/*remove object*/ /*remove object*/
MapManager.inst.currentMap.RemoveObject(obj.Key); MapManager.inst.currentMap.RemoveObject(obj.Key);
...@@ -94,7 +94,7 @@ public class Mirror : Wall, IBulletInteractor, IBreakable ...@@ -94,7 +94,7 @@ public class Mirror : Wall, IBulletInteractor, IBreakable
if ((dir ? plyFloor.mapPos.y : plyFloor.mapPos.x) == j) if ((dir ? plyFloor.mapPos.y : plyFloor.mapPos.x) == j)
{ {
if (IsInRay(parRay, PointToParRay(stPos, plyFloor.mapPos, false))) if (IsInRay(parRay, PointToParRay(stPos, plyFloor.mapPos, false), -0.1f))
{ {
/*remove player*/ /*remove player*/
PlayerController.inst.RemovePlayer(plyFloor.mapPos); PlayerController.inst.RemovePlayer(plyFloor.mapPos);
...@@ -108,7 +108,7 @@ public class Mirror : Wall, IBulletInteractor, IBreakable ...@@ -108,7 +108,7 @@ public class Mirror : Wall, IBulletInteractor, IBreakable
{ {
if ((dir ? floor.Key.y : floor.Key.x) == j) if ((dir ? floor.Key.y : floor.Key.x) == j)
{ {
if (IsInRay(parRay, PointToParRay(stPos, floor.Key, false))) if (IsInRay(parRay, PointToParRay(stPos, floor.Key, false), 0))
{ {
/*remove floor*/ /*remove floor*/
MapManager.inst.currentMap.RemoveFloor(floor.Key); MapManager.inst.currentMap.RemoveFloor(floor.Key);
...@@ -124,10 +124,9 @@ public class Mirror : Wall, IBulletInteractor, IBreakable ...@@ -124,10 +124,9 @@ public class Mirror : Wall, IBulletInteractor, IBreakable
float wallPos = (dir ? wall.Key.y : wall.Key.x); float wallPos = (dir ? wall.Key.y : wall.Key.x);
if (wall.Value.GetInstanceID() != GetInstanceID() && (side < 0 ? wallPos < rangeL && wallPos > rangeR : wallPos > rangeL && wallPos < rangeR)) if (wall.Value.GetInstanceID() != GetInstanceID() && (side < 0 ? wallPos < rangeL && wallPos > rangeR : wallPos > rangeL && wallPos < rangeR))
{ {
Pair<float, float> pair = new Pair<float, float>(PointToParRay(stPos, wall.Value.ldPos, false), PointToParRay(stPos, wall.Value.rdPos, false)); Pair pair = new Pair (PointToParRay(stPos, wall.Value.ldPos, false), PointToParRay(stPos, wall.Value.rdPos, false));
if (pair.l > pair.r) pair.Swap(); if (pair.l > pair.r) pair.Swap();
if (IsInRay(parRay, pair, 0))
if (IsInRay(parRay, pair.l) && IsInRay(parRay, pair.r))
{ {
/*remove wall*/ /*remove wall*/
MapManager.inst.currentMap.RemoveWall(wall.Key); MapManager.inst.currentMap.RemoveWall(wall.Key);
...@@ -142,8 +141,9 @@ public class Mirror : Wall, IBulletInteractor, IBreakable ...@@ -142,8 +141,9 @@ public class Mirror : Wall, IBulletInteractor, IBreakable
float wallPos = (dir ? wall.Key.y : wall.Key.x); float wallPos = (dir ? wall.Key.y : wall.Key.x);
if (wall.Value.GetInstanceID() != GetInstanceID() && (side < 0 ? wallPos < rangeL && wallPos > rangeR : wallPos > rangeL && wallPos < rangeR)) if (wall.Value.GetInstanceID() != GetInstanceID() && (side < 0 ? wallPos < rangeL && wallPos > rangeR : wallPos > rangeL && wallPos < rangeR))
{ {
Pair<float, float> pair = new Pair<float, float>(PointToParRay(stPos, wall.Value.ldPos, false), PointToParRay(stPos, wall.Value.rdPos, false)); Pair pair = new Pair (PointToParRay(stPos, wall.Value.ldPos, false), PointToParRay(stPos, wall.Value.rdPos, false));
if (IsInRay(parRay, pair.l) && IsInRay(parRay, pair.r)) if (pair.l > pair.r) pair.Swap();
if (IsInRay(parRay, pair, 0))
{ {
/*remove wall*/ /*remove wall*/
MapManager.inst.currentMap.RemoveWall(wall.Key); MapManager.inst.currentMap.RemoveWall(wall.Key);
...@@ -163,11 +163,54 @@ public class Mirror : Wall, IBulletInteractor, IBreakable ...@@ -163,11 +163,54 @@ public class Mirror : Wall, IBulletInteractor, IBreakable
// check after reflect, if obj or floor, copy else if wall or mirror, Subtract // check after reflect, if obj or floor, copy else if wall or mirror, Subtract
for (; Mathf.Abs(i) < MapManager.inst.currentMap.maxMapSize; i += side) for (; Mathf.Abs(i) < MapManager.inst.currentMap.maxMapSize; i += side)
{ {
float rangeL = i - 0.25f * side;
float rangeR = i + 0.25f * side;
foreach (var wall in copyWallGrid)
{
float wallPos = (dir ? wall.Key.y : wall.Key.x);
if (wall.Value.GetInstanceID() != GetInstanceID() && (side < 0 ? wallPos < rangeL && wallPos > rangeR : wallPos > rangeL && wallPos < rangeR))
{
Pair pair = new Pair(PointToParRay(stPos, wall.Value.ldPos, true), PointToParRay(stPos, wall.Value.rdPos, true));
if (pair.l > pair.r) pair = pair.Swap();
if (IsInRay(parRay, pair, 0))
{
/*copy wall*/
float nextx = dir ? wall.Key.x : 2 * mapPos.x - wall.Key.x;
float nexty = dir ? 2 * mapPos.y - wall.Key.y : wall.Key.y;
MapManager.inst.currentMap.CreateWall(new Vector2(nextx, nexty), wall.Value.type);
SubtractRay(parRay, pair);
yield return null;
}
}
}
rangeL = i - 0.25f * side + 0.5f;
rangeR = i + 0.25f * side + 0.5f;
foreach (var wall in copyWallGrid)
{
float wallPos = (dir ? wall.Key.y : wall.Key.x);
if (wall.Value.GetInstanceID() != GetInstanceID() && (side < 0 ? wallPos < rangeL && wallPos > rangeR : wallPos > rangeL && wallPos < rangeR))
{
Pair pair = new Pair(PointToParRay(stPos, wall.Value.ldPos, true), PointToParRay(stPos, wall.Value.rdPos, true));
if (pair.l > pair.r) pair = pair.Swap();
if (IsInRay(parRay, pair, 0))
{
/*copy wall*/
float nextx = dir ? wall.Key.x : 2 * mapPos.x - wall.Key.x;
float nexty = dir ? 2 * mapPos.y - wall.Key.y : wall.Key.y;
MapManager.inst.currentMap.CreateWall(new Vector2(nextx, nexty), wall.Value.type);
SubtractRay(parRay, pair);
yield return null;
}
}
}
//Debug.Log(i + "th Wall End");
foreach (var floor in copyFloorGrid) foreach (var floor in copyFloorGrid)
{ {
if ((dir ? floor.Key.y : floor.Key.x) == i) if ((dir ? floor.Key.y : floor.Key.x) == i)
{ {
if (IsInRay(parRay, PointToParRay(stPos, floor.Key, true))) if (IsInRay(parRay, PointToParRay(stPos, floor.Key, true), 0.5f))
{ {
/*copy floor*/ /*copy floor*/
int nextx = dir ? floor.Key.x : Mathf.RoundToInt(2 * ldPos.x - floor.Key.x); int nextx = dir ? floor.Key.x : Mathf.RoundToInt(2 * ldPos.x - floor.Key.x);
...@@ -182,7 +225,7 @@ public class Mirror : Wall, IBulletInteractor, IBreakable ...@@ -182,7 +225,7 @@ public class Mirror : Wall, IBulletInteractor, IBreakable
{ {
if ((dir ? obj.Key.y : obj.Key.x) == i) if ((dir ? obj.Key.y : obj.Key.x) == i)
{ {
if (IsInRay(parRay, PointToParRay(stPos, obj.Key, true))) if (IsInRay(parRay, PointToParRay(stPos, obj.Key, true), 0.5f))
{ {
/*copy object*/ /*copy object*/
int nextx = dir ? obj.Key.x : Mathf.RoundToInt(2 * ldPos.x - obj.Key.x); int nextx = dir ? obj.Key.x : Mathf.RoundToInt(2 * ldPos.x - obj.Key.x);
...@@ -199,7 +242,7 @@ public class Mirror : Wall, IBulletInteractor, IBreakable ...@@ -199,7 +242,7 @@ public class Mirror : Wall, IBulletInteractor, IBreakable
Floor plyFloor = ply.GetComponent<Player>().currentFloor; Floor plyFloor = ply.GetComponent<Player>().currentFloor;
if ((dir ? plyFloor.mapPos.y : plyFloor.mapPos.x) == i) if ((dir ? plyFloor.mapPos.y : plyFloor.mapPos.x) == i)
{ {
if (IsInRay(parRay, PointToParRay(stPos, plyFloor.mapPos, true))) if (IsInRay(parRay, PointToParRay(stPos, plyFloor.mapPos, true), 0.5f))
{ {
/*copy player*/ /*copy player*/
int nextx = dir ? plyFloor.mapPos.x : Mathf.RoundToInt(2 * ldPos.x - plyFloor.mapPos.x); int nextx = dir ? plyFloor.mapPos.x : Mathf.RoundToInt(2 * ldPos.x - plyFloor.mapPos.x);
...@@ -211,49 +254,6 @@ public class Mirror : Wall, IBulletInteractor, IBreakable ...@@ -211,49 +254,6 @@ public class Mirror : Wall, IBulletInteractor, IBreakable
} }
} }
//Debug.Log(i + "th Object End"); //Debug.Log(i + "th Object End");
float rangeL = i - 0.25f * side;
float rangeR = i + 0.25f * side;
foreach (var wall in copyWallGrid)
{
float wallPos = (dir ? wall.Key.y : wall.Key.x);
if (wall.Value.GetInstanceID() != GetInstanceID() && (side < 0 ? wallPos < rangeL && wallPos > rangeR : wallPos > rangeL && wallPos < rangeR))
{
Pair<float, float> pair = new Pair<float, float>(PointToParRay(stPos, wall.Value.ldPos, true), PointToParRay(stPos, wall.Value.rdPos, true));
if (pair.l > pair.r) pair = pair.Swap();
if (IsInRay(parRay, pair))
{
/*copy wall*/
float nextx = dir ? wall.Key.x : 2 * mapPos.x - wall.Key.x;
float nexty = dir ? 2 * mapPos.y - wall.Key.y : wall.Key.y;
MapManager.inst.currentMap.CreateWall(new Vector2(nextx, nexty), wall.Value.type);
SubtractRay(parRay, pair);
yield return null;
}
}
}
rangeL = i - 0.25f * side + 0.5f;
rangeR = i + 0.25f * side + 0.5f;
foreach (var wall in copyWallGrid)
{
float wallPos = (dir ? wall.Key.y : wall.Key.x);
if (wall.Value.GetInstanceID() != GetInstanceID() && (side < 0 ? wallPos < rangeL && wallPos > rangeR : wallPos > rangeL && wallPos < rangeR))
{
Pair<float, float> pair = new Pair<float, float>(PointToParRay(stPos, wall.Value.ldPos, true), PointToParRay(stPos, wall.Value.rdPos, true));
if (pair.l > pair.r) pair = pair.Swap();
if (IsInRay(parRay, pair))
{
/*copy wall*/
float nextx = dir ? wall.Key.x : 2 * mapPos.x - wall.Key.x;
float nexty = dir ? 2 * mapPos.y - wall.Key.y : wall.Key.y;
MapManager.inst.currentMap.CreateWall(new Vector2(nextx, nexty), wall.Value.type);
SubtractRay(parRay, pair);
yield return null;
}
}
}
//Debug.Log(i + "th Wall End");
} }
MapManager.inst.currentMap.RemoveWall(mapPos); MapManager.inst.currentMap.RemoveWall(mapPos);
} }
...@@ -263,10 +263,10 @@ public class Mirror : Wall, IBulletInteractor, IBreakable ...@@ -263,10 +263,10 @@ public class Mirror : Wall, IBulletInteractor, IBreakable
/// </summary> /// </summary>
/// <param name="_parRay">ray list to subtracted</param> /// <param name="_parRay">ray list to subtracted</param>
/// <param name="_sub">ray to subtract</param> /// <param name="_sub">ray to subtract</param>
void SubtractRay(List<Pair<float, float>> _parRay, Pair<float, float> _sub) void SubtractRay(List<Pair> _parRay, Pair _sub)
{ {
Pair<float, float> toAdd = null; Pair toAdd = null;
foreach (Pair<float, float> pair in _parRay) foreach (Pair pair in _parRay)
{ {
if (pair.r < _sub.l || pair.l > _sub.r) continue; if (pair.r < _sub.l || pair.l > _sub.r) continue;
float[] arr = { pair.l, pair.r, _sub.l, _sub.r }; float[] arr = { pair.l, pair.r, _sub.l, _sub.r };
...@@ -297,7 +297,7 @@ public class Mirror : Wall, IBulletInteractor, IBreakable ...@@ -297,7 +297,7 @@ public class Mirror : Wall, IBulletInteractor, IBreakable
} }
else if (arr[1] == _sub.l && arr[2] == _sub.r) else if (arr[1] == _sub.l && arr[2] == _sub.r)
{ {
toAdd = new Pair<float, float>(_sub.r, pair.r); toAdd = new Pair(_sub.r, pair.r);
pair.r = _sub.l; pair.r = _sub.l;
} }
} }
...@@ -320,12 +320,13 @@ public class Mirror : Wall, IBulletInteractor, IBreakable ...@@ -320,12 +320,13 @@ public class Mirror : Wall, IBulletInteractor, IBreakable
/// <param name="_parRay">ray list to be checked</param> /// <param name="_parRay">ray list to be checked</param>
/// <param name="_range">range to check</param> /// <param name="_range">range to check</param>
/// <returns>if _range is included in _parRay, return true</returns> /// <returns>if _range is included in _parRay, return true</returns>
bool IsInRay(List<Pair<float, float>> _parRay, Pair<float, float> _range) bool IsInRay(List<Pair> _parRay, Pair _range, float margin)
{ {
bool output = false; bool output = false;
foreach (Pair<float, float> pair in _parRay) foreach (Pair pair in _parRay)
{ {
if (pair.r <= _range.l || pair.l >= _range.r) continue; Pair temp = pair.ApplyMargin(margin);
if (temp.r <= _range.l || temp.l >= _range.r) continue;
else else
{ {
output = true; output = true;
...@@ -335,11 +336,12 @@ public class Mirror : Wall, IBulletInteractor, IBreakable ...@@ -335,11 +336,12 @@ public class Mirror : Wall, IBulletInteractor, IBreakable
return output; return output;
} }
bool IsInRay(List<Pair<float, float>> _parRay, float _obj) bool IsInRay(List<Pair> _parRay, float _obj, float margin)
{ {
foreach (Pair<float, float> pair in _parRay) foreach (Pair pair in _parRay)
{ {
if (pair.l <= _obj && pair.r >= _obj) return true; Pair temp = pair.ApplyMargin(margin);
if (temp.l <= _obj && temp.r >= _obj) return true;
} }
return false; return false;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment