Exemplo n.º 1
0
    //Applies forces to neighbour and returns reaction force to this
    Vector3 ApplyTorqueToPair(int alphaIndex, int betaIndex)
    {
        CellNeighbour alphaNeighbour = GetNeighbour(alphaIndex);
        CellNeighbour betaNeighbour  = GetNeighbour(betaIndex);

        Vector3 alphaVector = alphaNeighbour.coreToThis; //Allways after = lower angle
        Vector3 betaVector  = betaNeighbour.coreToThis;  //Allways ahead = lower angle
        //float angle = Vector3.Angle(alphaVector, betaVector);
        float angle = LongAngle(alphaVector, betaVector);

        float goalAngle = CardinalDirectionHelper.GetAngleBetween(alphaIndex, betaIndex);
        //Debug.Log(this.id + " Spring: " + alphaNeighbour.cell.id + "-" + betaNeighbour.cell.id + " = GoalA: " + goalAngle + " A: " + angle);
        float diff = (goalAngle - angle);


        float   k1         = 0.0005f;
        float   k2         = 0.0003f;
        float   magnitude  = k1 * diff + Mathf.Sign(diff) * k2 * diff * diff;
        Vector3 alphaForce = Vector3.Cross(alphaVector, new Vector3(0f, 0f, 1f)) * magnitude;
        Vector3 betaForce  = Vector3.Cross(betaVector, new Vector3(0f, 0f, -1f)) * magnitude;

        alphaNeighbour.cell.GetComponent <Rigidbody2D>().AddForce(alphaForce, ForceMode2D.Impulse);
        betaNeighbour.cell.GetComponent <Rigidbody2D>().AddForce(betaForce, ForceMode2D.Impulse);

        return(-(alphaForce + betaForce));
    }
Exemplo n.º 2
0
 void ConnectCells()
 {
     foreach (Cell cell in cellList)
     {
         Vector2i center = cell.mapPosition;
         for (int direction = 0; direction < 6; direction++)
         {
             Vector2i gridNeighbourPos = cellMap.GetGridNeighbourGridPosition(center, direction); // GetGridNeighbour(center, CardinalDirectionHelper.ToCardinalDirection(direction));
             if (gridNeighbourPos != null)
             {
                 cell.SetNeighbourCell(CardinalDirectionHelper.ToCardinalDirection(direction), cellMap.GetGridNeighbourCell(center, direction) /*grid[gridNeighbourPos.x, gridNeighbourPos.y].transform.GetComponent<Cell>()*/);
             }
             else
             {
                 cell.SetNeighbourCell(CardinalDirectionHelper.ToCardinalDirection(direction), null);
             }
         }
         cell.UpdateSpringConnections();
         cell.UpdateGroups();
     }
 }
Exemplo n.º 3
0
 void Update()
 {
     spriteTransform.localRotation = Quaternion.Euler(0f, 0f, CardinalDirectionHelper.ToAngle(heading));
 }
Exemplo n.º 4
0
 public bool HasNeighbour(CardinalDirection direction)
 {
     return(HasNeighbourCell(CardinalDirectionHelper.ToIndex(direction)));
 }
Exemplo n.º 5
0
 public CellNeighbour GetNeighbour(CardinalDirection direction)
 {
     return(GetNeighbour(CardinalDirectionHelper.ToIndex(direction)));
 }
Exemplo n.º 6
0
 public void SetNeighbourCell(CardinalDirection direction, Cell cell)
 {
     index_Neighbour[CardinalDirectionHelper.ToIndex(direction)].cell = cell;
 }
Exemplo n.º 7
0
 public Vector2i GetGridNeighbourGridPosition(Vector2i gridPosition, CardinalDirection direction)
 {
     return(GetGridNeighbourGridPosition(gridPosition, CardinalDirectionHelper.ToIndex(direction)));
 }
Exemplo n.º 8
0
    public void Generate(Genotype genotype, Creature creature)
    {
        timeOffset = Random.Range(0f, 7f);

        Clear();

        List <Cell> spawningFromCells = new List <Cell>();

        spawningFromCells.Add(SpawnCell(genotype.GetGeneAt(0), new Vector2i(), 0, CardinalDirectionHelper.ToIndex(CardinalDirection.north), creature)); //root

        List <Cell> nextSpawningFromCells = new List <Cell>();

        for (int buildOrderIndex = 1; spawningFromCells.Count != 0 && buildOrderIndex < 4; buildOrderIndex++)
        {
            foreach (Cell cell in spawningFromCells)
            {
                for (int referenceDirection = 0; referenceDirection < 6; referenceDirection++)
                {
                    if (cell.gene.getReference(referenceDirection) != null)
                    {
                        int      referenceHeading         = (cell.heading + referenceDirection + 5) % 6; //!!
                        Gene     referenceGene            = genotype.GetGeneAt((int)cell.gene.getReference(referenceDirection));
                        Vector2i referenceCellMapPosition = cellMap.GetGridNeighbourGridPosition(cell.mapPosition, referenceHeading);

                        if (cellMap.IsLegalPosition(referenceCellMapPosition))
                        {
                            Cell residentCell = cellMap.GetCell(referenceCellMapPosition);
                            if (residentCell == null)
                            {
                                //only time we spawn a cell if there is a vacant spot
                                Cell newCell = SpawnCell(referenceGene, referenceCellMapPosition, buildOrderIndex, referenceHeading, creature);
                                nextSpawningFromCells.Add(newCell);
                                cellList.Add(cell);
                            }
                            else
                            {
                                if (residentCell.buildOrderIndex > buildOrderIndex)
                                {
                                    throw new System.Exception("Trying to spawn a cell at a location where a cell of higher build order are allready present.");
                                }
                                else if (residentCell.buildOrderIndex == buildOrderIndex)
                                {
                                    //trying to spawn a cell where ther is one allready with the same buildOrderIndex, in fight over this place bothe cwlls will loose, so the resident will be removed
                                    GameObject.Destroy(residentCell.gameObject);
                                    cellList.Remove(residentCell);
                                    cellMap.removeCellAtGridPosition(residentCell.mapPosition);
                                    nextSpawningFromCells.Remove(residentCell);
                                    cellMap.MarkAsIllegal(residentCell.mapPosition);
                                }
                                else
                                {
                                    // trying to spawn a cell where there is one with lowerBuildOrder index, no action needed
                                }
                            }
                        }
                    }
                }
            }
            spawningFromCells.Clear();
            spawningFromCells.AddRange(nextSpawningFromCells);
            nextSpawningFromCells.Clear();
            if (buildOrderIndex == 99)
            {
                throw new System.Exception("Creature generation going on for too long");
            }
        }

        ConnectCells();
        edges.GenerateWings(cellList);
        UpdateSpringsFrequenze();
    }