]> git.localhorst.tv Git - l2e.git/blob - src/map/Entity.cpp
extracted tile coordinates calculation
[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 using geometry::Vector;
11
12 namespace map {
13
14 Entity::Entity()
15 : follower(0)
16 , animation(0)
17 , orientation(ORIENTATION_NORTH)
18 , speed(0) {
19         runner.SetFrameShift(1);
20 }
21
22
23 void Entity::SetOrientation(Orientation o) {
24         orientation = o;
25         UpdateVelocity();
26         runner.SetColOffset(orientation);
27 }
28
29 void Entity::SetSpeed(float s) {
30         speed = s;
31         UpdateVelocity();
32 }
33
34 void Entity::StartAnimation(app::Application &ctrl) {
35         runner.Start(ctrl);
36 }
37
38 void Entity::StartAnimation(app::State &ctrl) {
39         runner.Start(ctrl);
40 }
41
42 void Entity::StopAnimation() {
43         runner.Stop();
44 }
45
46 void Entity::AddFollower(Entity *f) {
47         if (follower) {
48                 follower->AddFollower(f);
49         } else {
50                 follower = f;
51         }
52 }
53
54 void Entity::RemoveFollower(Entity *f) {
55         if (follower == f) {
56                 follower = follower->follower;
57         } else if (follower) {
58                 follower->RemoveFollower(f);
59         }
60 }
61
62 void Entity::SetAnimation(const graphics::Animation *a) {
63         animation = a;
64         runner.ChangeAnimation(animation);
65 }
66
67
68 void Entity::UpdateVelocity() {
69         if (speed == 0.0f) {
70                 velocity = Vector<float>();
71                 return;
72         }
73         switch (orientation) {
74                 case ORIENTATION_NORTH:
75                         velocity = Vector<float>(0.0f, -speed);
76                         break;
77                 case ORIENTATION_EAST:
78                         velocity = Vector<float>(speed, 0.0f);
79                         break;
80                 case ORIENTATION_SOUTH:
81                         velocity = Vector<float>(0.0f, speed);
82                         break;
83                 case ORIENTATION_WEST:
84                         velocity = Vector<float>(-speed, 0.0f);
85                         break;
86         }
87 }
88
89
90 bool Entity::TileLock(int width, int height) const {
91         Vector<int> tilePosition(
92                         position.X() - (width / 2),
93                         position.Y());
94         return (tilePosition.X() % width == 0) && (tilePosition.Y() % height == 0);
95 }
96
97
98 void Entity::Update(float deltaT) {
99         position += velocity * deltaT;
100 }
101
102
103 void Entity::Render(SDL_Surface *dest, const Vector<int> &offset) const {
104         // TODO: configurable sprite offsets
105         if (runner.Running()) {
106                 runner.DrawCenter(dest, offset + position);
107         } else {
108                 animation->GetSprite()->DrawCenter(dest, offset + position, orientation);
109         }
110 }
111
112 }