Files
integration/luprex/syscpp/animqueue.hpp

135 lines
3.8 KiB
C++
Raw Normal View History

2021-01-12 14:14:38 -05:00
///////////////////////////////////////////////////////////////////
//
// ANIMATION QUEUES
//
// An animation queue is a fifo queue of animation steps. New animations are
// pushed on the back, and old ones are popped from the front.
//
// An animation step has an "action" which is usually the name of an animation,
// or it's a special token like "walk" or "warp." Each animation step shows the
// resulting AnimState that the player finds himself in after executing the
// action.
//
// The first step in an animation queue always has id=0 and action="". This step
// represents the initial state of the sprite before any animations or
// movements.
//
// To add new items to the AnimQueue, use this process: first, call add(id,
// action). This adds a new step to the queue. Then, call set_xyz, set_facing,
// set_plane, or an other setter. These setters are meant to only be used
// immediately after calling 'add' to populate the new step.
//
///////////////////////////////////////////////////////////////////
#ifndef ANIMQUEUE_HPP
#define ANIMQUEUE_HPP
#include <set>
#include <string>
#include <deque>
#include <cassert>
2021-01-22 19:10:47 -05:00
#include <unordered_map>
2021-01-12 14:14:38 -05:00
#include "util.hpp"
class AnimStep {
friend class AnimQueue;
public:
enum {
HAS_FACING = 1,
HAS_XYZ = 2,
HAS_GRAPHIC = 4,
HAS_PLANE = 8,
2021-01-16 01:24:33 -05:00
HAS_EVERYTHING = 15,
2021-01-12 14:14:38 -05:00
};
private:
int64_t id_;
std::string action_;
int bits_;
float facing_;
util::XYZ xyz_;
std::string graphic_;
std::string plane_;
public:
AnimStep();
~AnimStep();
int64_t id() const { return id_; }
const std::string &action() const { return action_; }
int bits() const { return bits_; }
double facing() const { return facing_; }
util::XYZ xyz() const { return xyz_; }
const std::string &graphic() const { return graphic_; }
const std::string &plane() const { return plane_; }
bool has_facing() const { return bits_ & AnimStep::HAS_FACING; }
bool has_xyz() const { return bits_ & AnimStep::HAS_XYZ; }
bool has_graphic() const { return bits_ & AnimStep::HAS_GRAPHIC; }
bool has_plane() const { return bits_ & AnimStep::HAS_PLANE; }
};
class AnimQueue {
private:
2021-01-22 19:10:47 -05:00
int64_t id_;
2021-01-12 14:14:38 -05:00
int size_limit_;
std::deque<AnimStep> steps_;
public:
2021-01-17 16:23:10 -05:00
AnimQueue();
2021-01-22 19:10:47 -05:00
int64_t get_id() const { return id_; }
void set_id(int64_t id) { id_ = id; }
2021-01-12 14:14:38 -05:00
const AnimStep &nth(int n) const { return steps_[n]; }
int size() const { return steps_.size(); }
// Mutators to create new steps.
void add(int64_t id, const std::string &action);
void set_facing(float f);
void set_xyz(util::XYZ xyz);
void set_graphic(const std::string &g);
void set_plane(const std::string &p);
2021-01-16 01:24:33 -05:00
2021-01-22 17:35:23 -05:00
// Get the final resting place after all animations are complete.
const std::string &get_graphic() const;
2021-01-16 01:24:33 -05:00
const std::string &get_plane() const;
const util::XYZ &get_xyz() const;
2021-01-17 16:23:10 -05:00
// Functions for unit testing.
void set_size_limit(int n);
2021-01-12 14:14:38 -05:00
};
2021-01-22 17:35:23 -05:00
class AnimView {
2021-01-22 19:10:47 -05:00
friend class AnimViewMap;
2021-01-22 17:35:23 -05:00
private:
2021-01-22 19:10:47 -05:00
int64_t id_;
2021-01-22 17:35:23 -05:00
std::string graphic_;
std::string plane_;
util::XYZ xyz_;
2021-01-22 19:10:47 -05:00
bool updated_; // Used by AnimViewMap.
public:
AnimView() : id_(0), updated_(true) {}
int64_t get_id() const { return id_; }
const std::string &get_graphic() const { return graphic_; }
const std::string &get_plane() const { return plane_; }
const util::XYZ &get_xyz() const { return xyz_; }
void update_from(const AnimQueue &queue);
};
2021-01-23 14:44:06 -05:00
// AnimViewMap: a unordered_map from tangible ID to AnimView. Adds a few
// methods for garbage collection and updating.
2021-01-22 19:10:47 -05:00
//
2021-01-23 14:44:06 -05:00
class AnimViewMap : public std::unordered_map<int64_t, AnimView> {
2021-01-22 17:35:23 -05:00
public:
2021-01-22 19:10:47 -05:00
AnimView *get_one(int64_t id);
void clear_updated_bits();
void delete_non_updated();
void update_one(const AnimQueue &queue);
2021-01-22 17:35:23 -05:00
};
2021-01-12 14:14:38 -05:00
#endif // ANIMQUEUE_HPP