X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fmap%2FEntity.h;h=13044880121569cfa54c1ef4f7ce34dbc6a64457;hb=4309d259becd96ead792678257e910c03a6b4a3d;hp=d6ba5f3a3763dc2acd3aefd4697fde9d33bef8c5;hpb=657eed00ae73b8d06470cec0d955aeada537a90d;p=l2e.git diff --git a/src/map/Entity.h b/src/map/Entity.h index d6ba5f3..1304488 100644 --- a/src/map/Entity.h +++ b/src/map/Entity.h @@ -1,15 +1,17 @@ -/* - * Entity.h - * - * Created on: Sep 29, 2012 - * Author: holy - */ - #ifndef MAP_ENTITY_H_ #define MAP_ENTITY_H_ -#include "../geometry/Vector.h" -#include "../graphics/fwd.h" +namespace app { + class Application; + class State; +} +namespace battle { + class Monster; + class PartyLayout; +} + +#include "../math/Fixed.h" +#include "../math/Vector.h" #include "../graphics/Animation.h" #include @@ -17,37 +19,150 @@ namespace map { +/// An entity that can be placed on a map, moved around, animated, and possibly +/// interact with the player. class Entity { +public: + static const int TYPE_ID = 605; + public: Entity(); ~Entity() { } public: - geometry::Vector &Position() { return position; } - const geometry::Vector &Position() const { return position; } - - geometry::Vector &Velocity() { return velocity; } - const geometry::Vector &Velocity() const { return velocity; } + enum Orientation { + ORIENTATION_NORTH = 0, + ORIENTATION_EAST = 1, + ORIENTATION_SOUTH = 2, + ORIENTATION_WEST = 3, + }; + enum Flags { + FLAG_NONBLOCKING = 0x01, + FLAG_PUSHABLE = 0x02, + FLAG_FIXED_ORIENTATION = 0x04, + }; +public: + /// Pixel resolved position of the entity's top left corner on the map. + math::Vector > &Position() { return position; } + const math::Vector > &Position() const { return position; } + + /// Velocity of the entity in pixels per second. + math::Vector > &Velocity() { return velocity; } + const math::Vector > &Velocity() const { return velocity; } + + /// Offset of the entity's sprite's to left corner relative to Position(). + math::Vector &SpriteOffset() { return spriteOffset; } + const math::Vector &SpriteOffset() const { return spriteOffset; } + + /// Reset the entity to the stored tile coordinates (usually set when + /// loading game data). + void ResetPosition(const math::Vector &tileSize) { position = tilePosition * tileSize; } + + /// Set the animation to use for animated entities. + /// For orientable entities, the animation should have north, south, east, + /// and west sprites at offsets (0,0), (1,0), (2,0), and (3,0) respectively. + /// If the entity can carry, row offset 2 is used. + /// If the entity can push, row offset 4 is used. + void SetAnimation(const graphics::Animation *a); + /// Start the animation on a global timer. + void StartAnimation(app::Application &ctrl); + /// Start the animation on a state timer. + void StartAnimation(app::State &ctrl); + /// Stop the animation. + void StopAnimation(); + /// Check if an animation is running. + bool AnimationRunning() const { return runner.Running(); } + + /// Set the sprite used for the non-animated state. + /// For orientable entities, the sprite should have north, south, east, and + /// west sprites at offsets (0,0), (1,0), (2,0), and (3,0) respectively. void SetSprite(const graphics::Sprite *s) { sprite = s; } - graphics::AnimationRunner &Animation() { return animation; } - const graphics::AnimationRunner &Animation() const { return animation; } - bool TileLock(int width, int height) const; + /// Change the entity's orientation to given one. + /// If the entity is moving, velocity is changed accordingly. + void SetOrientation(Orientation); + Orientation GetOrientation() const { return orientation; } + /// Set the entity's speed in pixels per second. + /// This speed is then combined with the orientation to form a velocity. + void SetSpeed(math::Fixed<8>); + + /// Change to a natural, relaxed animation state (row offset 0). + void SetHandsFree(); + /// Change animation to represent a carrying thingamabob (row offset 2). + void SetCarrying(); + /// Set a pushy animation state (row offset 4). + void SetPushing(); + + /// Set some basic boolean properties. + /// Parameter should be a combination from the Flags enum. + void SetFlags(int f) { flags = f; } + /// Check if the entity is blocking other entities from occupying its tile. + bool Blocking() const { return !(flags & FLAG_NONBLOCKING); } + /// Check if a battle should be launched when stepping onto a neighboring + /// tile. + bool Hostile() const { return partyLayout && numMonsters > 0; } + /// Check if this entity can be pushed around. + bool Pushable() const { return flags & FLAG_PUSHABLE; } + /// Check if the entity's orientation has any effect on the column rendered + /// from the animation or sprite. + bool CanTurn() const { return !(flags & FLAG_FIXED_ORIENTATION); } + + /// Set a layout in battle for the party described by SetMonsters(). + void SetPartyLayout(battle::PartyLayout *l) { partyLayout = l; } + /// Get the layout in battle for the party described by + /// Monsters{Begin,End}(). + battle::PartyLayout *PartyLayout() { return partyLayout; } + + /// Add monsters. This will cause the entity to be Hostile() and result in a + /// battle scene with given monsters when touched. + void SetMonsters(battle::Monster **m, int num) { monsters = m; numMonsters = num; } + battle::Monster **MonstersBegin() { return monsters; } + battle::Monster **MonstersEnd() { return monsters + numMonsters; } + + /// Get an entity that should follow in this one's steps or 0 if none. + Entity *Follower() { return follower; } + const Entity *Follower() const { return follower; } + /// Add an entity that follows this one. + /// If this already has a follower, it is added to that one instead. + void AddFollower(Entity *); + /// Remove given entity from this entity or its follower. + void RemoveFollower(Entity *); + + /// Check if position locks into grid defined by given tileSize. + bool TileLock(const math::Vector &tileSize) const; + + /// Integrate this entity's physical properties over given time interval. + void Update(Uint32 deltaT); + + void Render(SDL_Surface *, const math::Vector &offset) const; + + static void CreateTypeDescription(); + static void Construct(void *); + static void Load(void *); - void Update(float deltaT); - - void Render(SDL_Surface *, const geometry::Vector &offset) const; +private: + void UpdateVelocity(); private: + Entity *follower; + const graphics::Animation *animation; const graphics::Sprite *sprite; - graphics::AnimationRunner animation; - geometry::Vector position; - geometry::Vector velocity; + battle::PartyLayout *partyLayout; + battle::Monster **monsters; + int numMonsters; + graphics::AnimationRunner runner; + math::Vector spriteOffset; + math::Vector tilePosition; + math::Vector > position; + math::Vector > velocity; + Orientation orientation; + math::Fixed<8> speed; + int flags; }; } -#endif /* MAP_ENTITY_H_ */ +#endif