Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #include "Athena.h"
- #include "Map_Controller.h"
- #include <Math.h>
- #include "tile_base.h"
- #define PI 3.14159265
- // Sets default values
- AMap_Controller::AMap_Controller()
- {
- // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
- PrimaryActorTick.bCanEverTick = true;
- //Search for blueprint classes
- static ConstructorHelpers::FObjectFinder<UClass> tile_dirt_bp(TEXT("Blueprint'/Game/Blueprints/Tiles/BP_tile_dirt.BP_tile_dirt_C'")); //Dirt blueprint
- static ConstructorHelpers::FObjectFinder<UClass> tile_water_bp(TEXT("Blueprint'/Game/Blueprints/Tiles/BP_tile_water.BP_tile_water_C'")); //Water blueprint
- static ConstructorHelpers::FObjectFinder<UClass> tile_grass_bp(TEXT("Blueprint'/Game/Blueprints/Tiles/BP_tile_grass.BP_tile_grass_C'")); //Grass blueprint
- static ConstructorHelpers::FObjectFinder<UClass> tile_tree_bp(TEXT("Blueprint'/Game/Blueprints/Tiles/BP_tile_tree.BP_tile_tree_C'")); //Tree blueprint
- game_controller_root = CreateDefaultSubobject<USceneComponent>(TEXT("root"));
- RootComponent = game_controller_root;
- //Add ISM components
- grass_ISM = CreateDefaultSubobject<UInstancedStaticMeshComponent>(TEXT("grass_ISM"));
- water_ISM = CreateDefaultSubobject<UInstancedStaticMeshComponent>(TEXT("water_ISM"));
- dirt_ISM = CreateDefaultSubobject<UInstancedStaticMeshComponent>(TEXT("dirt_ISM"));
- //Add ISMs to array
- tiles_ISM.Add(grass_ISM);
- tiles_ISM.Add(water_ISM);
- tiles_ISM.Add(dirt_ISM);
- //Save tile bluerpint classes
- map_tile_classes.Add(tile_dirt_bp.Object);
- map_tile_classes.Add(tile_water_bp.Object);
- map_tile_classes.Add(tile_grass_bp.Object);
- //map_tile_classes.Add(tile_tree_bp.Object);
- }
- // Called when the game starts or when spawned
- void AMap_Controller::BeginPlay()
- {
- Super::BeginPlay();
- //Create and spawn random hex grid
- generate_hex_grid();
- //Generate neighbour connections for graph
- generate_graph();
- //Do cellular automata for the graph a set number of times
- //for(int i = 0 ; i < map_cellular_automata_max_depth ; i++)
- //generate_cellular_automata();
- }
- // Called every frame
- void AMap_Controller::Tick( float DeltaTime )
- {
- Super::Tick( DeltaTime );
- }
- void AMap_Controller::generate_hex_grid()
- {
- //Variable setup
- int column_init = 0,
- row_init = 0;
- //Offset for the triangles in the hex grid
- double xOff = FMath::Cos(FMath::DegreesToRadians(30)) * (map_tile_radius),
- yOff = FMath::Sin(FMath::DegreesToRadians(30)) * (map_tile_radius);
- //Hexagonal grid generation
- for (int current_row = row_init; current_row < map_max_row_count; current_row++)
- {
- int current_row_columns = map_max_row_count - abs(current_row - map_row_height);
- for (int current_column = column_init; current_column < current_row_columns; current_column++)
- {
- float tile_location_x = yOff * (current_row - map_row_height) * 3,
- tile_location_y = xOff * (current_column * 2 + 1 - current_row_columns),
- tile_location_z = 0; //Todo: heightmap
- int matrix_coordinate_x = current_row < map_row_height ? current_column - current_row : current_column - map_row_height,
- matrix_coordinate_y = current_row - map_row_height;
- FTransform tile_ISM_transform(FRotator(0,0,0), FVector(tile_location_x, tile_location_y, tile_location_z), FVector(1,1,1));
- int tile_random_class = (int)FMath::RandRange(0, tiles_ISM.Num() - 1);
- tiles_ISM[tile_random_class]->AddInstanceWorldSpace(tile_ISM_transform);
- tile_indexer.Add(Ftile_indexer(tile_random_class, tiles_ISM[tile_random_class]->GetInstanceCount() - 1, matrix_coordinate_x, matrix_coordinate_y));
- }
- }
- }
- void AMap_Controller::generate_graph()
- {
- //Offsets for the 6 directions of this tile
- int hex_neighbour_offsets[6][2] = { { 0, 1 },
- {-1, 0 },
- { 1, 0 },
- { 0, -1 },
- {-1, 1 },
- { 1, -1 } };
- for (Ftile_indexer current_indexed_tile : tile_indexer)
- {
- //Get current tile index matrix position
- int tile_matrix_position_x = current_indexed_tile.matrix_position_x;
- int tile_matrix_position_y = current_indexed_tile.matrix_position_y;
- //Empty neighbours array
- current_indexed_tile.neighbours.Empty();
- TArray<Fneighbour> neighbours;
- for (int i = 0; i < 6; i++)
- {
- //Get the neighbour index
- Ftile_indexer neighbour_index = find_tile_index_from_matrix_coordinate(current_indexed_tile.matrix_position_x + hex_neighbour_offsets[i][0], current_indexed_tile.matrix_position_y + hex_neighbour_offsets[i][1]);
- if (neighbour_index.ISM_index != -1) //If index exists add to neighbours array for this tile index
- {
- current_indexed_tile.add_neighbour(neighbour_index.ISM_index, neighbour_index.instance_index, neighbour_index.matrix_position_x, neighbour_index.matrix_position_y);
- }
- //GEngine->AddOnScreenDebugMessage(-1, 2, FColor::Red, FString::FromInt(neighbours.Num()));
- //GEngine->AddOnScreenDebugMessage(-1, 2, FColor::Red, "ISM: " + FString::FromInt(current_indexed_tile.ISM_index) + ", Index: " + FString::FromInt(current_indexed_tile.instance_index) + ", X: " + FString::FromInt(current_indexed_tile.matrix_position_x) + ", Y: " + FString::FromInt(current_indexed_tile.matrix_position_y) + ", Neighbours: " + FString::FromInt(current_indexed_tile.neighbours.Num()));
- }
- }
- }
- FVector2D AMap_Controller::find_tile_matrix_coordinate_from_index(int ISM_index, int instance_index)
- {
- for (Ftile_indexer current_indexed_tile : tile_indexer)
- {
- if (current_indexed_tile.ISM_index == ISM_index && current_indexed_tile.instance_index == instance_index)
- return FVector2D(current_indexed_tile.matrix_position_x, current_indexed_tile.matrix_position_y);
- }
- return FVector2D(-1,-1);
- }
- Ftile_indexer AMap_Controller::find_tile_index_from_matrix_coordinate(int matrix_position_x, int matrix_position_y)
- {
- for (Ftile_indexer current_indexed_tile : tile_indexer)
- {
- if (current_indexed_tile.matrix_position_x == matrix_position_x && current_indexed_tile.matrix_position_y == matrix_position_y)
- return current_indexed_tile;
- }
- return Ftile_indexer(-1,-1,-1,-1);
- }
- TArray<Fneighbour> AMap_Controller::find_tile_index_neighbours(int ISM_index, int instance_index)
- {
- for (Ftile_indexer current_indexed_tile : tile_indexer)
- {
- if (current_indexed_tile.ISM_index == ISM_index && current_indexed_tile.instance_index == instance_index)
- {
- GEngine->AddOnScreenDebugMessage(-1, 2, FColor::Red, "Found");
- return current_indexed_tile.get_neighbours();
- }
- }
- TArray<Fneighbour> null;
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement