diff --git a/Assets/Scripts/Generals/Pair.cs b/Assets/Scripts/Generals/Pair.cs index 217fda404c55249a2de2f105956662744137bcf3..783e7218d8cd4e13470cc018457448353eddaa73 100644 --- a/Assets/Scripts/Generals/Pair.cs +++ b/Assets/Scripts/Generals/Pair.cs @@ -2,19 +2,24 @@ using System.Collections.Generic; using UnityEngine; -public class Pair<L, R> +public class Pair { - public L l; - public R r; + public float l; + public float r; - public Pair(L _l, R _r) + public Pair(float _l, float _r) { this.l = _l; 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); } } diff --git a/Assets/Scripts/Map/Floor.cs b/Assets/Scripts/Map/Floor.cs index c13cb136dce6f7a8f4619d91efe6c422a656bf47..6929a6ec0f9421f78616ac3d0f98f44c87dc6c25 100644 --- a/Assets/Scripts/Map/Floor.cs +++ b/Assets/Scripts/Map/Floor.cs @@ -9,6 +9,7 @@ public class Floor : MonoBehaviour /// </summary> public Vector2Int mapPos; public bool isGoalFloor = false; + public IObject objOnFloor = null; // Start is called before the first frame update void Start() diff --git a/Assets/Scripts/Map/Mirror.cs b/Assets/Scripts/Map/Mirror.cs index f3ebc765e59dc93ab82a729a5485b42f18e0e4b9..dbd5c0357ac99a0216e7ca082067a5b0aea8acef 100644 --- a/Assets/Scripts/Map/Mirror.cs +++ b/Assets/Scripts/Map/Mirror.cs @@ -28,9 +28,9 @@ public class Mirror : Wall, IBulletInteractor, IBreakable { Vector2 stPos = _shooter.pos; // position of shooter's cell //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; @@ -58,7 +58,7 @@ public class Mirror : Wall, IBulletInteractor, IBreakable float wallPos = (dir ? wall.Key.y : wall.Key.x); 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(); SubtractRay(parRay, pair); yield return null; @@ -73,12 +73,12 @@ public class Mirror : Wall, IBulletInteractor, IBreakable // remove backside of mirror for (int j = iBack; Mathf.Abs(j) < MapManager.inst.currentMap.maxMapSize; j -= side) { - Debug.Log(j); + //Debug.Log(j); foreach (var obj in copyObjGrid) { 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*/ MapManager.inst.currentMap.RemoveObject(obj.Key); @@ -94,7 +94,7 @@ public class Mirror : Wall, IBulletInteractor, IBreakable 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*/ PlayerController.inst.RemovePlayer(plyFloor.mapPos); @@ -108,7 +108,7 @@ public class Mirror : Wall, IBulletInteractor, IBreakable { 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*/ MapManager.inst.currentMap.RemoveFloor(floor.Key); @@ -124,10 +124,9 @@ public class Mirror : Wall, IBulletInteractor, IBreakable 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, 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 (IsInRay(parRay, pair.l) && IsInRay(parRay, pair.r)) + if (IsInRay(parRay, pair, 0)) { /*remove wall*/ MapManager.inst.currentMap.RemoveWall(wall.Key); @@ -142,8 +141,9 @@ public class Mirror : Wall, IBulletInteractor, IBreakable 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, false), PointToParRay(stPos, wall.Value.rdPos, false)); - if (IsInRay(parRay, pair.l) && IsInRay(parRay, pair.r)) + Pair pair = new Pair (PointToParRay(stPos, wall.Value.ldPos, false), PointToParRay(stPos, wall.Value.rdPos, false)); + if (pair.l > pair.r) pair.Swap(); + if (IsInRay(parRay, pair, 0)) { /*remove wall*/ MapManager.inst.currentMap.RemoveWall(wall.Key); @@ -163,11 +163,54 @@ public class Mirror : Wall, IBulletInteractor, IBreakable // check after reflect, if obj or floor, copy else if wall or mirror, Subtract 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) { 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*/ int nextx = dir ? floor.Key.x : Mathf.RoundToInt(2 * ldPos.x - floor.Key.x); @@ -182,7 +225,7 @@ public class Mirror : Wall, IBulletInteractor, IBreakable { 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*/ int nextx = dir ? obj.Key.x : Mathf.RoundToInt(2 * ldPos.x - obj.Key.x); @@ -199,7 +242,7 @@ public class Mirror : Wall, IBulletInteractor, IBreakable Floor plyFloor = ply.GetComponent<Player>().currentFloor; 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*/ int nextx = dir ? plyFloor.mapPos.x : Mathf.RoundToInt(2 * ldPos.x - plyFloor.mapPos.x); @@ -211,49 +254,6 @@ public class Mirror : Wall, IBulletInteractor, IBreakable } } //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); } @@ -263,10 +263,10 @@ public class Mirror : Wall, IBulletInteractor, IBreakable /// </summary> /// <param name="_parRay">ray list to subtracted</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; - foreach (Pair<float, float> pair in _parRay) + Pair toAdd = null; + foreach (Pair pair in _parRay) { if (pair.r < _sub.l || pair.l > _sub.r) continue; float[] arr = { pair.l, pair.r, _sub.l, _sub.r }; @@ -297,7 +297,7 @@ public class Mirror : Wall, IBulletInteractor, IBreakable } 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; } } @@ -320,12 +320,13 @@ public class Mirror : Wall, IBulletInteractor, IBreakable /// <param name="_parRay">ray list to be checked</param> /// <param name="_range">range to check</param> /// <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; - 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 { output = true; @@ -335,11 +336,12 @@ public class Mirror : Wall, IBulletInteractor, IBreakable 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; }