Skip to content

Commit

Permalink
Merge pull request #61 from S1yGus/60-add-periodic-motion-to-the-food…
Browse files Browse the repository at this point in the history
…-model

Add periodic motion to the food model
  • Loading branch information
S1yGus authored Mar 14, 2024
2 parents 575e6ee + 9b303b6 commit 8091669
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 5 deletions.
Binary file modified Content/World/BP_Food.uasset
Binary file not shown.
5 changes: 5 additions & 0 deletions Source/Snake_Game/World/SG_BaseCellObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,10 @@ void ASG_BaseCellObject::OnChangingScale()
{
Mesh->SetRelativeScale3D(TargetScale);
GetWorldTimerManager().ClearTimer(ScaleTimerHandle);
OnScalingDone();
}
}

void ASG_BaseCellObject::OnScalingDone()
{
}
7 changes: 4 additions & 3 deletions Source/Snake_Game/World/SG_BaseCellObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SNAKE_GAME_API ASG_BaseCellObject : public AActor
/**
* Restarts the scale of the object from zero to the target scale
*/
void RestartScaling();
virtual void RestartScaling();

/**
* Reproduces the teardown effect
Expand All @@ -49,10 +49,10 @@ class SNAKE_GAME_API ASG_BaseCellObject : public AActor
UPROPERTY(EditDefaultsOnly, Category = "Materials")
FName MaterialColorParameterName{"Color"};

UPROPERTY(EditDefaultsOnly, Category = "Settings")
UPROPERTY(EditDefaultsOnly, Category = "Settings", Meta = (ClampMin = "0.0"))
float ScaleInterpSpeed{1.0f};

UPROPERTY(EditDefaultsOnly, Category = "Settings")
UPROPERTY(EditDefaultsOnly, Category = "Settings", Meta = (ClampMin = "0.0"))
float CellSizeFactor{1.0f};

UPROPERTY(EditDefaultsOnly, Category = "Effects")
Expand All @@ -69,4 +69,5 @@ class SNAKE_GAME_API ASG_BaseCellObject : public AActor
FLinearColor ObjectColor{FLinearColor::Black};

void OnChangingScale();
virtual void OnScalingDone();
};
21 changes: 20 additions & 1 deletion Source/Snake_Game/World/SG_Food.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ void ASG_Food::Tick(float DeltaTime)

if (TSharedPtr<Food> Food = CoreFood.Pin(); Food.IsValid())
{
SetActorLocation(GridOrigin + Utils::PosToVector(Food->position(), GridSize, CellSize));
FVector NewLocation = GridOrigin + Utils::PosToVector(Food->position(), GridSize, CellSize);
if (bScalingDone)
{
NewLocation.Z += FMath::Abs(FMath::Sin(PeriodicTime * PeriodicSpeed)) * CellSize * PeriodicAmplitudeFactor;
PeriodicTime += DeltaTime;
}
SetActorLocation(NewLocation);
}
}

Expand All @@ -46,3 +52,16 @@ void ASG_Food::UpdateMesh(UStaticMesh* NewMesh)
Mesh->SetMaterial(0, NewMesh->GetMaterial(0));
}
}

void ASG_Food::RestartScaling()
{
Super::RestartScaling();

bScalingDone = false;
}

void ASG_Food::OnScalingDone()
{
PeriodicTime = 0.0f;
bScalingDone = true;
}
16 changes: 16 additions & 0 deletions Source/Snake_Game/World/SG_Food.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,25 @@ class SNAKE_GAME_API ASG_Food : public ASG_BaseCellObject
*/
void UpdateMesh(UStaticMesh* NewMesh);

/**
* Restarts the scale of the object from zero to the target scale
*/
virtual void RestartScaling() override;

protected:
UPROPERTY(EditDefaultsOnly, Category = "Settings", Meta = (ClampMin = "0.0", ClampMax = "1.0"))
float PeriodicAmplitudeFactor{0.2f};

UPROPERTY(EditDefaultsOnly, Category = "Settings", Meta = (ClampMin = "0.0"))
float PeriodicSpeed{2.0f};

private:
TWeakPtr<SnakeGame::Food> CoreFood;
SnakeGame::Dim GridSize;
uint32 CellSize;
FVector GridOrigin;
bool bScalingDone{false};
float PeriodicTime{0.0f};

virtual void OnScalingDone() override;
};
2 changes: 1 addition & 1 deletion Source/Snake_Game/World/SG_Snake.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class SNAKE_GAME_API ASG_Snake : public AActor
UPROPERTY(EditDefaultsOnly, Category = "Settings")
TSubclassOf<ASG_SnakeLink> SnakeLinkClass;

UPROPERTY(EditDefaultsOnly, Category = "Settings")
UPROPERTY(EditDefaultsOnly, Category = "Settings", Meta = (ClampMin = "0"))
int32 ReservedLinksNumber{10};

private:
Expand Down

0 comments on commit 8091669

Please sign in to comment.