IEnumerator MoveSnake() { while (true) { yield return(new WaitForSeconds(snake_move_time)); // Calculate next head coordinates Coordinate head_next_coordinate = CalculateNextTile(head); TileController head_next = BoardData.GetTile(head_next_coordinate); snake_next_memory.Add(head, head_next_coordinate); // if the next is an apple, don't move the tail if (head_next.tileType == TileType.Apple) { EventBus.Publish <SnakeAteAppleEvent>(new SnakeAteAppleEvent()); } else { // Move the tail MoveTail(); // Only Mark the next head as used if not an apple BoardData.MarkUsed(head_next); } // set sprites if (head_next_coordinate.x == tail.x && head_next_coordinate.y == tail.y) { // if snake_length is 1, don't do fancy sprites head_next.RemoveSnakeSprite(); } else { BoardData.GetTile(head).SetSnakeSprite(head_next_coordinate, head, prev_head); head_next.SetSnakeSprite(new Coordinate(-1, -1), head_next_coordinate, head); BoardData.GetTile(tail).SetSnakeSprite(snake_next_memory[tail], tail, new Coordinate(-1, -1)); } head_next.SetTileType(TileType.Snake); // Move the head snake_length++; prev_head = head; head = head_next_coordinate; if (snake_length == BoardData.GetHeight() * BoardData.GetWidth()) { EventBus.Publish <BoardFullEvent>(new BoardFullEvent()); } } }
void _OnBoardGenerated(BoardGeneratedEvent e) { float ui_ratio = 240 / user_interface.sizeDelta.x; float height = BoardData.GetHeight(); float width = BoardData.GetWidth() / (1 - ui_ratio); float ui_width = width - BoardData.GetWidth(); // adjust position transform.position = new Vector3((width) / 2f - 0.5f - ui_ratio * width, (height) / 2f - 0.5f, transform.position.z); // adjust height Camera.main.orthographicSize = Mathf.Max(height / 2f, (width + ui_width / 2f) / (2f * Camera.main.aspect)); }
void _OnGoldChange(GoldChangeEvent e) { cost = BoardData.GetWidth() * BoardData.GetHeight(); if (e.gold > cost / 2) { go.SetActive(true); } // check interatability if (e.gold - cost >= 0) { b.interactable = true; } else { b.interactable = false; } }
void _OnBoardGeneratedEvent(BoardGeneratedEvent e) { cost = BoardData.GetWidth() * BoardData.GetHeight(); c.text = cost.ToString(); }
Coordinate CalculateNextTile(Coordinate cur) { Coordinate next_move = PathGenerator.GetNextTile(cur); // if the snake is quite large, just return next_move if (snake_length > BoardData.GetHeight() * BoardData.GetWidth() * 0.6) { return(next_move); } // calculate potential other paths List <Coordinate> possible_moves = new List <Coordinate>(); Coordinate p = new Coordinate(cur.x - 1, cur.y); if (p.x >= 0 && p.x < BoardData.GetWidth() && p.y >= 0 && p.y < BoardData.GetHeight() && BoardData.GetTile(p).tileType != TileType.Snake && p != next_move) { possible_moves.Add(p); } p = new Coordinate(cur.x + 1, cur.y); if (p.x >= 0 && p.x < BoardData.GetWidth() && p.y >= 0 && p.y < BoardData.GetHeight() && BoardData.GetTile(p).tileType != TileType.Snake && p != next_move) { possible_moves.Add(p); } p = new Coordinate(cur.x, cur.y - 1); if (p.x >= 0 && p.x < BoardData.GetWidth() && p.y >= 0 && p.y < BoardData.GetHeight() && BoardData.GetTile(p).tileType != TileType.Snake && p != next_move) { possible_moves.Add(p); } p = new Coordinate(cur.x, cur.y + 1); if (p.x >= 0 && p.x < BoardData.GetWidth() && p.y >= 0 && p.y < BoardData.GetHeight() && BoardData.GetTile(p).tileType != TileType.Snake && p != next_move) { possible_moves.Add(p); } // if surrounded by snake, just return the next step if (possible_moves.Count == 0) { return(next_move); } // if we find an apple, just continue down path while (true) { // if we find one of the possible moves, go that way instead foreach (Coordinate possible_move in possible_moves) { if (next_move == possible_move) { return(next_move); } } if (BoardData.GetTile(next_move).tileType != TileType.Clear) { return(PathGenerator.GetNextTile(cur)); } next_move = PathGenerator.GetNextTile(next_move); } Debug.Assert(false); return(new Coordinate(-1, -1)); }
void _OnBoardGenerated(BoardGeneratedEvent e) { int height = BoardData.GetHeight(); int width = BoardData.GetWidth(); // num nodes = (height / 2) * (width / 2) int node_height = height / 2; int node_width = width / 2; // Create MST with Kruskal's algorithm List <Edge> edges = new List <Edge>(); // not sorted in any order MstNode[, ] nodes = new MstNode[node_width, node_height]; // populate nodes and edges for (int x = 0; x < node_width; ++x) { for (int y = 0; y < node_height; ++y) { nodes[x, y] = new MstNode(x, y); // add edge 0 if not at top of grid if (y < node_height - 1) { edges.Add(new Edge(new Coordinate(x, y), 0)); } // add edge 1 if not at right side of grid if (x < node_width - 1) { edges.Add(new Edge(new Coordinate(x, y), 1)); } } } Debug.Assert(edges.Count == (node_height * (node_width - 1) + node_width * (node_height - 1))); int added_edges = 0; while (added_edges < node_width * node_height - 1) { // select a random edge int i = Random.Range(0, edges.Count); Edge edge = edges[i]; Coordinate vertex2; if (edge.output_side == 0) { vertex2 = new Coordinate(edge.vertex.x, edge.vertex.y + 1); } else { vertex2 = new Coordinate(edge.vertex.x + 1, edge.vertex.y); } // remove from edges edges[i] = edges[edges.Count - 1]; edges.RemoveAt(edges.Count - 1); // if the edge forms a cycle, discard, else, add // check representative Coordinate repA = GetRepresentative(nodes, edge.vertex); Coordinate repB = GetRepresentative(nodes, vertex2); // if reps are equal, there is a cycle (do nothing) // otherwise add the edges to mstTree, update representatives if (repA != repB) { added_edges++; nodes[edge.vertex.x, edge.vertex.y].nearby_edges[edge.output_side] = true; nodes[vertex2.x, vertex2.y].nearby_edges[edge.output_side + 2] = true; nodes[repA.x, repA.y].representative = repB; } } // Assign paths based on MST next_map = new Coordinate[width, height]; // start at lower left edge Coordinate current_step = new Coordinate(0, 0); while (current_step != new Coordinate(1, 0)) { MstNode node = nodes[current_step.x / 2, current_step.y / 2]; Coordinate next = new Coordinate(0, 0); // check an edge based on position in node quadrant // lower left if (current_step.x % 2 == 0 && current_step.y % 2 == 0) { if (node.nearby_edges[3]) { next = new Coordinate(current_step.x - 1, current_step.y); } else { next = new Coordinate(current_step.x, current_step.y + 1); } } // top left else if (current_step.x % 2 == 0 && current_step.y % 2 == 1) { if (node.nearby_edges[0]) { next = new Coordinate(current_step.x, current_step.y + 1); } else { next = new Coordinate(current_step.x + 1, current_step.y); } } // top right else if (current_step.x % 2 == 1 && current_step.y % 2 == 1) { if (node.nearby_edges[1]) { next = new Coordinate(current_step.x + 1, current_step.y); } else { next = new Coordinate(current_step.x, current_step.y - 1); } } // bottom right else if (current_step.x % 2 == 1 && current_step.y % 2 == 0) { if (node.nearby_edges[2]) { next = new Coordinate(current_step.x, current_step.y - 1); } else { next = new Coordinate(current_step.x - 1, current_step.y); } } next_map[current_step.x, current_step.y] = next; current_step = next; } }