Skip to content

Commit

Permalink
code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
Narre committed Aug 5, 2023
1 parent 0f8f765 commit 1b61e53
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 101 deletions.
124 changes: 62 additions & 62 deletions src/badguy/yeti.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,36 @@ const float SNOW_EXPLOSIONS_VY = -200; /**< Speed of snowballs */

Yeti::Yeti(const ReaderMapping& reader) :
BadGuy(reader, "images/creatures/yeti/yeti.sprite"),
state(),
state_timer(),
safe_timer(),
stomp_count(),
hit_points(),
hud_head(),
left_stand_x(),
right_stand_x(),
left_jump_x(),
right_jump_x(),
fixed_pos(),
hud_icon()
m_state(),
m_state_timer(),
m_safe_timer(),
m_stomp_count(),
m_hit_points(),
m_hud_head(),
m_left_stand_x(),
m_right_stand_x(),
m_left_jump_x(),
m_right_jump_x(),
m_fixed_pos(),
m_hud_icon()
{
reader.get("lives", hit_points, INITIAL_HITPOINTS);
reader.get("lives", m_hit_points, INITIAL_HITPOINTS);
m_countMe = true;
SoundManager::current()->preload("sounds/yeti_gna.wav");
SoundManager::current()->preload("sounds/yeti_roar.wav");

reader.get("hud-icon", hud_icon, "images/creatures/yeti/hudlife.png");
hud_head = Surface::from_file(hud_icon);
reader.get("hud-icon", m_hud_icon, "images/creatures/yeti/hudlife.png");
m_hud_head = Surface::from_file(m_hud_icon);
m_allowed_directions = {};

initialize();

reader.get("fixed-pos", fixed_pos, false);
if (fixed_pos) {
left_stand_x = 80;
right_stand_x = 1140;
left_jump_x = 528;
right_jump_x = 692;
reader.get("fixed-pos", m_fixed_pos, false);
if (m_fixed_pos) {
m_left_stand_x = 80;
m_right_stand_x = 1140;
m_left_jump_x = 528;
m_right_jump_x = 692;
} else {
recalculate_pos();
}
Expand All @@ -102,22 +102,22 @@ void
Yeti::recalculate_pos()
{
if (m_dir == Direction::RIGHT) {
left_stand_x = m_col.m_bbox.get_left();
right_stand_x = left_stand_x + RUN_DISTANCE;
m_left_stand_x = m_col.m_bbox.get_left();
m_right_stand_x = m_left_stand_x + RUN_DISTANCE;
} else {
right_stand_x = m_col.m_bbox.get_left();
left_stand_x = right_stand_x - RUN_DISTANCE;
m_right_stand_x = m_col.m_bbox.get_left();
m_left_stand_x = m_right_stand_x - RUN_DISTANCE;
}

left_jump_x = left_stand_x + JUMP_SPACE;
right_jump_x = right_stand_x - JUMP_SPACE;
m_left_jump_x = m_left_stand_x + JUMP_SPACE;
m_right_jump_x = m_right_stand_x - JUMP_SPACE;
}

void
Yeti::draw(DrawingContext& context)
{
// we blink when we are safe
if (safe_timer.started() && size_t(g_game_time * 40) % 2)
if (m_safe_timer.started() && size_t(g_game_time * 40) % 2)
return;

draw_hit_points(context);
Expand All @@ -128,15 +128,15 @@ Yeti::draw(DrawingContext& context)
void
Yeti::draw_hit_points(DrawingContext& context)
{
if (hud_head)
if (m_hud_head)
{
context.push_transform();
context.set_translation(Vector(0, 0));
context.transform().scale = 1.f;

for (int i = 0; i < hit_points; ++i)
for (int i = 0; i < m_hit_points; ++i)
{
context.color().draw_surface(hud_head, Vector(BORDER_X + (static_cast<float>(i * hud_head->get_width())), BORDER_Y + 1), LAYER_FOREGROUND1);
context.color().draw_surface(m_hud_head, Vector(BORDER_X + (static_cast<float>(i * m_hud_head->get_width())), BORDER_Y + 1), LAYER_FOREGROUND1);
}

context.pop_transform();
Expand All @@ -146,28 +146,28 @@ Yeti::draw_hit_points(DrawingContext& context)
void
Yeti::active_update(float dt_sec)
{
switch (state) {
switch (m_state) {
case JUMP_DOWN:
m_physic.set_velocity_x((m_dir==Direction::RIGHT)?+JUMP_DOWN_VX:-JUMP_DOWN_VX);
break;
case RUN:
m_physic.set_velocity_x((m_dir==Direction::RIGHT)?+RUN_VX:-RUN_VX);
if (((m_dir == Direction::RIGHT) && (get_pos().x >= right_jump_x)) || ((m_dir == Direction::LEFT) && (get_pos().x <= left_jump_x))) jump_up();
if (((m_dir == Direction::RIGHT) && (get_pos().x >= m_right_jump_x)) || ((m_dir == Direction::LEFT) && (get_pos().x <= m_left_jump_x))) jump_up();
break;
case JUMP_UP:
m_physic.set_velocity_x((m_dir==Direction::RIGHT)?+JUMP_UP_VX:-JUMP_UP_VX);
if (((m_dir == Direction::RIGHT) && (get_pos().x >= right_stand_x)) || ((m_dir == Direction::LEFT) && (get_pos().x <= left_stand_x))) be_angry();
if (((m_dir == Direction::RIGHT) && (get_pos().x >= m_right_stand_x)) || ((m_dir == Direction::LEFT) && (get_pos().x <= m_left_stand_x))) be_angry();
break;
case BE_ANGRY:
if (state_timer.check() && on_ground()) {
if (m_state_timer.check() && on_ground()) {
m_physic.set_velocity_y(STOMP_VY);
set_action("stomp", m_dir);
SoundManager::current()->play("sounds/yeti_gna.wav", get_pos());
}
break;
case SQUISHED:
{
Direction newdir = (int(state_timer.get_timeleft() * SNOW_EXPLOSIONS_FREQUENCY) % 2) ? Direction::LEFT : Direction::RIGHT;
Direction newdir = (int(m_state_timer.get_timeleft() * SNOW_EXPLOSIONS_FREQUENCY) % 2) ? Direction::LEFT : Direction::RIGHT;
if (m_dir != newdir && m_dir == Direction::RIGHT) {
SoundManager::current()->play("sounds/stomp.wav", get_pos());
add_snow_explosions();
Expand All @@ -176,9 +176,9 @@ Yeti::active_update(float dt_sec)
m_dir = newdir;
set_action("jump", m_dir);
}
if (state_timer.check()) {
if (m_state_timer.check()) {
BadGuy::kill_fall();
state = FALLING;
m_state = FALLING;
m_physic.set_velocity_y(JUMP_UP_VY / 2); // Move up a bit before falling
// Add some extra explosions
for (int i = 0; i < 10; i++) {
Expand All @@ -200,7 +200,7 @@ Yeti::jump_down()
set_action("jump", m_dir);
m_physic.set_velocity_x((m_dir==Direction::RIGHT)?(+JUMP_DOWN_VX):(-JUMP_DOWN_VX));
m_physic.set_velocity_y(JUMP_DOWN_VY);
state = JUMP_DOWN;
m_state = JUMP_DOWN;
}

void
Expand All @@ -209,7 +209,7 @@ Yeti::run()
set_action("walking", m_dir);
m_physic.set_velocity_x((m_dir==Direction::RIGHT)?(+RUN_VX):(-RUN_VX));
m_physic.set_velocity_y(0);
state = RUN;
m_state = RUN;
}

void
Expand All @@ -218,7 +218,7 @@ Yeti::jump_up()
set_action("jump", m_dir);
m_physic.set_velocity_x((m_dir==Direction::RIGHT)?(+JUMP_UP_VX):(-JUMP_UP_VX));
m_physic.set_velocity_y(JUMP_UP_VY);
state = JUMP_UP;
m_state = JUMP_UP;
}

void
Expand All @@ -229,9 +229,9 @@ Yeti::be_angry()

set_action("stand", m_dir);
m_physic.set_velocity_x(0);
stomp_count = 0;
state = BE_ANGRY;
state_timer.start(STOMP_WAIT);
m_stomp_count = 0;
m_state = BE_ANGRY;
m_state_timer.start(STOMP_WAIT);
}

bool
Expand All @@ -254,27 +254,27 @@ Yeti::kill_squished(GameObject& object)

void Yeti::take_hit(Player& )
{
if (safe_timer.started())
if (m_safe_timer.started())
return;

SoundManager::current()->play("sounds/yeti_roar.wav", get_pos());
hit_points--;
m_hit_points--;

if (hit_points <= 0) {
if (m_hit_points <= 0) {
// We're dead
m_physic.set_velocity_x(((m_dir==Direction::RIGHT)?+RUN_VX:-RUN_VX)/5);
m_physic.set_velocity_y(0);

// Set the badguy layer to be above the foremost, so that
// this does not reveal secret tilemaps:
m_layer = Sector::get().get_foremost_layer() + 1;
state = SQUISHED;
state_timer.start(YETI_SQUISH_TIME);
m_state = SQUISHED;
m_state_timer.start(YETI_SQUISH_TIME);
set_colgroup_active(COLGROUP_MOVING_ONLY_STATIC);
//sprite->set_action("dead"); // This sprite does not look very good
}
else {
safe_timer.start(SAFE_TIME);
m_safe_timer.start(SAFE_TIME);
}
}

Expand All @@ -296,16 +296,16 @@ Yeti::drop_stalactite()
for (auto& stalactite : Sector::get().get_objects_by_type<YetiStalactite>())
{
if (stalactite.is_hanging()) {
if (hit_points >= 3) {
if (m_hit_points >= 3) {
// drop stalactites within 3 of player, going out with each jump
float distancex = fabsf(stalactite.get_bbox().get_middle().x - player->get_bbox().get_middle().x);
if (distancex < static_cast<float>(stomp_count) * 32.0f) {
if (distancex < static_cast<float>(m_stomp_count) * 32.0f) {
stalactite.start_shaking();
}
}
else { /* if (hitpoints < 3) */
// drop every 3rd pair of stalactites
if ((((static_cast<int>(stalactite.get_pos().x) + 16) / 64) % 3) == (stomp_count % 3)) {
if ((((static_cast<int>(stalactite.get_pos().x) + 16) / 64) % 3) == (m_stomp_count % 3)) {
stalactite.start_shaking();
}
}
Expand All @@ -320,7 +320,7 @@ Yeti::collision_solid(const CollisionHit& hit)
if (hit.top || hit.bottom) {
// hit floor or roof
m_physic.set_velocity_y(0);
switch (state) {
switch (m_state) {
case JUMP_DOWN:
run();
break;
Expand All @@ -330,17 +330,17 @@ Yeti::collision_solid(const CollisionHit& hit)
break;
case BE_ANGRY:
// we just landed
if (!state_timer.started()) {
if (!m_state_timer.started()) {
set_action("stand", m_dir);
stomp_count++;
m_stomp_count++;
drop_stalactite();

// go to other side after 3 jumps
if (stomp_count == 3) {
if (m_stomp_count == 3) {
jump_down();
} else {
// jump again
state_timer.start(STOMP_WAIT);
m_state_timer.start(STOMP_WAIT);
}
}
break;
Expand All @@ -351,7 +351,7 @@ Yeti::collision_solid(const CollisionHit& hit)
}
} else if (hit.left || hit.right) {
// hit wall
if(state != SQUISHED && state != FALLING)
if(m_state != SQUISHED && m_state != FALLING)
jump_up();
}
}
Expand All @@ -367,9 +367,9 @@ Yeti::get_settings()
{
ObjectSettings result = BadGuy::get_settings();

result.add_text("hud-icon", &hud_icon, "hud-icon", std::string("images/creatures/yeti/hudlife.png"), OPTION_HIDDEN);
result.add_bool(_("Fixed position"), &fixed_pos, "fixed-pos", false);
result.add_int(_("Lives"), &hit_points, "lives", 5);
result.add_text("hud-icon", &m_hud_icon, "hud-icon", std::string("images/creatures/yeti/hudlife.png"), OPTION_HIDDEN);
result.add_bool(_("Fixed position"), &m_fixed_pos, "fixed-pos", false);
result.add_int(_("Lives"), &m_hit_points, "lives", 5);

return result;
}
Expand Down
31 changes: 15 additions & 16 deletions src/badguy/yeti.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Yeti final : public BadGuy
void take_hit(Player& player);

void add_snow_explosions();
void recalculate_pos();

private:
enum YetiState {
Expand All @@ -66,22 +67,20 @@ class Yeti final : public BadGuy
};

private:
YetiState state;
Timer state_timer;
Timer safe_timer;
int stomp_count;
int hit_points;
SurfacePtr hud_head;

float left_stand_x;
float right_stand_x;
float left_jump_x;
float right_jump_x;

void recalculate_pos();

bool fixed_pos;
std::string hud_icon;
YetiState m_state;
Timer m_state_timer;
Timer m_safe_timer;
int m_stomp_count;
int m_hit_points;
SurfacePtr m_hud_head;

float m_left_stand_x;
float m_right_stand_x;
float m_left_jump_x;
float m_right_jump_x;

bool m_fixed_pos;
std::string m_hud_icon;

class SnowExplosionParticle: public BadGuy
{
Expand Down
2 changes: 1 addition & 1 deletion src/editor/object_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#include <assert.h>
#include <sexp/value.hpp>

#include "supertux/direction.hpp"
#include "util/gettext.hpp"
#include "video/color.hpp"
#include "supertux/direction.hpp"

ObjectSettings::ObjectSettings(const std::string& name) :
m_name(name),
Expand Down
Loading

0 comments on commit 1b61e53

Please sign in to comment.