public List<UnitBase> selectedUnits = new List<UnitBase>();
public void Init()
{
GameManager.Instance.OnDayStart += StartDay;
GameManager.Instance.OnNightStart += StartNight;
GameManager.Instance.OnEveningStart += StartNight;
isActive = true;
}
private void Update()
{
if(!isActive) return;
if (GameManager.Instance.DeckController.handController.IsDragging || GameManager.Instance.VillagersPlacer.dragging)
{
isSelecting = false;
isDragging = false;
return;
}
if (Input.GetMouseButtonDown(0) && !isDragging)
{
if (currentBuilding != null)
{
var placerHelper = currentBuilding.transform.GetComponent<PlacerHelper>();
placerHelper.EnableOutline(false);
}
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, buildingLayerMask) && isDay)
{
if (currentBuilding != null)
{
currentBuilding.showingPanel = false;
}
currentBuilding = hit.transform.GetComponentInParent<BuildingBase>();
if (currentBuilding.IsBuilded && !currentBuilding.IsDestroyed && currentBuilding.CanStartPanel)
{
hit.transform.GetComponentInParent<PlacerHelper>().EnableOutline(true);
currentBuilding.InitPanel();
}
}
}
// On mouse down, save the initial position
if (Input.GetMouseButtonDown(0))
{
isSelecting = true;
isDragging = false; // Reset the dragging flag
mousePosition1 = Input.mousePosition;
if (selectedUnits.Count > 0)
{
foreach (var unit in selectedUnits)
{
unit.EnableHighlight(false);
}
selectedUnits.Clear();
}
}
if (selectedUnits.Count > 0)
{
if (Input.GetMouseButtonDown(0))
{
foreach (var unit in selectedUnits)
{
unit.EnableHighlight(false);
}
selectedUnits.Clear();
}
else if(Input.GetMouseButtonDown(1))
{
MoveUnits();
}
}
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, unitLayerMask))
{
Debug.Log(hit.transform.gameObject);
UnitBase unitBase = hit.transform.gameObject.GetComponent<UnitBase>();
if (unitBase != null)
{
unitBase.EnableHighlight(true);
if(!selectedUnits.Contains(unitBase)) selectedUnits.Add(unitBase);
}
}
}
// Check if mouse is being dragged
if (isSelecting && (Input.mousePosition - mousePosition1).magnitude > minDragDistance)
{
isDragging = true;
//If dragging disable panel
if (currentBuilding != null)
{
var placerHelper = currentBuilding.transform.GetComponent<PlacerHelper>();
placerHelper.EnableOutline(false);
GameManager.Instance.BuildingPanelManager.DisableAllPanels();
}
}
// On mouse up, clear the selecting status and perform selection if dragged
if (Input.GetMouseButtonUp(0))
{
isSelecting = false;
if (isDragging)
{
PerformSelection();
isDragging = false; // Reset the dragging flag
}
}
}
public void EnableSelector(bool enabled)
{
isActive = enabled;
if (!enabled)
{
isDragging = false;
}
}
private void MoveUnits()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity, terrainLayerMask))
{
Vector3 hitPoint = hit.point;
CalculateFormation(hitPoint);
}
}
private void OnGUI()
{
// Draw the selection rectangle if the user is dragging the mouse
if (isDragging)
{
Rect rect = GetScreenRect(mousePosition1, Input.mousePosition);
DrawScreenRect(rect, new Color(0.8f, 0.8f, 0.95f, 0.25f)); // Tint color and alpha
DrawScreenRectBorder(rect, 2, Color.white);
}
}
// Utility function to create a rectangle
public static Rect GetScreenRect(Vector3 screenPosition1, Vector3 screenPosition2)
{
// Move origin from bottom left to top left
screenPosition1.y = Screen.height - screenPosition1.y;
screenPosition2.y = Screen.height - screenPosition2.y;
// Calculate corners
var topLeft = Vector3.Min(screenPosition1, screenPosition2);
var bottomRight = Vector3.Max(screenPosition1, screenPosition2);
// Create the rect
return Rect.MinMaxRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
}
// Utility function to draw a rectangle on the screen
public static void DrawScreenRect(Rect rect, Color color)
{
GUI.color = color;
GUI.DrawTexture(rect, Texture2D.whiteTexture);
}
// Utility function to draw a border around the rectangle
public static void DrawScreenRectBorder(Rect rect, float thickness, Color color)
{
// Top
DrawScreenRect(new Rect(rect.xMin, rect.yMin, rect.width, thickness), color);
// Left
DrawScreenRect(new Rect(rect.xMin, rect.yMin, thickness, rect.height), color);
// Right
DrawScreenRect(new Rect(rect.xMax - thickness, rect.yMin, thickness, rect.height), color);
// Bottom
DrawScreenRect(new Rect(rect.xMin, rect.yMax - thickness, rect.width, thickness), color);
}
// Placeholder for detecting selected objects within the rectangle
// This needs to be defined based on your specific needs
private void PerformSelection()
{
if (!isDragging) return;
// Define your fixed distance from the camera
float fixedDistance = 100.0f;
// Get the rect and the two points representing opposite corners
Rect selectionRect = GetScreenRect(mousePosition1, Input.mousePosition);
Vector3 screenPos1 = new Vector3(selectionRect.xMin, Screen.height - selectionRect.yMin, Camera.main.nearClipPlane);
Vector3 screenPos2 = new Vector3(selectionRect.xMax, Screen.height - selectionRect.yMax, Camera.main.nearClipPlane);
Vector3 screenPos3 = new Vector3(selectionRect.xMin, Screen.height - selectionRect.yMax, Camera.main.nearClipPlane);
Vector3 screenPos4 = new Vector3(selectionRect.xMax, Screen.height - selectionRect.yMin, Camera.main.nearClipPlane);
// Convert the corners of the rectangle from screen space to world space at a fixed distance
worldPos1 = Camera.main.ScreenToWorldPoint(screenPos1);
worldPos2 = Camera.main.ScreenToWorldPoint(screenPos2);
worldPos3 = Camera.main.ScreenToWorldPoint(screenPos3);
worldPos4 = Camera.main.ScreenToWorldPoint(screenPos4);
// Calculate the center and size of the box in world space
center = (worldPos1 + worldPos2) / 2f;
size = new Vector3(Vector3.Distance(worldPos1, worldPos4), Vector3.Distance(worldPos1, worldPos3), 0);
// Optionally, visualize the box for debugging (comment out for production code)
Debug.DrawLine(worldPos1, worldPos2, Color.green, 2f);
// Perform the box cast
RaycastHit[] hitObjects = Physics.BoxCastAll(center, size / 2f, Camera.main.transform.forward, Camera.main.transform.rotation, fixedDistance, unitLayerMask);
foreach (RaycastHit hit in hitObjects)
{
UnitBase unitBase = hit.transform.gameObject.GetComponent<UnitBase>();
if (unitBase != null)
{
unitBase.EnableHighlight(true);
if(!selectedUnits.Contains(unitBase)) selectedUnits.Add(unitBase);
}
}
}
public void CalculateFormation(Vector3 targetPosition)
{
int totalUnits = selectedUnits.Count; // Include the commander in the formation count
int sideLength = Mathf.CeilToInt(Mathf.Sqrt(totalUnits)); // Calculate the side length of the formation square
int soldierCount = 0;
// Find the center of the formation grid
int centerIndex = sideLength / 2;
for (int x = 0; x < sideLength; x++)
{
for (int z = 0; z < sideLength; z++)
{
if (soldierCount >= selectedUnits.Count)
break; // We’ve positioned all the soldiers
// Calculate the position for each soldier relative to the center
Vector3 offsetToCenter = new Vector3(
(x - centerIndex) * offsetBetweenSoldiers,
0,
(z - centerIndex) * offsetBetweenSoldiers
);
Vector3 soldierPosition = targetPosition + offsetToCenter;
// Move soldier to the computed position
selectedUnits[soldierCount].MoveTo(soldierPosition, true);
soldierCount++;
}
}
}
private void StartNight(object sender, EventArgs e)
{
isDay = false;
}
private void StartDay(object sender, EventArgs e)
{
isDay = true;
}
private void OnDestroy()
{
GameManager.Instance.OnDayStart -= StartDay;
GameManager.Instance.OnNightStart -= StartNight;
}
}
using System;
using System.Collections.Generic;
using Game.Systems;
using Game.Systems.Buildings;
using UnityEngine;
namespace Game.ImprovedAI
{
public class MouseSelectionBox : MonoBehaviour
{
[SerializeField] private float minDragDistance = 0.1f;
[SerializeField] private LayerMask unitLayerMask, terrainLayerMask, buildingLayerMask;
[SerializeField] private float offsetBetweenSoldiers = 1;
private bool isSelecting, isDragging, isDay = true, isActive;
private Vector3 mouseStartPosition, worldPos1, worldPos2, worldPos3, worldPos4, selectionCenter, selectionSize;
private BuildingBase currentBuilding;
public List<UnitBase> selectedUnits = new List<UnitBase>();
public void Init()
{
GameManager.Instance.OnDayStart += StartDay;
GameManager.Instance.OnNightStart += StartNight;
GameManager.Instance.OnEveningStart += StartNight;
isActive = true;
}
private void Update()
{
if (!isActive) return;
if (GameManager.Instance.DeckController.handController.IsDragging || GameManager.Instance.VillagersPlacer.dragging)
{
ResetSelection();
return;
}
HandleMouseInput();
}
private void HandleMouseInput()
{
if (Input.GetMouseButtonDown(0))
{
HandleLeftClick();
}
if (isSelecting && (Input.mousePosition - mouseStartPosition).magnitude > minDragDistance)
{
StartDragging();
}
if (Input.GetMouseButtonUp(0))
{
FinalizeSelection();
}
if (selectedUnits.Count > 0 && Input.GetMouseButtonDown(1))
{
MoveUnits();
}
}
private void HandleLeftClick()
{
DeselectUnits();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, unitLayerMask))
{
SelectUnit(hit.transform.gameObject.GetComponent<UnitBase>());
}
else if (Physics.Raycast(ray, out hit, Mathf.Infinity, buildingLayerMask) && isDay)
{
SelectBuilding(hit.transform.GetComponentInParent<BuildingBase>());
}
else
{
isSelecting = true;
mouseStartPosition = Input.mousePosition;
}
}
private void SelectUnit(UnitBase unitBase)
{
if (unitBase != null)
{
unitBase.EnableHighlight(true);
if (!selectedUnits.Contains(unitBase)) selectedUnits.Add(unitBase);
}
}
private void SelectBuilding(BuildingBase building)
{
currentBuilding?.transform.GetComponent<PlacerHelper>().EnableOutline(false);
if (building.IsBuilded && !building.IsDestroyed && building.CanStartPanel)
{
currentBuilding = building;
currentBuilding.InitPanel();
building.transform.GetComponent<PlacerHelper>().EnableOutline(true);
}
}
private void StartDragging()
{
isDragging = true;
currentBuilding?.transform.GetComponent<PlacerHelper>().EnableOutline(false);
GameManager.Instance.BuildingPanelManager.DisableAllPanels();
}
private void FinalizeSelection()
{
if (isDragging)
{
PerformSelection();
}
ResetSelection();
}
private void ResetSelection()
{
isSelecting = false;
isDragging = false;
}
private void DeselectUnits()
{
foreach (var unit in selectedUnits)
{
unit.EnableHighlight(false);
}
selectedUnits.Clear();
}
private void OnGUI()
{
if (isDragging)
{
Rect rect = GetScreenRect(mouseStartPosition, Input.mousePosition);
DrawScreenRect(rect, new Color(0.8f, 0.8f, 0.95f, 0.25f));
DrawScreenRectBorder(rect, 2, Color.white);
}
}
private void MoveUnits()
{
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, Mathf.Infinity, terrainLayerMask))
{
CalculateFormation(hit.point);
}
}
private static Rect GetScreenRect(Vector3 screenPosition1, Vector3 screenPosition2)
{
screenPosition1.y = Screen.height - screenPosition1.y;
screenPosition2.y = Screen.height - screenPosition2.y;
Vector3 topLeft = Vector3.Min(screenPosition1, screenPosition2);
Vector3 bottomRight = Vector3.Max(screenPosition1, screenPosition2);
return Rect.MinMaxRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
}
private static void DrawScreenRect(Rect rect, Color color)
{
GUI.color = color;
GUI.DrawTexture(rect, Texture2D.whiteTexture);
}
private static void DrawScreenRectBorder(Rect rect, float thickness, Color color)
{
DrawScreenRect(new Rect(rect.xMin, rect.yMin, rect.width, thickness), color);
DrawScreenRect(new Rect(rect.xMin, rect.yMin, thickness, rect.height), color);
DrawScreenRect(new Rect(rect.xMax - thickness, rect.yMin, thickness, rect.height), color);
DrawScreenRect(new Rect(rect.xMin, rect.yMax - thickness, rect.width, thickness), color);
}
private void PerformSelection()
{
if (!isDragging) return;
Rect selectionRect = GetScreenRect(mouseStartPosition, Input.mousePosition);
worldPos1 = Camera.main.ScreenToWorldPoint(new Vector3(selectionRect.xMin, Screen.height - selectionRect.yMin, Camera.main.nearClipPlane));
worldPos2 = Camera.main.ScreenToWorldPoint(new Vector3(selectionRect.xMax, Screen.height - selectionRect.yMax, Camera.main.nearClipPlane));
worldPos3 = Camera.main.ScreenToWorldPoint(new Vector3(selectionRect.xMin, Screen.height - selectionRect.yMax, Camera.main.nearClipPlane));
worldPos4 = Camera.main.ScreenToWorldPoint(new Vector3(selectionRect.xMax, Screen.height - selectionRect.yMin, Camera.main.nearClipPlane));
selectionCenter = (worldPos1 + worldPos2) / 2f;
selectionSize = new Vector3(Vector3.Distance(worldPos1, worldPos4), Vector3.Distance(worldPos1, worldPos3), 0);
RaycastHit[] hitObjects = Physics.BoxCastAll(selectionCenter, selectionSize / 2f, Camera.main.transform.forward, Camera.main.transform.rotation, 100f, unitLayerMask);
foreach (RaycastHit hit in hitObjects)
{
SelectUnit(hit.transform.gameObject.GetComponent<UnitBase>());
}
}
private void CalculateFormation(Vector3 targetPosition)
{
int totalUnits = selectedUnits.Count;
int sideLength = Mathf.CeilToInt(Mathf.Sqrt(totalUnits));
int soldierCount = 0;
int centerIndex = sideLength / 2;
for (int x = 0; x < sideLength; x++)
{
for (int z = 0; z < sideLength && soldierCount < totalUnits; z++, soldierCount++)
{
Vector3 offsetToCenter = new Vector3((x - centerIndex) * offsetBetweenSoldiers, 0, (z - centerIndex) * offsetBetweenSoldiers);
selectedUnits[soldierCount].MoveTo(targetPosition + offsetToCenter, true);
}
}
}
private void StartNight(object sender, EventArgs e) => isDay = false;
private void StartDay(object sender, EventArgs e) => isDay = true;
private void OnDestroy()
{
GameManager.Instance.OnDayStart -= StartDay;
GameManager.Instance.OnNightStart -= StartNight;
}
}
}