4 * Created on: Sep 29, 2012
10 #include "../battle/Monster.h"
11 #include "../battle/PartyLayout.h"
12 #include "../graphics/Animation.h"
13 #include "../graphics/Sprite.h"
14 #include "../loader/Interpreter.h"
15 #include "../loader/TypeDescription.h"
17 using battle::Monster;
18 using battle::PartyLayout;
19 using graphics::Animation;
20 using graphics::Sprite;
21 using geometry::Vector;
22 using loader::FieldDescription;
23 using loader::Interpreter;
24 using loader::TypeDescription;
35 , orientation(ORIENTATION_NORTH)
38 runner.SetFrameShift(1);
42 void Entity::SetOrientation(Orientation o) {
46 runner.SetColOffset(orientation);
50 void Entity::SetSpeed(float s) {
56 void Entity::StartAnimation(app::Application &ctrl) {
60 void Entity::StartAnimation(app::State &ctrl) {
64 void Entity::StopAnimation() {
69 void Entity::SetHandsFree() {
70 runner.SetRowOffset(0);
73 void Entity::SetCarrying() {
74 runner.SetRowOffset(2);
77 void Entity::SetPushing() {
78 runner.SetRowOffset(4);
82 void Entity::AddFollower(Entity *f) {
84 follower->AddFollower(f);
90 void Entity::RemoveFollower(Entity *f) {
92 follower = follower->follower;
93 } else if (follower) {
94 follower->RemoveFollower(f);
98 void Entity::SetAnimation(const graphics::Animation *a) {
100 runner.ChangeAnimation(animation);
102 sprite = animation->GetSprite();
107 void Entity::UpdateVelocity() {
109 velocity = Vector<float>();
112 switch (orientation) {
113 case ORIENTATION_NORTH:
114 velocity = Vector<float>(0.0f, -speed);
116 case ORIENTATION_EAST:
117 velocity = Vector<float>(speed, 0.0f);
119 case ORIENTATION_SOUTH:
120 velocity = Vector<float>(0.0f, speed);
122 case ORIENTATION_WEST:
123 velocity = Vector<float>(-speed, 0.0f);
129 bool Entity::TileLock(const geometry::Vector<int> &tileSize) const {
130 // TODO: change position to point to the top-left corner of a tile
131 Vector<int> tilePosition(position);
132 return (tilePosition.X() % tileSize.X() == 0) && (tilePosition.Y() % tileSize.Y() == 0);
136 void Entity::Update(float deltaT) {
137 position += velocity * deltaT;
141 void Entity::Render(SDL_Surface *dest, const Vector<int> &offset) const {
142 // TODO: configurable sprite offsets
143 if (runner.Running()) {
144 runner.Draw(dest, offset + position + spriteOffset);
146 sprite->Draw(dest, offset + position + spriteOffset, CanTurn() ? orientation : 0);
151 void Entity::CreateTypeDescription() {
154 TypeDescription &td(TypeDescription::Create(TYPE_ID, "Entity"));
155 td.SetConstructor(&Construct);
157 td.SetSize(sizeof(Entity));
159 td.AddField("animation", FieldDescription(((char *)&e.animation) - ((char *)&e), Animation::TYPE_ID).SetReferenced());
160 td.AddField("sprite", FieldDescription(((char *)&e.sprite) - ((char *)&e), Sprite::TYPE_ID).SetReferenced());
161 td.AddField("partyLayout", FieldDescription(((char *)&e.partyLayout) - ((char *)&e), PartyLayout::TYPE_ID).SetReferenced());
162 td.AddField("monsters", FieldDescription(((char *)&e.monsters) - ((char *)&e), Monster::TYPE_ID).SetReferenced().SetAggregate());
163 td.AddField("spriteOffset", FieldDescription(((char *)&e.spriteOffset) - ((char *)&e), Interpreter::VECTOR_ID));
164 td.AddField("position", FieldDescription(((char *)&e.tilePosition) - ((char *)&e), Interpreter::VECTOR_ID));
165 td.AddField("flags", FieldDescription(((char *)&e.flags) - ((char *)&e), Interpreter::NUMBER_ID));
168 void Entity::Construct(void *data) {
172 void Entity::Load(void *data) {
173 Entity *entity(reinterpret_cast<Entity *>(data));
174 if (entity->animation) {
175 entity->runner.ChangeAnimation(entity->animation);
176 if (!entity->sprite) {
177 entity->sprite = entity->animation->GetSprite();