Advertisement
Guest User

Untitled

a guest
Oct 12th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.60 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #include "Athena.h"
  4. #include "Map_Controller.h"
  5. #include <Math.h>
  6. #include "tile_base.h"
  7.  
  8. #define PI 3.14159265
  9.  
  10. // Sets default values
  11. AMap_Controller::AMap_Controller()
  12. {
  13.     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
  14.     PrimaryActorTick.bCanEverTick = true;
  15.  
  16.     //Search for blueprint classes
  17.     static ConstructorHelpers::FObjectFinder<UClass> tile_dirt_bp(TEXT("Blueprint'/Game/Blueprints/Tiles/BP_tile_dirt.BP_tile_dirt_C'"));       //Dirt blueprint
  18.     static ConstructorHelpers::FObjectFinder<UClass> tile_water_bp(TEXT("Blueprint'/Game/Blueprints/Tiles/BP_tile_water.BP_tile_water_C'"));    //Water blueprint
  19.     static ConstructorHelpers::FObjectFinder<UClass> tile_grass_bp(TEXT("Blueprint'/Game/Blueprints/Tiles/BP_tile_grass.BP_tile_grass_C'"));    //Grass blueprint
  20.     static ConstructorHelpers::FObjectFinder<UClass> tile_tree_bp(TEXT("Blueprint'/Game/Blueprints/Tiles/BP_tile_tree.BP_tile_tree_C'"));       //Tree blueprint
  21.  
  22.     game_controller_root = CreateDefaultSubobject<USceneComponent>(TEXT("root"));
  23.     RootComponent = game_controller_root;
  24.  
  25.     //Add ISM components
  26.     grass_ISM   = CreateDefaultSubobject<UInstancedStaticMeshComponent>(TEXT("grass_ISM"));
  27.     water_ISM   = CreateDefaultSubobject<UInstancedStaticMeshComponent>(TEXT("water_ISM"));
  28.     dirt_ISM    = CreateDefaultSubobject<UInstancedStaticMeshComponent>(TEXT("dirt_ISM"));
  29.  
  30.     //Add ISMs to array
  31.     tiles_ISM.Add(grass_ISM);
  32.     tiles_ISM.Add(water_ISM);
  33.     tiles_ISM.Add(dirt_ISM);
  34.  
  35.     //Save tile bluerpint classes
  36.     map_tile_classes.Add(tile_dirt_bp.Object);
  37.     map_tile_classes.Add(tile_water_bp.Object);
  38.     map_tile_classes.Add(tile_grass_bp.Object);
  39.     //map_tile_classes.Add(tile_tree_bp.Object);
  40. }
  41.  
  42. // Called when the game starts or when spawned
  43. void AMap_Controller::BeginPlay()
  44. {
  45.     Super::BeginPlay();
  46.  
  47.     //Create and spawn random hex grid
  48.     generate_hex_grid();
  49.  
  50.     //Generate neighbour connections for graph
  51.     generate_graph();
  52.  
  53.     //Do cellular automata for the graph a set number of times
  54.     //for(int i = 0 ; i < map_cellular_automata_max_depth ; i++)
  55.         //generate_cellular_automata();
  56.  
  57. }
  58.  
  59. // Called every frame
  60. void AMap_Controller::Tick( float DeltaTime )
  61. {
  62.     Super::Tick( DeltaTime );
  63.  
  64.  
  65. }
  66.  
  67. void AMap_Controller::generate_hex_grid()
  68. {
  69.     //Variable setup
  70.     int     column_init = 0,
  71.             row_init = 0;
  72.  
  73.     //Offset for the triangles in the hex grid
  74.     double  xOff            = FMath::Cos(FMath::DegreesToRadians(30)) * (map_tile_radius),
  75.             yOff            = FMath::Sin(FMath::DegreesToRadians(30)) * (map_tile_radius);
  76.  
  77.     //Hexagonal grid generation
  78.     for (int current_row = row_init; current_row < map_max_row_count; current_row++)
  79.     {
  80.         int current_row_columns = map_max_row_count - abs(current_row - map_row_height);
  81.  
  82.         for (int current_column = column_init; current_column < current_row_columns; current_column++)
  83.         {
  84.  
  85.             float   tile_location_x = yOff * (current_row - map_row_height) * 3,
  86.                     tile_location_y = xOff * (current_column * 2 + 1 - current_row_columns),
  87.                     tile_location_z = 0;    //Todo: heightmap
  88.  
  89.             int     matrix_coordinate_x = current_row < map_row_height ? current_column - current_row : current_column - map_row_height,
  90.                     matrix_coordinate_y = current_row - map_row_height;
  91.  
  92.             FTransform tile_ISM_transform(FRotator(0,0,0), FVector(tile_location_x, tile_location_y, tile_location_z), FVector(1,1,1));
  93.  
  94.  
  95.             int tile_random_class = (int)FMath::RandRange(0, tiles_ISM.Num() - 1);
  96.             tiles_ISM[tile_random_class]->AddInstanceWorldSpace(tile_ISM_transform);
  97.  
  98.             tile_indexer.Add(Ftile_indexer(tile_random_class, tiles_ISM[tile_random_class]->GetInstanceCount() - 1, matrix_coordinate_x, matrix_coordinate_y));
  99.         }
  100.     }
  101. }
  102.  
  103. void AMap_Controller::generate_graph()
  104. {
  105.     //Offsets for the 6 directions of this tile
  106.     int hex_neighbour_offsets[6][2] = { { 0,   1 },
  107.                                         {-1,   0 },
  108.                                         { 1,   0 },
  109.                                         { 0,  -1 },
  110.                                         {-1,   1 },
  111.                                         { 1,  -1 } };
  112.  
  113.    
  114.     for (Ftile_indexer current_indexed_tile : tile_indexer)
  115.     {
  116.         //Get current tile index matrix position
  117.         int tile_matrix_position_x = current_indexed_tile.matrix_position_x;
  118.         int tile_matrix_position_y = current_indexed_tile.matrix_position_y;
  119.  
  120.         //Empty neighbours array
  121.         current_indexed_tile.neighbours.Empty();
  122.         TArray<Fneighbour> neighbours;
  123.  
  124.         for (int i = 0; i < 6; i++)
  125.         {
  126.             //Get the neighbour index
  127.             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]);
  128.            
  129.             if (neighbour_index.ISM_index != -1)    //If index exists add to neighbours array for this tile index
  130.             {
  131.                 current_indexed_tile.add_neighbour(neighbour_index.ISM_index, neighbour_index.instance_index, neighbour_index.matrix_position_x, neighbour_index.matrix_position_y);
  132.             }
  133.  
  134.             //GEngine->AddOnScreenDebugMessage(-1, 2, FColor::Red, FString::FromInt(neighbours.Num()));
  135.             //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()));
  136.         }
  137.     }
  138. }
  139.  
  140. FVector2D AMap_Controller::find_tile_matrix_coordinate_from_index(int ISM_index, int instance_index)
  141. {
  142.     for (Ftile_indexer current_indexed_tile : tile_indexer)
  143.     {
  144.         if (current_indexed_tile.ISM_index == ISM_index && current_indexed_tile.instance_index == instance_index)
  145.             return FVector2D(current_indexed_tile.matrix_position_x, current_indexed_tile.matrix_position_y);
  146.     }
  147.  
  148.     return FVector2D(-1,-1);
  149. }
  150.  
  151. Ftile_indexer AMap_Controller::find_tile_index_from_matrix_coordinate(int matrix_position_x, int matrix_position_y)
  152. {
  153.     for (Ftile_indexer current_indexed_tile : tile_indexer)
  154.     {
  155.         if (current_indexed_tile.matrix_position_x == matrix_position_x && current_indexed_tile.matrix_position_y == matrix_position_y)
  156.             return current_indexed_tile;
  157.     }
  158.  
  159.     return Ftile_indexer(-1,-1,-1,-1);
  160. }
  161.  
  162. TArray<Fneighbour> AMap_Controller::find_tile_index_neighbours(int ISM_index, int instance_index)
  163. {
  164.     for (Ftile_indexer current_indexed_tile : tile_indexer)
  165.     {
  166.         if (current_indexed_tile.ISM_index == ISM_index && current_indexed_tile.instance_index == instance_index)
  167.         {
  168.             GEngine->AddOnScreenDebugMessage(-1, 2, FColor::Red, "Found");
  169.             return current_indexed_tile.get_neighbours();
  170.         }
  171.     }
  172.  
  173.     TArray<Fneighbour> null;
  174.     return null;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement