forgot some commits when merging water animation branch:

...random start for animations
This commit is contained in:
Jérémy Rosen 2010-04-02 14:27:36 +00:00
parent eff514d4aa
commit 8c40e72fa2
4 changed files with 47 additions and 17 deletions

View File

@ -79,6 +79,7 @@ public:
bool animation_finished_potential() const; bool animation_finished_potential() const;
int get_animation_time() const; int get_animation_time() const;
int get_animation_time_potential() const; int get_animation_time_potential() const;
void set_animation_time(int time);
int get_animation_duration() const; int get_animation_duration() const;
const T& get_current_frame() const; const T& get_current_frame() const;
@ -119,7 +120,7 @@ private:
bool does_not_change_; // Optimization for 1-frame permanent animations bool does_not_change_; // Optimization for 1-frame permanent animations
bool started_; bool started_;
bool need_first_update_; bool force_next_update_;
std::vector<frame> frames_; std::vector<frame> frames_;
// These are only valid when anim is started // These are only valid when anim is started

View File

@ -47,7 +47,7 @@ animated<T,T_void_value>::animated(int start_time) :
starting_frame_time_(start_time), starting_frame_time_(start_time),
does_not_change_(true), does_not_change_(true),
started_(false), started_(false),
need_first_update_(false), force_next_update_(false),
frames_(), frames_(),
start_tick_(0), start_tick_(0),
cycles_(false), cycles_(false),
@ -62,7 +62,7 @@ animated<T,T_void_value>::animated(const std::vector<std::pair<int,T> > &cfg, in
starting_frame_time_(start_time), starting_frame_time_(start_time),
does_not_change_(true), does_not_change_(true),
started_(false), started_(false),
need_first_update_(false), force_next_update_(false),
frames_(), frames_(),
start_tick_(0), start_tick_(0),
cycles_(false), cycles_(false),
@ -104,7 +104,7 @@ void animated<T,T_void_value>::start_animation(int start_time, bool cycles)
cycles_ = cycles; cycles_ = cycles;
if(acceleration_ <=0) acceleration_ = 1; if(acceleration_ <=0) acceleration_ = 1;
current_frame_key_= 0; current_frame_key_= 0;
need_first_update_ = !frames_.empty(); force_next_update_ = !frames_.empty();
} }
@ -122,8 +122,8 @@ void animated<T,T_void_value>::update_last_draw_time(double acceleration)
start_tick_ +=current_ticks -last_update_tick_; start_tick_ +=current_ticks -last_update_tick_;
} }
last_update_tick_ = current_ticks; last_update_tick_ = current_ticks;
if (need_first_update_) { if (force_next_update_) {
need_first_update_ = false; force_next_update_ = false;
return; return;
} }
if(does_not_change_) if(does_not_change_)
@ -153,7 +153,7 @@ void animated<T,T_void_value>::update_last_draw_time(double acceleration)
template<typename T, typename T_void_value> template<typename T, typename T_void_value>
bool animated<T,T_void_value>::need_update() const bool animated<T,T_void_value>::need_update() const
{ {
if(need_first_update_) { if(force_next_update_) {
return true; return true;
} }
if(does_not_change_) { if(does_not_change_) {
@ -218,6 +218,17 @@ int animated<T,T_void_value>::get_animation_time() const
return tick_to_time(last_update_tick_); return tick_to_time(last_update_tick_);
} }
template<typename T, typename T_void_value>
void animated<T,T_void_value>::set_animation_time(int time)
{
last_update_tick_ = current_ticks;
start_tick_ = last_update_tick_ +
static_cast<int>(( starting_frame_time_ - time)/acceleration_);
current_frame_key_= 0;
force_next_update_ = true;
}
template<typename T, typename T_void_value> template<typename T, typename T_void_value>
int animated<T,T_void_value>::get_animation_duration() const int animated<T,T_void_value>::get_animation_duration() const
{ {

View File

@ -67,18 +67,19 @@ terrain_builder::tile::tile() :
images(), images(),
images_foreground(), images_foreground(),
images_background(), images_background(),
last_tod("invalid_tod") last_tod("invalid_tod"),
rand_seed(rand())
{} {}
void terrain_builder::tile::add_image_to_cache(const std::string &tod, ordered_ri_list::const_iterator itor) void terrain_builder::tile::add_image_to_cache(const std::string &tod, ordered_ri_list::const_iterator itor)
{ {
rule_image_variantlist::const_iterator tod_variant = rule_image_variantlist::const_iterator tod_variant =
itor->second->variants.find(tod); itor->second.second->variants.find(tod);
if(tod_variant == itor->second->variants.end()) if(tod_variant == itor->second.second->variants.end())
tod_variant = itor->second->variants.find(""); tod_variant = itor->second.second->variants.find("");
if(tod_variant != itor->second->variants.end()) { if(tod_variant != itor->second.second->variants.end()) {
//calculate original y-value and layer from list index //calculate original y-value and layer from list index
int layer = itor->first / BASE_Y_INTERVAL; int layer = itor->first / BASE_Y_INTERVAL;
int basey = itor->first % BASE_Y_INTERVAL; int basey = itor->first % BASE_Y_INTERVAL;
@ -90,8 +91,10 @@ void terrain_builder::tile::add_image_to_cache(const std::string &tod, ordered_r
if(layer < 0 || (layer == 0 && basey < UNITPOS)) { if(layer < 0 || (layer == 0 && basey < UNITPOS)) {
images_background.push_back(tod_variant->second.image); images_background.push_back(tod_variant->second.image);
images_background.back().set_animation_time(itor->second.first%images_background.back().get_animation_duration());
} else { } else {
images_foreground.push_back(tod_variant->second.image); images_foreground.push_back(tod_variant->second.image);
images_foreground.back().set_animation_time(itor->second.first%images_foreground.back().get_animation_duration());
} }
} }
} }
@ -111,6 +114,7 @@ void terrain_builder::tile::clear()
{ {
flags.clear(); flags.clear();
images.clear(); images.clear();
rand_seed=rand();
images_foreground.clear(); images_foreground.clear();
images_background.clear(); images_background.clear();
last_tod = "invalid_tod"; last_tod = "invalid_tod";
@ -128,6 +132,7 @@ void terrain_builder::tilemap::reload(int x, int y)
y_ = y; y_ = y;
std::vector<terrain_builder::tile> new_tiles((x + 4) * (y + 4)); std::vector<terrain_builder::tile> new_tiles((x + 4) * (y + 4));
tiles_.swap(new_tiles); tiles_.swap(new_tiles);
reset();
} }
bool terrain_builder::tilemap::on_map(const map_location &loc) const bool terrain_builder::tilemap::on_map(const map_location &loc) const
@ -320,8 +325,10 @@ bool terrain_builder::rule_valid(const building_rule &rule) const
// we already precached file existence in the constructor // we already precached file existence in the constructor
// but only for filenames not using ".." // but only for filenames not using ".."
if(!image::exists("terrain/" + s + ".png", s.find("..") == std::string::npos)) if(!image::exists("terrain/" + s + ".png", s.find("..") == std::string::npos)){
// printf("%s\n",s.c_str());
return false; return false;
}
} }
} }
} }
@ -950,9 +957,16 @@ void terrain_builder::apply_rule(const terrain_builder::building_rule &rule, con
// We want to order the images by layer first and base-y second, // We want to order the images by layer first and base-y second,
// so we sort by layer*BASE_Y_INTERVAL + BASE_Y_INTERVAL/2 + basey // so we sort by layer*BASE_Y_INTERVAL + BASE_Y_INTERVAL/2 + basey
// Thus, allowed values for basey are from -50000 to 49999 // Thus, allowed values for basey are from -50000 to 49999
int rand_seed;
if(tile_map_.on_map(loc)){
rand_seed = tile_map_[loc].rand_seed;
} else {
rand_seed= 0;
}
for(img = constraint->second.images.begin(); img != constraint->second.images.end(); ++img) { for(img = constraint->second.images.begin(); img != constraint->second.images.end(); ++img) {
btile.images.insert(std::pair<int, const rule_image*>( btile.images.insert(std::pair<int,std::pair<int, const rule_image*> >(
img->layer*BASE_Y_INTERVAL + BASE_Y_INTERVAL/2 + img->basey, &*img)); img->layer*BASE_Y_INTERVAL + BASE_Y_INTERVAL/2 + img->basey,
std::pair<int,const rule_image*>(rand_seed,&*img)));
} }
// Sets flags // Sets flags

View File

@ -263,7 +263,7 @@ public:
struct tile struct tile
{ {
/** An ordered rule_image list */ /** An ordered rule_image list */
typedef std::multimap<int, const rule_image*> ordered_ri_list; typedef std::multimap<int,std::pair<int, const rule_image*> > ordered_ri_list;
/** Contructor for the tile() structure */ /** Contructor for the tile() structure */
tile(); tile();
@ -312,6 +312,10 @@ public:
* The time-of-day to which the image caches correspond. * The time-of-day to which the image caches correspond.
*/ */
std::string last_tod; std::string last_tod;
/**
* Unique random number used to start all animations originating from this tile
*/
int rand_seed;
}; };
@ -371,7 +375,7 @@ private:
tiles_((x + 4) * (y + 4)), tiles_((x + 4) * (y + 4)),
x_(x), x_(x),
y_(y) y_(y)
{} {reset();}
/** /**
* Returns a reference to the tile which is at the position * Returns a reference to the tile which is at the position