feat: action management

This commit is contained in:
nils0607@icloud.com 2025-09-18 03:24:40 +02:00
parent 4f2f5d4112
commit 466c6004a5
5 changed files with 127841 additions and 153034 deletions

File diff suppressed because it is too large Load diff

View file

@ -163,6 +163,11 @@ public static class EditorCustom
if (GUILayout.Button("Save as new"))
gm.SaveLevelData();
GUILayout.Space(20);
if (GUILayout.Button("Execute Round End"))
gm.ExecuteRoundEnd();
}
// Custom Scene Interaction while GameManager selected

View file

@ -1,3 +1,5 @@
using System;
using Unity.VisualScripting;
using UnityEngine;
[ExecuteAlways]
@ -25,14 +27,35 @@ public class Connection : MonoBehaviour
private void Update()
{
// On Click
if (Input.GetMouseButtonDown(0) && hovered && allowed)
{
int player = GameManager.Instance.currentPlayer;
if (state == BuildState.BUILT && nodeA.Owner == player && nodeB.Owner == player)
{
bool explode = Input.GetKey(KeyCode.LeftShift);
var action = new GameManager.Action { nodeToId = nodeA.id, nodeFromId = nodeB.id, player = player, type = explode ? GameManager.ActionType.EXPLODE_CON : GameManager.ActionType.DESTRUCT_CON };
GameManager.Instance.ExecuteAction(action);
}
else if(state == BuildState.EMPTY && (nodeA.Owner == player || nodeB.Owner == player))
{
var action = new GameManager.Action { nodeToId = nodeA.id, nodeFromId = nodeB.id, player = player, type = GameManager.ActionType.MOVE_UNITS };
GameManager.Instance.ExecuteAction(action);
}
}
if (Application.isPlaying && !allowed)
lineRenderer.enabled = false;
else
{
float width = (hovered ? 0.6f : 0.3f) * (allowed ? 1f : 0.3f);
float width = (hovered ? 0.6f : 0.3f) * (allowed ? state != BuildState.BUILT ? 0.6f : 1f : 0.1f);
lineRenderer.startColor = allowed ? nodeA.transform.GetChild(0).GetComponent<Renderer>().sharedMaterial.color : new Color(0.2f, 0.2f, 0.2f);
lineRenderer.endColor = allowed ? nodeB.transform.GetChild(0).GetComponent<Renderer>().sharedMaterial.color : new Color(0.2f, 0.2f, 0.2f);
Color unbuiltColor = !allowed || state == BuildState.EMPTY ? new Color(0.2f, 0.2f, 0.2f) : new Color(0.8f, 0.8f, 0f);
lineRenderer.startColor = allowed && state == BuildState.BUILT ? nodeA.transform.GetChild(0).GetComponent<Renderer>().sharedMaterial.color : unbuiltColor;
lineRenderer.endColor = allowed && state == BuildState.BUILT ? nodeB.transform.GetChild(0).GetComponent<Renderer>().sharedMaterial.color : unbuiltColor;
lineRenderer.startWidth = width;
lineRenderer.endWidth = width;

View file

@ -8,6 +8,7 @@ using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UIElements;
using static UnityEngine.GraphicsBuffer;
using static UnityEngine.UI.GridLayoutGroup;
[ExecuteAlways]
public class GameManager : MonoBehaviour
@ -19,6 +20,7 @@ public class GameManager : MonoBehaviour
public Player players;
public int currentPlayer = -1;
public Node pressedNode = null;
public static GameManager Instance { get; private set; }
@ -33,6 +35,16 @@ public class GameManager : MonoBehaviour
[SerializeField] [HideInInspector] public List<LevelData> levels = new List<LevelData>();
public enum ActionType { MOVE_UNITS, DESTRUCT_CON, EXPLODE_CON };
public struct Action
{
public ActionType type;
public int nodeFromId;
public int nodeToId;
public int player;
}
[Serializable]
public class LevelData
{
@ -78,17 +90,118 @@ public class GameManager : MonoBehaviour
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
pressedNode = GetNodes().Find(n => n.hovered && n.Owner == currentPlayer);
}
if (Input.GetMouseButtonUp(0))
{
if (pressedNode != null)
{
Node toNode = GetNodes().Find(n => n.hovered && pressedNode != n);
if (toNode)
{
Connection con = Instance.GetConnections().Find(c => (c.nodeA == pressedNode && c.nodeB == toNode) || (c.nodeB == pressedNode && c.nodeA == toNode));
if (con)
{
var action = new Action { nodeFromId = pressedNode.id, nodeToId = toNode.id, type = ActionType.MOVE_UNITS, player = Instance.currentPlayer };
Instance.ExecuteAction(action);
}
}
}
}
}
public List<Node> GetNodes() => NodeParent.GetComponentsInChildren<Node>().ToList();
public List<Connection> GetConnections() => ConnectionParent.GetComponentsInChildren<Connection>().ToList();
public void OnNodesDragged(Node nodeA, Node nodeB)
public void ExecuteRoundEnd()
{
foreach(Connection con in GetConnections())
{
if (con.state == Connection.BuildState.CONSTRUCTING)
con.state = Connection.BuildState.BUILT;
else if (con.state == Connection.BuildState.DECONSTRUCTING)
con.state = Connection.BuildState.EMPTY;
}
}
public void ExecuteAction(Action action)
{
Node nodeTo = GetNodes().Find(n => n.id == action.nodeToId);
Node nodeFrom = GetNodes().Find(n => n.id == action.nodeFromId);
Connection con = GetConnections().Find(c => (c.nodeA == nodeTo && c.nodeB == nodeFrom) || (c.nodeA == nodeFrom && c.nodeB == nodeTo));
bool hostile = nodeTo.Owner != action.player;
switch (action.type)
{
case ActionType.MOVE_UNITS:
switch (con.state)
{
case Connection.BuildState.EMPTY:
// Build Connection
con.state = Connection.BuildState.CONSTRUCTING;
Debug.Log("Starting construction from " + nodeTo.id + " to " + nodeFrom.id);
return;
case Connection.BuildState.CONSTRUCTING:
if (hostile)
{
Debug.Log("Attacking hostile construction from " + nodeTo.id + " to " + nodeFrom.id);
}
else
{
Debug.Log("This should not be possible O_o");
}
return;
case Connection.BuildState.DECONSTRUCTING:
if (hostile)
{
Debug.Log("Attacking hostile construction from " + nodeTo.id + " to " + nodeFrom.id);
}
else
{
Debug.Log("This should not be possible O_o");
}
return;
case Connection.BuildState.BUILT:
if (hostile)
{
Debug.Log("Attacking hostile units at " + nodeFrom.id);
}
else
{
Debug.Log("Moving units from " + nodeTo.id + " to " + nodeFrom.id);
}
return;
default:
return;
}
case ActionType.DESTRUCT_CON:
if (!hostile)
{
Debug.Log("Starten deconstruction from " + nodeTo.id + " to " + nodeFrom.id);
con.state = Connection.BuildState.DECONSTRUCTING;
}
else
{
Debug.Log("This should not be possible O_o");
}
return;
case ActionType.EXPLODE_CON:
Debug.Log("Exploding connection from " + nodeTo.id + " to " + nodeFrom.id);
con.state = Connection.BuildState.EMPTY;
return;
}
}
public void GenerateAlongSphere()
{
for (int i = NodeParent.childCount - 1; i >= 0; i--)

View file

@ -21,8 +21,6 @@ public class Node : MonoBehaviour
private int Nachos = 0;
public static Node pressedNode = null;
void Awake()
{
UpdateColor();
@ -47,13 +45,6 @@ public class Node : MonoBehaviour
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0) && hovered)
{
Debug.Log($"Largest Nacho: {Nachos}");
}
unitText.enabled = hovered;
unitText.text = Nachos.ToString();
unitText.transform.forward = Camera.main.transform.forward;