/// <summary> /// Cancel the icon dragging operation. /// </summary> void CancelDrag() { mDragShip = null; mDragPrefab = null; mDragResource = -1; mDragTown = null; }
/// <summary> /// Unassigns the specified ship from this trade route. /// </summary> public void UnassignShip(AvailableShips.Owned ship) { if (ship.tradeRoute == this) { ships.Remove(ship); ship.tradeRoute = null; if (ship.asset != null) { Object.Destroy(ship.asset); ship.asset = null; } } }
/// <summary> /// Assigns the specified ship to this trade route. /// </summary> public void AssignShip(AvailableShips.Owned ship) { if (ship.tradeRoute != this) { if (ship.tradeRoute != null) { ship.tradeRoute.UnassignShip(ship); } ship.tradeRoute = this; ships.Add(ship); if (ship.prefab != null) { Vector3 start = mOriginal.Sample(0f, SplineV.SampleType.Linear); Vector3 next = mOriginal.Sample(1f, SplineV.SampleType.Linear); GameObject go = Instantiate(ship.prefab.prefab, start, Quaternion.LookRotation(next - start)) as GameObject; if (go != null) { // Replace the possible multiple colliders with a single one residing at root Collider[] cols = go.GetComponentsInChildren <Collider>(); if (cols.Length > 1) { foreach (Collider col in cols) { Destroy(col); } go.AddComponent <BoxCollider>(); } ship.asset = go; go.AddComponent <Highlightable>(); TradeShip script = go.AddComponent <TradeShip>(); script.tradeRoute = this; script.prefab = ship.prefab; script.speed = ship.prefab.speed; } } } }
/// <summary> /// Draw trade-related UI. /// </summary> void DrawTradeUI() { Vector2 mousePos = UI.GetMousePos(); // Imports and exports for this town Town opposite = mSelectedRoute.GetConnectedTown(this); // Draw the trade window Rect rect = new Rect(Screen.width * 0.5f - 75f, Screen.height * 0.5f - 270f, 150f, 512f); // Allow resources to be dropped here if (mDragResource != -1 && Input.GetMouseButtonUp(0) && rect.Contains(mousePos)) { mSelectedRoute.SetExportedResource(mDragTown, mDragResource); CancelDrag(); } // Allow ships to be dropped here else if (mDragShip != null && Input.GetMouseButtonUp(0) && rect.Contains(mousePos)) { mSelectedRoute.AssignShip(mDragShip); CancelDrag(); } rect = UI.DrawPanel(rect); Rect titleRect = new Rect(rect.x, rect.y - 25.0f, rect.width, 40.0f); UI.DrawPanel(titleRect); UI.DrawTitle(titleRect, "Trade", Config.Instance.headerStyle); rect = UI.Bevel(rect, 8f); // Main town imports from the connected city { UI.DrawTitle(new Rect(rect.x, rect.y, rect.width, 30f), "Imports", Config.Instance.infoStyle); rect.y += 30f; rect.height -= 30f; // Draw the arrow UI.DrawTexture(rect.x + rect.width - 70f + Mathf.Sin(Time.time * 5f) * 10f, rect.y + rect.width * 0.5f - Icons.Instance.arrowRight.height * 0.5f, Icons.Instance.arrowRight); // Draw the grid for icons bool tint = (mDragResource != -1 && mDragTown != null && mDragTown != this); Color prev = GUI.color; GUI.color = tint ? new Color(0f, 1f, 0f, prev.a) : prev; List<Vector2> grid = UI.DrawGrid(rect, 50f, 50f, 4f, 4f, 4f, 4); GUI.color = prev; // Draw the imported resources DrawTradedResourcesUI(grid, opposite); rect.y += rect.width; rect.height -= rect.width; } // Assigned ships { UI.DrawTitle(new Rect(rect.x, rect.y, rect.width, 30f), "Ship Capacity", Config.Instance.infoStyle); rect.y += 30f; rect.height -= 30f; // Draw the grid, coloring it green if we're dragging an assignable ship Color prev = GUI.color; GUI.color = (mDragShip == null) ? prev : new Color(0f, 1f, 0f, prev.a); List<Vector2> grid = UI.DrawGrid(rect, 50f, 50f, 4f, 4f, 4f, 4); GUI.color = prev; rect.y += rect.width; rect.height -= rect.width; int count = 0; // Draw all assigned ships' icons foreach (AvailableShips.Owned ship in AvailableShips.Instance.list) { if (ship.tradeRoute == mSelectedRoute) { Vector2 pos = grid[count++]; Rect shipRect = new Rect(pos.x, pos.y, 50f, 50f); if (ship.prefab.icon != null) { UI.DrawTexture(pos.x, pos.y, ship.prefab.icon); } UI.DrawLabel(shipRect, ship.prefab.cargo.ToString(), Config.Instance.infoStyle, Color.white, Color.black, true); // Allow dragging of this icon if (Input.GetMouseButtonDown(0) && shipRect.Contains(mousePos)) { CancelDrag(); mDragShip = ship; } } } } // Main city exports to the connected city { UI.DrawTitle(new Rect(rect.x, rect.y, rect.width, 30f), "Exports", Config.Instance.infoStyle); rect.y += 30f; rect.height -= 30f; // Draw the arrow UI.DrawTexture(rect.x + rect.width - 70f + Mathf.Sin(Time.time * 5f) * 10f, rect.y + rect.width * 0.5f - Icons.Instance.arrowRight.height * 0.5f, Icons.Instance.arrowRight); // Draw the grid for icons bool tint = (mDragResource != -1 && mDragTown == this); Color prev = GUI.color; GUI.color = tint ? new Color(0f, 1f, 0f, prev.a) : prev; List<Vector2> grid = UI.DrawGrid(rect, 50f, 50f, 4f, 4f, 4f, 4); GUI.color = prev; // Draw the imported resources DrawTradedResourcesUI(grid, this); } }
/// <summary> /// Draw the available ships window. /// </summary> void DrawFreeShipsUI() { Vector2 mousePos = UI.GetMousePos(); Rect rect = new Rect(Screen.width * 0.5f - 425f, Screen.height * 0.5f + 20f, 292f, 166f); rect = UI.DrawWindow(rect, "Owned Ships"); rect = UI.Bevel(rect, 8f); // Draw the grid, coloring it green if we're dragging something useful Color prev = GUI.color; GUI.color = (mDragShip == null && mDragPrefab == null) ? prev : new Color(0f, 1f, 0f, prev.a); List<Vector2> grid = UI.DrawGrid(rect, 50f, 50f, 4f, 4f, 4f, 8); GUI.color = prev; // Drop functionality for purchased ships if (Input.GetMouseButtonUp(0) && rect.Contains(mousePos)) { if (mDragPrefab != null) { if (Config.Instance.gold >= mDragPrefab.price) { Config.Instance.gold -= mDragPrefab.price; // Add a new ship to the list of owned ships AvailableShips.Owned ship = new AvailableShips.Owned(); ship.prefab = mDragPrefab; AvailableShips.Instance.list.Add(ship); } } else if (mDragShip != null) { // Clear the ship's trade route mSelectedRoute.UnassignShip(mDragShip); } CancelDrag(); } int count = 0; // Run through all owned ships and display the free ones foreach (AvailableShips.Owned ship in AvailableShips.Instance.list) { if (ship.tradeRoute == null) { if (count >= grid.Count) break; Vector3 pos = grid[count++]; Rect shipRect = new Rect(pos.x, pos.y, 50f, 50f); if (ship.prefab.icon != null) { UI.DrawTexture(pos.x, pos.y, ship.prefab.icon); } // Allow dragging of this icon if (Input.GetMouseButtonDown(0) && shipRect.Contains(mousePos)) { CancelDrag(); mDragShip = ship; } } } }
/// <summary> /// Draw the available ships window. /// </summary> void DrawFreeShipsUI() { Vector2 mousePos = UI.GetMousePos(); Rect rect = new Rect(Screen.width * 0.5f - 425f, Screen.height * 0.5f + 20f, 292f, 166f); rect = UI.DrawWindow(rect, "Owned Ships"); rect = UI.Bevel(rect, 8f); // Draw the grid, coloring it green if we're dragging something useful Color prev = GUI.color; GUI.color = (mDragShip == null && mDragPrefab == null) ? prev : new Color(0f, 1f, 0f, prev.a); List <Vector2> grid = UI.DrawGrid(rect, 50f, 50f, 4f, 4f, 4f, 8); GUI.color = prev; // Drop functionality for purchased ships if (Input.GetMouseButtonUp(0) && rect.Contains(mousePos)) { if (mDragPrefab != null) { if (Config.Instance.gold >= mDragPrefab.price) { Config.Instance.gold -= mDragPrefab.price; // Add a new ship to the list of owned ships AvailableShips.Owned ship = new AvailableShips.Owned(); ship.prefab = mDragPrefab; AvailableShips.Instance.list.Add(ship); } } else if (mDragShip != null) { // Clear the ship's trade route mSelectedRoute.UnassignShip(mDragShip); } CancelDrag(); } int count = 0; // Run through all owned ships and display the free ones foreach (AvailableShips.Owned ship in AvailableShips.Instance.list) { if (ship.tradeRoute == null) { if (count >= grid.Count) { break; } Vector3 pos = grid[count++]; Rect shipRect = new Rect(pos.x, pos.y, 50f, 50f); if (ship.prefab.icon != null) { UI.DrawTexture(pos.x, pos.y, ship.prefab.icon); } // Allow dragging of this icon if (Input.GetMouseButtonDown(0) && shipRect.Contains(mousePos)) { CancelDrag(); mDragShip = ship; } } } }
/// <summary> /// Draw trade-related UI. /// </summary> void DrawTradeUI() { Vector2 mousePos = UI.GetMousePos(); // Imports and exports for this town Town opposite = mSelectedRoute.GetConnectedTown(this); // Draw the trade window Rect rect = new Rect(Screen.width * 0.5f - 75f, Screen.height * 0.5f - 270f, 150f, 512f); // Allow resources to be dropped here if (mDragResource != -1 && Input.GetMouseButtonUp(0) && rect.Contains(mousePos)) { mSelectedRoute.SetExportedResource(mDragTown, mDragResource); CancelDrag(); } // Allow ships to be dropped here else if (mDragShip != null && Input.GetMouseButtonUp(0) && rect.Contains(mousePos)) { mSelectedRoute.AssignShip(mDragShip); CancelDrag(); } rect = UI.DrawPanel(rect); Rect titleRect = new Rect(rect.x, rect.y - 25.0f, rect.width, 40.0f); UI.DrawPanel(titleRect); UI.DrawTitle(titleRect, "Trade", Config.Instance.headerStyle); rect = UI.Bevel(rect, 8f); // Main town imports from the connected city { UI.DrawTitle(new Rect(rect.x, rect.y, rect.width, 30f), "Imports", Config.Instance.infoStyle); rect.y += 30f; rect.height -= 30f; // Draw the arrow UI.DrawTexture(rect.x + rect.width - 70f + Mathf.Sin(Time.time * 5f) * 10f, rect.y + rect.width * 0.5f - Icons.Instance.arrowRight.height * 0.5f, Icons.Instance.arrowRight); // Draw the grid for icons bool tint = (mDragResource != -1 && mDragTown != null && mDragTown != this); Color prev = GUI.color; GUI.color = tint ? new Color(0f, 1f, 0f, prev.a) : prev; List <Vector2> grid = UI.DrawGrid(rect, 50f, 50f, 4f, 4f, 4f, 4); GUI.color = prev; // Draw the imported resources DrawTradedResourcesUI(grid, opposite); rect.y += rect.width; rect.height -= rect.width; } // Assigned ships { UI.DrawTitle(new Rect(rect.x, rect.y, rect.width, 30f), "Ship Capacity", Config.Instance.infoStyle); rect.y += 30f; rect.height -= 30f; // Draw the grid, coloring it green if we're dragging an assignable ship Color prev = GUI.color; GUI.color = (mDragShip == null) ? prev : new Color(0f, 1f, 0f, prev.a); List <Vector2> grid = UI.DrawGrid(rect, 50f, 50f, 4f, 4f, 4f, 4); GUI.color = prev; rect.y += rect.width; rect.height -= rect.width; int count = 0; // Draw all assigned ships' icons foreach (AvailableShips.Owned ship in AvailableShips.Instance.list) { if (ship.tradeRoute == mSelectedRoute) { Vector2 pos = grid[count++]; Rect shipRect = new Rect(pos.x, pos.y, 50f, 50f); if (ship.prefab.icon != null) { UI.DrawTexture(pos.x, pos.y, ship.prefab.icon); } UI.DrawLabel(shipRect, ship.prefab.cargo.ToString(), Config.Instance.infoStyle, Color.white, Color.black, true); // Allow dragging of this icon if (Input.GetMouseButtonDown(0) && shipRect.Contains(mousePos)) { CancelDrag(); mDragShip = ship; } } } } // Main city exports to the connected city { UI.DrawTitle(new Rect(rect.x, rect.y, rect.width, 30f), "Exports", Config.Instance.infoStyle); rect.y += 30f; rect.height -= 30f; // Draw the arrow UI.DrawTexture(rect.x + rect.width - 70f + Mathf.Sin(Time.time * 5f) * 10f, rect.y + rect.width * 0.5f - Icons.Instance.arrowRight.height * 0.5f, Icons.Instance.arrowRight); // Draw the grid for icons bool tint = (mDragResource != -1 && mDragTown == this); Color prev = GUI.color; GUI.color = tint ? new Color(0f, 1f, 0f, prev.a) : prev; List <Vector2> grid = UI.DrawGrid(rect, 50f, 50f, 4f, 4f, 4f, 4); GUI.color = prev; // Draw the imported resources DrawTradedResourcesUI(grid, this); } }