Skip to content

Commit

Permalink
Better constness (game)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertvaka committed Dec 1, 2024
1 parent 0f4b1f9 commit 55cbb3e
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/bipedal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void Bipedal::Update(float dt)
}
}

const vec* damageFromPlayerPos = ReceiveDamageFromPlayer(headHitBox, hitTimer > 0.f);
std::optional<vec> damageFromPlayerPos = ReceiveDamageFromPlayer(headHitBox, hitTimer > 0.f);
if (damageFromPlayerPos) {
TakeDamage();
if (alive == false) return;
Expand Down
14 changes: 8 additions & 6 deletions src/common_enemy.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <optional>

#include "player.h"
#include "assets_sounds.h"
#include "bullet.h"
Expand All @@ -15,25 +17,25 @@

// returns the position of the attack if damaged or null otherwise. don't store the returned pointer for longer than this frame
template<typename B>
const vec* ReceiveDamageFromPlayer(const B& bounds, bool enemyInvulnerable) {
std::optional<vec> ReceiveDamageFromPlayer(const B& bounds, bool enemyInvulnerable) {
for (Bullet* b : Bullet::GetAll()) {
if (!b->alive) continue;
if (Collide(b->Bounds(), bounds)) {
b->Explode();
if (enemyInvulnerable) return nullptr;
return &(b->pos);
if (enemyInvulnerable) return std::nullopt;
return b->pos;
}
}

Player* player = Player::instance();
if (player && player->playerAttack.alive) {
if (Collide(player->playerAttack.Bounds(), bounds)) {
player->DealDamage(bounds.Center());
if (enemyInvulnerable) return nullptr;
return &(player->playerAttack.pos);
if (enemyInvulnerable) return std::nullopt;
return player->playerAttack.pos;
}
}
return nullptr;
return std::nullopt;
}

template<typename B>
Expand Down
8 changes: 4 additions & 4 deletions src/dialogs.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const inline DialogCharacter protaChar = { "Warrior", AnimLib::PORTRAIT_WARRIOR,
const inline DialogCharacter randomNpcChar = { "Random NPC", AnimLib::PORTRAIT_RANDOM_NPC, Assets::aiVoice };

namespace Color {
static const TextColor BLACK(0, 0, 0);
static const TextColor WHITE(255, 255, 255);
static const TextColor PINK(250, 10, 250);
static const TextColor YELLOW(250, 250, 10);
static constexpr TextColor BLACK(0, 0, 0);
static constexpr TextColor WHITE(255, 255, 255);
static constexpr TextColor PINK(250, 10, 250);
static constexpr TextColor YELLOW(250, 250, 10);
}

inline Dialog dialogWithRandomNpcWithChoices = {{
Expand Down
2 changes: 1 addition & 1 deletion src/flyingalien.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void FlyingAlien::Update(float dt)
}

hitTimer -= dt;
const vec* damageFromPlayerPos = ReceiveDamageFromPlayer(Bounds(), hitTimer > 0.f);
std::optional<vec> damageFromPlayerPos = ReceiveDamageFromPlayer(Bounds(), hitTimer > 0.f);
if (damageFromPlayerPos) {
TakeDamage(*damageFromPlayerPos);
if (alive == false) return;
Expand Down
2 changes: 1 addition & 1 deletion src/goomba.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void Goomba::Update(float dt)
break;
}

const vec* hitPos = ReceiveDamageFromPlayer(Bounds(), false);
std::optional<vec> hitPos = ReceiveDamageFromPlayer(Bounds(), false);
if (hitPos) {
if (type == Type::SHIELDER) {
bool hitFromRight = (player->pos.x > pos.x);
Expand Down
2 changes: 1 addition & 1 deletion src/mantis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void Mantis::Update(float dt)
}
}

const vec* damageFromPlayerPos = ReceiveDamageFromPlayer(Bounds(), hitTimer > 0.f);
std::optional<vec> damageFromPlayerPos = ReceiveDamageFromPlayer(Bounds(), hitTimer > 0.f);
if (damageFromPlayerPos) {
TakeDamage(*damageFromPlayerPos);
if (alive == false) return;
Expand Down
2 changes: 1 addition & 1 deletion src/minotaur.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void Minotaur::Update(float dt)
}
}

const vec* damageFromPlayerPos = ReceiveDamageFromPlayer(Bounds(), hitTimer > 0.f);
std::optional<vec> damageFromPlayerPos = ReceiveDamageFromPlayer(Bounds(), hitTimer > 0.f);
if (damageFromPlayerPos) {
wasAttacked = true;
TakeDamage();
Expand Down
92 changes: 46 additions & 46 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,77 +23,77 @@
extern float mainClock;

// input
const float kIsJustPressedIntervalTime = 0.15f;
const float kJumpTimeAfterBeingGrounded = 0.07f; //~4 frames
constexpr float kIsJustPressedIntervalTime = 0.15f;
constexpr float kJumpTimeAfterBeingGrounded = 0.07f; //~4 frames

// accel
const float kRunAcc = 1400;
const float kRunAcc_OnAir = 1000;
const float kGravityAcc = 660;
const float kDiveHorizontalAcc = 10;
constexpr float kRunAcc = 1400;
constexpr float kRunAcc_OnAir = 1000;
constexpr float kGravityAcc = 660;
constexpr float kDiveHorizontalAcc = 10;

// friction X
const float kFrictAccFloor = 1000;
const float kFrictAccFloor_Crouched = 450;
const float kFrictAcc_OnAir = 500;
constexpr float kFrictAccFloor = 1000;
constexpr float kFrictAccFloor_Crouched = 450;
constexpr float kFrictAcc_OnAir = 500;

// friction Y
const float kFrictAccVert_WallUp = 400;
const float kFrictAccVert_WallDown = 540;
constexpr float kFrictAccVert_WallUp = 400;
constexpr float kFrictAccVert_WallDown = 540;

// jump
const float kVelJump = -200; // Y axis
const float kVelWalljump = 140; // X axis
const float kVelSlopejump = 140; // X axis
const float kJumpHeightDiff = 72.f; // vertical pixels (note: this is the height at wich we stop adding vertical velocity, but we jump slightly higher thant his, ~8px more;
const float kTimeCrouchedToJumpDownOneWayTile = 0.2f;
const float kTimeToJumpFromWallAfterLettingGo = 0.2f;
constexpr float kVelJump = -200; // Y axis
constexpr float kVelWalljump = 140; // X axis
constexpr float kVelSlopejump = 140; // X axis
constexpr float kJumpHeightDiff = 72.f; // vertical pixels (note: this is the height at wich we stop adding vertical velocity, but we jump slightly higher thant his, ~8px more;
constexpr float kTimeCrouchedToJumpDownOneWayTile = 0.2f;
constexpr float kTimeToJumpFromWallAfterLettingGo = 0.2f;

// dash
const float kVelDash = 400;
const float kDashCooldown = 1.5f;
const float kDashDuration = 0.22f;
constexpr float kVelDash = 400;
constexpr float kDashCooldown = 1.5f;
constexpr float kDashDuration = 0.22f;

// dive
const float kVelDive = 400;
const float kDiveRestTime = 0.4f; // time you can't move after touching ground
const float kDiveRestTimeAfterHitting = 0.3f;
constexpr float kVelDive = 400;
constexpr float kDiveRestTime = 0.4f; // time you can't move after touching ground
constexpr float kDiveRestTimeAfterHitting = 0.3f;

// limits
const vec kVelMax(220, 350);
constexpr vec kVelMax(220, 350);

// bfg
const float kBulletVel = 400.f;
const float kBfgCooldown = 0.2f;
const float kBfgPushBack = 150.f;
constexpr float kBulletVel = 400.f;
constexpr float kBfgCooldown = 0.2f;
constexpr float kBfgPushBack = 150.f;

// knockback
const float kDoDamageKnockbackVel = 100.f;
const float kDoDamageKnockbackVelGrounded = 140.f;
const float kDoDamageUpKnockbackVel = 180.f;
const float kDoDamageDownKnockbackVel = -220.f;
const vec kTakeDamageKnockbackVel(180.f, -150.f);
const float kInvencibleTimeAfterHit = 1.0f;
constexpr float kDoDamageKnockbackVel = 100.f;
constexpr float kDoDamageKnockbackVelGrounded = 140.f;
constexpr float kDoDamageUpKnockbackVel = 180.f;
constexpr float kDoDamageDownKnockbackVel = -220.f;
constexpr vec kTakeDamageKnockbackVel(180.f, -150.f);
constexpr float kInvencibleTimeAfterHit = 1.0f;

// sword attack
const float kSwordAttackRadius = 23.3f;
const vec kSwordAttackOffset = vec(16.f,-17.f);
constexpr float kSwordAttackRadius = 23.3f;
constexpr vec kSwordAttackOffset = vec(16.f,-17.f);

const float kSwordAttackDownRadius = 13.f;
const vec kSwordAttackDownOffset = vec(2.f,3.f);
constexpr float kSwordAttackDownRadius = 13.f;
constexpr vec kSwordAttackDownOffset = vec(2.f,3.f);

const float kSwordAttackUpRadius = 21.3f;
const vec kSwordAttackUpOffset = vec(0.f, -30.f);
constexpr float kSwordAttackUpRadius = 21.3f;
constexpr vec kSwordAttackUpOffset = vec(0.f, -30.f);

const float kSwordAttackWallSlideRadius = 23.f;
const vec kSwordAttackWallSlideOffset = vec(-16.f, -15.5f);
constexpr float kSwordAttackWallSlideRadius = 23.f;
constexpr vec kSwordAttackWallSlideOffset = vec(-16.f, -15.5f);

// Sprite
const vec kStandingSize = vec(13, 32);
const vec kCrouchedSize = vec(13, 22);
const int kOffsetSwordlessSpritesheet = 413;
constexpr vec kStandingSize = vec(13, 32);
constexpr vec kCrouchedSize = vec(13, 22);
constexpr int kOffsetSwordlessSpritesheet = 413;

const float kHealthAnimationTime = 0.3f;
constexpr float kHealthAnimationTime = 0.3f;

void DestroyTilesWithSword(const CircleBounds& e) {
GaemTileMap* map = GaemTileMap::instance();
Expand Down Expand Up @@ -990,7 +990,7 @@ void Player::DrawGUI(bool discreteHealth)
}
else {
float scale = 1.5f;
const vec pos(6, 28 - 19);
constexpr vec pos(6, 28 - 19);
const int size = 8 * maxHealth;

// Background
Expand Down
6 changes: 3 additions & 3 deletions src/rocketlauncher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#include "common_tilemapcharacter.h"

const float kTimeBetweenMissiles = 3.5f;
const vec kCanonOrigin = vec(0, -4);
const vec kCanonLength = vec(10, 0);
const vec kAwokenLedOffset = vec(-3, -7);
constexpr vec kCanonOrigin = vec(0, -4);
constexpr vec kCanonLength = vec(10, 0);
constexpr vec kAwokenLedOffset = vec(-3, -7);

extern float mainClock;

Expand Down
2 changes: 1 addition & 1 deletion src/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int ScreenManager::FindScreenContaining(vec pos) {
return FindIndexOfSmallestBoundsContaining(pos, screens);
}

const void ScreenManager::ClampCameraToScreen(vec& camPos) {
void ScreenManager::ClampCameraToScreen(vec& camPos) {
if (currentScreen < 0) return;

const BoxBounds& screenBounds = CurrentBounds();
Expand Down
2 changes: 1 addition & 1 deletion src/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct ScreenManager
return screens[screen];
}

static const void ClampCameraToScreen(vec& camPos);
static void ClampCameraToScreen(vec& camPos);

static void UpdateCurrentScreen(vec pos);

Expand Down
2 changes: 1 addition & 1 deletion src/steering_behavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ vec SteeringBehavior::FollowPath()
// Produces a steering force that keeps a SteeringEntity at a specified offset
// from a leader SteeringEntity
//------------------------------------------------------------------------
vec SteeringBehavior::OffsetPursuit(const Entity* leader, const float offset)
vec SteeringBehavior::OffsetPursuit(const Entity* leader, float offset)
{
vec leaderHeading = leader->vel.Normalized();
vec displacement = leaderHeading * - offset;
Expand Down
10 changes: 5 additions & 5 deletions src/steering_behavior.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct SteeringBehavior

//this behavior maintains a position, in the direction of offset
//from the target SteeringEntity
vec OffsetPursuit(const Entity* leader, const float offset);
vec OffsetPursuit(const Entity* leader, float offset);

//this behavior makes the agent wander about randomly
vec Wander(float WanderRad, float WanderDist, float WanderJitterPerSec, float dt);
Expand All @@ -62,19 +62,19 @@ struct SteeringBehavior

//helper method for Hide. Returns a position located on the other
//side of an obstacle to the pursuer
vec GetHidingPosition(vec posOb, const float radiusOb, vec posHunter);
vec GetHidingPosition(vec posOb, float radiusOb, vec posHunter);

bool avoidingTileMap = false;
bool avoidingBounds = false;

//protected:

//a pointer to the owner of this instance
SteeringEntity* steeringEntity;
SteeringEntity* steeringEntity;

//the current position on the wander circle the agent is
//attempting to steer towards
vec m_vWanderTarget;
vec m_vWanderTarget;

};

Expand Down Expand Up @@ -210,7 +210,7 @@ vec SteeringBehavior::ObstacleAvoidance(const std::vector<T*>& obstacles, float

//apply a braking force proportional to the obstacles distance from
//the SteeringEntity.
const float BrakingWeight = 0.2f;
constexpr float BrakingWeight = 0.2f;

SteeringForce.x = (ClosestIntersectingObstacle->radius - LocalPosOfClosestObstacle.x) * BrakingWeight;
}
Expand Down
2 changes: 1 addition & 1 deletion src/steering_behavior_applier.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ struct SteeringBehaviorApplier : SteeringBehavior {
void ObstacleAvoidanceOn() { m_iFlags |= obstacle_avoidance; }
void BoundsAvoidanceOn(const BoxBounds& bounds) { m_bounds = bounds; m_iFlags |= bounds_avoidance; }
void HideOn(Entity* v) { m_iFlags |= hide; hideTarget = v; }
void OffsetPursuitOn(Entity* v1, const float offset) { m_iFlags |= offset_pursuit; m_vOffset = offset; pursuitTarget = v1; }
void OffsetPursuitOn(Entity* v1, float offset) { m_iFlags |= offset_pursuit; m_vOffset = offset; pursuitTarget = v1; }
void TileMapAvoidanceOn(GaemTileMap* tilemap) { m_iFlags |= tilemap_avoidance; m_tilemap = tilemap; }

void ForwardOff() { if (On(forward)) m_iFlags ^= forward; }
Expand Down

0 comments on commit 55cbb3e

Please sign in to comment.