Last active
January 31, 2016 15:01
-
-
Save stormsson/51653f9569c0e0eaf13c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved. | |
#include "Ten.h" | |
#include "TenBlock.h" | |
#include "TenBlockGrid.h" | |
ATenBlock::ATenBlock() | |
{ | |
// Structure to hold one-time initialization | |
struct FConstructorStatics | |
{ | |
ConstructorHelpers::FObjectFinderOptional<UStaticMesh> PlaneMesh; | |
ConstructorHelpers::FObjectFinderOptional<UMaterialInstance> BlueMaterial; | |
ConstructorHelpers::FObjectFinderOptional<UMaterialInstance> OrangeMaterial; | |
FConstructorStatics() | |
: PlaneMesh(TEXT("/Game/Puzzle/Meshes/PuzzleCube.PuzzleCube")) | |
, BlueMaterial(TEXT("/Game/Puzzle/Meshes/BlueMaterial.BlueMaterial")) | |
, OrangeMaterial(TEXT("/Game/Puzzle/Meshes/OrangeMaterial.OrangeMaterial")) | |
{ | |
} | |
}; | |
static FConstructorStatics ConstructorStatics; | |
// Create dummy root scene component | |
DummyRoot = CreateDefaultSubobject<USceneComponent>(TEXT("Dummy0")); | |
RootComponent = DummyRoot; | |
// Create static mesh component | |
BlockMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BlockMesh0")); | |
BlockMesh->SetStaticMesh(ConstructorStatics.PlaneMesh.Get()); | |
BlockMesh->SetRelativeScale3D(FVector(1.f,1.f,0.25f)); | |
BlockMesh->SetRelativeLocation(FVector(0.f,0.f,25.f)); | |
BlockMesh->SetMaterial(0, ConstructorStatics.BlueMaterial.Get()); | |
BlockMesh->AttachTo(DummyRoot); | |
BlockMesh->OnClicked.AddDynamic(this, &ATenBlock::BlockClicked); | |
BlockMesh->OnInputTouchBegin.AddDynamic(this, &ATenBlock::OnFingerPressedBlock); | |
// Save a pointer to the orange material | |
OrangeMaterial = ConstructorStatics.OrangeMaterial.Get(); | |
} | |
void ATenBlock::BlockClicked(UPrimitiveComponent* ClickedComp) | |
{ | |
// Check we are not already active | |
if(!bIsActive) | |
{ | |
bIsActive = true; | |
// Change material | |
BlockMesh->SetMaterial(0, OrangeMaterial); | |
// Tell the Grid | |
if(OwningGrid != NULL) | |
{ | |
OwningGrid->AddScore(); | |
} | |
} | |
} | |
void ATenBlock::OnFingerPressedBlock(ETouchIndex::Type FingerIndex, UPrimitiveComponent* TouchedComponent) | |
{ | |
BlockClicked(TouchedComponent); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved. | |
#pragma once | |
#include "GameFramework/Actor.h" | |
#include "TenBlock.generated.h" | |
/** A block that can be clicked */ | |
UCLASS(minimalapi) | |
class ATenBlock : public AActor | |
{ | |
GENERATED_BODY() | |
/** Dummy root component */ | |
UPROPERTY(Category = Block, VisibleDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) | |
class USceneComponent* DummyRoot; | |
/** StaticMesh component for the clickable block */ | |
UPROPERTY(Category = Block, VisibleDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) | |
class UStaticMeshComponent* BlockMesh; | |
public: | |
ATenBlock(); | |
/** Are we currently active? */ | |
bool bIsActive; | |
/** Pointer to orange material used on active blocks */ | |
UPROPERTY() | |
class UMaterialInstance* OrangeMaterial; | |
/** Grid that owns us */ | |
UPROPERTY() | |
class ATenBlockGrid* OwningGrid; | |
/** Handle the block being clicked */ | |
UFUNCTION() | |
void BlockClicked(UPrimitiveComponent* ClickedComp); | |
/** Handle the block being touched */ | |
UFUNCTION() | |
void OnFingerPressedBlock(ETouchIndex::Type FingerIndex, UPrimitiveComponent* TouchedComponent); | |
public: | |
/** Returns DummyRoot subobject **/ | |
FORCEINLINE class USceneComponent* GetDummyRoot() const { return DummyRoot; } | |
/** Returns BlockMesh subobject **/ | |
FORCEINLINE class UStaticMeshComponent* GetBlockMesh() const { return BlockMesh; } | |
}; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved. | |
#include "Ten.h" | |
#include "TenBlockGrid.h" | |
#include "TenBlock.h" | |
#include "Components/TextRenderComponent.h" | |
#define LOCTEXT_NAMESPACE "PuzzleBlockGrid" | |
ATenBlockGrid::ATenBlockGrid() | |
{ | |
// Create dummy root scene component | |
DummyRoot = CreateDefaultSubobject<USceneComponent>(TEXT("Dummy0")); | |
RootComponent = DummyRoot; | |
// Create static mesh component | |
ScoreText = CreateDefaultSubobject<UTextRenderComponent>(TEXT("ScoreText0")); | |
ScoreText->SetRelativeLocation(FVector(200.f,0.f,0.f)); | |
ScoreText->SetRelativeRotation(FRotator(90.f,0.f,0.f)); | |
ScoreText->SetText(FText::Format(LOCTEXT("ScoreFmt", "Score: {0}"), FText::AsNumber(0))); | |
ScoreText->AttachTo(DummyRoot); | |
// Set defaults | |
Size = 5; | |
BlockSpacing = 250.f; | |
} | |
void ATenBlockGrid::BeginPlay() | |
{ | |
Super::BeginPlay(); | |
// Number of blocks | |
const int32 NumBlocks = Size * Size; | |
// Loop to spawn each block | |
for(int32 BlockIndex=0; BlockIndex<NumBlocks; BlockIndex++) | |
{ | |
const float XOffset = (BlockIndex/Size) * BlockSpacing; // Divide by dimension | |
const float YOffset = (BlockIndex%Size) * BlockSpacing; // Modulo gives remainder | |
// Make postion vector, offset from Grid location | |
const FVector BlockLocation = FVector(XOffset, YOffset, 0.f) + GetActorLocation(); | |
// Spawn a block | |
ATenBlock* NewBlock = GetWorld()->SpawnActor<ATenBlock>(BlockLocation, FRotator(0,0,0)); | |
// Tell the block about its owner | |
if(NewBlock != NULL) | |
{ | |
NewBlock->OwningGrid = this; | |
} | |
} | |
} | |
void ATenBlockGrid::AddScore() | |
{ | |
// Increment score | |
Score++; | |
// Update text | |
ScoreText->SetText(FText::Format(LOCTEXT("ScoreFmt", "Score: {0}"), FText::AsNumber(Score))); | |
} | |
#undef LOCTEXT_NAMESPACE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved. | |
#pragma once | |
#include "GameFramework/Actor.h" | |
#include "TenBlockGrid.generated.h" | |
/** Class used to spawn blocks and manage score */ | |
UCLASS(minimalapi) | |
class ATenBlockGrid : public AActor | |
{ | |
GENERATED_BODY() | |
/** Dummy root component */ | |
UPROPERTY(Category = Grid, VisibleDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) | |
class USceneComponent* DummyRoot; | |
/** Text component for the score */ | |
UPROPERTY(Category = Grid, VisibleDefaultsOnly, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) | |
class UTextRenderComponent* ScoreText; | |
public: | |
ATenBlockGrid(); | |
/** How many blocks have been clicked */ | |
int32 Score; | |
/** Number of blocks along each side of grid */ | |
UPROPERTY(Category=Grid, EditAnywhere, BlueprintReadOnly) | |
int32 Size; | |
/** Spacing of blocks */ | |
UPROPERTY(Category=Grid, EditAnywhere, BlueprintReadOnly) | |
float BlockSpacing; | |
// Begin AActor interface | |
virtual void BeginPlay() override; | |
// End AActor interface | |
/** Handle the block being clicked */ | |
void AddScore(); | |
public: | |
/** Returns DummyRoot subobject **/ | |
FORCEINLINE class USceneComponent* GetDummyRoot() const { return DummyRoot; } | |
/** Returns ScoreText subobject **/ | |
FORCEINLINE class UTextRenderComponent* GetScoreText() const { return ScoreText; } | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment