修正TargetUI初始化StayMode Button可使用的问题 修正TargetUI可使用按钮逻辑问题 修正TargetUI与TargetController联动问题 修正Enemy击杀后不死亡问题
130 lines
4.8 KiB
C#
130 lines
4.8 KiB
C#
using UnityEngine;
|
|
|
|
public class MouseInMap : MonoBehaviour
|
|
{
|
|
public Camera playCamera;
|
|
public GameObject environmentObj;
|
|
public GameObject mousePreviewObj;
|
|
public GameObject enemyContainerObj;
|
|
public GameObject sceneBlockContainerObj;
|
|
public GameObject targetControllerObj;
|
|
public GameObject HUDObj;
|
|
|
|
private Vector3 nowHitPosition = Vector3.zero;
|
|
private Vector3 nowHitPositionRelative = Vector3.zero;
|
|
private LayerMask groundMask;
|
|
private int randBlockNum;
|
|
private GameObject preSet;
|
|
private TargetController targetCon;
|
|
private MousePreview mousePreviewCon;
|
|
private EnemyContainer enemyCon;
|
|
private SceneBlockContainer sceneBlockCon;
|
|
private TargetUIController targetUICon;
|
|
|
|
public enum MouseMode
|
|
{
|
|
Default,
|
|
AttackSet,
|
|
GotoSet,
|
|
EnemySet
|
|
}
|
|
|
|
public MouseMode mouseMode = MouseMode.Default;
|
|
|
|
private void Start()
|
|
{
|
|
groundMask = LayerMask.GetMask("Ground");
|
|
targetCon = targetControllerObj.GetComponent<TargetController>();
|
|
mousePreviewCon = mousePreviewObj.GetComponent<MousePreview>();
|
|
enemyCon = enemyContainerObj.GetComponent<EnemyContainer>();
|
|
sceneBlockCon = sceneBlockContainerObj.GetComponent<SceneBlockContainer>();
|
|
targetUICon = HUDObj.GetComponent<TargetUIController>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
nowHitPosition = GetMouseOnMapPosition();
|
|
nowHitPositionRelative = nowHitPosition - environmentObj.transform.position;
|
|
// if mouse position in area, update mouseInMapPosition as nowHitPosition
|
|
if (nowHitPositionRelative.x < targetCon.maxAgentAreaX && nowHitPositionRelative.x > targetCon.minAgentAreaX && nowHitPositionRelative.z < targetCon.maxEnemyAreaZ && nowHitPositionRelative.z > targetCon.minAgentAreaZ)
|
|
{
|
|
mousePreviewCon.UpdatePreviewPosition(nowHitPosition);
|
|
// Mouse button R pressed
|
|
if (Input.GetMouseButtonDown(1))
|
|
{
|
|
switch (mouseMode)
|
|
{
|
|
case MouseMode.AttackSet:
|
|
sceneBlockCon.CreateNewBlock(SceneBlockContainer.Targets.Attack, randBlockNum, nowHitPositionRelative);
|
|
sceneBlockCon.InitializeBlock(environmentObj);
|
|
targetCon.AttackModeChange(nowHitPositionRelative);
|
|
ChangeMouseModeTo(MouseMode.Default);
|
|
break;
|
|
|
|
case MouseMode.GotoSet:
|
|
sceneBlockCon.CreateNewBlock(SceneBlockContainer.Targets.Go, randBlockNum, nowHitPositionRelative);
|
|
sceneBlockCon.InitializeBlock(environmentObj);
|
|
targetCon.GotoModeChange(nowHitPositionRelative);
|
|
ChangeMouseModeTo(MouseMode.Default);
|
|
break;
|
|
|
|
case MouseMode.EnemySet:
|
|
enemyCon.InitEnemyAtHere(new Vector3(nowHitPositionRelative.x, 1, nowHitPositionRelative.z));
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ChangeMouseModeTo(MouseMode thisMouseMode)
|
|
{
|
|
mouseMode = thisMouseMode;
|
|
switch (thisMouseMode)
|
|
{
|
|
case MouseMode.AttackSet:
|
|
// random choose attack scene block type and set as preview
|
|
randBlockNum = Random.Range(0, sceneBlockCon.attackBlockPrefabs.Length);
|
|
preSet = sceneBlockCon.attackBlockPrefabs[randBlockNum];
|
|
mousePreviewCon.ChangePreviewTo(preSet);
|
|
break;
|
|
|
|
case MouseMode.GotoSet:
|
|
// random choose Goto scene block type and set as preview
|
|
randBlockNum = Random.Range(0, sceneBlockCon.goBlockPrefabs.Length);
|
|
preSet = sceneBlockCon.goBlockPrefabs[randBlockNum];
|
|
mousePreviewCon.ChangePreviewTo(preSet);
|
|
break;
|
|
|
|
case MouseMode.EnemySet:
|
|
preSet = enemyCon.enemyPrefab;
|
|
mousePreviewCon.ChangePreviewTo(preSet);
|
|
break;
|
|
|
|
default:
|
|
mousePreviewCon.DeleteAllPreviewModele();
|
|
break;
|
|
}
|
|
}
|
|
|
|
// get mouse position on map, return an absolute Vector3 coordinate
|
|
public Vector3 GetMouseOnMapPosition()
|
|
{
|
|
// 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
|
|
Debug.DrawRay(ray.origin, ray.direction * 100, Color.red);
|
|
return thisHit.point;
|
|
}
|
|
else
|
|
{
|
|
return Vector3.zero;
|
|
}
|
|
}
|
|
} |