3 #include "../battle/Monster.h"
4 #include "../battle/PartyLayout.h"
5 #include "../graphics/Animation.h"
6 #include "../graphics/Sprite.h"
7 #include "../loader/Interpreter.h"
8 #include "../loader/TypeDescription.h"
10 using battle::Monster;
11 using battle::PartyLayout;
12 using graphics::Animation;
13 using graphics::Sprite;
16 using loader::FieldDescription;
17 using loader::Interpreter;
18 using loader::TypeDescription;
29 , orientation(ORIENTATION_NORTH)
32 runner.SetFrameShift(1);
36 void Entity::SetOrientation(Orientation o) {
40 runner.SetColOffset(orientation);
44 void Entity::SetSpeed(Fixed<8> s) {
50 void Entity::StartAnimation(app::Application &ctrl) {
54 void Entity::StartAnimation(app::State &ctrl) {
58 void Entity::StopAnimation() {
63 void Entity::SetHandsFree() {
64 runner.SetRowOffset(0);
67 void Entity::SetCarrying() {
68 runner.SetRowOffset(2);
71 void Entity::SetPushing() {
72 runner.SetRowOffset(4);
76 void Entity::AddFollower(Entity *f) {
78 follower->AddFollower(f);
84 void Entity::RemoveFollower(Entity *f) {
86 follower = follower->follower;
87 } else if (follower) {
88 follower->RemoveFollower(f);
92 void Entity::SetAnimation(const graphics::Animation *a) {
94 runner.ChangeAnimation(animation);
96 sprite = animation->GetSprite();
101 void Entity::UpdateVelocity() {
103 velocity = Vector<Fixed<8> >();
106 switch (orientation) {
107 case ORIENTATION_NORTH:
108 velocity = Vector<Fixed<8> >(0, -speed);
110 case ORIENTATION_EAST:
111 velocity = Vector<Fixed<8> >(speed, 0);
113 case ORIENTATION_SOUTH:
114 velocity = Vector<Fixed<8> >(0, speed);
116 case ORIENTATION_WEST:
117 velocity = Vector<Fixed<8> >(-speed, 0);
123 bool Entity::TileLock(const math::Vector<int> &tileSize) const {
124 // TODO: change position to point to the top-left corner of a tile
125 Vector<int> tilePosition(ToInt(position));
126 return (tilePosition.X() % tileSize.X() == 0) && (tilePosition.Y() % tileSize.Y() == 0);
130 void Entity::Update(Uint32 deltaT) {
131 position += velocity * deltaT;
135 void Entity::Render(SDL_Surface *dest, const Vector<int> &offset) const {
136 // TODO: configurable sprite offsets
137 if (runner.Running()) {
138 runner.Draw(dest, offset + ToInt(position) + spriteOffset);
140 sprite->Draw(dest, offset + ToInt(position) + spriteOffset, CanTurn() ? orientation : 0);
145 void Entity::CreateTypeDescription() {
148 TypeDescription &td(TypeDescription::Create(TYPE_ID, "Entity"));
149 td.SetConstructor(&Construct);
151 td.SetSize(sizeof(Entity));
153 td.AddField("animation", FieldDescription(((char *)&e.animation) - ((char *)&e), Animation::TYPE_ID).SetReferenced());
154 td.AddField("sprite", FieldDescription(((char *)&e.sprite) - ((char *)&e), Sprite::TYPE_ID).SetReferenced());
155 td.AddField("partyLayout", FieldDescription(((char *)&e.partyLayout) - ((char *)&e), PartyLayout::TYPE_ID).SetReferenced());
156 td.AddField("monsters", FieldDescription(((char *)&e.monsters) - ((char *)&e), Monster::TYPE_ID).SetReferenced().SetAggregate());
157 td.AddField("spriteOffset", FieldDescription(((char *)&e.spriteOffset) - ((char *)&e), Interpreter::VECTOR_ID));
158 td.AddField("position", FieldDescription(((char *)&e.tilePosition) - ((char *)&e), Interpreter::VECTOR_ID));
159 td.AddField("flags", FieldDescription(((char *)&e.flags) - ((char *)&e), Interpreter::NUMBER_ID));
162 void Entity::Construct(void *data) {
166 void Entity::Load(void *data) {
167 Entity *entity(reinterpret_cast<Entity *>(data));
168 if (entity->animation) {
169 entity->runner.ChangeAnimation(entity->animation);
170 if (!entity->sprite) {
171 entity->sprite = entity->animation->GetSprite();