Example #1
0
    public override List <Field> getBites(SuperPosition sp)
    {
        List <Field> result     = new List <Field>();
        Field        fieldBite1 = null;
        Field        fieldBite2 = null;

        if (direction == Direction.Up)
        {
            fieldBite1 = board.Map(piece.position.x - 1, piece.position.y + 1);
            fieldBite2 = board.Map(piece.position.x + 1, piece.position.y + 1);
        }
        if (direction == Direction.Left)
        {
            fieldBite1 = board.Map(piece.position.x - 1, piece.position.y - 1);
            fieldBite2 = board.Map(piece.position.x - 1, piece.position.y + 1);
        }
        if (direction == Direction.Down)
        {
            fieldBite1 = board.Map(piece.position.x - 1, piece.position.y - 1);
            fieldBite2 = board.Map(piece.position.x + 1, piece.position.y - 1);
        }
        if (direction == Direction.Right)
        {
            fieldBite1 = board.Map(piece.position.x + 1, piece.position.y - 1);
            fieldBite2 = board.Map(piece.position.x + 1, piece.position.y + 1);
        }

        result.Add(fieldBite1);
        result.Add(fieldBite2);

        return(result);
    }
Example #2
0
    public bool isBitten(SuperPosition sp, Field field)
    {
        List <Field> bittenFields = findOutBittenFields(sp);
        bool         result       = bittenFields.Contains(field);

        return(result);
    }
Example #3
0
    private GameObject spawn(SuperPosition sup, int i, int j, string key)
    {
        GameObject g = sup.gameObject;
        GameObject n;

        if (pool.ContainsKey(sup.pool) && pool[sup.pool].Count > 0)
        {
            n = pool[sup.pool].Last.Value;
            pool[sup.pool].RemoveLast();
            n.SetActive(true);
        }
        else
        {
            n = GameObject.Instantiate(g);
        }
        n.name = g.name;
        n.transform.localScale = new Vector3(1, 0.0001f, 1f);
        grid[i][j]             = n.GetComponent <SuperPosition>();
        grid[i][j].aliveness   = 0;
        grid[i][j].dying       = false;
        if (key != null)
        {
            grid[i][j].key = key;
        }
        else
        {
            grid[i][j].key = sup.key;
        }
        grid[i][j].register();
        updatePos(i, j);
        return(n);
    }
Example #4
0
    protected override List <Field> getTransfers(SuperPosition sp)
    {
        List <Field> result = new List <Field>();

        result = getBites(sp);

        return(result);
    }
Example #5
0
    public SuperPosition Clone()
    {
        SuperPosition newSuperposition = new SuperPosition();

        foreach (var s in state)
        {
            newSuperposition.state.Add(s.Key, s.Value);
        }
        return(newSuperposition);
    }
Example #6
0
    // return all fields piece can move
    public List <Field> getMoves(SuperPosition sp)
    {
        List <Field> result = getTransfers(sp);

        foreach (Rule rule in board.rule)
        {
            result = rule.onGettingMoves(piece, result);
        }

        return(result);
    }
Example #7
0
    protected override List <Field> getTransfers(SuperPosition sp)
    {
        List <Field> result          = new List <Field>();
        Field        fieldMove       = null;
        Field        fieldDoubleMove = null;

        if (direction == Direction.Up)
        {
            fieldMove       = board.Map(piece.position.x, piece.position.y + 1);
            fieldDoubleMove = board.Map(piece.position.x, piece.position.y + 2);
        }
        if (direction == Direction.Left)
        {
            fieldMove       = board.Map(piece.position.x - 1, piece.position.y);
            fieldDoubleMove = board.Map(piece.position.x - 2, piece.position.y);
        }
        if (direction == Direction.Down)
        {
            fieldMove       = board.Map(piece.position.x, piece.position.y - 1);
            fieldDoubleMove = board.Map(piece.position.x, piece.position.y - 2);
        }
        if (direction == Direction.Right)
        {
            fieldMove       = board.Map(piece.position.x + 1, piece.position.y);
            fieldDoubleMove = board.Map(piece.position.x + 2, piece.position.y);
        }


        if (!occupied(sp, fieldMove) && fieldMove)
        {
            result.Add(fieldMove);
        }

        if (canDoubleMove && !occupied(sp, fieldDoubleMove) && !occupied(sp, fieldMove) && fieldMove && fieldDoubleMove)
        {
            result.Add(fieldDoubleMove);
        }
        List <Field> biteFields = getBites(sp);

        if (canBite(sp, biteFields[0]))
        {
            result.Add(biteFields[0]);
        }

        if (canBite(sp, biteFields[1]))
        {
            result.Add(biteFields[1]);
        }

        return(result);
    }
