2023-04-09 14:35:38 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
public class MouseInMap : MonoBehaviour
|
|
|
|
{
|
|
|
|
public Camera playCamera;
|
2023-06-30 09:30:12 +00:00
|
|
|
public GameObject environmentObj;
|
2023-04-09 14:35:38 +00:00
|
|
|
public GameObject selectEffect;
|
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 mouseInMapPosition = Vector3.zero;
|
|
|
|
private Vector3 nowHitPosition = Vector3.zero;
|
|
|
|
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;
|
|
|
|
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-04-09 14:35:38 +00:00
|
|
|
mousePreviewCon = this.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-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-04-09 14:35:38 +00:00
|
|
|
// if mouse position in area, update mouseInMapPosition as nowHitPosition
|
2023-06-29 06:18:10 +00:00
|
|
|
if (nowHitPosition.x < targetCon.maxAgentAreaX && nowHitPosition.x > targetCon.minAgentAreaX && nowHitPosition.z < targetCon.maxEnemyAreaZ && nowHitPosition.z > targetCon.minAgentAreaZ)
|
2023-04-09 14:35:38 +00:00
|
|
|
{
|
|
|
|
mouseInMapPosition = nowHitPosition;
|
2023-06-30 09:30:12 +00:00
|
|
|
mousePreviewCon.UpdatePreviewPosition(mouseInMapPosition);
|
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-06-30 09:30:12 +00:00
|
|
|
sceneBlockCon.CreateNewBlock(SceneBlockContainer.Targets.Attack, randBlockNum, mouseInMapPosition);
|
|
|
|
sceneBlockCon.InitializeBlock(environmentObj);
|
|
|
|
targetCon.AttackModeChange();
|
|
|
|
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-06-30 09:30:12 +00:00
|
|
|
sceneBlockCon.CreateNewBlock(SceneBlockContainer.Targets.Go, randBlockNum, mouseInMapPosition);
|
|
|
|
sceneBlockCon.InitializeBlock(environmentObj);
|
|
|
|
targetCon.GotoModeChange();
|
|
|
|
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-06-30 09:30:12 +00:00
|
|
|
enemyCon.InitEnemyAtHere(new Vector3(mouseInMapPosition.x, 1, mouseInMapPosition.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-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
|
|
|
default:
|
2023-06-30 09:30:12 +00:00
|
|
|
mousePreviewCon.DeleteAllPreviewModele();
|
2023-04-09 14:35:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get mouse position on map, return a Vector3
|
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;
|
|
|
|
Color rayColor = Color.red;
|
|
|
|
Vector3 onMapPosition;
|
|
|
|
Ray ray = playCamera.ScreenPointToRay(Input.mousePosition);
|
|
|
|
// if raycast hit gameobject
|
|
|
|
if (Physics.Raycast(ray, out thisHit, Mathf.Infinity, groundMask))
|
|
|
|
{
|
|
|
|
//draw raycast
|
|
|
|
Debug.DrawRay(ray.origin, ray.direction * 100, rayColor);
|
|
|
|
return onMapPosition = thisHit.point;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return Vector3.zero;
|
|
|
|
}
|
|
|
|
}
|
2023-06-30 09:30:12 +00:00
|
|
|
}
|