4 * Created on: Sep 29, 2012
10 #include "../loader/TypeDescription.h"
12 using geometry::Vector;
13 using loader::FieldDescription;
14 using loader::TypeDescription;
25 , orientation(ORIENTATION_NORTH)
28 runner.SetFrameShift(1);
32 void Entity::SetOrientation(Orientation o) {
36 runner.SetColOffset(orientation);
40 void Entity::SetSpeed(float s) {
46 void Entity::StartAnimation(app::Application &ctrl) {
50 void Entity::StartAnimation(app::State &ctrl) {
54 void Entity::StopAnimation() {
59 void Entity::SetHandsFree() {
60 runner.SetRowOffset(0);
63 void Entity::SetCarrying() {
64 runner.SetRowOffset(2);
67 void Entity::SetPushing() {
68 runner.SetRowOffset(4);
72 void Entity::AddFollower(Entity *f) {
74 follower->AddFollower(f);
80 void Entity::RemoveFollower(Entity *f) {
82 follower = follower->follower;
83 } else if (follower) {
84 follower->RemoveFollower(f);
88 void Entity::SetAnimation(const graphics::Animation *a) {
90 runner.ChangeAnimation(animation);
92 sprite = animation->GetSprite();
97 void Entity::UpdateVelocity() {
99 velocity = Vector<float>();
102 switch (orientation) {
103 case ORIENTATION_NORTH:
104 velocity = Vector<float>(0.0f, -speed);
106 case ORIENTATION_EAST:
107 velocity = Vector<float>(speed, 0.0f);
109 case ORIENTATION_SOUTH:
110 velocity = Vector<float>(0.0f, speed);
112 case ORIENTATION_WEST:
113 velocity = Vector<float>(-speed, 0.0f);
119 bool Entity::TileLock(const geometry::Vector<int> &tileSize) const {
120 // TODO: change position to point to the top-left corner of a tile
121 Vector<int> tilePosition(position);
122 return (tilePosition.X() % tileSize.X() == 0) && (tilePosition.Y() % tileSize.Y() == 0);
126 void Entity::Update(float deltaT) {
127 position += velocity * deltaT;
131 void Entity::Render(SDL_Surface *dest, const Vector<int> &offset) const {
132 // TODO: configurable sprite offsets
133 if (runner.Running()) {
134 runner.Draw(dest, offset + position + spriteOffset);
136 sprite->Draw(dest, offset + position + spriteOffset, CanTurn() ? orientation : 0);
141 void Entity::CreateTypeDescription() {
144 int animationId(TypeDescription::GetTypeId("Animation"));
145 int monsterId(TypeDescription::GetTypeId("Monster"));
146 int numberId(TypeDescription::GetTypeId("Number"));
147 int partyLayoutId(TypeDescription::GetTypeId("PartyLayout"));
148 int spriteId(TypeDescription::GetTypeId("Sprite"));
149 int vectorId(TypeDescription::GetTypeId("Vector"));
151 TypeDescription &td(TypeDescription::CreateOrGet("Entity"));
152 td.SetConstructor(&Construct);
154 td.SetSize(sizeof(Entity));
156 td.AddField("animation", FieldDescription(((char *)&e.animation) - ((char *)&e), animationId).SetReferenced());
157 td.AddField("sprite", FieldDescription(((char *)&e.sprite) - ((char *)&e), spriteId).SetReferenced());
158 td.AddField("partyLayout", FieldDescription(((char *)&e.partyLayout) - ((char *)&e), partyLayoutId).SetReferenced());
159 td.AddField("monsters", FieldDescription(((char *)&e.monsters) - ((char *)&e), monsterId).SetReferenced().SetAggregate());
160 td.AddField("spriteOffset", FieldDescription(((char *)&e.spriteOffset) - ((char *)&e), vectorId));
161 td.AddField("position", FieldDescription(((char *)&e.tilePosition) - ((char *)&e), vectorId));
162 td.AddField("flags", FieldDescription(((char *)&e.flags) - ((char *)&e), numberId));
165 void Entity::Construct(void *data) {
169 void Entity::Load(void *data) {
170 Entity *entity(reinterpret_cast<Entity *>(data));
171 if (entity->animation) {
172 entity->runner.ChangeAnimation(entity->animation);
173 if (!entity->sprite) {
174 entity->sprite = entity->animation->GetSprite();