Advertisement
Guest User

TestGeomPawn.cpp

a guest
Feb 11th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.31 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #include "QuickStart.h"
  4. #include "TestGeomPawn.h"
  5.  
  6.  
  7. // Sets default values
  8. ATestGeomPawn::ATestGeomPawn() : OurMesh(NULL), bGrowing(false)
  9. {
  10.  
  11.     PrimaryActorTick.bCanEverTick = true;
  12.     AutoPossessPlayer = EAutoReceiveInput::Player0;
  13.  
  14.     // root is a scene component so this is placeable in the editor
  15.     RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
  16.  
  17.     UCameraComponent* Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("OurCamera"));
  18.     Camera->SetupAttachment(RootComponent);
  19.     Camera->SetRelativeLocation(FVector(-250.0f, 0.0f, 250.0f));
  20.     Camera->SetRelativeRotation(FRotator(-45.0f, 0.0f, 0.0f));
  21.  
  22.     //RegenGeometry();
  23.  
  24. }
  25.  
  26.  
  27. // Called when the game starts or when spawned
  28. void ATestGeomPawn::BeginPlay()
  29. {
  30.     Super::BeginPlay();
  31.     //RegenGeometry();
  32. }
  33.  
  34. // Called to bind functionality to input
  35. void ATestGeomPawn::SetupPlayerInputComponent(class UInputComponent* InComp)
  36. {
  37.     Super::SetupPlayerInputComponent(InputComponent);
  38.     InComp->BindAction("Regen", IE_Pressed, this, &ATestGeomPawn::RegenGeometry);
  39.     InComp->BindAction("Grow", IE_Pressed, this, &ATestGeomPawn::StartGrowing);
  40.     InComp->BindAction("Grow", IE_Released, this, &ATestGeomPawn::StopGrowing);
  41. }
  42.  
  43. void ATestGeomPawn::RegenGeometry() {
  44.  
  45.     if (OurMesh) {
  46.         OurMesh->DestroyComponent();
  47.         OurMesh = NULL;
  48.     }
  49.  
  50.     static const float rverts[8][3] = {
  51.         { 1, 1, 1 },
  52.         { 1, 1, -1 },
  53.         { 1, -1, 1 },
  54.         { 1, -1, -1 },
  55.         { -1, 1, -1 },
  56.         { -1, 1, 1 },
  57.         { -1, -1, -1 },
  58.         { -1, -1, 1 }
  59.     };
  60.  
  61.     static const int rtris[12][3] = {
  62.         { 1, 3, 4 },
  63.         { 4, 2, 1 },
  64.         { 5, 7, 8 },
  65.         { 8, 6, 5 },
  66.         { 5, 6, 1 },
  67.         { 1, 2, 5 },
  68.         { 8, 7, 4 },
  69.         { 4, 3, 8 },
  70.         { 6, 8, 3 },
  71.         { 3, 1, 6 },
  72.         { 2, 4, 7 },
  73.         { 7, 5, 2 }
  74.     };
  75.  
  76.     float scales[8];
  77.     for (int n = 0; n < 8; ++n)
  78.         scales[n] = FMath::RandRange(30.f, 60.f);
  79.  
  80.     TArray<FCustomMeshTriangle> tris;
  81.    
  82.     for (int n = 0; n < 12; ++n) {
  83.         FCustomMeshTriangle t;
  84.         int a = rtris[n][2] - 1;
  85.         int b = rtris[n][1] - 1;
  86.         int c = rtris[n][0] - 1;
  87.         t.Vertex0 = FVector(rverts[a][0], rverts[a][1], rverts[a][2]) * scales[a];
  88.         t.Vertex1 = FVector(rverts[b][0], rverts[b][1], rverts[b][2]) * scales[b];
  89.         t.Vertex2 = FVector(rverts[c][0], rverts[c][1], rverts[c][2]) * scales[c];
  90.         tris.Add(t);
  91.     }
  92.  
  93.     // For generating different names:
  94.     //static int counter = 1;
  95.     //FName name = *(FString("RandomMesh") + FString::FormatAsNumber(counter));
  96.     //++counter;
  97.  
  98.     //OurMesh = NewNamedObject<UCustomMeshComponent>(this, name);
  99.     OurMesh = NewObject<UCustomMeshComponent>(this);
  100.     OurMesh->SetCustomMeshTriangles(tris);
  101.     OurMesh->SetupAttachment(RootComponent);
  102.  
  103. }
  104.  
  105. // Called every frame
  106. void ATestGeomPawn::Tick(float DeltaTime)
  107. {
  108.     Super::Tick(DeltaTime);
  109.     // Handle growing and shrinking based on our "Grow" action
  110.     if (OurMesh) {
  111.         float CurrentScale = OurMesh->GetComponentScale().X;
  112.         if (bGrowing)
  113.         {
  114.             // Grow to double size over the course of one second
  115.             CurrentScale += DeltaTime;
  116.         }
  117.         else
  118.         {
  119.             // Shrink half as fast as we grow
  120.             CurrentScale -= (DeltaTime * 0.5f);
  121.         }
  122.         // Make sure we never drop below our starting size, or increase past double size.
  123.         CurrentScale = FMath::Clamp(CurrentScale, 1.0f, 2.0f);
  124.         OurMesh->SetWorldScale3D(FVector(CurrentScale));
  125.     }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement