In process of adding luaconsole.cpp
This commit is contained in:
134
luprex/cpp/animqueue.hpp
Normal file
134
luprex/cpp/animqueue.hpp
Normal file
@@ -0,0 +1,134 @@
|
||||
///////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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>
|
||||
#include <unordered_map>
|
||||
#include "util.hpp"
|
||||
|
||||
|
||||
class AnimStep {
|
||||
friend class AnimQueue;
|
||||
public:
|
||||
enum {
|
||||
HAS_FACING = 1,
|
||||
HAS_XYZ = 2,
|
||||
HAS_GRAPHIC = 4,
|
||||
HAS_PLANE = 8,
|
||||
HAS_EVERYTHING = 15,
|
||||
};
|
||||
|
||||
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:
|
||||
int64_t id_;
|
||||
int size_limit_;
|
||||
std::deque<AnimStep> steps_;
|
||||
public:
|
||||
AnimQueue();
|
||||
int64_t get_id() const { return id_; }
|
||||
void set_id(int64_t id) { id_ = id; }
|
||||
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);
|
||||
|
||||
// Get the final resting place after all animations are complete.
|
||||
const std::string &get_graphic() const;
|
||||
const std::string &get_plane() const;
|
||||
const util::XYZ &get_xyz() const;
|
||||
|
||||
// Functions for unit testing.
|
||||
void set_size_limit(int n);
|
||||
};
|
||||
|
||||
class AnimView {
|
||||
friend class AnimViewMap;
|
||||
private:
|
||||
int64_t id_;
|
||||
std::string graphic_;
|
||||
std::string plane_;
|
||||
util::XYZ xyz_;
|
||||
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);
|
||||
};
|
||||
|
||||
// AnimViewMap: a unordered_map from tangible ID to AnimView. Adds a few
|
||||
// methods for garbage collection and updating.
|
||||
//
|
||||
class AnimViewMap : public std::unordered_map<int64_t, AnimView> {
|
||||
public:
|
||||
AnimView *get_one(int64_t id);
|
||||
void clear_updated_bits();
|
||||
void delete_non_updated();
|
||||
void update_one(const AnimQueue &queue);
|
||||
};
|
||||
|
||||
#endif // ANIMQUEUE_HPP
|
||||
|
||||
Reference in New Issue
Block a user