]> git.localhorst.tv Git - l2e.git/blob - Entity.cpp
6bce2adfde82b28d092cd08a03eae7fd39246f7e
[l2e.git] / Entity.cpp
1 #include "Entity.h"
2
3 #include "../battle/Monster.h"
4 #include "../battle/PartyLayout.h"
5 #include "../graphics/Animation.h"
6 #include "../graphics/Sprite.h"
7 #include "../loader/Interpreter.h"
8 #include "../loader/TypeDescription.h"
9
10 using battle::Monster;
11 using battle::PartyLayout;
12 using graphics::Animation;
13 using graphics::Sprite;
14 using math::Fixed;
15 using math::Vector;
16 using loader::FieldDescription;
17 using loader::Interpreter;
18 using loader::TypeDescription;
19
20 namespace map {
21
22 Entity::Entity()
23 : follower(0)
24 , animation(0)
25 , sprite(0)
26 , partyLayout(0)
27 , monsters(0)
28 , numMonsters(0)
29 , direction(ORIENTATION_NORTH)
30 , orientation(ORIENTATION_NORTH)
31 , speed(0)
32 , flags(0) {
33         runner.SetFrameShift(1);
34 }
35
36
37 void Entity::SetOrientation(Orientation o) {
38         orientation = o;
39         if (CanTurn()) {
40                 runner.SetColOffset(o);
41         }
42 }
43
44 void Entity::SetDirection(Orientation o) {
45         direction = o;
46         UpdateVelocity();
47         SetOrientation(o);
48 }
49
50 void Entity::SetSpeed(Fixed<8> s) {
51         speed = s;
52         UpdateVelocity();
53 }
54
55
56 void Entity::StartAnimation(app::Application &ctrl) {
57         runner.Start(ctrl);
58 }
59
60 void Entity::StartAnimation(app::State &ctrl) {
61         runner.Start(ctrl);
62 }
63
64 void Entity::StopAnimation() {
65         runner.Stop();
66 }
67
68
69 void Entity::SetHandsFree() {
70         runner.SetRowOffset(0);
71 }
72
73 void Entity::SetCarrying() {
74         runner.SetRowOffset(2);
75 }
76
77 void Entity::SetPushing() {
78         runner.SetRowOffset(4);
79 }
80
81
82 void Entity::AddFollower(Entity *f) {
83         if (follower) {
84                 follower->AddFollower(f);
85         } else {
86                 follower = f;
87         }
88 }
89
90 void Entity::RemoveFollower(Entity *f) {
91         if (follower == f) {
92                 follower = follower->follower;
93         } else if (follower) {
94                 follower->RemoveFollower(f);
95         }
96 }
97
98 void Entity::SetAnimation(const graphics::Animation *a) {
99         animation = a;
100         runner.ChangeAnimation(animation);
101         if (!sprite) {
102                 sprite = animation->GetSprite();
103         }
104 }
105
106
107 void Entity::UpdateVelocity() {
108         if (speed == 0) {
109                 velocity = Vector<Fixed<8> >();
110                 return;
111         }
112         switch (direction) {
113                 case ORIENTATION_NORTH:
114                         velocity = Vector<Fixed<8> >(0, -speed);
115                         break;
116                 case ORIENTATION_EAST:
117                         velocity = Vector<Fixed<8> >(speed, 0);
118                         break;
119                 case ORIENTATION_SOUTH:
120                         velocity = Vector<Fixed<8> >(0, speed);
121                         break;
122                 case ORIENTATION_WEST:
123                         velocity = Vector<Fixed<8> >(-speed, 0);
124                         break;
125         }
126 }
127
128
129 bool Entity::TileLock(const math::Vector<int> &tileSize) const {
130         Vector<int> tilePosition(ToInt(position));
131         return tilePosition % tileSize == Vector<int>();
132 }
133
134
135 void Entity::Update(Uint32 deltaT) {
136         position += velocity * deltaT;
137 }
138
139
140 void Entity::Render(SDL_Surface *dest, const Vector<int> &offset) const {
141         if (runner.Running()) {
142                 runner.Draw(dest, offset + ToInt(position) + spriteOffset);
143         } else {
144                 sprite->Draw(dest, offset + ToInt(position) + spriteOffset, CanTurn() ? orientation : 0);
145         }
146 }
147
148
149 void Entity::CreateTypeDescription() {
150         Entity e;
151
152         TypeDescription &td(TypeDescription::Create(TYPE_ID, "Entity"));
153         td.SetConstructor(&Construct);
154         td.SetLoader(&Load);
155         td.SetSize(sizeof(Entity));
156
157         td.AddField("animation", FieldDescription(((char *)&e.animation) - ((char *)&e), Animation::TYPE_ID).SetReferenced());
158         td.AddField("sprite", FieldDescription(((char *)&e.sprite) - ((char *)&e), Sprite::TYPE_ID).SetReferenced());
159         td.AddField("partyLayout", FieldDescription(((char *)&e.partyLayout) - ((char *)&e), PartyLayout::TYPE_ID).SetReferenced());
160         td.AddField("monsters", FieldDescription(((char *)&e.monsters) - ((char *)&e), Monster::TYPE_ID).SetReferenced().SetAggregate());
161         td.AddField("spriteOffset", FieldDescription(((char *)&e.spriteOffset) - ((char *)&e), Interpreter::VECTOR_ID));
162         td.AddField("position", FieldDescription(((char *)&e.tilePosition) - ((char *)&e), Interpreter::VECTOR_ID));
163         td.AddField("flags", FieldDescription(((char *)&e.flags) - ((char *)&e), Interpreter::NUMBER_ID));
164 }
165
166 void Entity::Construct(void *data) {
167         new (data) Entity;
168 }
169
170 void Entity::Load(void *data) {
171         Entity *entity(reinterpret_cast<Entity *>(data));
172         if (entity->animation) {
173                 entity->runner.ChangeAnimation(entity->animation);
174                 if (!entity->sprite) {
175                         entity->sprite = entity->animation->GetSprite();
176                 }
177         }
178 }
179
180 }