Example #8
0
    private void OnCollisionEnter(Collision other)
    {
        if (dying)
        {
            return;
        }

        if (other.rigidbody)
        {
            SuperPosition sup = other.rigidbody.gameObject.GetComponent <SuperPosition>();
            if (sup.dying)
            {
                return;
            }
            if (sup.key == key)
            {
                if (sup.GetHashCode() < this.GetHashCode())
                {
                    CollapseSelf();
                }
                else
                {
                    sup.CollapseSelf();
                }
            }
        }

        if (other.rigidbody)
        {
            Player otherPlayer = other.rigidbody.gameObject.GetComponent <Player>();
            if (otherPlayer)
            {
                Player ourPlayer = gameObject.GetComponent <Player>();
                if (ourPlayer)
                {
                    if (ourPlayer.GetHashCode() < otherPlayer.GetHashCode())
                    {
                        CollapseOthers();
                    }
                }
                else
                {
                    CollapseOthers();
                }
            }
        }
        if (other.rigidbody && other.rigidbody.gameObject.GetComponent <Bullet>())
        {
            CollapseSelf();
        }
    }
Example #9
0
 protected bool canBite(SuperPosition sp, Field field)
 {
     if (field)
     {
         if (sp.GetPiece(field))
         {
             if (Game.Diplomate.get_state(sp.GetPiece(field).color, piece.color) == DiplomateState.Enemy)
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Example #10
0
 public static void cleanup(SuperPosition sup)
 {
     if (sup.gameObject == null)
     {
         //Debug.Log("failed to find gameobject");
         return;
     }
     sup.gameObject.SetActive(false);
     if (!Spawner.pool.ContainsKey(sup.pool))
     {
         Spawner.pool[sup.pool] = new LinkedList <GameObject>();
     }
     Spawner.pool[sup.pool].AddLast(sup.gameObject);
 }
Example #11
0
    public override List <Field> getBites(SuperPosition sp)
    {
        List <Field> result = new List <Field>();
        Field        field  = board.Map(piece.position.x + 1, piece.position.y + 1);

        while (canMove(sp, field))
        {
            result.Add(field);
            if (occupied(sp, field))
            {
                break;
            }
            field = board.Map(field.x + 1, field.y + 1);
        }

        field = board.Map(piece.position.x + 1, piece.position.y - 1);
        while (canMove(sp, field))
        {
            result.Add(field);
            if (occupied(sp, field))
            {
                break;
            }
            field = board.Map(field.x + 1, field.y - 1);
        }

        field = board.Map(piece.position.x - 1, piece.position.y + 1);
        while (canMove(sp, field))
        {
            result.Add(field);
            if (occupied(sp, field))
            {
                break;
            }
            field = board.Map(field.x - 1, field.y + 1);
        }

        field = board.Map(piece.position.x - 1, piece.position.y - 1);
        while (canMove(sp, field))
        {
            result.Add(field);
            if (occupied(sp, field))
            {
                break;
            }
            field = board.Map(field.x - 1, field.y - 1);
        }

        return(result);
    }
Example #12
0
    protected override List <Field> getTransfers(SuperPosition sp)
    {
        List <Field> result = new List <Field>();

        List <Field> bites = getBites(sp);

        foreach (Field f in bites)
        {
            if (canMove(sp, f))
            {
                result.Add(f);
            }
        }

        return(result);
    }
Example #13
0
 public bool occupied(SuperPosition sp, Field field)
 {
     if (field == null)
     {
         return(false);
     }
     else
     if (sp.GetPiece(field) == null)
     {
         return(false);
     }
     else
     {
         return(true);
     }
 }
Example #14
0
    public override List <Field> getBites(SuperPosition sp)
    {
        List <Field> result = new List <Field>();
        Field        field  = board.Map(piece.position.x + 1, piece.position.y + 2);

        if (field)
        {
            result.Add(field);
        }
        field = board.Map(piece.position.x + 2, piece.position.y + 1);
        if (field)
        {
            result.Add(field);
        }
        field = board.Map(piece.position.x + 1, piece.position.y - 2);
        if (field)
        {
            result.Add(field);
        }
        field = board.Map(piece.position.x + 2, piece.position.y - 1);
        if (field)
        {
            result.Add(field);
        }
        field = board.Map(piece.position.x - 1, piece.position.y + 2);
        if (field)
        {
            result.Add(field);
        }
        field = board.Map(piece.position.x - 2, piece.position.y + 1);
        if (field)
        {
            result.Add(field);
        }
        field = board.Map(piece.position.x - 1, piece.position.y - 2);
        if (field)
        {
            result.Add(field);
        }
        field = board.Map(piece.position.x - 2, piece.position.y - 1);
        if (field)
        {
            result.Add(field);
        }

        return(result);
    }
Example #15
0
    List <Field> findOutBittenFields(SuperPosition sp)
    {
        List <Field> bittenFields = new List <Field>();

        foreach (Piece p in sp.state.Values)
        {
            if (Game.Diplomate.get_state(piece.color, p.color) == DiplomateState.Enemy)
            {
                List <Field> bit = p.movingController.getBites(sp);
                foreach (Field f in bit)
                {
                    bittenFields.Add(f);
                }
            }
        }

        return(bittenFields);
    }
Example #16
0
    void initialize()
    {
        initializeFields();

        GameOption = GameObject.Find("Game option").GetComponent <GameOption>();

        Transform t = GameObject.Find("Rules").transform;

        for (int i = 0; i < t.childCount; i++)
        {
            rule.Add(t.GetChild(i).gameObject.GetComponent <Rule>());
        }

        t = GameObject.Find("Pieces").transform;
        for (int i = 0; i < t.childCount; i++)
        {
            piece.Add(t.GetChild(i).gameObject.GetComponent <Piece>());
        }

        foreach (var p in piece)
        {
            float a = p.transform.localPosition.x, b = p.transform.localPosition.z;
            float a1 = Game.Board.x_component.x, a2 = Game.Board.y_component.x;
            float b1 = Game.Board.x_component.y, b2 = Game.Board.y_component.y;

            float new_x = (a * b2 - a2 * b) / (a1 * b2 - a2 * b1), new_y = (a1 * b - a * b1) / (a1 * b2 - a2 * b1);

            p.position         = Game.Board.Map((int)(new_x + 0.5f), (int)(new_y + 0.5f));
            p.position.piece   = p;
            p.movingController = p.gameObject.GetComponent <MovingContoller>();
        }

        SuperPosition startSp = new SuperPosition();

        foreach (var p in piece)
        {
            startSp.state.Add(p.position, p);
        }
        superPosition.Add(startSp);
    }
Example #17
0
    public void reset()
    {
        popup.SetActive(false);
        playerEnabled = true;
        timer         = 0f;
        if (gridSize > 0)
        {
            if (grid != null)
            {
                for (int i = 0; i < gridSize; ++i)
                {
                    for (int j = 0; j < gridSize; ++j)
                    {
                        if (grid[i][j] != null && grid[i][j] != player)
                        {
                            grid[i][j].CollapseSelf();
                        }
                    }
                }
            }
            grid = new SuperPosition[gridSize][];

            for (int i = 0; i < gridSize; ++i)
            {
                grid[i] = new SuperPosition[gridSize];
                for (int j = 0; j < gridSize; ++j)
                {
                    if (Random.Range(0, 100) == 0)
                    {
                        spawn(spawns[Random.Range(0, spawns.Length)], i, j, "" + Random.Range(100, 100000));
                    }
                }
            }

            grid[gridSize / 2][gridSize / 2] = player;
            updatePos(gridSize / 2, gridSize / 2);
        }
    }
Example #18
0
 public bool isBitten(SuperPosition sp)
 {
     return(isBitten(sp, piece.position));
 }
Example #19
0
 public bool occupied(SuperPosition sp, int x, int y)
 {
     return(occupied(sp, board.Map(x, y)));
 }
Example #20
0
 protected bool canBite(SuperPosition sp, int x, int y)
 {
     return(canBite(sp, board.Map(x, y)));
 }
Example #21
0
    void Update()
    {
        const int frameWait = 5;

        if (gridSize > 0)
        {
            if (Input.GetKeyDown(KeyCode.R))
            {
                reset();
                return;
            }
            if (grid == null)
            {
                return;
            }
            Vector2Int?movement = null;
            if (playerEnabled)
            {
                if (Input.GetKey(KeyCode.S))
                {
                    movement = new Vector2Int(0, -1);
                }
                if (Input.GetKey(KeyCode.D))
                {
                    movement = new Vector2Int(-1, 0);
                }
                if (Input.GetKey(KeyCode.A))
                {
                    movement = new Vector2Int(1, 0);
                }
                if (Input.GetKey(KeyCode.W))
                {
                    movement = new Vector2Int(0, 1);
                }
            }
            const int death = 30;
            for (int i = 0; i < gridSize; ++i)
            {
                for (int j = 0; j < gridSize; ++j)
                {
                    if (grid[i][j] == null || grid[i][j].gameObject == null || grid[i][j].dying)
                    {
                        grid[i][j] = null;
                        if (Time.frameCount % frameWait == 0)
                        {
                            string        keyFound            = "";
                            int           surroundingLiveness = 0;
                            SuperPosition lastSup             = null;

                            foreachSurrounding(i, j, (sup) => {
                                if (sup == null)
                                {
                                    return(false);
                                }
                                if (!sup.dying && sup.aliveness < death * 2 / 3)
                                {
                                    if (lastSup == null || keyFound == sup.key)
                                    {
                                        surroundingLiveness += sup.aliveness;
                                        keyFound             = sup.key;
                                        lastSup              = sup;
                                    }
                                }
                                return(false);
                            });
                            if (lastSup != null && surroundingLiveness > death * 1 / 3)
                            {
                                GameObject g          = spawn(lastSup, i, j, null);
                                Vector3    localScale = g.transform.localScale;
                                localScale.y           = 0.0001f;
                                g.transform.localScale = localScale;
                            }
                        }
                    }
                    else if (grid[i][j].key == "player")
                    {
                        if (Time.frameCount % frameWait == 0)
                        {
                            if (movement != null)
                            {
                                Vector2Int v  = movement.Value;
                                int        i2 = i + v.x;
                                int        j2 = j + v.y;
                                if (i2 < 0 || i2 >= gridSize || j2 < 0 || j2 >= gridSize)
                                {
                                    continue;
                                }
                                if (grid[i2][j2] != null)
                                {
                                    lost();
                                    return;
                                }
                                grid[i2][j2] = grid[i][j];
                                updatePos(i2, j2);
                                playerPos  = new Vector2Int(i2, j2);
                                grid[i][j] = null;
                                movement   = null;
                            }
                            bool justLost = foreachSurrounding(i, j, (sup) => {
                                return(sup != null && sup.aliveness > death / 3);
                            });
                            if (justLost)
                            {
                                lost();
                                return;
                            }
                        }
                    }
                    else
                    {
                        if (Time.frameCount % frameWait == 0 && !grid[i][j].dying)
                        {
                            grid[i][j].aliveness++;
                            if (grid[i][j].aliveness > death)
                            {
                                cleanup(grid[i][j]);
                                grid[i][j].dying = true;
                                grid[i][j]       = null;
                                continue;
                            }

                            foreachSurrounding(i, j, (sup) => {
                                if (sup == null)
                                {
                                    return(false);
                                }
                                if (sup.key != grid[i][j].key && sup.gameObject != null && sup.aliveness > death * 2 / 3)
                                {
                                    foreach (var sup2 in new SuperPosition[] { sup, grid[i][j] })
                                    {
                                        List <SuperPosition> sups = SuperPosition.superPositions[sup2.key];
                                        var supSup = sups[Random.Range(0, sups.Count)];
                                        supSup.CollapseOthers();
                                        supSup.aliveness = 0;
                                    }
                                }
                                return(false);
                            });
                        }
                        float fractionToNext = Time.frameCount % frameWait / (float)frameWait;
                        grid[i][j].gameObject.transform.localScale = new Vector3(1, 0.001f + 2 * Mathf.Sin((grid[i][j].aliveness + fractionToNext) / death * Mathf.PI), 1);
                    }
                }
            }
            //gridState = !gridState;
        }

        if (playerEnabled)
        {
            float oldTimer = timer;
            timer += Time.deltaTime;
            // Spawn rate in seconds
            for (int i = (int)(oldTimer / spawnRate); i < (int)(timer / spawnRate); ++i)
            {
                spawnRandom();
            }
            timeDisplay.text = myFormat(timer);
        }
    }
Example #22
0
 public bool isBitten(SuperPosition sp, int x, int y)
 {
     return(isBitten(sp, board.Map(x, y)));
 }
Example #23
0
 public virtual bool IsBittingCorrect(SuperPosition sp, Field f)
 {
     return(getBites(sp).Contains(f) && occupied(sp, f));
 }
Example #24
0
 public virtual bool IsMovingCorrect(SuperPosition sp, Field f)
 {
     return(getMoves(sp).Contains(f));
 }
Example #25
0
 //return all fields piece can bite
 public abstract List <Field> getBites(SuperPosition sp);
Example #26
0
 protected abstract List <Field> getTransfers(SuperPosition sp);