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); }
public bool isBitten(SuperPosition sp, Field field) { List <Field> bittenFields = findOutBittenFields(sp); bool result = bittenFields.Contains(field); return(result); }
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); }
protected override List <Field> getTransfers(SuperPosition sp) { List <Field> result = new List <Field>(); result = getBites(sp); return(result); }
public SuperPosition Clone() { SuperPosition newSuperposition = new SuperPosition(); foreach (var s in state) { newSuperposition.state.Add(s.Key, s.Value); } return(newSuperposition); }
// 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); }
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); }
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(); } }
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); }
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); }
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); }
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); }
public bool occupied(SuperPosition sp, Field field) { if (field == null) { return(false); } else if (sp.GetPiece(field) == null) { return(false); } else { return(true); } }
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); }
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); }
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); }
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); } }
public bool isBitten(SuperPosition sp) { return(isBitten(sp, piece.position)); }
public bool occupied(SuperPosition sp, int x, int y) { return(occupied(sp, board.Map(x, y))); }
protected bool canBite(SuperPosition sp, int x, int y) { return(canBite(sp, board.Map(x, y))); }
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); } }
public bool isBitten(SuperPosition sp, int x, int y) { return(isBitten(sp, board.Map(x, y))); }
public virtual bool IsBittingCorrect(SuperPosition sp, Field f) { return(getBites(sp).Contains(f) && occupied(sp, f)); }
public virtual bool IsMovingCorrect(SuperPosition sp, Field f) { return(getMoves(sp).Contains(f)); }
//return all fields piece can bite public abstract List <Field> getBites(SuperPosition sp);
protected abstract List <Field> getTransfers(SuperPosition sp);