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;
|
2023-08-22 17:58:50 +00:00
|
|
|
private int blockNum;
|
|
|
|
private GameObject previewModel;
|
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;
|
|
|
|
|
2023-08-22 17:58:50 +00:00
|
|
|
private const string ErrorMessageColor = "#800000ff";
|
|
|
|
private const string LevelTooHighError = "[Error]MouseInMap:ChangeBlockLevelTo:";
|
|
|
|
private const string AttackLevelErrorMsg = "Attack Level is too high!";
|
|
|
|
private const string GotoLevelErrorMsg = "Goto Level is too high!";
|
|
|
|
|
2023-04-09 14:35:38 +00:00
|
|
|
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;
|
2023-08-22 17:58:50 +00:00
|
|
|
public int blockLevel = 0;
|
2023-04-09 14:35:38 +00:00
|
|
|
|
|
|
|
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!" },
|
2023-08-22 17:58:50 +00:00
|
|
|
new List<string> { messageCon.errorColor });
|
2023-08-08 16:11:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if agent or enemy is not nearby, create new block
|
2023-08-22 17:58:50 +00:00
|
|
|
sceneBlockCon.CreateNewBlock(SceneBlockContainer.Targets.Attack, blockLevel, blockNum, nowHitPositionRelative);
|
2023-08-08 16:11:25 +00:00
|
|
|
sceneBlockCon.InitializeBlock(environmentObj);
|
|
|
|
targetCon.AttackModeChange(nowHitPositionRelative);
|
2023-08-22 17:58:50 +00:00
|
|
|
ChangeMouseMode(MouseMode.Default);
|
2023-08-08 16:11:25 +00:00
|
|
|
}
|
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!" },
|
2023-08-22 17:58:50 +00:00
|
|
|
new List<string> { messageCon.errorColor });
|
2023-08-08 16:11:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// if agent or enemy is not nearby, create new block
|
2023-08-22 17:58:50 +00:00
|
|
|
sceneBlockCon.CreateNewBlock(SceneBlockContainer.Targets.Go, blockLevel, blockNum, nowHitPositionRelative);
|
2023-08-08 16:11:25 +00:00
|
|
|
sceneBlockCon.InitializeBlock(environmentObj);
|
|
|
|
targetCon.GotoModeChange(nowHitPositionRelative);
|
2023-08-22 17:58:50 +00:00
|
|
|
ChangeMouseMode(MouseMode.Default);
|
2023-08-08 16:11:25 +00:00
|
|
|
}
|
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!" },
|
2023-08-22 17:58:50 +00:00
|
|
|
new List<string> { messageCon.errorColor });
|
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-08-22 17:58:50 +00:00
|
|
|
// change mouse mode, level and PreviewModel
|
|
|
|
public void ChangeMouseMode(MouseMode mouseMode, int level = -1, int blockNum = -1)
|
2023-04-09 14:35:38 +00:00
|
|
|
{
|
2023-08-22 17:58:50 +00:00
|
|
|
this.mouseMode = mouseMode;
|
|
|
|
SceneBlockContainer.Targets nowTargetType;
|
|
|
|
switch (mouseMode)
|
2023-04-09 14:35:38 +00:00
|
|
|
{
|
|
|
|
case MouseMode.AttackSet:
|
|
|
|
case MouseMode.GotoSet:
|
2023-08-22 17:58:50 +00:00
|
|
|
// set target type
|
|
|
|
nowTargetType = GetTargetTypeByMouseMode(mouseMode);
|
|
|
|
// set blockLevel or send error message
|
|
|
|
if (level < 0)
|
|
|
|
{
|
|
|
|
// while blockLevel is not set, send error message
|
|
|
|
messageCon.PushMessage(new List<string> { "[ERROR]MouseInMap:ChangeMouseMode:", "Level not set!", "mouseMode=", mouseMode.ToString() },
|
|
|
|
new List<string> { messageCon.errorColor });
|
|
|
|
blockLevel = sceneBlockCon.scenePrefabSet.GetLevelNumber(nowTargetType);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// while blockLevel is set
|
|
|
|
blockLevel = level;
|
|
|
|
}
|
|
|
|
blockLevel = level;
|
|
|
|
// set blockNum or random choose attack scene block type
|
|
|
|
if (blockNum < 0)
|
|
|
|
{
|
|
|
|
// while blockNum is not set, random choose block type
|
|
|
|
this.blockNum = Random.Range(0, sceneBlockCon.scenePrefabSet.GetBlockNumber(nowTargetType, blockLevel));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// while blockNum is set
|
|
|
|
this.blockNum = blockNum;
|
|
|
|
}
|
|
|
|
// set previewModel
|
|
|
|
previewModel = sceneBlockCon.scenePrefabSet.GetPrefab(nowTargetType, blockLevel, this.blockNum);
|
|
|
|
mousePreviewCon.ChangePreviewTo(previewModel);
|
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-22 17:58:50 +00:00
|
|
|
// set enemy as preview
|
|
|
|
previewModel = enemyCon.enemyPrefab;
|
|
|
|
mousePreviewCon.ChangePreviewTo(previewModel, 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-08-22 17:58:50 +00:00
|
|
|
// default mode, delete all preview model
|
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
|
2023-08-22 17:58:50 +00:00
|
|
|
RaycastHit nowHit;
|
2023-04-09 14:35:38 +00:00
|
|
|
Ray ray = playCamera.ScreenPointToRay(Input.mousePosition);
|
|
|
|
// if raycast hit gameobject
|
2023-08-22 17:58:50 +00:00
|
|
|
if (Physics.Raycast(ray, out nowHit, Mathf.Infinity, groundMask))
|
2023-04-09 14:35:38 +00:00
|
|
|
{
|
|
|
|
//draw raycast
|
2023-07-28 10:44:02 +00:00
|
|
|
Debug.DrawRay(ray.origin, ray.direction * 100, Color.red);
|
2023-08-22 17:58:50 +00:00
|
|
|
return nowHit.point;
|
2023-04-09 14:35:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Vector3.zero;
|
|
|
|
}
|
|
|
|
}
|
2023-08-08 16:11:25 +00:00
|
|
|
|
2023-08-22 17:58:50 +00:00
|
|
|
// check if agent or enemy is too close to nowHitPosition
|
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-08-22 17:58:50 +00:00
|
|
|
|
|
|
|
// change to TargetType from MouseMode
|
|
|
|
private SceneBlockContainer.Targets GetTargetTypeByMouseMode(MouseMode mode)
|
|
|
|
{
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case MouseMode.AttackSet:
|
|
|
|
return SceneBlockContainer.Targets.Attack;
|
|
|
|
|
|
|
|
case MouseMode.GotoSet:
|
|
|
|
return SceneBlockContainer.Targets.Go;
|
|
|
|
|
|
|
|
default:
|
|
|
|
messageCon.PushMessage(new List<string> { "[Error]MouseInMap:GetTargetTypeByMouseMode:", "Mouse Mode not found!", "mode=", mode.ToString() },
|
|
|
|
new List<string> { messageCon.errorColor });
|
|
|
|
return SceneBlockContainer.Targets.Num;
|
|
|
|
}
|
|
|
|
}
|
2023-06-30 09:30:12 +00:00
|
|
|
}
|