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