]> git.localhorst.tv Git - l2e.git/blob - src/map/Entity.cpp
implemented followers
[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(const geometry::Vector<int> &tileSize) const {
91         // TODO: change position to point to the top-left corner of a tile
92         Vector<int> tilePosition(position);
93         return (tilePosition.X() % tileSize.X() == 0) && (tilePosition.Y() % tileSize.Y() == 0);
94 }
95
96
97 void Entity::Update(float deltaT) {
98         position += velocity * deltaT;
99 }
100
101
102 void Entity::Render(SDL_Surface *dest, const Vector<int> &offset) const {
103         // TODO: configurable sprite offsets
104         if (runner.Running()) {
105                 runner.Draw(dest, offset + position + spriteOffset);
106         } else {
107                 animation->GetSprite()->Draw(dest, offset + position + spriteOffset, orientation);
108         }
109 }
110
111 }