Commit 238ad08b authored by 18손재민's avatar 18손재민

Merge branch 'bullet'

parents 9a83270c 078c1d2a
fileFormatVersion: 2
guid: a32ca40f7f255ea47ac7ee9d558755ce
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pair<L, R>
{
public L l;
public R r;
public Pair(L _l, R _r)
{
this.l = _l;
this.r = _r;
}
public Pair<R, L> Swap()
{
return new Pair<R, L>(r, l);
}
}
fileFormatVersion: 2
guid: 6f1adc6ee7fc32943bb2c1a1dda4c4f9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -2,13 +2,23 @@
using System.Collections.Generic;
using UnityEngine;
public class Briefcase : MonoBehaviour, IPlayerInteractor
public class Briefcase : MonoBehaviour, IObject, IPlayerInteractor
{
[SerializeField]
private Floor floor = null;
public Vector2Int Position { get { return floor != null ? floor.mapPos : throw new UnassignedReferenceException("Floor of Interactor is not assigned"); } }
public void Init(Floor floor)
public GameObject GetObject()
{
return gameObject;
}
public Vector2 GetPos()
{
return new Vector2(transform.position.x, transform.position.z);
}
public void Init(Floor floor)
{
this.floor = floor;
PlayerController.inst.OnPlayerMove += Interact;
......@@ -23,4 +33,9 @@ public class Briefcase : MonoBehaviour, IPlayerInteractor
Destroy(gameObject);
}
}
ObjType IObject.GetType()
{
return ObjType.Briefcase;
}
}
......@@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;
public class CameraTurret : MonoBehaviour, IBreakable, IPlayerInteractor
public class CameraTurret : MonoBehaviour, IObject, IBreakable, IPlayerInteractor
{
[SerializeField]
private Floor floor = null;
......@@ -27,4 +27,19 @@ public class CameraTurret : MonoBehaviour, IBreakable, IPlayerInteractor
//TODO : Restart Level
}
}
public GameObject GetObject()
{
return gameObject;
}
public Vector2 GetPos()
{
return new Vector2(transform.position.x, transform.position.z);
}
ObjType IObject.GetType()
{
return ObjType.Camera;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum ObjType
{
NULL,
Briefcase,
Camera,
Mannequin
}
public interface IObject
{
GameObject GetObject();
Vector2 GetPos();
ObjType GetType();
}
fileFormatVersion: 2
guid: e7d883246c45c6f4f94bae2ca2d05d6f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
......@@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;
public class Mannequin : MonoBehaviour, IBulletInteractor
public class Mannequin : MonoBehaviour, IObject, IBulletInteractor
{
[SerializeField]
private Mesh[] mannequinMesh = new Mesh[2];
......@@ -48,4 +48,19 @@ public class Mannequin : MonoBehaviour, IBulletInteractor
{
Color = isWhite ? Color.white : Color.black;
}
public GameObject GetObject()
{
return gameObject;
}
public Vector2 GetPos()
{
return new Vector2(transform.position.x, transform.position.z);
}
ObjType IObject.GetType()
{
return ObjType.Mannequin;
}
}
......@@ -3,11 +3,8 @@ using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Mirror : MonoBehaviour, IBulletInteractor, IBreakable
public class Mirror : Wall, IBulletInteractor, IBreakable
{
private Camera camera;
private RenderTexture rt;
public void Break()
{
Destroy(gameObject);
......@@ -17,18 +14,201 @@ public class Mirror : MonoBehaviour, IBulletInteractor, IBreakable
{
if (bullet is FakeBullet)
{
//Break Mirror & Make reflected objects
// Make reflected objects
CopyObjects(PlayerController.inst.currentPlayer);
}
else if (bullet is TruthBullet)
{
// Break Mirror
}
}
private void Start()
/// <summary>
/// copy objects which reflected by this mirror
/// </summary>
/// <param name="_shooter">transform of shooter</param>
private void CopyObjects(Player _shooter)
{
camera = GetComponent<Camera>();
//TODO : Create RenderTexture and put it into Camera's targeTexture
Vector2Int stPos = _shooter.pos; // position of shooter's cell
List<Pair<float, float>> parRay = new List<Pair<float, float>>
{
new Pair<float, float>(0, 1)
};
// check before reflect (check walls and mirrors)
foreach (var wall in MapManager.inst.currentMap.wallGrid)
{
Pair<float, float> pair = new Pair<float, float>(PointToParRay(stPos, wall.Value.ldPos, false), PointToParRay(stPos, wall.Value.rdPos, false));
if (pair.l > pair.r) pair = pair.Swap();
SubtractRay(parRay, pair);
}
foreach (var mirr in MapManager.inst.currentMap.mirrorGrid)
{
if (mirr.Value != this)
{
Pair<float, float> pair = new Pair<float, float>(PointToParRay(stPos, mirr.Value.ldPos, false), PointToParRay(stPos, mirr.Value.rdPos, false));
if (pair.l > pair.r) pair = pair.Swap();
SubtractRay(parRay, pair);
}
}
// check after reflect, if obj or floor, copy else if wall or mirror, Subtract
int side, i;
if (dir) // horizontal, parallel with x
{
side = (ldPos.y - stPos.y > 0) ? -1 : 1;
i = ldPos.y;
}
else // vertical, parallel with y
{
side = (ldPos.x - stPos.x > 0) ? -1 : 1;
i = ldPos.x;
}
for (; i < MapManager.inst.currentMap.maxMapSize; i += side)
{
foreach (var floor in MapManager.inst.currentMap.floorGrid)
{
if ((dir ? floor.Key.y : floor.Key.x) == i)
{
if (IsInRay(parRay, PointToParRay(stPos, floor.Value.mapPos, true)))
{ // copy floor
int nextx = dir ? floor.Key.x : 2 * ldPos.x - floor.Key.x;
int nexty = dir ? 2 * ldPos.y - floor.Key.y : floor.Key.y;
MapManager.inst.currentMap.CreateFloor(nextx, nexty);
}
}
}
foreach (var obj in MapManager.inst.currentMap.objectGrid)
{
if ((dir ? obj.Key.y : obj.Key.x) == i)
{
if (IsInRay(parRay, PointToParRay(stPos, obj.Value.GetPos(), true)))
{
/*copy object*/
}
}
}
foreach (var wall in MapManager.inst.currentMap.wallGrid)
{
if ((dir ? wall.Key.y : wall.Key.x) == i)
{
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();
/*copy wall*/
SubtractRay(parRay, pair);
}
}
foreach (var mirr in MapManager.inst.currentMap.mirrorGrid)
{
if (mirr.Value != this && (dir ? mirr.Key.y : mirr.Key.x) == i)
{
Pair<float, float> pair = new Pair<float, float>(PointToParRay(stPos, mirr.Value.ldPos, true), PointToParRay(stPos, mirr.Value.rdPos, true));
if (pair.l > pair.r) pair = pair.Swap();
/*copy mirror*/
SubtractRay(parRay, pair);
}
}
}
}
private void Update()
/// <summary>
/// subtract _sub from _parRay
/// </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)
{
//TODO :Calculate Camera's Position and Rotation
foreach (Pair<float, float> pair in _parRay)
{
if (pair.r < _sub.l || pair.l > _sub.r) continue;
float[] arr = { pair.l, pair.r, _sub.l, _sub.r };
for (int i = 0; i < 4; i++) // sort arr
{
float smallest = arr[i];
int smallIdx = i;
for (int j = i + 1; j < 4; j++)
{
if (smallest > arr[j])
{
smallest = arr[j];
smallIdx = j;
}
}
float temp = arr[i];
arr[i] = smallest;
arr[smallIdx] = temp;
}
// subtract
if (arr[0] == _sub.l && arr[2] == _sub.r)
{
pair.l = _sub.r;
}
else if (arr[1] == _sub.l && arr[3] == _sub.r)
{
pair.r = _sub.l;
}
else if (arr[1] == _sub.l && arr[2] == _sub.r)
{
_parRay.Add(new Pair<float, float>(pair.r, _sub.r));
pair.r = _sub.l;
}
}
}
/// <summary>
/// check if _range is included in _parRay
/// </summary>
/// <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 output = false;
foreach (Pair<float, float> pair in _parRay)
{
if (pair.r <= _range.l || pair.l >= _range.r) continue;
else
{
output = true;
break;
}
}
return output;
}
bool IsInRay(List<Pair<float, float>> _parRay, float _obj)
{
foreach (Pair<float, float> pair in _parRay)
{
if (pair.l <= _obj && pair.r >= _obj) return true;
}
return false;
}
/// <summary>
/// calculate where _chPos is from _stPos
/// </summary>
/// <param name="_stPos">position of shooter</param>
/// <param name="_chPos">position of object</param>
/// <param name="_isRefl">if we calculate after reflecting, true</param>
/// <returns>float value of _chPos is posed</returns>
float PointToParRay(Vector2 _stPos, Vector2 _chPos, bool _isRefl)
{
if (dir) // horizontal
{
float dist = _chPos.y - _stPos.y + (_isRefl ? (ldPos.y - _chPos.y) * 2 : 0);
float spreadLen = len * dist / (ldPos.y - _stPos.y);
float rayStPos = _stPos.x + (ldPos.x - _stPos.x) * dist / (ldPos.y - _stPos.y);
return (_chPos.x - rayStPos) / spreadLen;
}
else // vertical
{
float dist = _chPos.x - _stPos.x + (_isRefl ? (ldPos.x - _chPos.x) * 2 : 0);
float spreadLen = len * dist / (ldPos.x - _stPos.x);
float rayStPos = _stPos.y + (ldPos.y - _stPos.y) * dist / (ldPos.x - _stPos.x);
return (_chPos.y - rayStPos) / spreadLen;
}
}
}
......@@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;
public class NormalWall : MonoBehaviour, IBulletInteractor
public class NormalWall : Wall, IBulletInteractor
{
public void Interact(Bullet bullet)
{
......@@ -12,7 +12,11 @@ public class NormalWall : MonoBehaviour, IBulletInteractor
}
else if (bullet is MirrorBullet)
{
gameObject.AddComponent<Mirror>();
Mirror mirror = gameObject.AddComponent<Mirror>();
GetComponent<Renderer>().material = GameManager.inst.mirrorMaterial;
mirror.SetmapPos(mapPos);
mirror.len = len;
mirror.type = WallType.Mirror;
Destroy(this);
}
}
......
......@@ -5,6 +5,7 @@ using System.Linq;
public class GameManager : SingletonBehaviour<GameManager>
{
public Material mirrorMaterial;
/*
private List<IPlayerInteractor> playerInteractors;
......
......@@ -7,8 +7,10 @@ public class Map : MonoBehaviour
{
public int testInputSizeX, testInputSizeY;
public int maxMapSize;
private Dictionary<Vector2Int, Floor> floorGrid;
private Dictionary<Vector2, Wall> wallGrid;
public Dictionary<Vector2Int, Floor> floorGrid;
public Dictionary<Vector2, Wall> wallGrid;
public Dictionary<Vector2, Mirror> mirrorGrid;
public Dictionary<Vector2Int, IObject> objectGrid;
public GameObject floors;
public GameObject walls;
public List<Floor> startFloors;
......
......@@ -2,12 +2,33 @@
using System.Collections.Generic;
using UnityEngine;
public enum WallType
{
NULL,
Normal,
Mirror
}
public class Wall : MonoBehaviour
{
/// <summary>
/// Position of this floor at the map.
/// </summary>
public Vector2 mapPos;
public Vector2Int ldPos // left down pos
{
get { return new Vector2Int((int)mapPos.x, (int)mapPos.y); }
}
public Vector2Int rdPos // right down pos
{
get { return ldPos + (dir ? new Vector2Int(len, 0) : new Vector2Int(0, len)); }
}
public bool dir // false: ver, true: hor
{
get { return (int)(transform.rotation.eulerAngles.y / 90) % 2 != 1; }
}
public int len = 1; // length of wall
public WallType type;
public void SetmapPos(Vector2 pos)
{
......
......@@ -5,6 +5,11 @@ using UnityEngine.AI;
public class Player : MonoBehaviour
{
public Vector2Int pos
{
get { return new Vector2Int((int)transform.position.x, (int)transform.position.y); }
}
Coroutine playerArrivalCheck;
public GameObject head;
......
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