// private int searchLimit = 100; public void SetUp(ScenarioData scenario, IntPoint2D startTile, ScenarioMgr.Direction startingDir, bool wandering, IntPoint2D dest, HouseManager house, bool movingIn) { Debug.Log("in citizen set up"); scenarioInfo = scenario; facing = startingDir; curTile = startTile; this.wandering = wandering; this.destTile = dest; Debug.Log("start tile: " + startTile.ToString()); Debug.Log("dest tile: " + destTile.ToString()); this.myHouse = house; this.movingIn = movingIn; havePath = false; startPath = false; if (!wandering) { Debug.Log("Starting TakePath Coroutine from SetUp"); StartCoroutine(TakePath()); } else { StartCoroutine(Wander()); } }
public GameObject GetWellWalker(IntPoint2D tileLoc, ScenarioMgr.Direction facing) { //Debug.Log ("Retrieving well walker"); Vector3 topLeft = scenario.ComputeTopLeftPointOfTile(tileLoc); GameObject walker; walker = (GameObject)Instantiate(wellWalkerPrefab); walker.transform.position = topLeft + new Vector3(walkerHorizOffset, walkerVertOffset, walkerHorizOffset); walker.transform.rotation = Quaternion.identity; if (facing == ScenarioMgr.Direction.Up) { walker.transform.Rotate(0, 90, 0); } else if (facing == ScenarioMgr.Direction.Left) { walker.transform.Rotate(0, 180, 0); } else if (facing == ScenarioMgr.Direction.Down) { walker.transform.Rotate(0, 270, 0); } walker.SetActive(true); return(walker); }
public void SetUp(ScenarioData scenario, IntPoint2D startTile, ScenarioMgr.Direction startingDir, bool leftTurns) { //Debug.Log ("in well walker set up"); scenarioInfo = scenario; facing = startingDir; tilesToGo = maxWellWalkerDistance; turningLeft = leftTurns; curTile = startTile; StartCoroutine("Walk"); }
public void SetUp(ScenarioData scenario, IntPoint2D startTile, ScenarioMgr.Direction startingDir, Stack <IntPoint2D> myPath, StoreManager store, HouseManager house) { Debug.Log("heading to store"); scenarioInfo = scenario; facing = startingDir; curTile = startTile; this.wandering = false; this.destStore = store; path = myPath; myHouse = house; movingIn = false; goingToHouse = false; goingToStore = true; startPath = false; havePath = true; StartCoroutine(TakePath()); }
IEnumerator TakePath() { bool havePath = true; bool stillTraveling = true; Vector3 startPos = gameObject.transform.position, endPos = startPos; Quaternion startAngle = gameObject.transform.rotation; Quaternion endAngle = startAngle; while (stillTraveling) { if (havePath && path.Count == 0) { // handle arrival at store amtToDeliver = destStore.ReceiveGoods(amtToDeliver); if (amtToDeliver == 0) { stillTraveling = false; Destroy(gameObject, 0.1f); } else { haveDestStore = havePath = false; } } while (stillTraveling && !havePath) { if (haveDestStore) { havePath = GetPathToStore(); } else { havePath = GetPathToAllStores(); } if (!havePath) { yield return(new WaitForSeconds(1)); } } if (stillTraveling && havePath) { IntPoint2D nextTile = path.Pop(); //Debug.Log ("moving to " + nextTile.ToString ()); startPos = gameObject.transform.position; //Debug.Log ("StartPos: " + startPos.ToString ()); ScenarioMgr.Direction nextFacing = facing; //check validity of tile if (!scenarioInfo.IsRoadTile(nextTile)) { havePath = false; yield return(new WaitForSeconds(1)); } else { // compute direction if (curTile.xCoord > nextTile.xCoord) { //Debug.Log ("headed left"); nextFacing = ScenarioMgr.Direction.Left; } else if (curTile.xCoord < nextTile.xCoord) { //Debug.Log ("headed right"); nextFacing = ScenarioMgr.Direction.Right; } else if (curTile.yCoord > nextTile.yCoord) { //Debug.Log ("headed up"); nextFacing = ScenarioMgr.Direction.Up; } else if (curTile.yCoord < nextTile.yCoord) { //Debug.Log ("headed down"); nextFacing = ScenarioMgr.Direction.Down; } else { stillTraveling = false; } if (stillTraveling) { // compute ending position switch (nextFacing) { case ScenarioMgr.Direction.Left: endPos = startPos + new Vector3(-1, 0, 0); endAngle = Quaternion.Euler(0, 180, 0); break; case ScenarioMgr.Direction.Up: endPos = startPos + new Vector3(0, 0, -1); endAngle = Quaternion.Euler(0, 90, 0); break; case ScenarioMgr.Direction.Down: endPos = startPos + new Vector3(0, 0, 1); endAngle = Quaternion.Euler(0, 270, 0); break; case ScenarioMgr.Direction.Right: endPos = startPos + new Vector3(1, 0, 0); endAngle = Quaternion.Euler(0, 0, 0); break; } //Debug.Log ("endPos: " + endPos.ToString ()); float elapsedTime = 0; if (facing != nextFacing) { // first handle the rotation while (elapsedTime < this.turnTime) { this.gameObject.transform.rotation = Quaternion.Slerp(startAngle, endAngle, (elapsedTime / this.turnTime)); elapsedTime += Time.deltaTime; yield return(0); } } // move to tile float moveTime = this.tileMoveTime; if (!scenarioInfo.IsRoadTile(curTile)) { moveTime = moveTime * 2; } elapsedTime = 0; while (elapsedTime < moveTime) { this.gameObject.transform.position = Vector3.Lerp(startPos, endPos, (elapsedTime / moveTime)); elapsedTime += Time.deltaTime; yield return(0); } // fix data for next tile this.facing = nextFacing; this.curTile = nextTile; startPos = endPos; startAngle = endAngle; } } } yield return(0); } }
IEnumerator TakePath() { bool stillTraveling = true; Vector3 startPos = gameObject.transform.position, endPos = startPos; Quaternion startAngle = gameObject.transform.rotation; Quaternion endAngle = startAngle; while (stillTraveling) { if (goingToHouse && curTile.Equals(destTile) || goingToStore && havePath && path.Count == 0) { stillTraveling = false; } else { if (!havePath) { // compute path from curTile to dest if (goingToHouse) { path = scenarioInfo.ComputePath(curTile, this.destTile, true); havePath = true; } else if (goingToStore) { havePath = GetPathToAllStores(); } else { stillTraveling = false; } yield return(0); } if (path.Count == 0) // got no path: wait for the world to change { yield return(new WaitForSeconds(1)); } else { IntPoint2D nextTile = path.Pop(); startPos = gameObject.transform.position; ScenarioMgr.Direction nextFacing = facing; //check validity of tile if (!scenarioInfo.IsPassableTile(nextTile)) { if (goingToHouse && nextTile.Equals(destTile) || goingToStore && havePath && path.Count == 0) { stillTraveling = false; } else if (goingToStore) { havePath = GetPathToStore(); } else { havePath = false; yield return(new WaitForSeconds(1)); } } else { // compute direction if (curTile.xCoord > nextTile.xCoord) { nextFacing = ScenarioMgr.Direction.Left; } else if (curTile.xCoord < nextTile.xCoord) { nextFacing = ScenarioMgr.Direction.Right; } else if (curTile.yCoord > nextTile.yCoord) { nextFacing = ScenarioMgr.Direction.Up; } else if (curTile.yCoord < nextTile.yCoord) { nextFacing = ScenarioMgr.Direction.Down; } else { stillTraveling = false; } if (stillTraveling) { endPos = scenarioInfo.ComputeTopLeftPointOfTile(nextTile) + citizenTileOffset; // compute ending position switch (nextFacing) { case ScenarioMgr.Direction.Left: endAngle = Quaternion.Euler(0, 180, 0); break; case ScenarioMgr.Direction.Up: endAngle = Quaternion.Euler(0, 90, 0); break; case ScenarioMgr.Direction.Down: endAngle = Quaternion.Euler(0, 270, 0); break; case ScenarioMgr.Direction.Right: endAngle = Quaternion.Euler(0, 0, 0); break; } float elapsedTime = 0; if (facing != nextFacing) { // first handle the rotation while (elapsedTime < this.turnTime) { this.gameObject.transform.rotation = Quaternion.Slerp(startAngle, endAngle, (elapsedTime / this.turnTime)); elapsedTime += Time.deltaTime; yield return(0); } } // move to tile float moveTime = this.tileMoveTime; if (!scenarioInfo.IsRoadTile(curTile)) { moveTime = moveTime * 2; } elapsedTime = 0; while (elapsedTime < moveTime) { this.gameObject.transform.position = Vector3.Lerp(startPos, endPos, (elapsedTime / moveTime)); elapsedTime += Time.deltaTime; yield return(0); } // fix data for next tile this.facing = nextFacing; this.curTile = nextTile; startPos = gameObject.transform.position; startAngle = endAngle; } } } } yield return(0); } Debug.Log("at destination"); if (movingIn && myHouse != null) { myHouse.RecordArrival(); } else if (goingToHouse && myHouse != null && goods > 0) { myHouse.ReceiveResource2(goods); } if (goingToStore && destStore != null) { DoStoreStuff(); } else { // done with this citizen basket.SetActive(false); Destroy(gameObject); // destroy me } }
IEnumerator Wander() { Vector3 startPos = gameObject.transform.position, endPos = startPos; Quaternion startAngle = gameObject.transform.rotation; Quaternion endAngle = startAngle; bool rotating = false; ScenarioMgr.Direction nextFacing = facing; IntPoint2D nextTile = curTile; while (wandering) { bool goingNowhere = false; int x = curTile.xCoord, y = curTile.yCoord; IntPoint2D tileAhead = curTile, tileLeft = curTile, tileRight = curTile, tileBehind = curTile; switch (facing) { case ScenarioMgr.Direction.Left: tileAhead = new IntPoint2D(x - 1, y); tileRight = new IntPoint2D(x, y - 1); tileLeft = new IntPoint2D(x, y + 1); tileBehind = new IntPoint2D(x + 1, y); break; case ScenarioMgr.Direction.Up: tileLeft = new IntPoint2D(x - 1, y); tileAhead = new IntPoint2D(x, y - 1); tileBehind = new IntPoint2D(x, y + 1); tileRight = new IntPoint2D(x + 1, y); break; case ScenarioMgr.Direction.Right: tileBehind = new IntPoint2D(x - 1, y); tileRight = new IntPoint2D(x, y + 1); tileLeft = new IntPoint2D(x, y - 1); tileAhead = new IntPoint2D(x + 1, y); break; case ScenarioMgr.Direction.Down: tileRight = new IntPoint2D(x - 1, y); tileBehind = new IntPoint2D(x, y - 1); tileAhead = new IntPoint2D(x, y + 1); tileLeft = new IntPoint2D(x + 1, y); break; } // pick direction int p = Random.Range(0, 100); if (p < 30) { goingNowhere = true; rotating = false; } else if (p < 60 && scenarioInfo.IsPassableTile(tileAhead)) { rotating = false; nextFacing = facing; nextTile = tileAhead; } else if (p < 80 && scenarioInfo.IsPassableTile(tileLeft)) { rotating = true; nextTile = tileLeft; nextFacing = ScenarioMgr.GetLeftTurn(facing); } else if (scenarioInfo.IsPassableTile(tileRight)) { rotating = true; nextTile = tileRight; nextFacing = ScenarioMgr.GetRightTurn(facing); } else if (scenarioInfo.IsPassableTile(tileBehind)) { rotating = true; nextTile = tileBehind; nextFacing = ScenarioMgr.GetReverseDirection(facing); } else { goingNowhere = true; rotating = false; } startPos = transform.position; startAngle = gameObject.transform.rotation; if (goingNowhere) { //Debug.Log("no road to move to"); // no rotation, no movement, no nothing -- just wait things out for a second yield return(new WaitForSeconds(tileMoveTime)); } else { endPos = scenarioInfo.ComputeTopLeftPointOfTile(nextTile) + citizenTileOffset; // compute ending position switch (nextFacing) { case ScenarioMgr.Direction.Left: endAngle = Quaternion.Euler(0, 180, 0); break; case ScenarioMgr.Direction.Up: endAngle = Quaternion.Euler(0, 90, 0); break; case ScenarioMgr.Direction.Down: endAngle = Quaternion.Euler(0, 270, 0); break; case ScenarioMgr.Direction.Right: endAngle = Quaternion.Euler(0, 0, 0); break; } float elapsedTime = 0; if (rotating) { // first handle the rotation while (elapsedTime < this.turnTime) { this.gameObject.transform.rotation = Quaternion.Slerp(startAngle, endAngle, (elapsedTime / this.turnTime)); elapsedTime += Time.deltaTime; yield return(0); } } //Debug.Log("starting movement from " + startPos.ToString() + " to " + endPos.ToString()); //Debug.Log("from tile " + curTile.ToString() + " to " + nextTile.ToString()); // move float moveTime = this.tileMoveTime; if (!scenarioInfo.IsRoadTile(curTile)) { moveTime = moveTime * 2; } elapsedTime = 0; while (elapsedTime <= moveTime) { this.gameObject.transform.position = Vector3.Lerp(startPos, endPos, (elapsedTime / moveTime)); elapsedTime += Time.deltaTime; yield return(0); } // fix data for next tile this.facing = nextFacing; this.curTile = nextTile; startPos = this.gameObject.transform.position; startAngle = endAngle; } } //Debug.Log("Starting takepath coroutine from Wander"); //startPath = true; }
IEnumerator Walk() { Vector3 startPos = gameObject.transform.position, endPos = startPos; Quaternion startAngle = gameObject.transform.rotation; Quaternion endAngle = startAngle; bool rotating = false; ScenarioMgr.Direction nextFacing = facing; IntPoint2D nextTile = curTile; while (tilesToGo > 0) { //Debug.Log("doing distribution"); // do water delivery scenarioInfo.DistributeWater(new IntPoint2D(curTile.xCoord, curTile.yCoord - 1)); scenarioInfo.DistributeWater(new IntPoint2D(curTile.xCoord, curTile.yCoord + 1)); scenarioInfo.DistributeWater(new IntPoint2D(curTile.xCoord - 1, curTile.yCoord)); scenarioInfo.DistributeWater(new IntPoint2D(curTile.xCoord + 1, curTile.yCoord)); // check for road tile ahead and turn appropriately int x = curTile.xCoord, y = curTile.yCoord; IntPoint2D tileAhead = curTile, tileLeft = curTile, tileRight = curTile, tileBehind = curTile; switch (facing) { case ScenarioMgr.Direction.Left: tileAhead = new IntPoint2D(x - 1, y); tileRight = new IntPoint2D(x, y - 1); tileLeft = new IntPoint2D(x, y + 1); tileBehind = new IntPoint2D(x + 1, y); break; case ScenarioMgr.Direction.Up: tileLeft = new IntPoint2D(x - 1, y); tileAhead = new IntPoint2D(x, y - 1); tileBehind = new IntPoint2D(x, y + 1); tileRight = new IntPoint2D(x + 1, y); break; case ScenarioMgr.Direction.Right: tileBehind = new IntPoint2D(x - 1, y); tileRight = new IntPoint2D(x, y + 1); tileLeft = new IntPoint2D(x, y - 1); tileAhead = new IntPoint2D(x + 1, y); break; case ScenarioMgr.Direction.Down: tileRight = new IntPoint2D(x - 1, y); tileBehind = new IntPoint2D(x, y - 1); tileAhead = new IntPoint2D(x, y + 1); tileLeft = new IntPoint2D(x + 1, y); break; } startPos = transform.position; startAngle = gameObject.transform.rotation; bool goingNowhere = false; if (scenarioInfo.IsRoadTile(tileAhead)) { // no rotation required, just moving straight ahead rotating = false; nextFacing = facing; nextTile = tileAhead; } else { rotating = true; // we can't just go straight ahead, so we're doing some sort of rotation if (turningLeft) { // check left first if (scenarioInfo.IsRoadTile(tileLeft)) { // turning left nextTile = tileLeft; nextFacing = ScenarioMgr.GetLeftTurn(facing); } else if (scenarioInfo.IsRoadTile(tileRight)) { // turn right because we can't turn left nextTile = tileRight; nextFacing = ScenarioMgr.GetRightTurn(facing); } else if (scenarioInfo.IsRoadTile(tileBehind)) { nextTile = tileBehind; nextFacing = ScenarioMgr.GetReverseDirection(facing); } else { goingNowhere = true; rotating = false; } } else { // check right first if (scenarioInfo.IsRoadTile(tileRight)) { nextTile = tileRight; nextFacing = ScenarioMgr.GetRightTurn(facing); } else if (scenarioInfo.IsRoadTile(tileLeft)) { // turning left because we can't turn right nextTile = tileLeft; nextFacing = ScenarioMgr.GetLeftTurn(facing); } else if (scenarioInfo.IsRoadTile(tileBehind)) { nextTile = tileBehind; nextFacing = ScenarioMgr.GetReverseDirection(facing); } else { goingNowhere = true; rotating = false; } } } if (goingNowhere) { //Debug.Log("no road to move to"); // no rotation, no movement, no nothing -- just wait things out for a second yield return(new WaitForSeconds(tileMoveTime)); } else { // compute ending position switch (nextFacing) { case ScenarioMgr.Direction.Left: endPos = startPos + new Vector3(-1, 0, 0); endAngle = Quaternion.Euler(0, 180, 0); break; case ScenarioMgr.Direction.Up: endPos = startPos + new Vector3(0, 0, -1); endAngle = Quaternion.Euler(0, 90, 0); break; case ScenarioMgr.Direction.Down: endPos = startPos + new Vector3(0, 0, 1); endAngle = Quaternion.Euler(0, 270, 0); break; case ScenarioMgr.Direction.Right: endPos = startPos + new Vector3(1, 0, 0); endAngle = Quaternion.Euler(0, 0, 0); break; } float elapsedTime = 0; if (rotating) { // first handle the rotation while (elapsedTime < this.turnTime) { this.gameObject.transform.rotation = Quaternion.Slerp(startAngle, endAngle, (elapsedTime / this.turnTime)); elapsedTime += Time.deltaTime; yield return(0); } } //Debug.Log("starting movement from " + startPos.ToString() + " to " + endPos.ToString()); //Debug.Log("from tile " + curTile.ToString() + " to " + nextTile.ToString()); // move elapsedTime = 0; while (elapsedTime < this.tileMoveTime) { this.gameObject.transform.position = Vector3.Lerp(startPos, endPos, (elapsedTime / this.tileMoveTime)); elapsedTime += Time.deltaTime; yield return(0); } // fix data for next tile this.facing = nextFacing; this.curTile = nextTile; startPos = endPos; startAngle = endAngle; } tilesToGo--; } Destroy(gameObject); }
//this is a well, so it always has plenty of its resource -- we don't have to worry about gathering, just distributing IEnumerator Distribute() { bool hadRoadLastTime = false; int roadTileIndex = -1; IntPoint2D [] neighborTiles = new IntPoint2D[4]; bool goingLeft = true; //The building isn't going to move, so compute the 4 neighboring tiles up front left=0, up=1, right=2, down=3; neighborTiles[0] = new IntPoint2D(buildingTile.xCoord - 1, buildingTile.yCoord); neighborTiles[1] = new IntPoint2D(buildingTile.xCoord, buildingTile.yCoord - 1); neighborTiles[2] = new IntPoint2D(buildingTile.xCoord + 1, buildingTile.yCoord); neighborTiles[3] = new IntPoint2D(buildingTile.xCoord, buildingTile.yCoord + 1); yield return(new WaitForSeconds(1)); while (true) { bool haveRoad = false; // check for a road if (hadRoadLastTime) { if (this.scenarioInfo.IsRoadTile(neighborTiles[roadTileIndex])) { haveRoad = true; } } for (int i = 0; !haveRoad && i < 4; i++) { if (this.scenarioInfo.IsRoadTile(neighborTiles[i])) { haveRoad = true; roadTileIndex = i; } } hadRoadLastTime = haveRoad; // if one is found, set up a walker and sleep for 30 secs //Debug.Log ("numWorkers at well is " + numWorkers.ToString()); if (haveRoad && numWorkers >= kMaxWorkers) { //Debug.Log("making a walker from a well"); ScenarioMgr.Direction facing = ScenarioMgr.Direction.Left; // figure out which direction the walker should be facing; if (goingLeft) { if (roadTileIndex == 0) { facing = ScenarioMgr.Direction.Down; } else if (roadTileIndex == 1) { facing = ScenarioMgr.Direction.Left; } else if (roadTileIndex == 2) { facing = ScenarioMgr.Direction.Up; } else { facing = ScenarioMgr.Direction.Right; } } else { if (roadTileIndex == 0) { facing = ScenarioMgr.Direction.Up; } else if (roadTileIndex == 1) { facing = ScenarioMgr.Direction.Right; } else if (roadTileIndex == 2) { facing = ScenarioMgr.Direction.Down; } else { facing = ScenarioMgr.Direction.Left; } } //get the walker created and working GameObject walker = walkerPool.GetWellWalker(neighborTiles[roadTileIndex], facing); walker.SetActive(true); //Debug.Log(walker.transform.position); //get the walker's script and get things working WellWalkerManager walkerMgr = walker.GetComponent("WellWalkerManager") as WellWalkerManager; walkerMgr.SetUp(this.scenarioInfo, neighborTiles[roadTileIndex], facing, goingLeft); goingLeft = !goingLeft; yield return(new WaitForSeconds(30)); } else { // if not, sleep for 1 second yield return(new WaitForSeconds(1)); } } }