// Update is called once per frame public virtual void Update() { if(!froze) { transform.position = Vector2.MoveTowards(transform.position, destination, speed*Time.deltaTime); //Transition to next cell if(Vector2.Distance(transform.position, destination)/Vector2.Distance(destination, current) < .5 && !transitioned) { World.WorldCoord destCoord = worldLocation+destDir; if(CanMoveInto(destCoord, destDir)) { transitioned = true; LeaveFrom(worldLocation, direction); MoveInto(destCoord, destDir, this.gameObject); worldLocation = destCoord; direction = destDir; } else { froze = true; } } if(transform.position == (Vector3)destination) { OnDestinationReached(); } } else { if(CanMoveInto(worldLocation+destDir, destDir)) froze = false; } }
public void TeleportTo(World.WorldCoord destCoord, World.WorldCoord newDir) { Assert.IsTrue(CanMoveInto(destCoord, newDir)); MoveInto(destCoord, newDir, this.gameObject); worldLocation = destCoord; direction = newDir; destDir = newDir; froze = false; current = GetWorldLocation(worldLocation, direction); destination = current; transform.position = current; MoveIfPossible(direction); }
public virtual void MoveIfPossible(World.WorldCoord dir) { Vector2 dest; //Try and move forward if(FindNewDestination(dir, out dest)) { ChangeDestination(dest); destDir = dir; //TODO: instantly snap to direction only for playable transform.right = new Vector2(dir.x, dir.y); } }
public override void Start() { base.Start(); inPath = new List<Car>(); inInfluence = new List<Car>(); var r = UnityEngine.Random.Range(0, (int)Mathf.Ceil(World.WORLD_WIDTH/2f)); var startTarget = new World.WorldCoord(r*2, -1); TeleportTo(startTarget, new World.WorldCoord(0, 1)); trainExitLocation = GameObject.FindGameObjectWithTag(trainExitTag); transform.position = trainExitLocation.transform.position; }
private void SpawnRandomCar() { World.WorldCoord teleLocation = new World.WorldCoord(Random.Range(0, World.WORLD_WIDTH), Random.Range(0, World.WORLD_HEIGHT)); World.WorldCoord teleDir = World.POSSIBLE_DIRECTIONS[Random.Range(0, World.POSSIBLE_DIRECTIONS.Length)]; int count = 0; while(!World.Instance.CanMoveInto(teleLocation, teleDir) || World.Instance.IsParkingSpot(teleLocation) || count >= MAX_GUESSES) { count++; teleLocation = new World.WorldCoord(Random.Range(0, World.WORLD_WIDTH), Random.Range(0, World.WORLD_HEIGHT)); teleDir = World.POSSIBLE_DIRECTIONS[Random.Range(0, World.POSSIBLE_DIRECTIONS.Length)]; } if(count < MAX_GUESSES) { Car newCar = ((GameObject)Instantiate(npcCarPrefab, Vector2.zero, Quaternion.identity)).GetComponent<Car>(); newCar.TeleportTo(teleLocation, teleDir); } }
public void Update() { // Find an empty spot. World.WorldCoord parkingSpot = new World.WorldCoord(0, 0); List<World.WorldCoord> parkingSpots = new List<World.WorldCoord>(); for (int i = 0; i < World.WORLD_WIDTH; ++ i) { for (int j = 0; j < World.WORLD_HEIGHT; ++j) { parkingSpot = new World.WorldCoord(i, j); if(World.Instance.IsParkingSpot(parkingSpot) && (World.Instance.ParkingSpotOpen(parkingSpot, World.POSSIBLE_DIRECTIONS[0]) || World.Instance.ParkingSpotOpen(parkingSpot, World.POSSIBLE_DIRECTIONS[1]) || World.Instance.ParkingSpotOpen(parkingSpot, World.POSSIBLE_DIRECTIONS[2]) || World.Instance.ParkingSpotOpen(parkingSpot, World.POSSIBLE_DIRECTIONS[3]))) { parkingSpots.Add(parkingSpot); break; } } } NPCCar[] npcCars = FindObjectsOfType(typeof(NPCCar)) as NPCCar[]; foreach(NPCCar npcCar in npcCars) { // Go to the nearest empty spot. int idxNearest = 0; int minDistance = int.MaxValue; for (int i = 0; i < parkingSpots.Count; ++ i) { int distance = Mathf.Abs(parkingSpots[i].x - npcCar.GetLocation().x) + Mathf.Abs(parkingSpots[i].y - npcCar.GetLocation().y); if (distance < minDistance) { idxNearest = i; } } if (parkingSpots.Count > 0) { SetDest(parkingSpots[idxNearest]); UpdateCar(npcCar); } else { // Fixed the NPC cars don't move when there is no spot. if (npcCar.transform.position == (Vector3)npcCar.GetDest()) { npcCar.MoveRandomDirection(); } } } }
public override void Update() { bool inputSensed = false; if(Input.GetAxis("Vertical") > 0) { inputSensed = true; nextDir = new World.WorldCoord(0, 1); } else if(Input.GetAxis("Vertical") < 0) { inputSensed = true; nextDir = new World.WorldCoord(0, -1); } else if(Input.GetAxis("Horizontal") < 0) { inputSensed = true; nextDir = new World.WorldCoord(-1, 0); } else if(Input.GetAxis("Horizontal") > 0) { inputSensed = true; nextDir = new World.WorldCoord(1, 0); } if(inputSensed) { if(inputResetRoutine != null) StopCoroutine(inputResetRoutine); StartCoroutine(inputDelayReset()); } if(Input.GetKeyUp (KeyCode.H)) { if(!audioSource.isPlaying) audioSource.PlayOneShot(horn); } base.Update(); }
private void GenerateParkedCars() { List<World.CoordAndDir> parking = new List<World.CoordAndDir>(); for(int i = 0; i < World.WORLD_WIDTH; i++) { for(int j = 0; j < World.WORLD_HEIGHT; j++) { var coord = new World.WorldCoord(i, j); if(World.Instance.IsParkingSpot(coord)) { if(World.Instance.CanMoveInto(coord, new World.WorldCoord(1, 0))) { parking.Add(new World.CoordAndDir(coord, new World.WorldCoord(1, 0))); parking.Add(new World.CoordAndDir(coord, new World.WorldCoord(-1, 0))); } else { parking.Add(new World.CoordAndDir(coord, new World.WorldCoord(0, 1))); parking.Add(new World.CoordAndDir(coord, new World.WorldCoord(0, -1))); } } } } for(int i = 0; i < numberOfOpenSpots; i++) { parking.RemoveAt(Random.Range(0, parking.Count)); } foreach(var pair in parking) { Car car = ((GameObject)Instantiate(standInCarPrefab, Vector2.zero, Quaternion.identity)).GetComponent<Car>(); car.GetComponentInChildren<StandInCar> ().ChangeColor (Random.Range (0,8)); car.TeleportTo(pair.c, pair.d); } }
private void SetDest(World.WorldCoord i_destination) { destination = i_destination; }
// Use this for initialization public override void Start() { audioSource = GetComponent<AudioSource> (); nextDir = direction; }
private IEnumerator inputDelayReset() { yield return new WaitForSeconds(INPUT_DELAY); nextDir = direction; }
protected void unhingeSelf() { //Leave towards left direction = new World.WorldCoord(-1, 0); GetComponent<BoxCollider2D>().enabled = false; foreach(var car in inPath) { car.StopWaiting(this); } FreeInGrasp(); }