Completed course 2 adn 3, working on 4

This commit is contained in:
2026-03-20 17:34:22 +00:00
parent 623ad6f509
commit db966bab55
507 changed files with 67348 additions and 4667 deletions

View File

@@ -28,30 +28,44 @@ public class UnitCommander : MonoBehaviour
{
if(Input.GetMouseButtonDown(1) && unitSelection.HasUnitSelected())
{
unitSelection.RemoveNullUnitsFromSelection();
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Unit[] selectedUnits = unitSelection.GetSelectedUnits();
if(Physics.Raycast(ray, out hit, 100, layerMask))
{
//are we clicking on the ground?
if(hit.collider.CompareTag("Ground"))
{
SelectionMarker marker = CreateSelectionMarker(hit.point, false);
UnitsMoveToPosition(hit.point, selectedUnits, marker);
}
//are we clicking on a resource?
else if(hit.collider.CompareTag("Resource"))
{
UnitsGatherResource(hit.collider.GetComponent<ResourceSource>(), selectedUnits);
CreateSelectionMarker(hit.point, true);
CreateSelectionMarker(hit.collider.transform.position, true);
}
//did we click on the enemy?
else if(hit.collider.CompareTag("Unit"))
{
Unit enemy = hit.collider.GetComponent<Unit>();
if(!Player.me.IsMyUnit(enemy))
{
UnitsAttackEnemy(enemy, selectedUnits);
CreateSelectionMarker(enemy.transform.position, false);
}
}
}
}
}
void UnitsMoveToPosition(Vector3 movePos, Unit[] units, SelectionMarker marker)
{
Vector3[] destinations = UnitMover.GetUnitGroupDestination(movePos, units.Length, 2);
for(int x = 0; x < units.Length; x++)
{
// Only the first unit owns the marker so it isn't destroyed multiple times
units[x].MoveToPosition(movePos, x == 0 ? marker : null);
units[x].MoveToPosition(destinations[x], x == 0 ? marker : null);
}
}
//creates a new selection marker at the position we right click
@@ -79,7 +93,7 @@ public class UnitCommander : MonoBehaviour
units[0].GatherResource(resource, UnitMover.GetUnitDestinationAroundResource(resource.transform.position));
}
else
{
{
Vector3[] destinations = UnitMover.GetUnitGroupDestinationsAroundResource(resource.transform.position, units.Length);
for(int x = 0; x <units.Length; x++)
{
@@ -87,4 +101,10 @@ public class UnitCommander : MonoBehaviour
}
}
}
//called when we command units to attack an enemy
void UnitsAttackEnemy(Unit enemy, Unit[] units)
{
for(int x = 0; x < units.Length; x++)
units[x].AttackUnit(enemy);
}
}