]> git.localhorst.tv Git - l2e.git/blob - src/map/Entity.cpp
added second map to transition to
[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 , flags(0) {
20         runner.SetFrameShift(1);
21 }
22
23
24 void Entity::SetOrientation(Orientation o) {
25         orientation = o;
26         UpdateVelocity();
27         runner.SetColOffset(orientation);
28 }
29
30 void Entity::SetSpeed(float s) {
31         speed = s;
32         UpdateVelocity();
33 }
34
35 void Entity::StartAnimation(app::Application &ctrl) {
36         runner.Start(ctrl);
37 }
38
39 void Entity::StartAnimation(app::State &ctrl) {
40         runner.Start(ctrl);
41 }
42
43 void Entity::StopAnimation() {
44         runner.Stop();
45 }
46
47 void Entity::AddFollower(Entity *f) {
48         if (follower) {
49                 follower->AddFollower(f);
50         } else {
51                 follower = f;
52         }
53 }
54
55 void Entity::RemoveFollower(Entity *f) {
56         if (follower == f) {
57                 follower = follower->follower;
58         } else if (follower) {
59                 follower->RemoveFollower(f);
60         }
61 }
62
63 void Entity::SetAnimation(const graphics::Animation *a) {
64         animation = a;
65         runner.ChangeAnimation(animation);
66 }
67
68
69 void Entity::UpdateVelocity() {
70         if (speed == 0.0f) {
71                 velocity = Vector<float>();
72                 return;
73         }
74         switch (orientation) {
75                 case ORIENTATION_NORTH:
76                         velocity = Vector<float>(0.0f, -speed);
77                         break;
78                 case ORIENTATION_EAST:
79                         velocity = Vector<float>(speed, 0.0f);
80                         break;
81                 case ORIENTATION_SOUTH:
82                         velocity = Vector<float>(0.0f, speed);
83                         break;
84                 case ORIENTATION_WEST:
85                         velocity = Vector<float>(-speed, 0.0f);
86                         break;
87         }
88 }
89
90
91 bool Entity::TileLock(const geometry::Vector<int> &tileSize) const {
92         // TODO: change position to point to the top-left corner of a tile
93         Vector<int> tilePosition(position);
94         return (tilePosition.X() % tileSize.X() == 0) && (tilePosition.Y() % tileSize.Y() == 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.Draw(dest, offset + position + spriteOffset);
107         } else {
108                 animation->GetSprite()->Draw(dest, offset + position + spriteOffset, orientation);
109         }
110 }
111
112 }