タップされたら購入に向かう処理を修正

This commit is contained in:
kimura 2021-09-08 12:37:10 +09:00
parent aeae4ad8f6
commit 99f11ac6dc
2 changed files with 79 additions and 55 deletions

View File

@ -21,6 +21,7 @@ public enum CustomerMovingType
public enum CustomerState
{
Walk,
WalkShop,
Wait,
Order,
Leave,
@ -68,6 +69,7 @@ public class CustomerController : MonoBehaviour
private Vector3 beginPos;
private Vector3 wayPoint;
private CustomerMovingType currentMovingType;
private bool isMoveOverride;
private List<(CustomerMovingType type, float duration)> moves = new List<(CustomerMovingType, float)>();
private float speed = 0f;
private float completedDuration;
@ -96,8 +98,12 @@ public class CustomerController : MonoBehaviour
durationDelta += Time.deltaTime;
return;
}
durationDelta = 0f;
moveEndSubject.Value = currentMovingType;
if (isMoveOverride)
{
isMoveOverride = false;
return;
}
if (moves.Count > 0)
{
var move = moves[0];
@ -115,7 +121,9 @@ public class CustomerController : MonoBehaviour
beginPos.x = leftEndPosision * walkSideDirection;
beginPos.y = Random.Range(walkSideMidPos, walkSideBottomPos);
transform.localPosition = beginPos;
currentMovingType = CustomerMovingType.StayBackOrder;
currentMovingType = CustomerMovingType.StayBack;
moveEndSubject.Value = CustomerMovingType.StayBack;
isMoveOverride = false;
SetWayPoint(beginPos);
}
@ -126,25 +134,19 @@ public class CustomerController : MonoBehaviour
switch (state)
{
case CustomerState.Walk:
SetMove(CustomerMovingType.WalkCenter);
ForceSetMove(CustomerMovingType.WalkCenter);
// 数秒立ち止まる
AddMove(CustomerMovingType.StayBack, 2f);
AddMove(CustomerMovingType.WalkSide);
break;
case CustomerState.WalkShop:
ForceSetMove(CustomerMovingType.WalkCenter);
break;
case CustomerState.Wait:
// 店に向かう&順番待ち
// 近くの場合その場に待機
// まだ遠い場合待機場所に移動
moves.Clear();
if (currentMovingType == CustomerMovingType.StayBack)
{
SetMove(CustomerMovingType.WalkBackHalf);
}
else
{
SetMove(CustomerMovingType.WalkCenter);
AddMove(CustomerMovingType.WalkBackHalf);
}
ForceSetMove(CustomerMovingType.WalkBackHalf);
AddMove(CustomerMovingType.StayBack);
break;
case CustomerState.Order:
@ -153,8 +155,7 @@ public class CustomerController : MonoBehaviour
AddMove(CustomerMovingType.StayBackOrder);
break;
case CustomerState.Leave:
moves.Clear();
SetMove(CustomerMovingType.WalkSide);
ForceSetMove(CustomerMovingType.WalkSide);
break;
case CustomerState.EatingLeave:
AddMove(CustomerMovingType.WalkFrontEat);
@ -170,6 +171,13 @@ public class CustomerController : MonoBehaviour
moves.Add((type, duration));
}
private void ForceSetMove(CustomerMovingType type, float duration = 0f)
{
moves.Clear();
isMoveOverride = true;
SetMove(type, duration);
}
// キャラの奥行き描画順のため、y方向に対応したz軸を設定
private void SetMove(CustomerMovingType type, float duration = 0f)
{
@ -178,10 +186,11 @@ public class CustomerController : MonoBehaviour
return;
}
completedDuration = duration;
durationDelta = 0f;
switch (type)
{
case CustomerMovingType.WalkSide:
SetWayPoint(new Vector3(-leftEndPosision * walkSideDirection, wayPoint.y));
SetWayPoint(new Vector3(-leftEndPosision * walkSideDirection, transform.localPosition.y));
speed = walkSideSpeed;
// アニメーション違和感回避
if (currentMovingType == CustomerMovingType.WalkCenter)
@ -191,13 +200,11 @@ public class CustomerController : MonoBehaviour
animator.SetTrigger(WalkSide);
break;
case CustomerMovingType.WalkSideEat:
SetWayPoint(new Vector3(-leftEndPosision * walkSideDirection, wayPoint.y));
SetWayPoint(new Vector3(-leftEndPosision * walkSideDirection, transform.localPosition.y));
speed = walkSideSpeed;
animator.SetTrigger(WalkSideEat);
break;
case CustomerMovingType.WalkCenter:
// 通り過ぎた人はwalkSideDirection基準にするとあるき過ぎるので改善したい
// 立ち止まったあと戻ってしまう場合があるので、中央付近だと戻りがないようにする
if (Mathf.Abs(transform.localPosition.x - stopPosision) <= stopPositionRange)
{
SetWayPoint(new Vector3(transform.localPosition.x, beginPos.y));
@ -224,18 +231,25 @@ public class CustomerController : MonoBehaviour
animator.SetTrigger(StayBack);
break;
case CustomerMovingType.WalkBack:
// 購入場所に行く
SetWayPoint(new Vector3(Random.value * orderPositionRange * walkSideDirection, orderPosision));
speed = walkFrontBackSpeed;
animator.SetTrigger(WalkBack);
break;
case CustomerMovingType.WalkBackHalf:
// 待機列に入る
SetWayPoint(new Vector3(wayPoint.x, waitOrderPosision));
if (Mathf.Abs(transform.localPosition.x - stopPosision) <= stopPositionRange)
{
SetWayPoint(new Vector3(transform.localPosition.x, waitOrderPosision));
}
else
{
SetWayPoint(new Vector3(stopPosision - Random.value * stopPositionRange * walkSideDirection, waitOrderPosision));
}
speed = walkFrontBackSpeed;
animator.SetTrigger(WalkBack);
break;
case CustomerMovingType.WalkFront:
// 購入場所に行く
SetWayPoint(new Vector3(wayPoint.x, Random.Range(walkSideLeavePos, walkSideTopPosition)));
speed = walkFrontBackSpeed;
animator.SetTrigger(WalkFront);

View File

@ -115,38 +115,31 @@ public class Market : MonoBehaviour
.Select(x => x.Value)
.Subscribe(customerController =>
{
customerController.State.Subscribe(c =>
{
if (c == CustomerState.EatingLeave)
customerController.State
.Where(x => x == CustomerState.EatingLeave)
.Subscribe(c =>
{
Debug.Log($"aa order:{customerList.Count(x => x.State.Value == CustomerState.Order)} EatingLeave");
orderSubject.OnNext(default);
}
}).AddTo(customerController);
}).AddTo(customerController);
if (shopState.Value == ShopState.Close)
{
customerController.ChangeCustomerState(CustomerState.Leave);
return;
}
// 店に向かう
customerController.ChangeCustomerState(CustomerState.Wait);
shopState.Subscribe(x =>
{
switch (x)
shopState
.Where(x => x == ShopState.Close)
.Subscribe(x =>
{
case ShopState.Open:
case ShopState.Busy:
break;
case ShopState.Close:
// 売り切れの場合帰る
if (customerController.State.Value == CustomerState.Order)
{
return;
}
this.CallWaitForSeconds(Random.Range(0f, .4f), () =>
{
customerController.ChangeCustomerState(CustomerState.Leave);
});
break;
default:
throw new ArgumentOutOfRangeException(nameof(x), x, null);
}
}).AddTo(customerController);
// 売り切れの場合帰る
if (customerController.State.Value == CustomerState.Order)
{
return;
}
customerController.ChangeCustomerState(CustomerState.Leave);
}).AddTo(customerController);
}).AddTo(this);
requestSubject.BatchFrame().Subscribe(customers =>
@ -356,6 +349,16 @@ public class Market : MonoBehaviour
customerList.Remove(customerController);
Destroy(customer);
break;
case CustomerMovingType.WalkCenter:
if (shopState.Value == ShopState.Close)
{
customerController.ChangeCustomerState(CustomerState.Leave);
}
if (customerController.State.Value == CustomerState.WalkShop)
{
customerList.Add(customerController);
}
break;
case CustomerMovingType.WalkBackHalf:
waitCustomerList.Add(customerController);
break;
@ -368,20 +371,27 @@ public class Market : MonoBehaviour
}).AddTo(customerController);
var customerObject = customer.transform.GetChild(0).gameObject;
var eventTrigger = customerObject.AddComponent<ObservableEventTrigger>();
if (x.isCustomer)
{
// 購入客リスト追加
customerList.Add(customerController);
if (customerObject.TryGetComponent(typeof(Collider2D), out var target))
{
Destroy(target);
}
// 近くまで歩く(タップされたらcustomerList.Add()
customerController.ChangeCustomerState(CustomerState.WalkShop);
eventTrigger.OnPointerClickAsObservable()
.TakeUntil(customerController.MoveEndObservable.Where(type => type == CustomerMovingType.WalkCenter))
.Take(1)
.Subscribe(_ =>
{
customerList.Add(customerController);
if (customerObject.TryGetComponent(typeof(Collider2D), out var target))
{
Destroy(target);
}
}).AddTo(customerController);
}
else
{
// 歩行者はタップ後購入客
// 歩行者はタップ後customerList.Add()
customerController.ChangeCustomerState(CustomerState.Walk);
var eventTrigger = customerObject.AddComponent<ObservableEventTrigger>();
eventTrigger.OnPointerClickAsObservable().Take(1).Subscribe(_ =>
{
customerList.Add(customerController);