public UIContextData GetNearestContext() { UIContextData val = null; var minDistance = 9999999; var worldRadius = FindObjectOfType <World>().Radius; foreach (var wobj in FindObjectsOfType <WorldObject>()) { if (wobj.Context == null) { continue; } if (wobj.Context.type == UIContextType.player) { continue; } var distance = WorldObject.Distance(wobj, worldObject); if (WorldObject.AreClose(wobj, worldObject)) { if (distance < minDistance) { distance = minDistance; val = wobj.Context; } } } return(val); }
void Update() { switch (state) { case LumberjackState.idle: var trees = FindObjectsOfType <TreeDataObject>(); var minDistance = 1000.0f; foreach (var tree in trees) { if (tree.Destroyed || tree.Targeted) { continue; } var treeObject = tree.gameObject.GetComponent <WorldObject>(); var distance = WorldObject.Distance(treeObject, worldObject); if (distance < minDistance) { minDistance = distance; targetTree = tree.gameObject; } } if (targetTree != null) { state = LumberjackState.goingToTree; targetTree.GetComponent <TreeDataObject>().Targeted = true; } break; case LumberjackState.goingToTree: if (targetTree == null || targetTree.GetComponent <TreeDataObject>().Destroyed) { state = LumberjackState.idle; break; } Move(((((targetTree.GetComponent <WorldObject>().Angle - worldObject.Angle + 360 + 180) % 360) - 180) < 0)); if (WorldObject.AreClose(targetTree.GetComponent <WorldObject>(), worldObject)) { state = LumberjackState.chopping; StartCoroutine(DelayedChop()); } break; case LumberjackState.chopping: Move(Mathf.Sin(20 * Time.time) > 0); break; case LumberjackState.goingBack: break; } }
// Update is called once per frame void Update() { switch (state) { case CommonCitizenState.idle: var workplaces = FindObjectsOfType <WorkPlace>(); var minDistance = 1000.0f; foreach (var workplace in workplaces) { if (!workplace.ContextVisible || !workplace.CanHire()) { continue; } var workPlaceObject = workplace.gameObject.GetComponent <WorldObject>(); var distance = WorldObject.Distance(workPlaceObject, worldObject); if (distance < minDistance) { minDistance = distance; targetWorkPlace = workplace.gameObject; } } if (targetWorkPlace != null) { state = CommonCitizenState.goingToWork; } break; case CommonCitizenState.goingToWork: if (targetWorkPlace == null || !targetWorkPlace.GetComponent <WorkPlace>().CanHire()) { state = CommonCitizenState.idle; break; } Move(((((targetWorkPlace.GetComponent <WorldObject>().Angle - worldObject.Angle + 360 + 180) % 360) - 180) < 0)); if (WorldObject.AreClose(targetWorkPlace.GetComponent <WorldObject>(), worldObject)) { targetWorkPlace.GetComponent <WorkPlace>().Hire(gameObject); } break; } }
public IEnumerator SpawnNew(WorldObject wobj, float angle) { var allWobj = FindObjectsOfType <WorldObject>(); var newWobj = Instantiate <WorldObject>(wobj, this.transform); newWobj.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f); newWobj.Context.ContextVisible = false; var color = newWobj.sprite.color; newWobj.sprite.color = new Color(); bool validPosition = false; var initialAngle = angle; int loopCount = 0; while (!validPosition) { angle += 0.1f / FindObjectOfType <World>().radiusMultiplier; newWobj.SetAngle(angle); foreach (var oldwobj in allWobj) { validPosition = true; if (!oldwobj.BlocksSpawn) { continue; } if (oldwobj != null && WorldObject.AreClose(newWobj, oldwobj)) { validPosition = false; break; } } if (angle >= initialAngle + 360) { Destroy(newWobj.gameObject); Debug.Log("Cant spawn new thing"); break; } loopCount++; if (loopCount % 10 == 0) { yield return(null); } } if (validPosition) { newWobj.sprite.color = color; for (int i = 0; i < newWobj.Context.AnimationTime; i++) { newWobj.transform.localScale = ((float)i / newWobj.Context.AnimationTime) * new Vector3(1.0f, 1.0f, 1.0f); yield return(null); } newWobj.Context.ContextVisible = true; newWobj.Context.Initialize(); } }