LifeSimulator
make a GameManager empty object and throw this script in itunknown
csharp
a year ago
14 kB
37
Indexable
using UnityEngine; using UnityEngine.UI; using System.Collections.Generic; public class LifeSimulator : MonoBehaviour { [System.Serializable] private class Character { public int Age; public float Health; public float Relationships; public float Career; public float Happiness; public Character() { Age = 0; Health = Relationships = Career = Happiness = 100f; } } [Header("Game Settings")] [SerializeField] private float dayLength = 5f; [SerializeField] private int maxAge = 100; [Header("Prefabs")] [SerializeField] private GameObject housePrefab; [SerializeField] private GameObject treePrefab; [Header("UI References")] [SerializeField] private Text healthText; [SerializeField] private Text relationshipsText; [SerializeField] private Text careerText; [SerializeField] private Text ageText; [SerializeField] private Text happinessText; [SerializeField] private Text eventText; [SerializeField] private Button exerciseButton; [SerializeField] private Button socializeButton; [SerializeField] private Button workButton; [SerializeField] private Button relaxButton; [SerializeField] private Image backgroundImage; private Character player; private List<GameObject> visualElements = new List<GameObject>(); private float currentTime = 0f; private ParticleSystem weatherSystem; private void Start() { player = new Character(); SetupUI(); GenerateEnvironment(); SetupWeatherSystem(); } private void Update() { if (player == null) return; currentTime += Time.deltaTime; if (currentTime >= dayLength) { ProcessDay(); currentTime = 0f; } UpdateVisuals(); UpdateUI(); UpdateWeather(); } private void SetupUI() { Canvas canvas = FindObjectOfType<Canvas>(); if (canvas == null) { GameObject canvasObj = new GameObject("Canvas"); canvas = canvasObj.AddComponent<Canvas>(); canvas.renderMode = RenderMode.ScreenSpaceOverlay; canvasObj.AddComponent<CanvasScaler>(); canvasObj.AddComponent<GraphicRaycaster>(); } if (backgroundImage == null) { GameObject bgObj = new GameObject("Background"); bgObj.transform.SetParent(canvas.transform, false); backgroundImage = bgObj.AddComponent<Image>(); backgroundImage.color = new Color(0.8f, 0.9f, 1f, 1f); RectTransform bgRect = backgroundImage.rectTransform; bgRect.anchorMin = Vector2.zero; bgRect.anchorMax = Vector2.one; bgRect.sizeDelta = Vector2.zero; } healthText = CreateText(canvas.gameObject, "Health: 100", new Vector2(10, -30)); relationshipsText = CreateText(canvas.gameObject, "Relationships: 100", new Vector2(10, -60)); careerText = CreateText(canvas.gameObject, "Career: 100", new Vector2(10, -90)); ageText = CreateText(canvas.gameObject, "Age: 0", new Vector2(10, -120)); happinessText = CreateText(canvas.gameObject, "Happiness: 100", new Vector2(10, -150)); eventText = CreateText(canvas.gameObject, "", new Vector2(0, -30)); if (eventText != null) eventText.alignment = TextAnchor.UpperCenter; exerciseButton = CreateButton(canvas.gameObject, "Exercise", new Vector2(-100, 70), () => PlayerChoice("exercise")); socializeButton = CreateButton(canvas.gameObject, "Socialize", new Vector2(-100, 130), () => PlayerChoice("socialize")); workButton = CreateButton(canvas.gameObject, "Work", new Vector2(-100, 190), () => PlayerChoice("work")); relaxButton = CreateButton(canvas.gameObject, "Relax", new Vector2(-100, 250), () => PlayerChoice("relax")); } private Text CreateText(GameObject parent, string txt, Vector2 position) { GameObject textObj = new GameObject("Text"); textObj.transform.SetParent(parent.transform, false); Text text = textObj.AddComponent<Text>(); text.font = Resources.GetBuiltinResource<Font>("LegacyRuntime.ttf"); text.text = txt; text.fontSize = 24; text.color = Color.black; RectTransform rectTransform = text.GetComponent<RectTransform>(); rectTransform.anchorMin = new Vector2(0, 1); rectTransform.anchorMax = new Vector2(0, 1); rectTransform.anchoredPosition = position; rectTransform.sizeDelta = new Vector2(200, 30); return text; } private Button CreateButton(GameObject parent, string txt, Vector2 position, UnityEngine.Events.UnityAction action) { GameObject buttonObj = new GameObject("Button"); buttonObj.transform.SetParent(parent.transform, false); Image image = buttonObj.AddComponent<Image>(); image.color = new Color(0.8f, 0.8f, 0.8f); Button button = buttonObj.AddComponent<Button>(); button.onClick.AddListener(action); Text text = CreateText(buttonObj, txt, Vector2.zero); text.alignment = TextAnchor.MiddleCenter; text.color = Color.black; RectTransform rectTransform = buttonObj.GetComponent<RectTransform>(); rectTransform.anchorMin = new Vector2(1, 0); rectTransform.anchorMax = new Vector2(1, 0); rectTransform.anchoredPosition = position; rectTransform.sizeDelta = new Vector2(160, 40); return button; } private void PlayerChoice(string choice) { switch (choice) { case "exercise": ModifyStat(ref player.Health, 5, 15); ModifyStat(ref player.Happiness, 1, 5); UpdateEventText("You exercised and gained health!"); break; case "socialize": ModifyStat(ref player.Relationships, 5, 15); ModifyStat(ref player.Happiness, 3, 8); UpdateEventText("You socialized and improved relationships!"); break; case "work": ModifyStat(ref player.Career, 5, 15); ModifyStat(ref player.Happiness, -5, -1); UpdateEventText("You worked hard and advanced your career!"); break; case "relax": ModifyStat(ref player.Happiness, 5, 15); ModifyStat(ref player.Health, 1, 5); UpdateEventText("You relaxed and improved your happiness!"); break; } } private void ModifyStat(ref float stat, int min, int max) { stat = Mathf.Clamp(stat + Random.Range(min, max + 1), 0, 100); } private void UpdateEventText(string message) { if (eventText != null) eventText.text = message; } private void ProcessDay() { player.Age++; ModifyStat(ref player.Health, -3, 0); ModifyStat(ref player.Relationships, -2, 0); ModifyStat(ref player.Career, -2, 0); ModifyStat(ref player.Happiness, -3, 0); if (Random.value < 0.2f) { int eventType = Random.Range(0, 4); string[] events = { "Health", "Relationship", "Career", "Happiness" }; ModifyStat(ref player.Health, eventType == 0 ? 5 : 0, eventType == 0 ? 15 : 0); ModifyStat(ref player.Relationships, eventType == 1 ? 5 : 0, eventType == 1 ? 15 : 0); ModifyStat(ref player.Career, eventType == 2 ? 5 : 0, eventType == 2 ? 15 : 0); ModifyStat(ref player.Happiness, eventType == 3 ? 5 : 0, eventType == 3 ? 15 : 0); UpdateEventText($"{events[eventType]} boost event!"); } CheckGameOver(); } private void CheckGameOver() { if (player.Health <= 0 || player.Relationships <= 0 || player.Career <= 0 || player.Happiness <= 0 || player.Age >= maxAge) { UpdateEventText($"Game Over! You lived for {player.Age} days."); SetButtonsInteractable(false); } } private void SetButtonsInteractable(bool interactable) { if (exerciseButton != null) exerciseButton.interactable = interactable; if (socializeButton != null) socializeButton.interactable = interactable; if (workButton != null) workButton.interactable = interactable; if (relaxButton != null) relaxButton.interactable = interactable; } private void GenerateEnvironment() { visualElements.Add(CreateHouse()); for (int i = 0; i < 5; i++) { visualElements.Add(CreateTree(Random.insideUnitSphere * 10)); } SetupCamera(); } private GameObject CreateHouse() { if (housePrefab != null) { return Instantiate(housePrefab, Vector3.zero, Quaternion.identity); } else { Debug.LogWarning("House prefab is missing. Using default cube."); GameObject house = GameObject.CreatePrimitive(PrimitiveType.Cube); house.transform.position = new Vector3(0, 0.5f, 0); house.transform.localScale = new Vector3(2, 1, 2); return house; } } private GameObject CreateTree(Vector3 position) { position.y = 0; if (treePrefab != null) { return Instantiate(treePrefab, position, Quaternion.identity); } else { Debug.LogWarning("Tree prefab is missing. Using default tree."); GameObject tree = new GameObject("Tree"); tree.transform.position = position; GameObject trunk = GameObject.CreatePrimitive(PrimitiveType.Cylinder); trunk.transform.SetParent(tree.transform); trunk.transform.localScale = new Vector3(0.5f, 2f, 0.5f); GameObject leaves = GameObject.CreatePrimitive(PrimitiveType.Sphere); leaves.transform.SetParent(tree.transform); leaves.transform.localPosition = new Vector3(0, 1.5f, 0); leaves.transform.localScale = new Vector3(2f, 2f, 2f); return tree; } } private void SetupCamera() { if (Camera.main != null) { Camera.main.transform.position = new Vector3(0, 10, -10); Camera.main.transform.LookAt(Vector3.zero); } } private void SetupWeatherSystem() { GameObject weatherObj = new GameObject("WeatherSystem"); weatherSystem = weatherObj.AddComponent<ParticleSystem>(); var main = weatherSystem.main; main.startLifetime = 5f; main.startSpeed = 1f; main.startSize = 0.1f; main.maxParticles = 1000; var emission = weatherSystem.emission; emission.rateOverTime = 100; var shape = weatherSystem.shape; shape.shapeType = ParticleSystemShapeType.Box; shape.scale = new Vector3(20, 1, 20); weatherObj.transform.position = new Vector3(0, 15, 0); } private void UpdateVisuals() { float wellbeing = (player.Health + player.Relationships + player.Career + player.Happiness) / 400f; if (visualElements.Count > 0 && visualElements[0] != null) { Renderer houseRenderer = visualElements[0].GetComponent<Renderer>(); if (houseRenderer != null) { houseRenderer.material.color = Color.Lerp(Color.red, Color.green, wellbeing); } } for (int i = 1; i < visualElements.Count; i++) { if (visualElements[i] != null) { Renderer[] renderers = visualElements[i].GetComponentsInChildren<Renderer>(); foreach (Renderer r in renderers) { if (r != null) { r.material.color = Color.Lerp(Color.white, Color.green, Random.Range(0.5f, 1f)); } } } } if (backgroundImage != null) { float timeOfDay = (currentTime / dayLength) % 1f; backgroundImage.color = Color.Lerp(new Color(0.8f, 0.9f, 1f), new Color(0.1f, 0.1f, 0.3f), timeOfDay); } } private void UpdateWeather() { if (weatherSystem != null) { var main = weatherSystem.main; main.startColor = Color.Lerp(Color.clear, Color.white, Mathf.PingPong(Time.time * 0.1f, 1f)); } } private void UpdateUI() { if (healthText != null) healthText.text = $"Health: {Mathf.Round(player.Health)}"; if (relationshipsText != null) relationshipsText.text = $"Relationships: {Mathf.Round(player.Relationships)}"; if (careerText != null) careerText.text = $"Career: {Mathf.Round(player.Career)}"; if (ageText != null) ageText.text = $"Age: {player.Age}"; if (happinessText != null) happinessText.text = $"Happiness: {Mathf.Round(player.Happiness)}"; } }
Editor is loading...
Leave a Comment