Skip to content

Instantly share code, notes, and snippets.

View olegknyazev's full-sized avatar

Oleg Knyazev olegknyazev

View GitHub Profile
@olegknyazev
olegknyazev / Turret-FindNearestTarget.cpp
Last active June 30, 2019 10:45
An example of bottom-up naming in Turret
void ATurret::Tick(float DeltaSeconds) {
...
TimeSinceLastTargetUpdate += DeltaSeconds;
if (TimeSinceLastTargetUpdate >= TargetSearchInterval) {
TimeSinceLastTargetUpdate = 0.f;
CurrentTarget = FindNearestTarget();
}
}
AActor* ATurret::FindNearestTarget() {
@olegknyazev
olegknyazev / HolyGrenade.cpp
Created June 30, 2019 10:42
An example usage of PredictFurtherLocation
void AHolyGrenade::Tick(float DeltaTime) {
...
const auto FurtherLocation = PredictFurtherLocation(...);
FurtherLocationBeacon->SetLocation(FurtherLocation);
}
@olegknyazev
olegknyazev / PredictFurtherLocation.cpp
Created June 30, 2019 10:41
An example of bottom-up naming
FVector PredictFurtherLocation(
const FVector& Location,
const FVector& Velocity,
const FVector& Acceleration,
float Time) {
...
}
@olegknyazev
olegknyazev / Turret-FindBestTarget.cpp
Last active June 30, 2019 10:44
An example of up-bottom naming in Turret
void ATurret::Tick(float DeltaSeconds) {
...
TimeSinceLastTargetUpdate += DeltaSeconds;
if (TimeSinceLastTargetUpdate >= TargetSearchInterval) {
TimeSinceLastTargetUpdate = 0.f;
CurrentTarget = FindBestTarget();
}
}
AActor* ATurret::FindBestTarget() {
@olegknyazev
olegknyazev / formatting-actor-get-attached-actors.cpp
Last active October 28, 2018 17:07
About Formatting: A highly 'professional' style
void AActor::GetAttachedActors(TArray<class AActor*>& OutActors) const
{
OutActors.Reset();
if (RootComponent != nullptr)
{
// Current set of components to check
TInlineComponentArray<USceneComponent*> CompsToCheck;
// Set of all components we have checked
TInlineComponentArray<USceneComponent*> CheckedComps;
@olegknyazev
olegknyazev / formatting-dense-code.cs
Created October 28, 2018 14:59
About Formatting: Writing dense code
public Vector3 WallNormal(Vector3 position, bool smoothNormalAtCorners = false) {
var clampedPosition = Clamp(position);
var positions =
_alignmentArea
.Where(path => !path.isEmpty)
.Select(path => {
int nearestSegIndex;
Vector2 nearestPoint2D;
var side = path.PointSide(Project(position), out nearestPoint2D, out nearestSegIndex);
var nearestPoint3D = Unproject(nearestPoint2D);
@olegknyazev
olegknyazev / formatting-empty-lines.scala
Created October 28, 2018 14:52
About Formatting: The empty line dilemma
def copyFrom
(source: ReadOnlySpace[ShapeT],
sourceRegion: CellBox = null,
destination: Int3 = this.lower): Unit = {
val sourceBounds =
if (sourceRegion != null) {
Check.argument(source.bounds contains sourceRegion)
sourceRegion
} else
@olegknyazev
olegknyazev / formatting-find-or-die.cs
Created October 28, 2018 14:38
About Formatting: Concise and dense but not functional
bool FindMaskOrDie() {
if (_destroyed)
return false;
mask = NearestMask(transform, out _affectedByMask)
?? NearestMask(transform, out _affectedByMask, enabledOnly: false);
if (mask == null) {
_destroyed = true;
DestroyImmediate(this);
return false;
}
@olegknyazev
olegknyazev / formatting-attack.scala
Last active October 28, 2018 14:37
About Formatting: Concise and dense code
case class Attack(from: Int3, to: Int3) extends Action {
override def apply(gs: GameState): GameState = {
Check.state(possible(gs))
gs mapWorld { world =>
(world.at(from), world.at(to)) match {
case (turret @ Building(_, _, tag @ Turret(ammo)), target @ Building(_, _, _)) =>
val damagedTarget = damage(target)
val worldAfterShot =
world.
replace(from, turret.copy(tag = tag.copy(ammo = ammo - 1))).
@olegknyazev
olegknyazev / formatting-actor-destroy.cpp
Created October 28, 2018 11:27
About Formatting: Actor::Destroy() from Unreal Engine
void AActor::BeginDestroy()
{
ULevel* OwnerLevel = GetLevel();
UnregisterAllComponents();
if (OwnerLevel && !OwnerLevel->HasAnyInternalFlags(EInternalObjectFlags::Unreachable))
{
OwnerLevel->Actors.RemoveSingleSwap(this, false);
}
Super::BeginDestroy();
}