Simplify enum output with std::transform

This commit is contained in:
Celtic Minstrel 2016-07-19 16:58:37 -04:00
parent 0b57922488
commit 63081cac46
4 changed files with 36 additions and 53 deletions

View file

@ -349,13 +349,13 @@ unit_animation::unit_animation(const config& cfg,const std::string& frame_string
std::vector<std::string>::iterator hit;
for(hit=hits_str.begin() ; hit != hits_str.end() ; ++hit) {
if(*hit == "yes" || *hit == "hit") {
hits_.push_back(HIT);
hits_.push_back(hit_type::HIT);
}
if(*hit == "no" || *hit == "miss") {
hits_.push_back(MISS);
hits_.push_back(hit_type::MISS);
}
if(*hit == "yes" || *hit == "kill" ) {
hits_.push_back(KILL);
hits_.push_back(hit_type::KILL);
}
}
std::vector<std::string> value2_str = utils::split(cfg["value_second"]);
@ -535,8 +535,8 @@ void unit_animation::fill_initial_animations( std::vector<unit_animation> & anim
animations.push_back(*itor);
animations.back().event_ = utils::split("defend");
animations.back().unit_anim_.override(0,animations.back().unit_anim_.get_animation_duration(),particule::NO_CYCLE,"","0.0,0.5:75,0.0:75,0.5:75,0.0",game_display::rgb(255,0,0));
animations.back().hits_.push_back(HIT);
animations.back().hits_.push_back(KILL);
animations.back().hits_.push_back(hit_type::HIT);
animations.back().hits_.push_back(hit_type::KILL);
animations.push_back(*itor);
animations.back().event_ = utils::split("defend");
@ -1102,37 +1102,14 @@ std::ostream& operator << (std::ostream& outstream, const unit_animation& u_anim
outstream << "\tstart_time=" << u_animation.get_begin_time() << '\n';
if (u_animation.hits_.size() > 0) {
outstream << "\thits=";
bool need_comma = false;
for (const unit_animation::hit_type hit_type : u_animation.hits_) {
if (need_comma) outstream << ',';
need_comma = true;
switch (hit_type) {
case (unit_animation::HIT) : outstream << "hit"; break;
case (unit_animation::MISS) : outstream << "miss"; break;
case (unit_animation::KILL) : outstream << "kill"; break;
case (unit_animation::INVALID) : outstream << "invalid"; break;
}
}
outstream << '\n';
std::vector<std::string> hits;
std::transform(u_animation.hits_.begin(), u_animation.hits_.end(), std::back_inserter(hits), unit_animation::hit_type::enum_to_string);
outstream << "\thits=" << utils::join(hits) << '\n';
}
if (u_animation.directions_.size() > 0) {
outstream << "\tdirections=";
bool need_comma = false;
for (const map_location::DIRECTION direction : u_animation.directions_) {
if (need_comma) outstream << ',';
need_comma = true;
switch (direction) {
case (map_location::NORTH) : outstream << "n"; break;
case (map_location::NORTH_EAST): outstream << "ne"; break;
case (map_location::SOUTH_EAST): outstream << "se"; break;
case (map_location::SOUTH) : outstream << "s"; break;
case (map_location::SOUTH_WEST): outstream << "sw"; break;
case (map_location::NORTH_WEST): outstream << "nw"; break;
default: break;
}
}
outstream << '\n';
std::vector<std::string> dirs;
std::transform(u_animation.directions_.begin(), u_animation.directions_.end(), std::back_inserter(dirs), map_location::write_direction);
outstream << "\tdirections=" << utils::join(dirs) << '\n';
}
if (u_animation.terrain_types_.size() > 0) {
outstream << "\tterrain=" << utils::join(u_animation.terrain_types_) << '\n';

View file

@ -19,6 +19,7 @@
#include "halo.hpp"
#include "units/frame.hpp"
#include "units/ptr.hpp"
#include "utils/make_enum.hpp"
class attack_type;
class display;
@ -30,13 +31,18 @@ class unit_animation
unit_animation();
public:
typedef enum { MATCH_FAIL=-10 , DEFAULT_ANIM=-9} variation_type;
typedef enum { HIT, MISS, KILL, INVALID} hit_type;
MAKE_ENUM(hit_type,
(HIT, "hit")
(MISS, "miss")
(KILL, "kill")
(INVALID, "invalid")
);
static const std::vector<std::string>& all_tag_names();
static void fill_initial_animations( std::vector<unit_animation> & animations, const config & cfg);
static void add_anims( std::vector<unit_animation> & animations, const config & cfg);
int matches(const display &disp,const map_location& loc,const map_location& second_loc,const unit* my_unit,const std::string & event="",const int value=0,hit_type hit=INVALID,const attack_type* attack=nullptr,const attack_type* second_attack = nullptr, int value2 =0) const;
int matches(const display &disp,const map_location& loc,const map_location& second_loc,const unit* my_unit,const std::string & event="",const int value=0,hit_type hit=hit_type::INVALID,const attack_type* attack=nullptr,const attack_type* second_attack = nullptr, int value2 =0) const;
const unit_frame& get_last_frame() const{ return unit_anim_.get_last_frame() ; }
@ -173,7 +179,7 @@ class unit_animator
, const std::string& text = ""
, const Uint32 text_color = 0
, const unit_animation::hit_type hit_type =
unit_animation::INVALID
unit_animation::hit_type::INVALID
, const attack_type* attack = nullptr
, const attack_type* second_attack = nullptr
, int value2 = 0);
@ -186,7 +192,7 @@ class unit_animator
, const std::string& text = ""
, const Uint32 text_color = 0
, const unit_animation::hit_type hit_type =
unit_animation::INVALID
unit_animation::hit_type::INVALID
, const attack_type* attack = nullptr
, const attack_type* second_attack = nullptr
, int value2 = 0);

View file

@ -65,7 +65,7 @@ public:
const map_location& loc, const std::string& event,
const map_location& second_loc = map_location::null_location(),
const int damage=0,
const unit_animation::hit_type hit_type = unit_animation::INVALID,
const unit_animation::hit_type hit_type = unit_animation::hit_type::INVALID,
const attack_type* attack=nullptr,const attack_type* second_attack = nullptr,
int swing_num =0);

View file

@ -128,7 +128,7 @@ static int move_unit_between(const map_location& a, const map_location& b,
disp.invalidate(a);
temp_unit->set_facing(a.get_relative_dir(b));
animator.replace_anim_if_invalid(temp_unit.get(),"movement",a,b,step_num,
false,"",0,unit_animation::INVALID,nullptr,nullptr,step_left);
false,"",0,unit_animation::hit_type::INVALID,nullptr,nullptr,step_left);
animator.start_animations();
animator.pause_animation();
disp.scroll_to_tiles(a, b, game_display::ONSCREEN, true, 0.0, false);
@ -507,8 +507,8 @@ void unit_draw_weapon(const map_location& loc, unit& attacker,
unit_animator animator;
attacker.set_facing(loc.get_relative_dir(defender_loc));
defender->set_facing(defender_loc.get_relative_dir(loc));
animator.add_animation(&attacker,"draw_weapon",loc,defender_loc,0,false,"",0,unit_animation::HIT,attack,secondary_attack,0);
animator.add_animation(defender,"draw_weapon",defender_loc,loc,0,false,"",0,unit_animation::MISS,secondary_attack,attack,0);
animator.add_animation(&attacker,"draw_weapon",loc,defender_loc,0,false,"",0,unit_animation::hit_type::HIT,attack,secondary_attack,0);
animator.add_animation(defender,"draw_weapon",defender_loc,loc,0,false,"",0,unit_animation::hit_type::MISS,secondary_attack,attack,0);
animator.start_animations();
animator.wait_for_end();
@ -524,10 +524,10 @@ void unit_sheath_weapon(const map_location& primary_loc, unit* primary_unit,
}
unit_animator animator;
if(primary_unit) {
animator.add_animation(primary_unit,"sheath_weapon",primary_loc,secondary_loc,0,false,"",0,unit_animation::INVALID,primary_attack,secondary_attack,0);
animator.add_animation(primary_unit,"sheath_weapon",primary_loc,secondary_loc,0,false,"",0,unit_animation::hit_type::INVALID,primary_attack,secondary_attack,0);
}
if(secondary_unit) {
animator.add_animation(secondary_unit,"sheath_weapon",secondary_loc,primary_loc,0,false,"",0,unit_animation::INVALID,secondary_attack,primary_attack,0);
animator.add_animation(secondary_unit,"sheath_weapon",secondary_loc,primary_loc,0,false,"",0,unit_animation::hit_type::INVALID,secondary_attack,primary_attack,0);
}
if(primary_unit || secondary_unit) {
@ -554,10 +554,10 @@ void unit_die(const map_location& loc, unit& loser,
}
unit_animator animator;
// hide the hp/xp bars of the loser (useless and prevent bars around an erased unit)
animator.add_animation(&loser,"death",loc,winner_loc,0,false,"",0,unit_animation::KILL,attack,secondary_attack,0);
animator.add_animation(&loser,"death",loc,winner_loc,0,false,"",0,unit_animation::hit_type::KILL,attack,secondary_attack,0);
// but show the bars of the winner (avoid blinking and show its xp gain)
animator.add_animation(winner,"victory",winner_loc,loc,0,true,"",0,
unit_animation::KILL,secondary_attack,attack,0);
unit_animation::hit_type::KILL,secondary_attack,attack,0);
animator.start_animations();
animator.wait_for_end();
@ -609,11 +609,11 @@ void unit_attack(display * disp, game_board & board,
unit_animation::hit_type hit_type;
if(damage >= defender.hitpoints()) {
hit_type = unit_animation::KILL;
hit_type = unit_animation::hit_type::KILL;
} else if(damage > 0) {
hit_type = unit_animation::HIT;
hit_type = unit_animation::hit_type::HIT;
}else {
hit_type = unit_animation::MISS;
hit_type = unit_animation::hit_type::MISS;
}
animator.add_animation(&attacker, "attack", att->get_location(),
def->get_location(), damage, true, text_2,
@ -802,7 +802,7 @@ void wml_animation_internal(unit_animator &animator, const vconfig &cfg, const m
attack_type *primary = nullptr;
attack_type *secondary = nullptr;
Uint32 text_color;
unit_animation::hit_type hits= unit_animation::INVALID;
unit_animation::hit_type hits= unit_animation::hit_type::INVALID;
std::vector<attack_type> attacks = u->attacks();
std::vector<attack_type>::iterator itor;
boost::scoped_ptr<attack_type> dummy_primary;
@ -849,13 +849,13 @@ void wml_animation_internal(unit_animator &animator, const vconfig &cfg, const m
}
if(cfg["hits"] == "yes" || cfg["hits"] == "hit") {
hits = unit_animation::HIT;
hits = unit_animation::hit_type::HIT;
}
if(cfg["hits"] == "no" || cfg["hits"] == "miss") {
hits = unit_animation::MISS;
hits = unit_animation::hit_type::MISS;
}
if( cfg["hits"] == "kill" ) {
hits = unit_animation::KILL;
hits = unit_animation::hit_type::KILL;
}
if(cfg["red"].empty() && cfg["green"].empty() && cfg["blue"].empty()) {
text_color = display::rgb(0xff,0xff,0xff);