public static GameObject CreatePlace(Place.Type type, int x, int y) { Quaternion startRotation = new Quaternion(); startRotation.eulerAngles = new Vector3(90f, 180f, 0); GameObject place = GameObject.CreatePrimitive(PrimitiveType.Plane); place.gameObject.transform.localScale = new Vector3(0.1f,0.1f,0.1f); place.gameObject.transform.rotation = startRotation; place.AddComponent<Place>(); switch (type) { case Place.Type.Bar: CreateBar(place); break; case Place.Type.Home: CreateHome(place); break; case Place.Type.Hospital: CreateHospital(place); break; case Place.Type.Office: CreateOffice(place); break; case Place.Type.Shop: CreateShop(place); break; } place.GetComponent<Place>().Category = type; place.GetComponent<Place>().GetComponent<Renderer>().material = (Material) Resources.Load(type.ToString()); place.GetComponent<Place>().X = x; place.GetComponent<Place>().Y = y; return place; }
private void VisitPlace(Place.Type type) { if (currentLocation == null || currentLocation.Category != type) { if (currentLocation != null) { currentLocation.Leave(this); currentLocation = null; } float bestDist = float.MaxValue; Place destination = null; // find nearest place foreach (Place p in GameObject.FindObjectsOfType<Place>().Where( (p) => { return p.Category == type; }) ) { float dist = Vector3.Distance(p.gameObject.transform.position, this.gameObject.transform.position); if (dist < bestDist) { destination = p; bestDist = dist; } } // decide if already there TODO: Collision checking if (bestDist < 0.2f) { destination.Visit(this); currentLocation = destination; } else { MoveTowards(destination); //Boredom++; //Happiness--; } } else --updateCount; }
// TODO: Unreachable places protected void MoveTowards(Place p) { if (p != null) { WorldMap.PathMap pm; if (IsInfected()) pm = FindObjectOfType<WorldMap>().InfectedDistance; else pm = FindObjectOfType<WorldMap>().Distance; Vector2 target = pm.NextPoint(Position(), p.Position()); if (pm.CalcDistance(Position(), target) < 10000) { gameObject.transform.LookAt(new Vector3(target.x, 0, target.y)); gameObject.transform.localPosition = Vector3.MoveTowards( gameObject.transform.localPosition, new Vector3(target.x, 0, target.y), BaseSpeed); } else Happiness--; } }