Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
curvedflats
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
6
Issues
6
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Flatland
curvedflats
Commits
f5a09f33
Commit
f5a09f33
authored
Aug 19, 2019
by
Chae Ho Shin
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'ui' into collision-model
parents
08c6c35c
748a100c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
600 additions
and
558 deletions
+600
-558
PathRenderer.cs
Assets/Scripts/PathRenderer.cs
+322
-278
UIManager.cs
Assets/Scripts/UIManager.cs
+278
-280
No files found.
Assets/Scripts/PathRenderer.cs
View file @
f5a09f33
using
System.Collections
;
using
System.Collections.Generic
;
using
UnityEngine
;
using
System.Linq
;
using
UnityEngine.UI
;
using
System
;
using
MathNet.Numerics.LinearAlgebra
;
using
MathNet.Numerics.LinearAlgebra.Double
;
public
class
PathRenderer
:
MonoBehaviour
{
[
SerializeField
]
Square
square
;
[
SerializeField
]
GameObject
PathColliderPrefab
;
[
SerializeField
]
Camera
playercamera
;
[
SerializeField
]
LevelManager
levelManager
;
[
SerializeField
]
BackgroundMovement
background
;
[
SerializeField
]
using
System.Collections
;
using
System.Collections.Generic
;
using
UnityEngine
;
using
System.Linq
;
using
UnityEngine.UI
;
using
System
;
using
MathNet.Numerics.LinearAlgebra
;
using
MathNet.Numerics.LinearAlgebra.Double
;
public
class
PathRenderer
:
MonoBehaviour
{
[
SerializeField
]
Square
square
;
[
SerializeField
]
GameObject
PathColliderPrefab
;
[
SerializeField
]
Camera
playercamera
;
[
SerializeField
]
LevelManager
levelManager
;
[
SerializeField
]
BackgroundMovement
background
;
[
SerializeField
]
LogScaleSlider
velocityslider
;
[
SerializeField
]
UIManager
ui
;
LineRenderer
_pathRenderer
;
float
_originalPathColliderY
;
public
BackgroundMovement
Background
{
get
{
return
background
;
}
set
{
background
=
value
;
}
}
// Start is called before the first frame update
void
Start
()
{
_originalPathColliderY
=
PathColliderPrefab
.
transform
.
localScale
.
y
/
2
;
_pathRenderer
=
GetComponent
<
LineRenderer
>();
_ResetPaths
();
}
// Update is called once per frame
void
Update
()
{
if
(
Input
.
GetMouseButtonDown
(
1
)
&&
levelManager
.
player
.
isInertial
())
{
RaycastHit
hit1
;
var
ray
=
Camera
.
main
.
ScreenPointToRay
(
Input
.
mousePosition
);
if
(
Physics
.
Raycast
(
ray
,
out
hit1
))
{
RaycastHit
hit2
;
ray
=
playercamera
.
ViewportPointToRay
(
hit1
.
textureCoord
);
if
(
Physics
.
Raycast
(
ray
,
out
hit2
))
{
if
(
Input
.
GetKey
(
KeyCode
.
LeftShift
))
{
_DrawMorePath
(
hit2
.
point
,
hit1
);
}
else
{
_DrawOnePath
(
hit2
.
point
,
hit1
);
}
}
}
/*if (Physics.Raycast(ray, out hit))
{
if (Input.GetKey(KeyCode.LeftShift))
{
_DrawMorePath(hit.point);
}
else
{
_DrawOnePath(hit.point);
}
}*/
}
}
private
void
_DrawOnePath
(
Vector3
point
,
RaycastHit
hit
)
{
_ResetPaths
();
square
.
pathList
[
0
]
=
transform
.
localPosition
;
square
.
pathVelocity
[
0
]
=
0.0f
;
var
tmp
=
transform
.
InverseTransformPoint
(
point
);
square
.
pathList
.
Add
(
new
Vector3
(
tmp
.
x
,
tmp
.
y
,
0.0f
));
square
.
pathVelocity
.
Add
(
velocityslider
.
GetLogScaleValue
());
_pathRenderer
.
positionCount
=
square
.
pathList
.
Count
();
_pathRenderer
.
SetPositions
(
square
.
pathList
.
ToArray
());
_InstantiatePathCollider
(
0
);
ui
.
PopupLastPathUI
(
_pathRenderer
.
positionCount
-
2
,
hit
);
}
private
void
_ResetPaths
()
{
_pathRenderer
.
positionCount
=
1
;
square
.
pathList
.
Clear
();
square
.
pathVelocity
.
Clear
();
square
.
pathList
.
Add
(
transform
.
localPosition
);
square
.
pathVelocity
.
Add
(
0.0f
);
_pathRenderer
.
SetPositions
(
square
.
pathList
.
ToArray
());
for
(
int
i
=
0
;
i
<
transform
.
childCount
;
i
++)
{
Destroy
(
transform
.
GetChild
(
i
).
gameObject
);
}
}
private
void
_DrawMorePath
(
Vector3
point
,
RaycastHit
hit
)
{
if
(
square
.
pathList
.
Count
==
0
)
{
_DrawOnePath
(
point
,
hit
);
}
else
{
var
tmp
=
transform
.
InverseTransformPoint
(
point
);
square
.
pathList
.
Add
(
new
Vector3
(
tmp
.
x
,
tmp
.
y
,
0.0f
));
square
.
pathVelocity
.
Add
(
velocityslider
.
GetLogScaleValue
());
_pathRenderer
.
positionCount
=
square
.
pathList
.
Count
();
_pathRenderer
.
SetPositions
(
square
.
pathList
.
ToArray
());
_InstantiatePathCollider
(
square
.
pathList
.
Count
()
-
2
);
ui
.
PopupLastPathUI
(
_pathRenderer
.
positionCount
-
2
,
hit
);
}
}
public
void
PathClear
()
{
_ResetPaths
();
}
/// <summary>
/// 현재 설정된 경로를 반환.
/// 0번은 내 위치
/// x y 가 공간 z 가 시간
/// </summary>
public
List
<
Vector3
>
PathPositionsXY
{
get
{
return
square
.
pathList
;
}
}
/// <summary>
/// 현재 설정된 경로를 반환.
/// 0번은 현재위치 //패트롤 고려
/// x z 가 공간 y 가 시간
/// </summary>
public
List
<
Vector3
>
PathPositionsXZ
{
get
{
List
<
Vector3
>
list
=
new
List
<
Vector3
>();
foreach
(
var
a
in
square
.
pathList
)
{
//xy -> xz
list
.
Add
(
new
Vector3
(
a
.
x
,
0
,
a
.
y
));
}
return
list
;
}
}
/// <summary>
/// 현재 설정된 경로의 속력을 반환.
/// 0번은 0.0f
/// </summary>
public
List
<
float
>
PathVelocities
{
get
{
return
new
List
<
float
>(
square
.
pathVelocity
);
}
}
private
void
_InstantiatePathCollider
(
int
n
)
{
var
_pathCollider
=
Instantiate
(
PathColliderPrefab
,
transform
);
_pathCollider
.
name
=
"PathCollider-"
+
n
;
_pathCollider
.
tag
=
"path"
;
_pathCollider
.
transform
.
localScale
=
new
Vector3
(
0.1f
,
_originalPathColliderY
,
0
);
_pathCollider
.
transform
.
localEulerAngles
=
new
Vector3
(
0
,
0
,
(
float
)
Constants
.
RadianToDegree
(
Mathf
.
PI
/
2
+
Mathf
.
Atan
(
square
.
GetTangent
(
square
.
GetNthPath
(
n
)))));
float
_newY
=
square
.
GetNthPath
(
n
).
magnitude
;
_pathCollider
.
transform
.
localScale
=
new
Vector3
(
0.1f
,
_newY
*
_originalPathColliderY
,
0
);
_pathCollider
.
transform
.
localPosition
=
(
square
.
pathList
[
n
]
+
square
.
pathList
[
n
+
1
])
/
2
;
}
public
IEnumerator
_StartMovingPath
(
int
finalPathNum
)
{
int
i
=
0
;
var
u
=
levelManager
.
player
.
v
;
int
tinterval
=
Constants
.
alphatinterval
;
VectorBuilder
<
double
>
V
=
Vector
<
double
>.
Build
;
while
(
i
<=
finalPathNum
)
{
Vector3
startpos
=
new
Vector3
(
levelManager
.
player
.
transform
.
position
.
x
,
0
,
levelManager
.
player
.
transform
.
position
.
z
);
Vector3
dest
=
new
Vector3
(
square
.
GetNthPath
(
i
).
x
,
0f
,
square
.
GetNthPath
(
i
).
y
);
Vector3
acceleration
=
dest
.
normalized
;
var
v
=
square
.
GetPathVelocity
(
i
);
var
atmp
=
(
float
)(
v
*
Constants
.
c
);
double
[]
vtmp
=
{
(
atmp
*
(
acceleration
.
x
/
acceleration
.
magnitude
)),
0.0
,
(
atmp
*
(
acceleration
.
z
/
acceleration
.
magnitude
))
};
double
[]
xtmp
=
{
dest
.
magnitude
/
(
v
),
dest
.
x
,
dest
.
z
};
var
deltavnaive
=
V
.
DenseOfArray
(
vtmp
);
var
deltaxnaive
=
V
.
DenseOfArray
(
xtmp
);
double
[]
tmp
=
{
Constants
.
c
*
Constants
.
Gamma
(
deltavnaive
.
L2Norm
()),
deltavnaive
[
0
]
*
Constants
.
Gamma
(
deltavnaive
.
L2Norm
()),
deltavnaive
[
2
]
*
Constants
.
Gamma
(
deltavnaive
.
L2Norm
())};
var
deltav
=
V
.
DenseOfArray
(
tmp
);
Vector
<
double
>
deltax
=
deltaxnaive
;
if
(
u
.
magnitude
>
0.0f
)
{
deltav
=
Constants
.
BoostMatrix
(-
u
)
*
deltav
;
deltax
=
Constants
.
BoostMatrix
(-
u
)
*
deltaxnaive
;
}
var
tt
=
deltav
[
0
]
/
Constants
.
c
;
Vector3
finaldeltav
=
new
Vector3
((
float
)(
deltav
[
1
]
/
tt
),
0.0f
,
(
float
)(
deltav
[
2
]
/
tt
));
Vector3
finaldeltax
=
new
Vector3
((
float
)
deltax
[
1
],
0.0f
,
(
float
)
deltax
[
2
]);
levelManager
.
player
.
v
=
finaldeltav
;
while
(
true
)
{
var
currentpos
=
new
Vector3
(
levelManager
.
player
.
transform
.
position
.
x
,
0
,
levelManager
.
player
.
transform
.
position
.
z
);
var
traveledpos
=
currentpos
-
startpos
;
if
(
traveledpos
.
magnitude
>=
finaldeltax
.
magnitude
)
{
break
;
}
yield
return
new
WaitForFixedUpdate
();
}
levelManager
.
player
.
v
=
u
;
levelManager
.
player
.
transform
.
position
=
new
Vector3
(
startpos
.
x
+
finaldeltax
.
x
,
levelManager
.
player
.
transform
.
position
.
y
,
startpos
.
z
+
finaldeltax
.
z
);
i
++;
}
Background
.
Toggle
=
true
;
}
public
void
DeletePathsAfterNthPath
(
int
n
)
{
square
.
pathList
.
RemoveRange
(
n
,
_pathRenderer
.
positionCount
-
n
);
square
.
pathVelocity
.
RemoveRange
(
n
,
_pathRenderer
.
positionCount
-
n
);
for
(
int
i
=
0
;
i
<
transform
.
childCount
;
i
++)
{
if
(
i
+
1
>=
n
)
Destroy
(
transform
.
GetChild
(
i
).
gameObject
);
}
_pathRenderer
.
positionCount
=
square
.
pathList
.
Count
();
}
}
UIManager
ui
;
LineRenderer
_pathRenderer
;
float
_originalPathColliderY
;
public
BackgroundMovement
Background
{
get
{
return
background
;
}
set
{
background
=
value
;
}
}
// Start is called before the first frame update
void
Start
()
{
_originalPathColliderY
=
PathColliderPrefab
.
transform
.
localScale
.
y
/
2
;
_pathRenderer
=
GetComponent
<
LineRenderer
>();
_ResetPaths
();
}
// Update is called once per frame
void
Update
()
{
if
(
Input
.
GetMouseButtonDown
(
1
)
&&
levelManager
.
player
.
isInertial
())
{
RaycastHit
hit1
;
var
ray
=
Camera
.
main
.
ScreenPointToRay
(
Input
.
mousePosition
);
if
(
Physics
.
Raycast
(
ray
,
out
hit1
))
{
RaycastHit
hit2
;
ray
=
playercamera
.
ViewportPointToRay
(
hit1
.
textureCoord
);
if
(
Physics
.
Raycast
(
ray
,
out
hit2
))
{
if
(
Input
.
GetKey
(
KeyCode
.
LeftShift
))
{
_DrawMorePath
(
hit2
.
point
,
hit1
);
}
else
{
_DrawOnePath
(
hit2
.
point
,
hit1
);
}
}
}
/*if (Physics.Raycast(ray, out hit))
{
if (Input.GetKey(KeyCode.LeftShift))
{
_DrawMorePath(hit.point);
}
else
{
_DrawOnePath(hit.point);
}
}*/
}
}
private
void
_DrawOnePath
(
Vector3
point
,
RaycastHit
hit
)
{
_ResetPaths
();
square
.
pathList
[
0
]
=
transform
.
localPosition
;
square
.
pathVelocity
[
0
]
=
0.0f
;
var
tmp
=
transform
.
InverseTransformPoint
(
point
);
square
.
pathList
.
Add
(
new
Vector3
(
tmp
.
x
,
tmp
.
y
,
0.0f
));
square
.
pathVelocity
.
Add
(
velocityslider
.
GetLogScaleValue
());
_pathRenderer
.
positionCount
=
square
.
pathList
.
Count
();
_pathRenderer
.
SetPositions
(
square
.
pathList
.
ToArray
());
_InstantiatePathCollider
(
0
);
ui
.
PopupLastPathUI
(
_pathRenderer
.
positionCount
-
2
,
hit
);
}
private
void
_ResetPaths
()
{
_pathRenderer
.
positionCount
=
1
;
square
.
pathList
.
Clear
();
square
.
pathVelocity
.
Clear
();
square
.
pathList
.
Add
(
transform
.
localPosition
);
square
.
pathVelocity
.
Add
(
0.0f
);
_pathRenderer
.
SetPositions
(
square
.
pathList
.
ToArray
());
for
(
int
i
=
0
;
i
<
transform
.
childCount
;
i
++)
{
Destroy
(
transform
.
GetChild
(
i
).
gameObject
);
}
}
private
void
_DrawMorePath
(
Vector3
point
,
RaycastHit
hit
)
{
if
(
square
.
pathList
.
Count
==
0
)
{
_DrawOnePath
(
point
,
hit
);
}
else
{
var
tmp
=
transform
.
InverseTransformPoint
(
point
);
square
.
pathList
.
Add
(
new
Vector3
(
tmp
.
x
,
tmp
.
y
,
0.0f
));
square
.
pathVelocity
.
Add
(
velocityslider
.
GetLogScaleValue
());
_pathRenderer
.
positionCount
=
square
.
pathList
.
Count
();
_pathRenderer
.
SetPositions
(
square
.
pathList
.
ToArray
());
_InstantiatePathCollider
(
square
.
pathList
.
Count
()
-
2
);
ui
.
PopupLastPathUI
(
_pathRenderer
.
positionCount
-
2
,
hit
);
}
}
public
void
PathClear
()
{
_ResetPaths
();
}
/// <summary>
/// 현재 설정된 경로를 반환.
/// 0번은 내 위치
/// x y 가 공간 z 가 시간
/// </summary>
public
List
<
Vector3
>
PathPositionsXY
{
get
{
return
square
.
pathList
;
}
}
/// <summary>
/// 현재 설정된 경로를 반환.
/// 0번은 현재위치 //패트롤 고려
/// x z 가 공간 y 가 시간
/// </summary>
public
List
<
Vector3
>
PathPositionsXZ
{
get
{
List
<
Vector3
>
list
=
new
List
<
Vector3
>();
foreach
(
var
a
in
square
.
pathList
)
{
//xy -> xz
list
.
Add
(
new
Vector3
(
a
.
x
,
0
,
a
.
y
));
}
return
list
;
}
}
/// <summary>
/// 현재 설정된 경로의 속력을 반환.
/// 0번은 0.0f
/// </summary>
public
List
<
float
>
PathVelocities
{
get
{
return
new
List
<
float
>(
square
.
pathVelocity
);
}
}
private
void
_InstantiatePathCollider
(
int
n
)
{
var
_pathCollider
=
Instantiate
(
PathColliderPrefab
,
transform
);
_pathCollider
.
name
=
"PathCollider-"
+
n
;
_pathCollider
.
tag
=
"path"
;
_pathCollider
.
transform
.
localScale
=
new
Vector3
(
0.1f
,
_originalPathColliderY
,
0
);
_pathCollider
.
transform
.
localEulerAngles
=
new
Vector3
(
0
,
0
,
(
float
)
Constants
.
RadianToDegree
(
Mathf
.
PI
/
2
+
Mathf
.
Atan
(
square
.
GetTangent
(
square
.
GetNthPath
(
n
)))));
float
_newY
=
square
.
GetNthPath
(
n
).
magnitude
;
_pathCollider
.
transform
.
localScale
=
new
Vector3
(
0.1f
,
_newY
*
_originalPathColliderY
,
0
);
_pathCollider
.
transform
.
localPosition
=
(
square
.
pathList
[
n
]
+
square
.
pathList
[
n
+
1
])
/
2
;
}
public
IEnumerator
_StartMovingPath
(
int
finalPathNum
)
{
int
i
=
0
;
var
u
=
levelManager
.
player
.
v
;
int
tinterval
=
Constants
.
alphatinterval
;
VectorBuilder
<
double
>
V
=
Vector
<
double
>.
Build
;
//square.PathList에서 0에서 시작해 finalPathNum까지 이동
var
startPosition
=
levelManager
.
player
.
transform
.
position
;
Debug
.
Log
(
startPosition
+
"test"
);
while
(
i
<=
finalPathNum
)
{
Vector3
startpos
=
new
Vector3
(
levelManager
.
player
.
transform
.
position
.
x
,
0
,
levelManager
.
player
.
transform
.
position
.
z
);
// 경로 이동 시작할때 위치
Vector3
dest
=
new
Vector3
(
square
.
GetNthPath
(
i
).
x
,
0f
,
square
.
GetNthPath
(
i
).
y
);
// 시작 관성계 기준 startpos에서 목표지점까지 공간 좌표
Vector3
acceleration
=
dest
.
normalized
;
var
v
=
square
.
GetPathVelocity
(
i
);
var
atmp
=
(
float
)(
v
*
Constants
.
c
);
double
[]
vtmp
=
{
(
atmp
*
(
acceleration
.
x
/
acceleration
.
magnitude
)),
0.0
,
(
atmp
*
(
acceleration
.
z
/
acceleration
.
magnitude
))
};
double
[]
xtmp
=
{
dest
.
magnitude
/
(
v
),
dest
.
x
,
dest
.
z
};
var
deltavnaive
=
V
.
DenseOfArray
(
vtmp
);
var
deltaxnaive
=
V
.
DenseOfArray
(
xtmp
);
double
[]
tmp
=
{
Constants
.
c
*
Constants
.
Gamma
(
deltavnaive
.
L2Norm
()),
deltavnaive
[
0
]
*
Constants
.
Gamma
(
deltavnaive
.
L2Norm
()),
deltavnaive
[
2
]
*
Constants
.
Gamma
(
deltavnaive
.
L2Norm
())};
var
deltav
=
V
.
DenseOfArray
(
tmp
);
Vector
<
double
>
deltax
=
deltaxnaive
;
if
(
u
.
magnitude
>
0.0f
)
{
deltav
=
Constants
.
BoostMatrix
(-
u
)
*
deltav
;
// 로렌츠 행렬을 곱해서 경로 이동시 속도가 월드 관성계에서 무슨 속도인지 구함(deltav)
deltax
=
Constants
.
BoostMatrix
(-
u
)
*
deltaxnaive
;
// 로렌츠 행렬을 곱해서 경로 이동시 위치 변화가 월드 관성계에서 얼마나 되는지 구함(deltax)
}
var
tt
=
deltav
[
0
]
/
Constants
.
c
;
Vector3
finaldeltav
=
new
Vector3
((
float
)(
deltav
[
1
]
/
tt
),
0.0f
,
(
float
)(
deltav
[
2
]
/
tt
));
Vector3
finaldeltax
=
new
Vector3
((
float
)
deltax
[
1
],
0.0f
,
(
float
)
deltax
[
2
]);
levelManager
.
player
.
v
=
finaldeltav
;
// 플레이어 월드 관성계 기준 속도를 deltav로 세팅
while
(
true
)
{
var
currentpos
=
new
Vector3
(
levelManager
.
player
.
transform
.
position
.
x
,
0
,
levelManager
.
player
.
transform
.
position
.
z
);
var
traveledpos
=
currentpos
-
startpos
;
if
(
traveledpos
.
magnitude
>=
finaldeltax
.
magnitude
)
{
break
;
}
yield
return
new
WaitForFixedUpdate
();
}
levelManager
.
player
.
v
=
u
;
// 원래 관성계로 돌아옴
levelManager
.
player
.
transform
.
position
=
new
Vector3
(
startpos
.
x
+
finaldeltax
.
x
,
levelManager
.
player
.
transform
.
position
.
y
,
startpos
.
z
+
finaldeltax
.
z
);
// 위치 보정해줌(fixed update오차)
// INSERT CODE TO REMOVE PATH FROM PATH RENDERER HERE
// 여기에 방금 이동한 경로를 안보이게 해야 함
HideNthPath
(
i
);
i
++;
}
Debug
.
Log
(
levelManager
.
player
.
transform
.
position
+
"test"
);
shiftPath
(
finalPathNum
+
1
,
levelManager
.
player
.
transform
.
position
-
startPosition
);
Background
.
Toggle
=
true
;
//아마 여기에 이동을 다 마친 경로들을 square.pathList랑 square.pathVelocity에서 없애주고, finalPathNum 다음 경로가 square.pathList[1]이 되게 해야할듯
// PathRenderer에 child로 되 있는 Path-n들 이름도 다 바꿔줘야 할꺼임(니가 최종 경로 가 몇번째인지 판정으로 GameObject 이름을 string으로 받아와서 하니까)
// 귀찮으면 아예 UIManager.cs에서 int pathNum = int.Parse(obj.name.Substring(13)); 이부분을 바꿔도 됨.
}
public
void
DeletePathsAfterNthPath
(
int
n
)
{
square
.
pathList
.
RemoveRange
(
n
,
_pathRenderer
.
positionCount
-
n
);
square
.
pathVelocity
.
RemoveRange
(
n
,
_pathRenderer
.
positionCount
-
n
);
for
(
int
i
=
n
-
1
;
i
<
transform
.
childCount
;
i
++)
{
Destroy
(
transform
.
GetChild
(
i
).
gameObject
);
}
_pathRenderer
.
positionCount
=
square
.
pathList
.
Count
();
}
public
void
HideNthPath
(
int
n
)
{
transform
.
GetChild
(
n
).
gameObject
.
GetComponent
<
MeshRenderer
>().
enabled
=
false
;
}
public
void
shiftPath
(
int
times
,
Vector3
deltaPosition
)
{
square
.
pathList
.
RemoveRange
(
1
,
times
);
square
.
pathVelocity
.
RemoveRange
(
1
,
times
);
deltaPosition
=
ui
.
getXYVectorfromXZVector
(
deltaPosition
);
Debug
.
Log
(
deltaPosition
+
"test"
);
int
i
;
for
(
i
=
0
;
i
<
times
;
i
++)
{
Destroy
(
transform
.
GetChild
(
i
).
gameObject
);
}
for
(
i
=
0
;
i
<
transform
.
childCount
-
times
;
i
++)
{
GameObject
obj
=
transform
.
GetChild
(
i
+
times
).
gameObject
;
obj
.
name
=
"PathCollider-"
+
i
;
obj
.
transform
.
localPosition
-=
deltaPosition
;
square
.
pathList
[
i
+
1
]
-=
deltaPosition
;
}
_pathRenderer
.
positionCount
=
square
.
pathList
.
Count
();
_pathRenderer
.
SetPositions
(
square
.
pathList
.
ToArray
());
}
}
Assets/Scripts/UIManager.cs
View file @
f5a09f33
using
System.Collections
;
using
System.Collections.Generic
;
using
UnityEngine.EventSystems
;
using
UnityEngine
;
using
UnityEngine.UI
;
using
System
;
using
System.Linq
;
public
class
UIManager
:
MonoBehaviour
{
[
SerializeField
]
Square
square
;
[
SerializeField
]
Camera
playercamera
;
[
SerializeField
]
LogScaleSlider
velocityslider
;
[
SerializeField
]
LogScaleSlider
accelslider
;
[
SerializeField
]
private
Text
interactText
;
public
Canvas
canvas
;
public
Text
mytime
;
//public Planemovement clock1;
//public Planemovement clock2;
//public Text clock1time;
//public Text clock2time;
public
Text
wintext
;
public
GameObject
_pathUI
;
public
LevelManager
levelManager
;
public
PlayerMovement
player
;
public
PathRenderer
pathRenderer
;
private
int
prevSelectPathNum
=
-
1
;
private
Text
pathName
;
private
Text
pathVelocity
;
private
Vector3
middlePoint
;
private
Vector3
canvasSize
;
private
int
sliderflag
=
0
;
GraphicRaycaster
gr
;
PointerEventData
ped
;
// Start is called before the first frame update
void
Start
()
{
_pathUI
.
SetActive
(
false
);
pathName
=
_pathUI
.
transform
.
Find
(
"Name"
).
GetComponent
<
Text
>();
pathVelocity
=
_pathUI
.
transform
.
Find
(
"Velocity"
).
GetComponent
<
Text
>();
middlePoint
=
_pathUI
.
transform
.
position
;
canvasSize
=
new
Vector3
(
Screen
.
width
,
Screen
.
height
,
0
);
gr
=
canvas
.
GetComponent
<
GraphicRaycaster
>();
ped
=
new
PointerEventData
(
null
);
}
// Update is called once per frame
void
Update
()
{
mytime
.
text
=
levelManager
.
player
.
time
.
ToString
()
+
" s"
;
//clock1time.text = clock1.GetTime().ToString() + " s";
//clock2time.text = clock2.GetTime().ToString() + " s";
if
(
levelManager
.
winstate
)
{
wintext
.
gameObject
.
SetActive
(
true
);
}
if
(
Input
.
GetMouseButtonDown
(
0
))
{
ped
.
position
=
Input
.
mousePosition
;
List
<
RaycastResult
>
results
=
new
List
<
RaycastResult
>();
gr
.
Raycast
(
ped
,
results
);
bool
isIgnored
=
true
;
//ui 클릭시.
foreach
(
RaycastResult
re
in
results
)
{
GameObject
obj
=
re
.
gameObject
;
//panel만 있는지 검사.
isIgnored
&=
(
obj
.
tag
==
"ignored"
);
//Debug.Log(obj);
//슬라이더일때
if
(
obj
.
tag
==
"VelocitySlider"
)
{
sliderflag
=
1
;
if
(
_pathUI
.
activeSelf
==
true
&&
prevSelectPathNum
!=
-
1
)
{
square
.
pathVelocity
[
prevSelectPathNum
+
1
]
=
velocityslider
.
GetLogScaleValue
();
pathVelocity
.
text
=
"Velocity: "
+
square
.
GetPathVelocity
(
prevSelectPathNum
).
ToString
()
+
"c"
;
break
;
}
}
}
if
(
isIgnored
)
{
//클릭된 ui가 패널만 있을때.
TryFIndPath
();
}
}
else
if
(
sliderflag
==
1
&&
Input
.
GetMouseButton
(
0
))
{
if
(
_pathUI
.
activeSelf
==
true
&&
prevSelectPathNum
!=
-
1
)
{
square
.
pathVelocity
[
prevSelectPathNum
+
1
]
=
velocityslider
.
GetLogScaleValue
();
pathVelocity
.
text
=
"Velocity: "
+
square
.
GetPathVelocity
(
prevSelectPathNum
).
ToString
()
+
"c"
;
}
}
else
if
(
Input
.
GetMouseButtonDown
(
1
))
{
sliderflag
=
0
;
}
//player movement
if
(
Input
.
GetKeyDown
(
"w"
))
{
var
tmp
=
accelslider
.
GetLogScaleValue
()
*
(
float
)
Constants
.
c
;
levelManager
.
player
.
alpha
+=
new
Vector3
(
0
,
0
,
tmp
);
}
else
if
(
Input
.
GetKeyDown
(
"a"
))
{
var
tmp
=
accelslider
.
GetLogScaleValue
()
*
(
float
)
Constants
.
c
;
levelManager
.
player
.
alpha
+=
new
Vector3
(-
tmp
,
0
,
0
);
}
else
if
(
Input
.
GetKeyDown
(
"s"
))
{
var
tmp
=
accelslider
.
GetLogScaleValue
()
*
(
float
)
Constants
.
c
;
levelManager
.
player
.
alpha
+=
new
Vector3
(
0
,
0
,
-
tmp
);
}
else
if
(
Input
.
GetKeyDown
(
"d"
))
{
var
tmp
=
accelslider
.
GetLogScaleValue
()
*
(
float
)
Constants
.
c
;
levelManager
.
player
.
alpha
+=
new
Vector3
(
tmp
,
0
,
0
);
}
if
(
Input
.
GetKeyUp
(
"w"
))
{
levelManager
.
player
.
alpha
-=
new
Vector3
(
0
,
0
,
levelManager
.
player
.
alpha
.
z
);
}
else
if
(
Input
.
GetKeyUp
(
"a"
))
{
levelManager
.
player
.
alpha
-=
new
Vector3
(
levelManager
.
player
.
alpha
.
x
,
0
,
0
);
}
else
if
(
Input
.
GetKeyUp
(
"s"
))
{
levelManager
.
player
.
alpha
-=
new
Vector3
(
0
,
0
,
levelManager
.
player
.
alpha
.
z
);
}
else
if
(
Input
.
GetKeyUp
(
"d"
))
{
levelManager
.
player
.
alpha
-=
new
Vector3
(
levelManager
.
player
.
alpha
.
x
,
0
,
0
);
}
}
private
Vector3
getMouseClickPosition
(
RaycastHit
hit
)
{
var
point
=
Camera
.
main
.
WorldToScreenPoint
(
hit
.
point
);
return
point
-
canvasSize
*
0.5f
+
middlePoint
;
}
private
Vector3
getDestVector
(
Vector3
ScreenVector
,
Vector3
RealVector
)
{
ScreenVector
.
z
=
0
;
RealVector
=
getXYVectorfromXZVector
(
RealVector
);
var
v
=
square
.
GetDestPoint
(
prevSelectPathNum
)
-
RealVector
;
var
k
=
ScreenVector
.
magnitude
/
RealVector
.
magnitude
;
return
k
*
v
;
}
using
System.Collections
;
using
System.Collections.Generic
;
using
UnityEngine.EventSystems
;
using
UnityEngine
;
using
UnityEngine.UI
;
using
System
;
using
System.Linq
;
public
class
UIManager
:
MonoBehaviour
{
[
SerializeField
]
Square
square
;
[
SerializeField
]
Camera
playercamera
;
[
SerializeField
]
LogScaleSlider
velocityslider
;
[
SerializeField
]
LogScaleSlider
accelslider
;
[
SerializeField
]
private
Text
interactText
;
public
Canvas
canvas
;
public
Text
mytime
;
//public Planemovement clock1;
//public Planemovement clock2;
//public Text clock1time;
//public Text clock2time;
public
Text
wintext
;
public
GameObject
_pathUI
;
public
LevelManager
levelManager
;
public
PlayerMovement
player
;
public
PathRenderer
pathRenderer
;
private
int
prevSelectPathNum
=
-
1
;
private
Text
pathName
;
private
Text
pathVelocity
;
private
Vector3
middlePoint
;
private
Vector3
canvasSize
;
private
int
sliderflag
=
0
;
GraphicRaycaster
gr
;
PointerEventData
ped
;
// Start is called before the first frame update
void
Start
()
{
_pathUI
.
SetActive
(
false
);
pathName
=
_pathUI
.
transform
.
Find
(
"Name"
).
GetComponent
<
Text
>();
pathVelocity
=
_pathUI
.
transform
.
Find
(
"Velocity"
).
GetComponent
<
Text
>();
middlePoint
=
_pathUI
.
transform
.
position
;
canvasSize
=
new
Vector3
(
Screen
.
width
,
Screen
.
height
,
0
);
gr
=
canvas
.
GetComponent
<
GraphicRaycaster
>();
ped
=
new
PointerEventData
(
null
);
}
// Update is called once per frame
void
Update
()
{
mytime
.
text
=
levelManager
.
player
.
time
.
ToString
()
+
" s"
;
//clock1time.text = clock1.GetTime().ToString() + " s";
//clock2time.text = clock2.GetTime().ToString() + " s";
if
(
levelManager
.
winstate
)
{
wintext
.
gameObject
.
SetActive
(
true
);
}
if
(
Input
.
GetMouseButtonDown
(
0
))
{
ped
.
position
=
Input
.
mousePosition
;
List
<
RaycastResult
>
results
=
new
List
<
RaycastResult
>();
gr
.
Raycast
(
ped
,
results
);
bool
isIgnored
=
true
;
//ui 클릭시.
foreach
(
RaycastResult
re
in
results
)
{
GameObject
obj
=
re
.
gameObject
;
//panel만 있는지 검사.
isIgnored
&=
(
obj
.
tag
==
"ignored"
);
//Debug.Log(obj);
//슬라이더일때
if
(
obj
.
tag
==
"VelocitySlider"
)
{
sliderflag
=
1
;
if
(
_pathUI
.
activeSelf
==
true
&&
prevSelectPathNum
!=
-
1
)
{
square
.
pathVelocity
[
prevSelectPathNum
+
1
]
=
velocityslider
.
GetLogScaleValue
();
pathVelocity
.
text
=
"Velocity: "
+
square
.
GetPathVelocity
(
prevSelectPathNum
).
ToString
()
+
"c"
;
break
;
}
}
}
if
(
isIgnored
)
{
//클릭된 ui가 패널만 있을때.
TryFIndPath
();
}
}
else
if
(
sliderflag
==
1
&&
Input
.
GetMouseButton
(
0
))
{
if
(
_pathUI
.
activeSelf
==
true
&&
prevSelectPathNum
!=
-
1
)
{
square
.
pathVelocity
[
prevSelectPathNum
+
1
]
=
velocityslider
.
GetLogScaleValue
();
pathVelocity
.
text
=
"Velocity: "
+
square
.
GetPathVelocity
(
prevSelectPathNum
).
ToString
()
+
"c"
;
}
}
else
if
(
Input
.
GetMouseButtonDown
(
1
))
{
sliderflag
=
0
;
}
//player movement
if
(
Input
.
GetKeyDown
(
"w"
))
{
var
tmp
=
accelslider
.
GetLogScaleValue
()
*
(
float
)
Constants
.
c
;
levelManager
.
player
.
alpha
+=
new
Vector3
(
0
,
0
,
tmp
);
}
else
if
(
Input
.
GetKeyDown
(
"a"
))
{
var
tmp
=
accelslider
.
GetLogScaleValue
()
*
(
float
)
Constants
.
c
;
levelManager
.
player
.
alpha
+=
new
Vector3
(-
tmp
,
0
,
0
);
}
else
if
(
Input
.
GetKeyDown
(
"s"
))
{
var
tmp
=
accelslider
.
GetLogScaleValue
()
*
(
float
)
Constants
.
c
;
levelManager
.
player
.
alpha
+=
new
Vector3
(
0
,
0
,
-
tmp
);
}
else
if
(
Input
.
GetKeyDown
(
"d"
))
{
var
tmp
=
accelslider
.
GetLogScaleValue
()
*
(
float
)
Constants
.
c
;
levelManager
.
player
.
alpha
+=
new
Vector3
(
tmp
,
0
,
0
);
}
if
(
Input
.
GetKeyUp
(
"w"
))
{
levelManager
.
player
.
alpha
-=
new
Vector3
(
0
,
0
,
levelManager
.
player
.
alpha
.
z
);
}
else
if
(
Input
.
GetKeyUp
(
"a"
))
{
levelManager
.
player
.
alpha
-=
new
Vector3
(
levelManager
.
player
.
alpha
.
x
,
0
,
0
);
}
else
if
(
Input
.
GetKeyUp
(
"s"
))
{
levelManager
.
player
.
alpha
-=
new
Vector3
(
0
,
0
,
levelManager
.
player
.
alpha
.
z
);
}
else
if
(
Input
.
GetKeyUp
(
"d"
))
{
levelManager
.
player
.
alpha
-=
new
Vector3
(
levelManager
.
player
.
alpha
.
x
,
0
,
0
);
}
}
private
Vector3
getMouseClickPosition
(
RaycastHit
hit
)
{
var
point
=
Camera
.
main
.
WorldToScreenPoint
(
hit
.
point
);
return
point
-
canvasSize
*
0.5f
+
middlePoint
;
}
private
Vector3
getDestVector
(
Vector3
ScreenVector
,
Vector3
RealVector
)
{
ScreenVector
.
z
=
0
;
RealVector
=
getXYVectorfromXZVector
(
RealVector
);
var
v
=
square
.
GetDestPoint
(
prevSelectPathNum
)
-
RealVector
;
var
k
=
ScreenVector
.
magnitude
/
RealVector
.
magnitude
;
return
k
*
v
;
}
private
Vector3
getVectorFromAtoB
(
Vector3
a
,
Vector3
b
)
{
var
ret
=
b
-
a
;
return
ret
;
}
p
rivate
Vector3
getXYVectorfromXZVector
(
Vector3
v
)
}
p
ublic
Vector3
getXYVectorfromXZVector
(
Vector3
v
)
{
return
new
Vector3
(
v
.
x
,
v
.
z
,
0.0f
);
}
private
void
updatePathInfo
(
int
pathNum
)
{
pathName
.
text
=
"Path - "
+
pathNum
;
pathVelocity
.
text
=
"Velocity: "
+
square
.
GetPathVelocity
(
pathNum
).
ToString
()
+
"c"
;
velocityslider
.
UpdateValuebyVelocity
(
square
.
GetPathVelocity
(
pathNum
));
}
private
void
TryFIndPath
()
{
RaycastHit
hit1
;
RaycastHit
hit2
;
var
ray
=
Camera
.
main
.
ScreenPointToRay
(
Input
.
mousePosition
);
if
(
Physics
.
Raycast
(
ray
,
out
hit1
))
{
ray
=
playercamera
.
ViewportPointToRay
(
hit1
.
textureCoord
);
if
(
Physics
.
Raycast
(
ray
,
out
hit2
))
{
var
obj
=
hit2
.
collider
.
gameObject
;
Debug
.
Log
(
obj
.
tag
);
if
(
obj
.
tag
==
"path"
)
{
int
pathNum
=
int
.
Parse
(
obj
.
name
.
Substring
(
13
));
if
(
pathNum
!=
prevSelectPathNum
)
{
prevSelectPathNum
=
pathNum
;
updatePathInfo
(
pathNum
);
var
mouseClickPosition
=
getMouseClickPosition
(
hit1
);
var
v
=
getDestVector
(
getVectorFromAtoB
(
middlePoint
,
mouseClickPosition
),
getVectorFromAtoB
(
player
.
transform
.
position
,
hit2
.
point
));
_pathUI
.
transform
.
position
=
mouseClickPosition
+
v
;
_pathUI
.
SetActive
(
true
);
}
}
else
{
_pathUI
.
SetActive
(
false
);
prevSelectPathNum
=
-
1
;
sliderflag
=
0
;
}
}
}
}
/// <summary>
/// 현재 지정된 path를 시작합니다.
/// </summary>
public
void
PathStart
()
{
pathRenderer
.
Background
.
Toggle
=
false
;
StartCoroutine
(
pathRenderer
.
_StartMovingPath
(
prevSelectPathNum
));
_pathUI
.
SetActive
(
false
);
prevSelectPathNum
=
-
1
;
pathRenderer
.
PathClear
();
}
public
void
PathCancel
()
{
pathRenderer
.
DeletePathsAfterNthPath
(
prevSelectPathNum
+
1
);
_pathUI
.
SetActive
(
false
);
prevSelectPathNum
=
-
1
;
}
public
void
OnDoubleClicked
()
{
try
{
if
(
levelManager
.
player
.
isInertial
())
{
RaycastHit
hit
;
var
ray
=
Camera
.
main
.
ScreenPointToRay
(
Input
.
mousePosition
);
if
(
Physics
.
Raycast
(
ray
,
out
hit
))
{
ray
=
playercamera
.
ViewportPointToRay
(
hit
.
textureCoord
);
if
(
Physics
.
Raycast
(
ray
,
out
hit
))
{
levelManager
.
player
.
MoveInfinitelyToAbPosition
(
pathRenderer
.
transform
.
InverseTransformPoint
(
hit
.
point
),
velocityslider
.
GetLogScaleValue
());
//levelManager.player.MoveToAbPosition(hit.point, velocityslider.GetLogScaleValue());
//Debug.Log(velocityslider.GetLogScaleValue() * (float)Constants.c);
//Debug.Log(hit.point);
}
}
}
}
catch
(
Exception
e
)
{
Debug
.
Log
(
e
);
}
}
public
string
InteractText
{
set
{
interactText
.
text
=
value
;
}
}
}
private
void
updatePathInfo
(
int
pathNum
)
{
pathName
.
text
=
"Path - "
+
pathNum
;
pathVelocity
.
text
=
"Velocity: "
+
square
.
GetPathVelocity
(
pathNum
).
ToString
()
+
"c"
;
velocityslider
.
UpdateValuebyVelocity
(
square
.
GetPathVelocity
(
pathNum
));
}
private
void
TryFIndPath
()
{
RaycastHit
hit1
;
RaycastHit
hit2
;
var
ray
=
Camera
.
main
.
ScreenPointToRay
(
Input
.
mousePosition
);
if
(
Physics
.
Raycast
(
ray
,
out
hit1
))
{
ray
=
playercamera
.
ViewportPointToRay
(
hit1
.
textureCoord
);
if
(
Physics
.
Raycast
(
ray
,
out
hit2
))
{
var
obj
=
hit2
.
collider
.
gameObject
;
Debug
.
Log
(
obj
.
tag
);
if
(
obj
.
tag
==
"path"
)
{
int
pathNum
=
int
.
Parse
(
obj
.
name
.
Substring
(
13
));
if
(
pathNum
!=
prevSelectPathNum
)
{
prevSelectPathNum
=
pathNum
;
updatePathInfo
(
pathNum
);
var
mouseClickPosition
=
getMouseClickPosition
(
hit1
);
var
v
=
getDestVector
(
getVectorFromAtoB
(
middlePoint
,
mouseClickPosition
),
getVectorFromAtoB
(
player
.
transform
.
position
,
hit2
.
point
));
_pathUI
.
transform
.
position
=
mouseClickPosition
+
v
;
_pathUI
.
SetActive
(
true
);
}
}
else
{
_pathUI
.
SetActive
(
false
);
prevSelectPathNum
=
-
1
;
sliderflag
=
0
;
}
}
}
}
/// <summary>
/// 현재 지정된 path를 시작합니다.
/// </summary>
public
void
PathStart
()
{
_pathUI
.
SetActive
(
false
);
pathRenderer
.
Background
.
Toggle
=
false
;
StartCoroutine
(
pathRenderer
.
_StartMovingPath
(
prevSelectPathNum
));
prevSelectPathNum
=
-
1
;
}
public
void
PathCancel
()
{
pathRenderer
.
DeletePathsAfterNthPath
(
prevSelectPathNum
+
1
);
_pathUI
.
SetActive
(
false
);
prevSelectPathNum
=
-
1
;
}
public
void
OnDoubleClicked
()
{
try
{
if
(
levelManager
.
player
.
isInertial
())
{
RaycastHit
hit
;
var
ray
=
Camera
.
main
.
ScreenPointToRay
(
Input
.
mousePosition
);
if
(
Physics
.
Raycast
(
ray
,
out
hit
))
{
ray
=
playercamera
.
ViewportPointToRay
(
hit
.
textureCoord
);
if
(
Physics
.
Raycast
(
ray
,
out
hit
))
{
levelManager
.
player
.
MoveInfinitelyToAbPosition
(
pathRenderer
.
transform
.
InverseTransformPoint
(
hit
.
point
),
velocityslider
.
GetLogScaleValue
());
//levelManager.player.MoveToAbPosition(hit.point, velocityslider.GetLogScaleValue());
//Debug.Log(velocityslider.GetLogScaleValue() * (float)Constants.c);
//Debug.Log(hit.point);
}
}
}
}
catch
(
Exception
e
)
{
Debug
.
Log
(
e
);
}
}
public
string
InteractText
{
set
{
interactText
.
text
=
value
;
}
}
public
void
PopupLastPathUI
(
int
pathNum
,
RaycastHit
hit
)
{
prevSelectPathNum
=
pathNum
;
updatePathInfo
(
pathNum
);
var
mouseClickPosition
=
getMouseClickPosition
(
hit
);
_pathUI
.
transform
.
position
=
mouseClickPosition
;
Debug
.
Log
(
pathNum
+
"test"
);
_pathUI
.
SetActive
(
true
);
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment