void Update() { if (Input.GetKeyDown("p")) { if (Time.timeScale == 1) { Time.timeScale = 0; } else { Time.timeScale = 1; } } if (!EventSystem.current.IsPointerOverGameObject()) { if (selectedEntityType != null) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, 300, LayerMask.GetMask("Ground", "WorldObject"))) { Vector3 locationOnGrid = hit.point; if (terrainActions != null) { locationOnGrid = terrainActions.FindGridLocation(hit.point, currentBuildHeight); } //Vector3 locationOnGrid = terrainActions.FindGridLocation(hit.point, currentBuildHeight); locationOnGrid.y = hit.point.y; // Place selected entity outline in the world at hit location if (entityGhost != null) { if (!entityGhost.activeSelf) { entityGhost.SetActive(true); } entityGhost.transform.position = locationOnGrid; // TODO: What about moving agents? - Do a OnTriggerEnter for the ghosts which could just clear out previousGhostPosition if (previousGhostPosition != entityGhost.transform.position) { buildableStatus = GetBuildableStatus(selectedEntityType, entityGhost); if (selectedEntityType.rotateToTerrain) { // Rotate and transform ghost so that it is just above the terrain entityGhost.transform.up = hit.normal; } else if (terrainActions != null) { // Calculate distance to move entityGhost in its up direction (normal to terrain) terrainActions.MoveObjectAboveTerrain(entityGhost); } previousGhostPosition = entityGhost.transform.position; //TODO: Makes a utility function for changing all of a nested objects materials Renderer[] renderers = entityGhost.GetComponentsInChildren <Renderer>(); if (renderers.Length == 0) { Debug.LogError("Entity Ghost has no renderer! " + entityGhost.name); } Material material; if (buildableStatus == BuildableStatus.Invalid) { material = invalidPlacementMaterial; } else { material = validPlacementMaterial; } for (int i = 0; i < renderers.Length; i++) { if (renderers[i].material != material) { Material[] materials = new Material[renderers[i].materials.Length]; for (var j = 0; j < renderers[i].materials.Length; j++) { materials[j] = material; } renderers[i].materials = materials; } } } if (Input.GetKeyDown("r")) { entityGhost.transform.Rotate(new Vector3(0, 90, 0)); previousGhostPosition = Vector3.zero; } } if (Input.GetMouseButtonDown(0)) { Quaternion rotation = Quaternion.identity; Vector3 scale = Vector3.one; if (entityGhost != null) { rotation = entityGhost.transform.rotation; scale = entityGhost.transform.localScale; // TODO: Fix this so its not so much of a hack - the .02 prevents the flashing from ghost over placed object // Could make every ghost be .02 larger and then always subtract .02 - also only do scale is FI can be scaled if (scale.x.ToString().IndexOf(".02") != -1) { scale.x = Mathf.Round(scale.x); scale.y = Mathf.Round(scale.y); scale.z = Mathf.Round(scale.z); } } // Create Entity with owner permissions set to selected faction and the selected agent if not in overhead view List <Agent> owners = new List <Agent>(); //if (!overheadCamera.enabled) // owners.Add(agent); GameObject newGameObject; if (entityGhost != null) { newGameObject = selectedEntityType.CreateEntity(prefabVariantIndex, entityGhost.transform.position, rotation, scale, agent); } else { newGameObject = selectedEntityType.CreateEntity(prefabVariantIndex, locationOnGrid, rotation, scale, agent); } if (selectedEntityType is AgentType) { // Add agent to the list of agents newGameObject.name = selectedEntityType.name; Agent a = newGameObject.GetComponent <Agent>(); agents.Add(a); AddAgent(a); } } } else if (entityGhost != null && entityGhost.activeSelf) { entityGhost.SetActive(false); } } else { // No Entity Selected - Allow player to select entities with mouse click // TODO: Add a create box select (hold mouse button down) and multi-entity select if (Input.GetMouseButtonDown(0)) { Debug.Log("Mouse Button Clicked"); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); Entity entity = null; if (for2D) { RaycastHit2D[] hits = Physics2D.RaycastAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, LayerMask.GetMask("Agent", "WorldObject", "AgentEvent")); if (hits.Length > 0) { float bestDistance = float.PositiveInfinity; foreach (RaycastHit2D hit in hits) { if (hit.distance < bestDistance) { Entity[] hitEntities = hit.collider.gameObject.GetComponentsInParent <Entity>(); foreach (Entity hitEntity in hitEntities) { if (hitEntity.inEntityInventory == null && hitEntity.enabled) { bestDistance = hit.distance; entity = hitEntity; } } } } } } else { RaycastHit[] hits = Physics.RaycastAll(ray, 500, LayerMask.GetMask("Agent", "WorldObject", "AgentEvent"), QueryTriggerInteraction.Collide); if (hits.Length > 0) { float bestDistance = float.PositiveInfinity; foreach (RaycastHit hit in hits) { if (hit.distance < bestDistance) { Entity[] hitEntities = hit.collider.gameObject.GetComponentsInParent <Entity>(); foreach (Entity hitEntity in hitEntities) { if (hitEntity.inEntityInventory == null && hitEntity.enabled) { bestDistance = hit.distance; entity = hitEntity; } } } } } } } } } if (driveSliders == null) { Transform driveTypeParent = transform.Find("DriveType Levels"); driveSliders = new List <Slider>(); int i = 0; foreach (DriveType driveType in agent.ActiveDrives().Keys) { if (driveType.showInUI) { RectTransform newDriveType = Instantiate(driveTypeSliderPrefab, driveTypeParent); newDriveType.transform.position = new Vector3(newDriveType.transform.position.x, newDriveType.transform.position.y - 30 * i); newDriveType.GetComponentInChildren <TextMeshProUGUI>().text = driveType.name; driveSliders.Add(newDriveType.GetComponentInChildren <Slider>()); ++i; } } } driveText.text = ""; int sliderIndex = 0; foreach (var drive in agent.ActiveDrives()) { if (drive.Key.name != "None" && drive.Key.showInUI) { float driveLevel = drive.Value.GetLevel(); driveText.text += drive.Key.name + ": " + Mathf.Round(driveLevel) + "\n"; driveSliders[sliderIndex].value = driveLevel / 100f; sliderIndex++; } } drivesRankedText.text = ""; int driveNumber = 1; if (agent.decider.currentDriveTypesRanked != null) { foreach (KeyValuePair <DriveType, float> drive in agent.decider.currentDriveTypesRanked) { drivesRankedText.text += driveNumber + ": " + drive.Key.name + " (" + System.Math.Round(drive.Value, 3) + ")\n"; ++driveNumber; } } if (agent.decider.CurrentDriveType != null) { drivesRankedText.text += "Current: " + agent.decider.CurrentDriveType.name + "\n"; } else { drivesRankedText.text += "Current: None\n"; } if (showAgentsMappingType) { for (int i = 0; i < markerTexts.Count; i++) { if (!agents[i].gameObject.activeSelf) { if (agents[i] == agent) { HandleCameraSwitching(true); } agents.Remove(agents[i]); GameObject item = markerTexts[i].gameObject; markerTexts.Remove(markerTexts[i]); Destroy(item); continue; } SetMarkerAboveObject(agents[i].gameObject, markerTexts[i]); markerTexts[i].color = Color.white; if (agents[i].decider.CurrentMapping != null) { markerTexts[i].text = agents[i].decider.CurrentMapping.mappingType.name; } else { markerTexts[i].text = "None"; } /* * // TODO: Get this to float up and not cover the mappingType name * HistoryType.OutputChangeLog outputChangeLog = agents[i].historyType.GetLastOutputChangeLog(agents[i]); * if (outputChangeLog != null && outputChangeLog.time > Time.time - 2f) * { * markerTexts[i].text = outputChangeLog.outputChange.ToString(); * if (outputChangeLog.outputChange.floatValue != outputChangeLog.amount) * markerTexts[i].text += "\nActual Amount = " + outputChangeLog.amount; * if (outputChangeLog.succeeded) * markerTexts[i].color = Color.green; * else * markerTexts[i].color = Color.red; * } */ } } HandleCameraSwitching(); // TODO: Only do this every X seconds nameText.text = agent.name + " - " + agent.CurrentAge(); timeText.text = timeManager.PrettyPrintDateTime(); }
public static string ToString(BuildableStatus status) { switch (status) { case BuildableStatus.NotEnoughFarm: return "Không đủ farm"; case BuildableStatus.NotEnoughWood: return "Không đủ gỗ"; case BuildableStatus.NotEnoughClay: return "Không đủ gạch"; case BuildableStatus.NotEnoughIron: return "Không đủ kim loại"; case BuildableStatus.BuildNumberExceed: return "Chỉ xây được 5 công trình"; case BuildableStatus.BuildingLevelExceed: return "Vượt quá level"; case BuildableStatus.JustDoIt: return "Có thể xây dựng"; case BuildableStatus.RequirementNotMet: return "Chưa đủ điều kiện xây dựng"; default: return ""; } }