]> git.localhorst.tv Git - l2e.git/blob - src/map/Entity.cpp
store complete entity in hero
[l2e.git] / src / map / Entity.cpp
1 /*
2  * Entity.cpp
3  *
4  *  Created on: Sep 29, 2012
5  *      Author: holy
6  */
7
8 #include "Entity.h"
9
10 #include "../loader/TypeDescription.h"
11
12 using geometry::Vector;
13 using loader::FieldDescription;
14 using loader::TypeDescription;
15
16 namespace map {
17
18 Entity::Entity()
19 : follower(0)
20 , animation(0)
21 , orientation(ORIENTATION_NORTH)
22 , speed(0)
23 , flags(0) {
24         runner.SetFrameShift(1);
25 }
26
27
28 void Entity::SetOrientation(Orientation o) {
29         orientation = o;
30         UpdateVelocity();
31         runner.SetColOffset(orientation);
32 }
33
34 void Entity::SetSpeed(float s) {
35         speed = s;
36         UpdateVelocity();
37 }
38
39 void Entity::StartAnimation(app::Application &ctrl) {
40         runner.Start(ctrl);
41 }
42
43 void Entity::StartAnimation(app::State &ctrl) {
44         runner.Start(ctrl);
45 }
46
47 void Entity::StopAnimation() {
48         runner.Stop();
49 }
50
51 void Entity::AddFollower(Entity *f) {
52         if (follower) {
53                 follower->AddFollower(f);
54         } else {
55                 follower = f;
56         }
57 }
58
59 void Entity::RemoveFollower(Entity *f) {
60         if (follower == f) {
61                 follower = follower->follower;
62         } else if (follower) {
63                 follower->RemoveFollower(f);
64         }
65 }
66
67 void Entity::SetAnimation(const graphics::Animation *a) {
68         animation = a;
69         runner.ChangeAnimation(animation);
70 }
71
72
73 void Entity::UpdateVelocity() {
74         if (speed == 0.0f) {
75                 velocity = Vector<float>();
76                 return;
77         }
78         switch (orientation) {
79                 case ORIENTATION_NORTH:
80                         velocity = Vector<float>(0.0f, -speed);
81                         break;
82                 case ORIENTATION_EAST:
83                         velocity = Vector<float>(speed, 0.0f);
84                         break;
85                 case ORIENTATION_SOUTH:
86                         velocity = Vector<float>(0.0f, speed);
87                         break;
88                 case ORIENTATION_WEST:
89                         velocity = Vector<float>(-speed, 0.0f);
90                         break;
91         }
92 }
93
94
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);
99 }
100
101
102 void Entity::Update(float deltaT) {
103         position += velocity * deltaT;
104 }
105
106
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);
111         } else {
112                 animation->GetSprite()->Draw(dest, offset + position + spriteOffset, orientation);
113         }
114 }
115
116
117 void Entity::CreateTypeDescription() {
118         Entity e;
119
120         int animationId(TypeDescription::GetTypeId("Animation"));
121         int vectorId(TypeDescription::GetTypeId("Vector"));
122
123         TypeDescription &td(TypeDescription::CreateOrGet("Entity"));
124         td.SetConstructor(&Construct);
125         td.SetLoader(&Load);
126         td.SetSize(sizeof(Entity));
127
128         td.AddField("animation", FieldDescription(((char *)&e.animation) - ((char *)&e), animationId, true));
129         td.AddField("spriteOffset", FieldDescription(((char *)&e.spriteOffset) - ((char *)&e), vectorId, false));
130 }
131
132 void Entity::Construct(void *data) {
133         new (data) Entity;
134 }
135
136 void Entity::Load(void *data) {
137         Entity *entity(reinterpret_cast<Entity *>(data));
138         entity->runner.ChangeAnimation(entity->animation);
139 }
140
141 }