2023-08-15 10:47:14 +00:00
|
|
|
using System.Collections.Generic;
|
2023-04-09 14:35:38 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class MouseInMap : MonoBehaviour
|
|
|
|
{
|
2023-08-08 16:11:25 +00:00
|
|
|
public float targetDistanceThreshold = 6f;
|
|
|
|
public float enemyDistanceThreshold = 1f;
|
|
|
|
|
2023-04-09 14:35:38 +00:00
|
|
|
public Camera playCamera;
|
2023-08-08 16:11:25 +00:00
|
|
|
public GameObject AgentObj;
|
2023-06-30 09:30:12 +00:00
|
|
|
public GameObject environmentObj;
|
2023-07-28 10:44:02 +00:00
|
|
|
public GameObject mousePreviewObj;
|
2023-06-30 09:30:12 +00:00
|
|
|
public GameObject enemyContainerObj;
|
|
|
|
public GameObject sceneBlockContainerObj;
|
|
|
|
public GameObject targetControllerObj;
|
2023-04-09 14:35:38 +00:00
|
|
|
public GameObject HUDObj;
|
|
|
|
|
|
|
|
private Vector3 nowHitPosition = Vector3.zero;
|
2023-07-28 10:44:02 +00:00
|
|
|
private Vector3 nowHitPositionRelative = Vector3.zero;
|
2023-04-09 14:35:38 +00:00
|
|
|
private LayerMask groundMask;
|
|
|
|
private int randBlockNum;
|
|
|
|
private GameObject preSet;
|
2023-06-29 06:18:10 +00:00
|
|
|
private TargetController targetCon;
|
2023-04-09 14:35:38 +00:00
|
|
|
private MousePreview mousePreviewCon;
|
|
|
|
private EnemyContainer enemyCon;
|
|
|
|
private SceneBlockContainer sceneBlockCon;
|
2023-08-08 16:11:25 +00:00
|
|
|
private MessageBoxController messageCon;
|
2023-04-09 14:35:38 +00:00
|
|
|
private TargetUIController targetUICon;
|
|
|
|
|
|
|
|
public enum MouseMode
|
|
|
|
{
|
|
|
|
Default,
|
|
|
|
AttackSet,
|
|
|
|
GotoSet,
|
|
|
|
EnemySet
|
|
|
|
}
|
2023-06-30 09:30:12 +00:00
|
|
|
|
2023-04-09 14:35:38 +00:00
|
|
|
public MouseMode mouseMode = MouseMode.Default;
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
{
|
|
|
|
groundMask = LayerMask.GetMask("Ground");
|
2023-06-30 09:30:12 +00:00
|
|
|
targetCon = targetControllerObj.GetComponent<TargetController>();
|
2023-07-28 10:44:02 +00:00
|
|
|
mousePreviewCon = mousePreviewObj.GetComponent<MousePreview>();
|
2023-06-30 09:30:12 +00:00
|
|
|
enemyCon = enemyContainerObj.GetComponent<EnemyContainer>();
|
|
|
|
sceneBlockCon = sceneBlockContainerObj.GetComponent<SceneBlockContainer>();
|
2023-04-09 14:35:38 +00:00
|
|
|
targetUICon = HUDObj.GetComponent<TargetUIController>();
|
2023-08-08 16:11:25 +00:00
|
|
|
messageCon = HUDObj.GetComponent<MessageBoxController>();
|
2023-04-09 14:35:38 +00:00
|
|
|
}
|
|
|
|
|
2023-06-30 09:30:12 +00:00
|
|
|
private void Update()
|
2023-04-09 14:35:38 +00:00
|
|
|
{
|
2023-06-30 09:30:12 +00:00
|
|
|
nowHitPosition = GetMouseOnMapPosition();
|
2023-07-28 10:44:02 +00:00
|
|
|
nowHitPositionRelative = nowHitPosition - environmentObj.transform.position;
|
2023-04-09 14:35:38 +00:00
|
|
|
// if mouse position in area, update mouseInMapPosition as nowHitPosition
|
2023-07-28 10:44:02 +00:00
|
|
|
if (nowHitPositionRelative.x < targetCon.maxAgentAreaX && nowHitPositionRelative.x > targetCon.minAgentAreaX && nowHitPositionRelative.z < targetCon.maxEnemyAreaZ && nowHitPositionRelative.z > targetCon.minAgentAreaZ)
|
2023-04-09 14:35:38 +00:00
|
|
|
{
|
2023-07-28 10:44:02 +00:00
|
|
|
mousePreviewCon.UpdatePreviewPosition(nowHitPosition);
|
2023-04-09 14:35:38 +00:00
|
|
|
// Mouse button R pressed
|
|
|
|
if (Input.GetMouseButtonDown(1))
|
|
|
|
{
|
2023-06-30 09:30:12 +00:00
|
|
|
switch (mouseMode)
|
2023-04-09 14:35:38 +00:00
|
|
|
{
|
|
|
|
case MouseMode.AttackSet:
|
2023-08-08 16:11:25 +00:00
|
|
|
if (IsAgentorEnemyWithinDistance(targetDistanceThreshold))
|
|
|
|
{
|
|
|
|
// if agent or enemy is nearby, do not create new block
|
2023-08-15 10:47:14 +00:00
|
|
|
messageCon.PushMessage(new List<string> { "Agent or Enemy is too close!" },
|
|
|
|
new List<string> { "#800000ff" });
|
2023-08-08 16:11:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if agent or enemy is not nearby, create new block
|
|
|
|
sceneBlockCon.CreateNewBlock(SceneBlockContainer.Targets.Attack, randBlockNum, nowHitPositionRelative);
|
|
|
|
sceneBlockCon.InitializeBlock(environmentObj);
|
|
|
|
targetCon.AttackModeChange(nowHitPositionRelative);
|
|
|
|
ChangeMouseModeTo(MouseMode.Default);
|
|
|
|
}
|
2023-04-09 14:35:38 +00:00
|
|
|
break;
|
2023-06-30 09:30:12 +00:00
|
|
|
|
2023-04-09 14:35:38 +00:00
|
|
|
case MouseMode.GotoSet:
|
2023-08-08 16:11:25 +00:00
|
|
|
if (IsAgentorEnemyWithinDistance(targetDistanceThreshold))
|
|
|
|
{
|
|
|
|
// if agent or enemy is nearby, do not create new block
|
2023-08-15 10:47:14 +00:00
|
|
|
messageCon.PushMessage(new List<string> { "Agent or Enemy is too close!" },
|
|
|
|
new List<string> { "#800000ff" });
|
2023-08-08 16:11:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if agent or enemy is not nearby, create new block
|
|
|
|
sceneBlockCon.CreateNewBlock(SceneBlockContainer.Targets.Go, randBlockNum, nowHitPositionRelative);
|
|
|
|
sceneBlockCon.InitializeBlock(environmentObj);
|
|
|
|
targetCon.GotoModeChange(nowHitPositionRelative);
|
|
|
|
ChangeMouseModeTo(MouseMode.Default);
|
|
|
|
}
|
2023-04-09 14:35:38 +00:00
|
|
|
break;
|
2023-06-30 09:30:12 +00:00
|
|
|
|
2023-04-09 14:35:38 +00:00
|
|
|
case MouseMode.EnemySet:
|
2023-08-08 16:11:25 +00:00
|
|
|
if (IsAgentorEnemyWithinDistance(enemyDistanceThreshold))
|
|
|
|
{
|
2023-08-15 10:47:14 +00:00
|
|
|
messageCon.PushMessage(new List<string> { "Agent or Enemy is too close!" },
|
|
|
|
new List<string> { "#800000ff" });
|
2023-08-08 16:11:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
enemyCon.InitEnemyAtHere(new Vector3(nowHitPositionRelative.x, 1, nowHitPositionRelative.z));
|
|
|
|
}
|
2023-04-09 14:35:38 +00:00
|
|
|
break;
|
2023-06-30 09:30:12 +00:00
|
|
|
|
2023-04-09 14:35:38 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-30 09:30:12 +00:00
|
|
|
public void ChangeMouseModeTo(MouseMode thisMouseMode)
|
2023-04-09 14:35:38 +00:00
|
|
|
{
|
|
|
|
mouseMode = thisMouseMode;
|
|
|
|
switch (thisMouseMode)
|
|
|
|
{
|
|
|
|
case MouseMode.AttackSet:
|
|
|
|
// random choose attack scene block type and set as preview
|
|
|
|
randBlockNum = Random.Range(0, sceneBlockCon.attackBlockPrefabs.Length);
|
2023-06-29 06:18:10 +00:00
|
|
|
preSet = sceneBlockCon.attackBlockPrefabs[randBlockNum];
|
2023-06-30 09:30:12 +00:00
|
|
|
mousePreviewCon.ChangePreviewTo(preSet);
|
2023-04-09 14:35:38 +00:00
|
|
|
break;
|
2023-06-30 09:30:12 +00:00
|
|
|
|
2023-04-09 14:35:38 +00:00
|
|
|
case MouseMode.GotoSet:
|
|
|
|
// random choose Goto scene block type and set as preview
|
|
|
|
randBlockNum = Random.Range(0, sceneBlockCon.goBlockPrefabs.Length);
|
|
|
|
preSet = sceneBlockCon.goBlockPrefabs[randBlockNum];
|
2023-06-30 09:30:12 +00:00
|
|
|
mousePreviewCon.ChangePreviewTo(preSet);
|
2023-04-09 14:35:38 +00:00
|
|
|
break;
|
2023-06-30 09:30:12 +00:00
|
|
|
|
2023-04-09 14:35:38 +00:00
|
|
|
case MouseMode.EnemySet:
|
|
|
|
preSet = enemyCon.enemyPrefab;
|
2023-08-15 10:47:14 +00:00
|
|
|
mousePreviewCon.ChangePreviewTo(preSet, true);
|
2023-04-09 14:35:38 +00:00
|
|
|
break;
|
2023-06-30 09:30:12 +00:00
|
|
|
|
2023-04-09 14:35:38 +00:00
|
|
|
default:
|
2023-06-30 09:30:12 +00:00
|
|
|
mousePreviewCon.DeleteAllPreviewModele();
|
2023-04-09 14:35:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-28 10:44:02 +00:00
|
|
|
// get mouse position on map, return an absolute Vector3 coordinate
|
2023-06-30 09:30:12 +00:00
|
|
|
public Vector3 GetMouseOnMapPosition()
|
2023-04-09 14:35:38 +00:00
|
|
|
{
|
|
|
|
// shoot raycast from mainCamera center to mousepositon
|
|
|
|
RaycastHit thisHit;
|
|
|
|
Ray ray = playCamera.ScreenPointToRay(Input.mousePosition);
|
|
|
|
// if raycast hit gameobject
|
|
|
|
if (Physics.Raycast(ray, out thisHit, Mathf.Infinity, groundMask))
|
|
|
|
{
|
|
|
|
//draw raycast
|
2023-07-28 10:44:02 +00:00
|
|
|
Debug.DrawRay(ray.origin, ray.direction * 100, Color.red);
|
|
|
|
return thisHit.point;
|
2023-04-09 14:35:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Vector3.zero;
|
|
|
|
}
|
|
|
|
}
|
2023-08-08 16:11:25 +00:00
|
|
|
|
|
|
|
private bool IsAgentorEnemyWithinDistance(float distance)
|
|
|
|
{
|
|
|
|
// check all child object in enemyContainerObj
|
|
|
|
// if any child object is near by nowHitPosition in 10, return true
|
|
|
|
Vector3 playerPosition = Vector3.zero;
|
|
|
|
for (int i = 0; i < enemyContainerObj.transform.childCount; i++)
|
|
|
|
{
|
|
|
|
//set enemy Position's y as 0
|
|
|
|
playerPosition = enemyContainerObj.transform.GetChild(i).position;
|
|
|
|
playerPosition.y = 0;
|
|
|
|
// check if enemy near by nowHitPositionRelative in distance, return true
|
|
|
|
if (Vector3.Distance(playerPosition, nowHitPosition) < distance)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// set agentObj position's y as 0
|
|
|
|
playerPosition = AgentObj.transform.position;
|
|
|
|
playerPosition.y = 0;
|
|
|
|
// check if agentObj near by nowHitPositionRelative in distance, return true
|
|
|
|
if (Vector3.Distance(playerPosition, nowHitPosition) < distance)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// if no enemy or agent near by, return false
|
|
|
|
return false;
|
|
|
|
}
|
2023-06-30 09:30:12 +00:00
|
|
|
}
|