]> git.localhorst.tv Git - l2e.git/blob - src/map/Entity.cpp
added comparison operators to vector template
[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 : sprite(0)
16 , orientation(ORIENTATION_NORTH)
17 , speed(0) {
18
19 }
20
21
22 void Entity::SetOrientation(Orientation o) {
23         orientation = o;
24         UpdateVelocity();
25         animation.SetColOffset(orientation);
26 }
27
28 void Entity::SetSpeed(float s) {
29         speed = s;
30         UpdateVelocity();
31 }
32
33 void Entity::UpdateVelocity() {
34         if (speed == 0.0f) {
35                 velocity = Vector<float>();
36                 return;
37         }
38         switch (orientation) {
39                 case ORIENTATION_NORTH:
40                         velocity = Vector<float>(0.0f, -speed);
41                         break;
42                 case ORIENTATION_EAST:
43                         velocity = Vector<float>(speed, 0.0f);
44                         break;
45                 case ORIENTATION_SOUTH:
46                         velocity = Vector<float>(0.0f, speed);
47                         break;
48                 case ORIENTATION_WEST:
49                         velocity = Vector<float>(-speed, 0.0f);
50                         break;
51         }
52 }
53
54
55 bool Entity::TileLock(int width, int height) const {
56         Vector<int> tilePosition(
57                         position.X() - (width / 2),
58                         position.Y());
59         return (tilePosition.X() % width == 0) && (tilePosition.Y() % height == 0);
60 }
61
62
63 void Entity::Update(float deltaT) {
64         position += velocity * deltaT;
65 }
66
67
68 void Entity::Render(SDL_Surface *dest, const Vector<int> &offset) const {
69         // TODO: configurable sprite offsets
70         if (animation.Running()) {
71                 animation.DrawCenter(dest, offset + position);
72         } else {
73                 sprite->DrawCenter(dest, offset + position, orientation);
74         }
75 }
76
77 }