]> git.localhorst.tv Git - l2e.git/blobdiff - src/map/Entity.cpp
switched geometric scalars from floating to fixed
[l2e.git] / src / map / Entity.cpp
index 01cbe7f619e8b37aaf20ee051c72e038837a27a7..93530f142735021f063f8dab333f66d694680f51 100644 (file)
-/*
- * Entity.cpp
- *
- *  Created on: Sep 29, 2012
- *      Author: holy
- */
-
 #include "Entity.h"
 
+#include "../battle/Monster.h"
+#include "../battle/PartyLayout.h"
+#include "../graphics/Animation.h"
+#include "../graphics/Sprite.h"
+#include "../loader/Interpreter.h"
+#include "../loader/TypeDescription.h"
+
+using battle::Monster;
+using battle::PartyLayout;
+using graphics::Animation;
+using graphics::Sprite;
+using math::Fixed;
+using math::Vector;
+using loader::FieldDescription;
+using loader::Interpreter;
+using loader::TypeDescription;
+
 namespace map {
 
 Entity::Entity()
-: sprite(0) {
+: follower(0)
+, animation(0)
+, sprite(0)
+, partyLayout(0)
+, monsters(0)
+, numMonsters(0)
+, orientation(ORIENTATION_NORTH)
+, speed(0)
+, flags(0) {
+       runner.SetFrameShift(1);
+}
+
+
+void Entity::SetOrientation(Orientation o) {
+       orientation = o;
+       UpdateVelocity();
+       if (CanTurn()) {
+               runner.SetColOffset(orientation);
+       }
+}
+
+void Entity::SetSpeed(Fixed<8> s) {
+       speed = s;
+       UpdateVelocity();
+}
+
+
+void Entity::StartAnimation(app::Application &ctrl) {
+       runner.Start(ctrl);
+}
 
+void Entity::StartAnimation(app::State &ctrl) {
+       runner.Start(ctrl);
 }
 
+void Entity::StopAnimation() {
+       runner.Stop();
+}
+
+
+void Entity::SetHandsFree() {
+       runner.SetRowOffset(0);
+}
+
+void Entity::SetCarrying() {
+       runner.SetRowOffset(2);
+}
+
+void Entity::SetPushing() {
+       runner.SetRowOffset(4);
+}
+
+
+void Entity::AddFollower(Entity *f) {
+       if (follower) {
+               follower->AddFollower(f);
+       } else {
+               follower = f;
+       }
+}
 
-void Entity::Update(float deltaT) {
+void Entity::RemoveFollower(Entity *f) {
+       if (follower == f) {
+               follower = follower->follower;
+       } else if (follower) {
+               follower->RemoveFollower(f);
+       }
+}
+
+void Entity::SetAnimation(const graphics::Animation *a) {
+       animation = a;
+       runner.ChangeAnimation(animation);
+       if (!sprite) {
+               sprite = animation->GetSprite();
+       }
+}
+
+
+void Entity::UpdateVelocity() {
+       if (speed == 0) {
+               velocity = Vector<Fixed<8> >();
+               return;
+       }
+       switch (orientation) {
+               case ORIENTATION_NORTH:
+                       velocity = Vector<Fixed<8> >(0, -speed);
+                       break;
+               case ORIENTATION_EAST:
+                       velocity = Vector<Fixed<8> >(speed, 0);
+                       break;
+               case ORIENTATION_SOUTH:
+                       velocity = Vector<Fixed<8> >(0, speed);
+                       break;
+               case ORIENTATION_WEST:
+                       velocity = Vector<Fixed<8> >(-speed, 0);
+                       break;
+       }
+}
+
+
+bool Entity::TileLock(const math::Vector<int> &tileSize) const {
+       // TODO: change position to point to the top-left corner of a tile
+       Vector<int> tilePosition(ToInt(position));
+       return (tilePosition.X() % tileSize.X() == 0) && (tilePosition.Y() % tileSize.Y() == 0);
+}
+
+
+void Entity::Update(Uint32 deltaT) {
        position += velocity * deltaT;
 }
 
 
-void Entity::Render(SDL_Surface *dest, const geometry::Vector<int> &offset) const {
-       if (animation.Running()) {
-               animation.DrawCenterBottom(dest, offset + position);
+void Entity::Render(SDL_Surface *dest, const Vector<int> &offset) const {
+       // TODO: configurable sprite offsets
+       if (runner.Running()) {
+               runner.Draw(dest, offset + ToInt(position) + spriteOffset);
        } else {
-               sprite->DrawCenterBottom(dest, offset + position);
+               sprite->Draw(dest, offset + ToInt(position) + spriteOffset, CanTurn() ? orientation : 0);
+       }
+}
+
+
+void Entity::CreateTypeDescription() {
+       Entity e;
+
+       TypeDescription &td(TypeDescription::Create(TYPE_ID, "Entity"));
+       td.SetConstructor(&Construct);
+       td.SetLoader(&Load);
+       td.SetSize(sizeof(Entity));
+
+       td.AddField("animation", FieldDescription(((char *)&e.animation) - ((char *)&e), Animation::TYPE_ID).SetReferenced());
+       td.AddField("sprite", FieldDescription(((char *)&e.sprite) - ((char *)&e), Sprite::TYPE_ID).SetReferenced());
+       td.AddField("partyLayout", FieldDescription(((char *)&e.partyLayout) - ((char *)&e), PartyLayout::TYPE_ID).SetReferenced());
+       td.AddField("monsters", FieldDescription(((char *)&e.monsters) - ((char *)&e), Monster::TYPE_ID).SetReferenced().SetAggregate());
+       td.AddField("spriteOffset", FieldDescription(((char *)&e.spriteOffset) - ((char *)&e), Interpreter::VECTOR_ID));
+       td.AddField("position", FieldDescription(((char *)&e.tilePosition) - ((char *)&e), Interpreter::VECTOR_ID));
+       td.AddField("flags", FieldDescription(((char *)&e.flags) - ((char *)&e), Interpreter::NUMBER_ID));
+}
+
+void Entity::Construct(void *data) {
+       new (data) Entity;
+}
+
+void Entity::Load(void *data) {
+       Entity *entity(reinterpret_cast<Entity *>(data));
+       if (entity->animation) {
+               entity->runner.ChangeAnimation(entity->animation);
+               if (!entity->sprite) {
+                       entity->sprite = entity->animation->GetSprite();
+               }
        }
 }