Skip to content

Commit

Permalink
unit animation fcns become const
Browse files Browse the repository at this point in the history
This commit makes the main unit animation accessor / mutator
functions applicable to const units, and makes the animation
state member variables mutable to allow this.

const'ed member functions:

set standning
idling
selecting
ghosted
disabed_ghosted
clear_haloes
refresh
invalidate
redraw

mutable member variables:

animation state enum
boost scoped pointer anim_ (can be reset in the redraw fcn)
next_idling_
frame_begin_time_
unit_halo_
refreshing_
draw_bars_

The purpose of this is to improve encapsulation of units, as now
we can use const units in the display functions, and reserve non-
const unit references for use in the game logic.
  • Loading branch information
cbeck88 committed Jun 10, 2014
1 parent 2282f6e commit 390babf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
22 changes: 11 additions & 11 deletions src/unit.cpp
Expand Up @@ -1803,7 +1803,7 @@ const surface unit::still_image(bool scaled) const
return unit_image;
}

void unit::set_standing(bool with_bars)
void unit::set_standing(bool with_bars) const
{
display *disp = display::get_singleton();
if (preferences::show_standing_animations()&& !incapacitated()) {
Expand All @@ -1815,28 +1815,28 @@ void unit::set_standing(bool with_bars)
}
}

void unit::set_ghosted(bool with_bars)
void unit::set_ghosted(bool with_bars) const
{
display *disp = display::get_singleton();
start_animation(INT_MAX, choose_animation(*disp, loc_, "ghosted"),
with_bars);
}

void unit::set_disabled_ghosted(bool with_bars)
void unit::set_disabled_ghosted(bool with_bars) const
{
display *disp = display::get_singleton();
start_animation(INT_MAX, choose_animation(*disp, loc_, "disabled_ghosted"),
with_bars);
}

void unit::set_idling()
void unit::set_idling() const
{
display *disp = display::get_singleton();
start_animation(INT_MAX, choose_animation(*disp, loc_, "idling"),
true, "", 0, STATE_FORGET);
}

void unit::set_selecting()
void unit::set_selecting() const
{
const display *disp = display::get_singleton();
if (preferences::show_standing_animations() && !get_state(STATE_PETRIFIED)) {
Expand All @@ -1848,8 +1848,8 @@ void unit::set_selecting()
}
}

void unit::start_animation(int start_time, const unit_animation *animation,
bool with_bars, const std::string &text, Uint32 text_color, STATE state)
void unit::start_animation (int start_time, const unit_animation *animation,
bool with_bars, const std::string &text, Uint32 text_color, STATE state) const
{
const display * disp = display::get_singleton();
if (!animation) {
Expand Down Expand Up @@ -1884,7 +1884,7 @@ void unit::set_facing(map_location::DIRECTION dir) {
// Else look at yourself (not available so continue to face the same direction)
}

void unit::redraw_unit()
void unit::redraw_unit () const
{
display &disp = *display::get_singleton();
const gamemap &map = disp.get_map();
Expand Down Expand Up @@ -2151,15 +2151,15 @@ void unit::redraw_unit()
refreshing_ = false;
}

void unit::clear_haloes()
void unit::clear_haloes () const
{
if(unit_halo_ != halo::NO_HALO) {
halo::remove(unit_halo_);
unit_halo_ = halo::NO_HALO;
}
if(anim_ ) anim_->clear_haloes();
}
bool unit::invalidate(const display & disp)
bool unit::invalidate (const display & disp) const
{
bool result = false;

Expand Down Expand Up @@ -3026,7 +3026,7 @@ int side_upkeep(int side)
return res;
}

void unit::refresh()
void unit::refresh() const
{
if (state_ == STATE_FORGET && anim_ && anim_->animation_finished_potential())
{
Expand Down
40 changes: 20 additions & 20 deletions src/unit.hpp
Expand Up @@ -204,7 +204,7 @@ class unit
void end_turn();
void new_scenario();
/** Called on every draw */
void refresh();
void refresh() const;

bool take_hit(int damage) { hit_points_ -= damage; return hit_points_ <= 0; }
void heal(int amount);
Expand Down Expand Up @@ -249,23 +249,22 @@ class unit
const surface still_image(bool scaled = false) const;

/** draw a unit. */
void redraw_unit();
void redraw_unit() const;
/** Clear unit_halo_ */
void clear_haloes();
void clear_haloes() const;

void set_standing(bool with_bars = true);
void set_standing(bool with_bars = true) const;

void set_ghosted(bool with_bars = true);
void set_disabled_ghosted(bool with_bars = true);
void set_ghosted(bool with_bars = true) const;
void set_disabled_ghosted(bool with_bars = true) const;

void set_idling();
void set_selecting();
unit_animation* get_animation() { return anim_.get();}
const unit_animation* get_animation() const { return anim_.get();}
void set_idling() const;
void set_selecting() const;
unit_animation* get_animation() const { return anim_.get();}
void set_facing(map_location::DIRECTION dir);
map_location::DIRECTION facing() const { return facing_; }

bool invalidate(const display & disp);
bool invalidate(const display & disp) const;
const std::vector<t_string>& trait_names() const { return trait_names_; }
const std::vector<t_string>& trait_descriptions() const { return trait_descriptions_; }
std::vector<std::string> get_traits_list() const;
Expand Down Expand Up @@ -327,9 +326,9 @@ class unit
STATE_STANDING, /** anim must fit in a hex */
STATE_FORGET, /** animation will be automatically replaced by a standing anim when finished */
STATE_ANIM}; /** normal anims */
void start_animation(int start_time, const unit_animation *animation,
void start_animation (int start_time, const unit_animation *animation,
bool with_bars, const std::string &text = "",
Uint32 text_color = 0, STATE state = STATE_ANIM);
Uint32 text_color = 0, STATE state = STATE_ANIM) const;

/** The name of the file to game_display (used in menus). */
std::string absolute_image() const;
Expand Down Expand Up @@ -488,7 +487,8 @@ class unit
config events_;
config filter_recall_;
bool emit_zoc_;
STATE state_;

mutable STATE state_; //animation state

std::vector<std::string> overlays_;

Expand All @@ -508,16 +508,16 @@ class unit
// Animations:
std::vector<unit_animation> animations_;

boost::scoped_ptr<unit_animation> anim_;
int next_idling_;
int frame_begin_time_;
mutable boost::scoped_ptr<unit_animation> anim_;
mutable int next_idling_; // used for animation
mutable int frame_begin_time_; // used for animation


int unit_halo_;
mutable int unit_halo_; // flag used for drawing / animation
bool getsHit_;
bool refreshing_; // avoid infinite recursion
mutable bool refreshing_; // avoid infinite recursion. flag used for drawing / animation
bool hidden_;
bool draw_bars_;
mutable bool draw_bars_; // flag used for drawing / animation
double hp_bar_scaling_, xp_bar_scaling_;

config modifications_;
Expand Down

0 comments on commit 390babf

Please sign in to comment.