void moveRockTo(Rock r, int x, int y) { r.originX = x; r.originY = y; var shape = curRock.shape; for (int i = 0; i != curRock.icons.Length; ++i) { curRock.icons[i].transform.position = field.calcPos(x + shape[i, 0], y + shape[i, 1]); } }
void initGame() { field = new GridField(field_row, field_column, field_size, this.transform); field.clear((int)FieldType.Empty); if (null != snake.icons) { foreach (var icon in snake.icons) { if (icon) { Destroy(icon); } } } snake.head = snake.tail = 0; int oriX = field_column / 2; int oriY = field_row / 2; int maxlen = field_column * field_row; snake.icons = new GameObject[maxlen]; snake.icons[snake.head] = Instantiate(block_prefab, field.calcPos(oriX, oriY), Quaternion.identity, this.transform); snake.icons[snake.head].GetComponent <SpriteRenderer>().color = head_color; snake.posX = new int[maxlen]; snake.posY = new int[maxlen]; snake.posX[snake.head] = oriX; snake.posY[snake.head] = oriY; snake.ori = SnakeStruct.Orientaion.Down; field[oriX, oriY] = (int)FieldType.Snake; food.x = 2; food.y = 2; if (null == food.icon) { food.icon = Instantiate(block_prefab, field.calcPos(food.x, food.y), Quaternion.identity, this.transform); food.icon.GetComponent <SpriteRenderer>().color = Color.red; } else { food.icon.transform.position = field.calcPos(food.x, food.y); } field[food.x, food.y] = (int)FieldType.Food; Random.InitState((int)(Time.time * 1000.0f)); }
GameState ClimbSnake() { timeline += Time.deltaTime; if (timeline > time_interval) { timeline -= time_interval; int nx = 0, ny = 0; switch (snake.ori) { case SnakeStruct.Orientaion.Up: ny = 1; break; case SnakeStruct.Orientaion.Down: ny = -1; break; case SnakeStruct.Orientaion.Left: nx = -1; break; case SnakeStruct.Orientaion.Right: nx = 1; break; } nx += snake.posX[snake.head]; ny += snake.posY[snake.head]; int?v = field.checkValid(nx, ny); //Debug.LogFormat("npos = {{{0}, {1}}}", nx, ny); if ((int)FieldType.Snake == v || null == v) { return(GameState.Fail); } else if ((int)FieldType.Empty == v) { int ot = snake.tail; int nh = (snake.head + 1) % snake.icons.Length; snake.icons[snake.head].GetComponent <SpriteRenderer>().color = Color.white; snake.icons[nh] = snake.icons[ot]; snake.icons[ot] = null; snake.icons[nh].transform.position = field.calcPos(nx, ny); snake.icons[nh].GetComponent <SpriteRenderer>().color = head_color; snake.head = nh; snake.tail = (snake.tail + 1) % snake.icons.Length; snake.posX[nh] = nx; snake.posY[nh] = ny; field.moveGrid(snake.posX[ot], snake.posY[ot], nx, ny, (int)FieldType.Empty); } else if ((int)FieldType.Food == v) { int maxlen = snake.icons.Length; int nh = (snake.head + 1) % maxlen; snake.icons[snake.head].GetComponent <SpriteRenderer>().color = Color.white; snake.icons[nh] = Instantiate(block_prefab, field.calcPos(nx, ny), Quaternion.identity, this.transform); snake.icons[nh].GetComponent <SpriteRenderer>().color = head_color; snake.posX[nh] = nx; snake.posY[nh] = ny; snake.head = nh; field[nx, ny] = (int)FieldType.Snake; if (snake.tail == (nh + 1) % maxlen) { return(GameState.Win); } int snakelen = (nh + maxlen - snake.tail + 1) % maxlen; int availspace = snake.icons.Length - snakelen; moveFood(availspace); } } return(GameState.Loop); }