public void Init(cellScript cell) { base.Init(); segController = GameObject.FindObjectOfType <SegregationController>(); currentCell = cell; this.transform.position = cell.transform.position; currentCell.isOccupied = true; currentCell.occupier = this; this.transform.localScale = Vector3.one * segController.cellSizeInGameWorld; agentType = SegregationUtilities.agentType.RED; // if(Random.Range(0f,1.0f) < 0.5f){ if (segController.rand.NextDouble() < 0.5f) { agentType = SegregationUtilities.agentType.GREEN; } if (agentType == SegregationUtilities.agentType.RED) { GetComponent <Renderer>().material.color = Color.red; } else { GetComponent <Renderer>().material.color = Color.green; } CreateStepper(FindNeighbours, 2, 100); CreateStepper(CheckNeighbourhoodIsOK, 2, 200); CreateStepper(Move, 2, 300, 1); }
private cellScript CheckCell(Vector3 direction) { Vector3 halfExtents = new Vector3(1f, 1f, 1f); Vector3 wallDetectionBox = new Vector3(0.25f, 0.25f, 0.25f); Collider[] colliders = Physics.OverlapBox(transform.position + direction * 3, halfExtents); foreach (var collider in colliders) { cellScript cell = collider.GetComponent <cellScript>(); if (cell != null) { Vector3 middle = (transform.position + cell.transform.position) / 2; if (Physics.OverlapBox(middle + Vector3.up * 2, wallDetectionBox).Length > 0) { neighbors.Add(cell, false); } else { neighbors.Add(cell, true); } } return(cell); } return(null); }
public void UpdateCurrentCell() { Vector3 halfExtents = new Vector3(1f, 1f, 1f); Collider[] colliders = Physics.OverlapBox(transform.position + Vector3.down * 3, halfExtents); current = colliders[0].GetComponent <cellScript>(); }
void GenerateAgents() { int envCapacity = environmentExtents.x * environmentExtents.y * environmentExtents.z; int numAgents = Mathf.CeilToInt(envCapacity * agentPopulationPerc); for (int i = 0; i < numAgents; i++) { GameObject a = Instantiate(agentPf); SegregationAgent sa = a.GetComponent <SegregationAgent>(); cellScript cell = FindFreeCell(); sa.Init(cell); } }
public cellScript FindFreeCell() { // cellScript cs = cells[Random.Range(0,cells.Length)]; // while(cs.isOccupied){ // cs = cells[Random.Range(0,cells.Length)]; // } // return (cs); cellScript cs = cells[rand.Next(cells.Length)]; while (cs.isOccupied) { cs = cells[rand.Next(cells.Length)]; } return(cs); }
void Move() { if (needToMove) { cellScript newCell = segController.FindFreeCell(); currentCell.isOccupied = false; currentCell.occupier = null; this.transform.position = newCell.transform.position; currentCell = newCell; currentCell.isOccupied = true; currentCell.occupier = this; Color c = GetComponent <Renderer>().material.color; c.a = 0.6f; GetComponent <Renderer>().material.color = c; } }
public Vector3 GetNextPosition(cellScript.Direction direction) { cellScript next = current.neighborInDirection[direction]; // there is a cell in this direction, ok if (next != null) { // check if that cell is movable or not if (current.neighbors[next]) { // if movable, then move // get x and z offset, y unchanged float xoffset = next.transform.position.x - transform.position.x; float zoffset = next.transform.position.z - transform.position.z; Vector3 motion = new Vector3(xoffset, 0.0f, zoffset); return(transform.position + motion); } // else stay } return(NotMovable); }