Onto lesson 2

This commit is contained in:
2026-03-19 17:31:51 +00:00
parent f19100a166
commit 539ffe3cd0
1204 changed files with 143736 additions and 77 deletions

View File

@@ -0,0 +1,50 @@
using System;
using UnityEngine;
using UnityEngine.Scripting.APIUpdating;
public class CameraController : MonoBehaviour
{
public float moveSpeed;
public float zoomSpeed;
public float minZoomDist;
public float maxZoomDist;
private Camera cam;
void Awake()
{
cam = Camera.main;
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
Move();
Zoom();
}
private void Move()
{
float xInput = Input.GetAxisRaw("Horizontal");
float zInput = Input.GetAxisRaw("Vertical");
Vector3 dir = transform.forward * zInput + transform.right * xInput;
transform.position += dir * moveSpeed * Time.deltaTime;
}
private void Zoom()
{
float scrollInput = Input.GetAxisRaw("Mouse ScrollWheel");
float dist = Vector3.Distance(transform.position, cam.transform.position);
if(dist < minZoomDist && scrollInput > 0.0f)
return;
if(dist > maxZoomDist && scrollInput < 0.0f)
return;
cam.transform.position += cam.transform.forward * scrollInput * zoomSpeed;
}
public void FocusOnPosition(Vector3 pos)
{
transform.position = pos;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 7c4c366b996989e429dc5d29e69b2fe2

View File

@@ -0,0 +1,21 @@
using UnityEngine;
[ExecuteInEditMode]
public class LookAtCamera : MonoBehaviour
{
private Camera cam;
void Awake()
{
cam = Camera.main;
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.eulerAngles = cam.transform.eulerAngles;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 6dda6ba0a5f404046b99eab148a42bd1

View File

@@ -0,0 +1,39 @@
using UnityEngine;
using UnityEngine.Events;
public enum ResourceType
{
Wood,
Stone,
Food
}
public class ResourceSource : MonoBehaviour
{
public ResourceType type;
public int quantity;
//events
public UnityEvent onQuantityChange;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void GatherResource(int amount, Player gatheringPlayer)
{
quantity -= amount;
int amountToGive = amount;
if(quantity < 0)
amountToGive = amount + quantity;
if(quantity <= 0)
Destroy(gameObject);
if(onQuantityChange != null)
onQuantityChange.Invoke();
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 3d6eff4e8dc901740b86085c50d7598e

View File

@@ -0,0 +1,32 @@
using UnityEngine;
using TMPro;
public class ResourceSourceUI : MonoBehaviour
{
public GameObject popupPanel;
public TextMeshProUGUI resourceQuantityText;
public ResourceSource resource;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnMouseEnter()
{
popupPanel.SetActive(true);
}
void OnMouseExit()
{
popupPanel.SetActive(false);
}
public void OnResourceQuantityChange()
{
resourceQuantityText.text = resource.quantity.ToString();
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 6291c4e6db2311d478563acd0dbb7f4e

View File

@@ -2,11 +2,11 @@ using UnityEngine;
public class SelectionMarker : MonoBehaviour
{
public float lifeTime = 1.0f;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
//Destroy(gameObject, lifeTime);
}
// Update is called once per frame
@@ -14,4 +14,8 @@ public class SelectionMarker : MonoBehaviour
{
}
public void DestroySelectionMarker()
{
Destroy(gameObject);
}
}

View File

@@ -1,22 +1,46 @@
using UnityEngine;
using UnityEngine.AI;
public class Unit : MonoBehaviour
{
[Header("Components")]
public GameObject selectionVisual;
private NavMeshAgent navAgent;
private SelectionMarker selectionMarker;
void Awake()
{
//get the components
navAgent = GetComponent<NavMeshAgent>();
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
if (selectionMarker != null && !navAgent.pathPending && navAgent.remainingDistance <= navAgent.stoppingDistance)
{
selectionMarker.DestroySelectionMarker();
selectionMarker = null;
}
}
public void ToggleSelectionVisual(bool selected)
{
selectionVisual.SetActive(selected);
}
public void MoveToPosition(Vector3 pos, SelectionMarker marker = null)
{
if (selectionMarker != null)
{
selectionMarker.DestroySelectionMarker();
selectionMarker = null;
}
navAgent.isStopped = false;
navAgent.SetDestination(pos);
selectionMarker = marker;
}
}

View File

@@ -1,7 +1,22 @@
using System.Diagnostics.Contracts;
using System.Xml.Serialization;
using UnityEngine;
public class UnitCommander : MonoBehaviour
{
public GameObject selectionMarkerPrefab;
public LayerMask layerMask;
//components
private UnitSelection unitSelection;
private Camera cam;
void Awake()
{
//get the components
unitSelection = GetComponent<UnitSelection>();
cam = Camera.main;
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
@@ -11,6 +26,54 @@ public class UnitCommander : MonoBehaviour
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(1) && unitSelection.HasUnitSelected())
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Unit[] selectedUnits = unitSelection.GetSelectedUnits();
if(Physics.Raycast(ray, out hit, 100, layerMask))
{
if(hit.collider.CompareTag("Ground"))
{
SelectionMarker marker = CreateSelectionMarker(hit.point, false);
UnitsMoveToPosition(hit.point, selectedUnits, marker);
}
else if(hit.collider.CompareTag("Resource"))
{
UnitsGatherResource(hit.collider.GetComponent<ResourceSource>(), selectedUnits);
CreateSelectionMarker(hit.point, true);
}
}
}
}
void UnitsMoveToPosition(Vector3 movePos, Unit[] units, SelectionMarker marker)
{
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);
}
}
//creates a new selection marker at the position we right click
SelectionMarker CreateSelectionMarker(Vector3 pos, bool large)
{
GameObject marker = Instantiate(selectionMarkerPrefab, pos + new Vector3(0, 0.01f, 0), Quaternion.identity);
if(large)
marker.transform.localScale = Vector3.one * 3;
return marker.GetComponent<SelectionMarker>();
}
//called when we command units to move somewhere
void MoveUnitsToPosition(Vector3 movePos, Unit[] units)
{
Vector3[] destinations = UnitMover.GetUnitGroupDestination(movePos, units.Length, 2);
for(int x = 0; x < units.Length; x++)
{
units[x].MoveToPosition(destinations[x]);
}
}
//called when we command units to gather a resource
void UnitsGatherResource(ResourceSource resource, Unit[] units)
{
}
}

View File

@@ -1,4 +1,5 @@
using UnityEngine;
using UnityEngine.UIElements;
public class UnitMover : MonoBehaviour
{
@@ -13,4 +14,41 @@ public class UnitMover : MonoBehaviour
{
}
//calculates a unit formation around a given location
public static Vector3[] GetUnitGroupDestination(Vector3 moveToPos, int numUnits, float unitGap)
{
//vector3 array for final destinations
Vector3[] destinations = new Vector3[numUnits];
//calculate the rows and columns
int rows = Mathf.RoundToInt(Mathf.Sqrt(numUnits));
int cols = Mathf.CeilToInt((float)numUnits / rows);
//we need to know the current row and column we're calculating
int curRow = 0;
int curCol = 0;
float width = ((float)rows - 1) * unitGap;
float length = ((float)cols - 1) * unitGap;
for(int x = 0; x < numUnits; x++)
{
destinations[x] = moveToPos = (new Vector3(curRow, 0, curCol) * unitGap) - new Vector3(length / 2, 0, width / 2);
curCol++;
if(curCol == rows)
{
curCol = 0;
curRow++;
}
}
return destinations;
}
public static Vector3[] GetUnitGroupDestinationsAroundResource(Vector3 resourcePos, int unitsNum)
{
Vector3[] destinations = new Vector3[unitsNum];
float unitDistanceGap = 160.0f / (float)unitsNum;
for(int x = 0; x < unitsNum; x++)
{
float angle = unitDistanceGap * x;
Vector3 dir = new Vector3(Mathf.Sin(angle * Mathf.Deg2Rad), 0, Mathf.Cos(angle * Mathf.Deg2Rad));
destinations[x] = resourcePos + dir;
}
return destinations;
}
}

View File

@@ -92,6 +92,16 @@ public class UnitSelection : MonoBehaviour
}
}
//returns whether or not we're selecting a unit or units
public bool HasUnitSelected()
{
return selectedUnits.Count > 0 ? true : false;
}
//return the selected units
public Unit[] GetSelectedUnits()
{
return selectedUnits.ToArray();
}
void ToggleSelectionVisual(bool selected)
{