Files
Travel_Run/Assets/Scripts/CourseObstacle.cs
T
2026-07-06 16:15:20 +09:00

201 lines
6.2 KiB
C#

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
// 코스 데이터가 발판 위에 따로 얹는 장애물 프리팹을 제어한다.
public class CourseObstacle : MonoBehaviour {
public enum ObstacleKind {
Jump,
Slide
}
public ObstacleKind obstacleKind = ObstacleKind.Jump;
public Transform lowObstacle;
public SlideObstaclePattern slidePattern;
public Transform ceilingPlatform;
private Platform.PlatformSkin reservedSkin = Platform.PlatformSkin.Airport;
private Platform.PlatformPattern reservedPlatformPattern = Platform.PlatformPattern.LowMid;
private SlideObstaclePattern.SlideLayout reservedSlideLayout = SlideObstaclePattern.SlideLayout.CenterPair;
public void Configure(
Platform.PlatformSkin platformSkin,
Platform.PlatformPattern platformPattern,
SlideObstaclePattern.SlideLayout slideLayout) {
reservedSkin = platformSkin;
reservedPlatformPattern = platformPattern;
reservedSlideLayout = slideLayout;
ApplyConfiguration();
}
private void Awake() {
CacheReferences();
}
private void OnEnable() {
ApplyConfiguration();
}
private void CacheReferences() {
if(lowObstacle == null)
{
Transform foundObstacle = transform.Find("Jump Obstacle");
if(foundObstacle != null)
{
lowObstacle = foundObstacle;
}
}
if(slidePattern == null)
{
slidePattern = GetComponent<SlideObstaclePattern>();
}
if(ceilingPlatform == null)
{
Transform foundCeiling = transform.Find("Ceiling Platform");
if(foundCeiling != null)
{
ceilingPlatform = foundCeiling;
}
}
}
private void ApplyConfiguration() {
CacheReferences();
if(obstacleKind == ObstacleKind.Jump)
{
ApplyJumpPosition();
}
else if(slidePattern != null)
{
slidePattern.ConfigureLayout(reservedSlideLayout);
}
ApplyVisualSkin();
}
private void ApplyJumpPosition() {
if(lowObstacle == null)
{
return;
}
Vector3 localPosition = lowObstacle.localPosition;
localPosition.x = GetJumpObstacleX(reservedPlatformPattern);
lowObstacle.localPosition = localPosition;
}
private float GetJumpObstacleX(Platform.PlatformPattern platformPattern) {
switch(platformPattern)
{
case Platform.PlatformPattern.LowLeft:
return -0.44f;
case Platform.PlatformPattern.LowRight:
return 0.44f;
case Platform.PlatformPattern.LowMid:
default:
return 0f;
}
}
private void ApplyVisualSkin() {
#if UNITY_EDITOR
if(obstacleKind == ObstacleKind.Jump)
{
ApplySprite(lowObstacle, LoadSpriteAtPath(GetLowObstacleSkinPath(reservedSkin)));
return;
}
Sprite hangingSprite = LoadSpriteAtPath(GetHangingObstacleSkinPath(reservedSkin));
Sprite ceilingSprite = LoadSpriteAtPath(GetCeilingSkinPath(reservedSkin));
if(slidePattern != null && slidePattern.obstacles != null)
{
for(int i = 0; i < slidePattern.obstacles.Length; i++)
{
ApplySprite(slidePattern.obstacles[i], hangingSprite);
}
}
ApplySprite(ceilingPlatform, ceilingSprite);
#endif
}
#if UNITY_EDITOR
private void ApplySprite(Transform target, Sprite sprite) {
if(target == null || sprite == null)
{
return;
}
SpriteRenderer spriteRenderer = target.GetComponent<SpriteRenderer>();
if(spriteRenderer != null)
{
spriteRenderer.sprite = sprite;
}
}
private Sprite LoadSpriteAtPath(string assetPath) {
if(string.IsNullOrEmpty(assetPath))
{
return null;
}
return AssetDatabase.LoadAssetAtPath<Sprite>(assetPath);
}
private string GetLowObstacleSkinPath(Platform.PlatformSkin platformSkin) {
switch(platformSkin)
{
case Platform.PlatformSkin.Japan:
return "Assets/Sprites/ObstacleSkins/Skin_Obstacle_StoneLantern.png";
case Platform.PlatformSkin.China:
return "Assets/Sprites/ObstacleSkins/Skin_Obstacle_MarketCrate.png";
case Platform.PlatformSkin.USA:
return "Assets/Sprites/ObstacleSkins/Skin_Obstacle_Cone.png";
case Platform.PlatformSkin.Airport:
default:
return "Assets/Sprites/ObstacleSkins/Skin_Obstacle_Suitcases.png";
}
}
private string GetHangingObstacleSkinPath(Platform.PlatformSkin platformSkin) {
switch(platformSkin)
{
case Platform.PlatformSkin.Japan:
return "Assets/Sprites/ObstacleSkins/Skin_Hanging_RoadSign.png";
case Platform.PlatformSkin.China:
return "Assets/Sprites/ObstacleSkins/Skin_Hanging_RedLanterns.png";
case Platform.PlatformSkin.USA:
return "Assets/Sprites/ObstacleSkins/Skin_Hanging_Banner.png";
case Platform.PlatformSkin.Airport:
default:
return "Assets/Sprites/ObstacleSkins/Skin_Hanging_AirportSign.png";
}
}
private string GetCeilingSkinPath(Platform.PlatformSkin platformSkin) {
if(platformSkin == Platform.PlatformSkin.Airport)
{
return "Assets/Sprites/ObstacleSkins/Skin_Platform_AirportCeiling.png";
}
switch(platformSkin)
{
case Platform.PlatformSkin.Japan:
return "Assets/Sprites/ObstacleSkins/Skin_Platform_Japan.png";
case Platform.PlatformSkin.China:
return "Assets/Sprites/ObstacleSkins/Skin_Platform_China.png";
case Platform.PlatformSkin.USA:
return "Assets/Sprites/ObstacleSkins/Skin_Platform_USA.png";
case Platform.PlatformSkin.Airport:
default:
return "Assets/Sprites/ObstacleSkins/Skin_Platform_AirportFloorTile.png";
}
}
#endif
}