4 * Created on: Sep 29, 2012
10 #include "../loader/TypeDescription.h"
12 using geometry::Vector;
13 using loader::FieldDescription;
14 using loader::TypeDescription;
21 , orientation(ORIENTATION_NORTH)
24 runner.SetFrameShift(1);
28 void Entity::SetOrientation(Orientation o) {
31 runner.SetColOffset(orientation);
34 void Entity::SetSpeed(float s) {
39 void Entity::StartAnimation(app::Application &ctrl) {
43 void Entity::StartAnimation(app::State &ctrl) {
47 void Entity::StopAnimation() {
51 void Entity::AddFollower(Entity *f) {
53 follower->AddFollower(f);
59 void Entity::RemoveFollower(Entity *f) {
61 follower = follower->follower;
62 } else if (follower) {
63 follower->RemoveFollower(f);
67 void Entity::SetAnimation(const graphics::Animation *a) {
69 runner.ChangeAnimation(animation);
73 void Entity::UpdateVelocity() {
75 velocity = Vector<float>();
78 switch (orientation) {
79 case ORIENTATION_NORTH:
80 velocity = Vector<float>(0.0f, -speed);
82 case ORIENTATION_EAST:
83 velocity = Vector<float>(speed, 0.0f);
85 case ORIENTATION_SOUTH:
86 velocity = Vector<float>(0.0f, speed);
88 case ORIENTATION_WEST:
89 velocity = Vector<float>(-speed, 0.0f);
95 bool Entity::TileLock(const geometry::Vector<int> &tileSize) const {
96 // TODO: change position to point to the top-left corner of a tile
97 Vector<int> tilePosition(position);
98 return (tilePosition.X() % tileSize.X() == 0) && (tilePosition.Y() % tileSize.Y() == 0);
102 void Entity::Update(float deltaT) {
103 position += velocity * deltaT;
107 void Entity::Render(SDL_Surface *dest, const Vector<int> &offset) const {
108 // TODO: configurable sprite offsets
109 if (runner.Running()) {
110 runner.Draw(dest, offset + position + spriteOffset);
112 animation->GetSprite()->Draw(dest, offset + position + spriteOffset, orientation);
117 void Entity::CreateTypeDescription() {
120 int animationId(TypeDescription::GetTypeId("Animation"));
121 int vectorId(TypeDescription::GetTypeId("Vector"));
123 TypeDescription &td(TypeDescription::CreateOrGet("Entity"));
124 td.SetConstructor(&Construct);
126 td.SetSize(sizeof(Entity));
128 td.AddField("animation", FieldDescription(((char *)&e.animation) - ((char *)&e), animationId, true));
129 td.AddField("spriteOffset", FieldDescription(((char *)&e.spriteOffset) - ((char *)&e), vectorId, false));
132 void Entity::Construct(void *data) {
136 void Entity::Load(void *data) {
137 Entity *entity(reinterpret_cast<Entity *>(data));
138 entity->runner.ChangeAnimation(entity->animation);