Oto kompletny system do generowania 500 turniejów tenisa rozłożonych na 15 dni w Unity:
1. Struktura danych turniejów
csharp
// Tournament.cs
[System.Serializable]
public class Tournament
{
public int id;
public string name;
public int dayNumber;
public TournamentType type;
public List<Player> participants;
public bool isCompleted;
public Player winner;
public Tournament(int id, string name, int day, TournamentType type)
{
this.id = id;
this.name = name;
this.dayNumber = day;
this.type = type;
this.participants = new List<Player>();
this.isCompleted = false;
}
}
public enum TournamentType
{
Local, // 8 graczy
Regional, // 16 graczy
National, // 32 graczy
Grand // 64 graczy
}
// Player.cs
[System.Serializable]
public class Player
{
public string name;
public int skillLevel; // 1-100
public int ranking;
public int stamina;
public Player(string name, int skill)
{
this.name = name;
this.skillLevel = skill;
this.stamina = 100;
}
}
2. Generator turniejów
csharp
// TournamentGenerator.cs
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class TournamentGenerator : MonoBehaviour
{
[Header("Tournament Settings")]
public int totalTournaments = 500;
public int totalDays = 15;
[Header("Tournament Distribution")]
[Range(0,1)] public float localTournamentRatio = 0.4f;
[Range(0,1)] public float regionalTournamentRatio = 0.3f;
[Range(0,1)] public float nationalTournamentRatio = 0.2f;
[Range(0,1)] public float grandTournamentRatio = 0.1f;
private List<Tournament> allTournaments = new List<Tournament>();
private Dictionary<int, List<Tournament>> tournamentsByDay;
void Start()
{
GenerateAllTournaments();
}
void GenerateAllTournaments()
{
tournamentsByDay = new Dictionary<int, List<Tournament>>();
// Inicjalizacja słownika dla każdego dnia
for (int i = 1; i <= totalDays; i++)
{
tournamentsByDay[i] = new List<Tournament>();
}
// Generowanie turniejów
for (int i = 0; i < totalTournaments; i++)
{
// Określ typ turnieju na podstawie proporcji
TournamentType type = DetermineTournamentType(i);
// Przypisz do dnia (równomierne rozłożenie)
int day = (i % totalDays) + 1;
// Dla bardziej realistycznego rozkładu, możesz użyć:
// int day = GetWeightedDayDistribution();
// Stwórz turniej
string tournamentName = GenerateTournamentName(type, i);
Tournament tournament = new Tournament(i, tournamentName, day, type);
// Dodaj uczestników
PopulateTournament(tournament);
// Dodaj do list
allTournaments.Add(tournament);
tournamentsByDay[day].Add(tournament);
}
Debug.Log($"Wygenerowano {totalTournaments} turniejów na {totalDays} dni");
PrintDailyDistribution();
}
TournamentType DetermineTournamentType(int index)
{
float normalizedIndex = (float)index / totalTournaments;
if (normalizedIndex < localTournamentRatio)
return TournamentType.Local;
else if (normalizedIndex < localTournamentRatio + regionalTournamentRatio)
return TournamentType.Regional;
else if (normalizedIndex < localTournamentRatio + regionalTournamentRatio + nationalTournamentRatio)
return TournamentType.National;
else
return TournamentType.Grand;
}
string GenerateTournamentName(TournamentType type, int index)
{
string[] prefixes = { "Spring", "Summer", "Autumn", "Winter", "Classic", "Open", "Cup", "Championship" };
string[] locations = { "London", "Paris", "Tokyo", "New York", "Sydney", "Madrid", "Berlin", "Rome" };
return $"{prefixes[Random.Range(0, prefixes.Length)]} {locations[Random.Range(0, locations.Length)]} {type} #{index}";
}
void PopulateTournament(Tournament tournament)
{
int playerCount = GetPlayerCount(tournament.type);
for (int i = 0; i < playerCount; i++)
{
// Generuj graczy z różnymi poziomami umiejętności w zależności od typu turnieju
int minSkill = tournament.type == TournamentType.Local ? 10 :
tournament.type == TournamentType.Regional ? 30 :
tournament.type == TournamentType.National ? 50 : 70;
int maxSkill = tournament.type == TournamentType.Local ? 50 :
tournament.type == TournamentType.Regional ? 70 :
tournament.type == TournamentType.National ? 85 : 100;
Player player = new Player(
$"Player_{tournament.id}_{i}",
Random.Range(minSkill, maxSkill)
);
tournament.participants.Add(player);
}
}
int GetPlayerCount(TournamentType type)
{
switch(type)
{
case TournamentType.Local: return 8;
case TournamentType.Regional: return 16;
case TournamentType.National: return 32;
case TournamentType.Grand: return 64;
default: return 8;
}
}
void PrintDailyDistribution()
{
for (int day = 1; day <= totalDays; day++)
{
Debug.Log($"Dzień {day}: {tournamentsByDay[day].Count} turniejów");
}
}
// Metoda do pobierania turniejów dla konkretnego dnia
public List<Tournament> GetTournamentsForDay(int day)
{
if (tournamentsByDay.ContainsKey(day))
return tournamentsByDay[day];
return new List<Tournament>();
}
}
3. System symulacji turniejów
csharp
4. UI Manager do wyświetlania
csharp
// TournamentUIManager.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
public class TournamentUIManager : MonoBehaviour
{
[Header("UI References")]
public Text dayText;
public Transform tournamentListParent;
public GameObject tournamentItemPrefab;
private TournamentGenerator generator;
void Start()
{
generator = FindObjectOfType<TournamentGenerator>();
}
public void DisplayDayTournaments(int day)
{
// Wyczyść listę
foreach (Transform child in tournamentListParent)
{
Destroy(child.gameObject);
}
dayText.text = $"Dzień {day}/15";
List<Tournament> tournaments = generator.GetTournamentsForDay(day);
foreach (Tournament tournament in tournaments)
{
GameObject item = Instantiate(tournamentItemPrefab, tournamentListParent);
TournamentItemUI itemUI = item.GetComponent<TournamentItemUI>();
itemUI.SetupTournament(tournament);
}
}
}
5. Optymalizacja dla lepszej wydajności
csharp
// TournamentOptimizer.cs
public class TournamentOptimizer : MonoBehaviour
{
// Użyj Object Pooling dla UI elementów
private Queue<GameObject> tournamentUIPool = new Queue<GameObject>();
// Serializuj dane turniejów
public void SaveTournamentsToFile()
{
string json = JsonUtility.ToJson(allTournaments);
System.IO.File.WriteAllText(Application.persistentDataPath + "/tournaments.json", json);
}
// Ładuj tylko turnieje z aktualnego dnia
public void LoadDayTournaments(int day)
{
// Implementacja lazy loading
}
}
Wskazówki implementacyjne:
Balansowanie rozkładu: Możesz użyć krzywej AnimationCurve do kontrolowania ile turniejów odbywa się każdego dnia
Różnorodność: Dodaj różne powierzchnie kortów (twarda, trawa, mączka)
System rankingowy: Implementuj ELO rating dla graczy
Wydarzenia specjalne: Niektóre dni mogą mieć więcej prestiżowych turniejów
Optymalizacja: Dla 500 turniejów używaj systemu LOD - symuluj szczegółowo tylko te, które gracz ogląda
Czy chcesz, żebym rozwinął jakiś konkretny aspekt tego systemu?
// TournamentSimulator.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TournamentSimulator : MonoBehaviour
{
private TournamentGenerator generator;
[Header("Simulation Settings")]
public float matchDuration = 2f; // czas symulacji meczu w sekundach
public bool autoSimulate = true;
private int currentDay = 1;
private bool isSimulating = false;
void Start()
{
generator = GetComponent<TournamentGenerator>();
if (autoSimulate)
StartCoroutine(SimulateAllDays());
}
IEnumerator SimulateAllDays()
{
yield return new WaitForSeconds(1f); // Czekaj na wygenerowanie turniejów
for (int day = 1; day <= 15; day++)
{
currentDay = day;
yield return StartCoroutine(SimulateDay(day));
// Przerwa między dniami
yield return new WaitForSeconds(1f);
}
Debug.Log("Zakończono symulację wszystkich 15 dni!");
}
IEnumerator SimulateDay(int day)
{
List<Tournament> todayTournaments = generator.GetTournamentsForDay(day);
Debug.Log($"=== DZIEŃ {day} - Rozpoczęcie {todayTournaments.Count} turniejów ===");
// Symuluj wszystkie turnieje równolegle
List<Coroutine> runningTournaments = new List<Coroutine>();
foreach (Tournament tournament in todayTournaments)
{
runningTournaments.Add(StartCoroutine(SimulateTournament(tournament)));
}
// Czekaj aż wszystkie turnieje się zakończą
foreach (var coroutine in runningTournaments)
{
yield return coroutine;
}
Debug.Log($"Dzień {day} zakończony!");
}
IEnumerator SimulateTournament(Tournament tournament)
{
Debug.Log($"Rozpoczynanie turnieju: {tournament.name}");
List<Player> currentRoundPlayers = new List<Player>(tournament.participants);
int round = 1;
// Symuluj rundy eliminacyjne
while (currentRoundPlayers.Count > 1)
{
List<Player> nextRoundPlayers = new List<Player>();
// Rozgrywaj mecze parami
for (int i = 0; i < currentRoundPlayers.Count; i += 2)
{
if (i + 1 < currentRoundPlayers.Count)
{
Player winner = SimulateMatch(currentRoundPlayers[i], currentRoundPlayers[i + 1]);
nextRoundPlayers.Add(winner);
// Wizualna przerwa między meczami
yield return new WaitForSeconds(matchDuration);
}
}
currentRoundPlayers = nextRoundPlayers;
round++;
}
// Ustaw zwycięzcę
tournament.winner = currentRoundPlayers[0];
tournament.isCompleted = true;
Debug.Log($"Turniej {tournament.name} zakończony! Zwycięzca: {tournament.winner.name}");
}
Player SimulateMatch(Player player1, Player player2)
{
// Prosta symulacja oparta na umiejętnościach i staminie
float player1Chance = player1.skillLevel * (player1.stamina / 100f);
float player2Chance = player2.skillLevel * (player2.stamina / 100f);
float totalChance = player1Chance + player2Chance;
float randomValue = Random.Range(0f, totalChance);
Player winner = randomValue < player1Chance ? player1 : player2;
Player loser = winner == player1 ? player2 : player1;
// Zmniejsz staminę
winner.stamina -= Random.Range(10, 20);
loser.stamina -= Random.Range(15, 25);
return winner;
}
}