]> git.localhorst.tv Git - sdl-test7.git/blob - src/game/Entity.cpp
imported current version
[sdl-test7.git] / src / game / Entity.cpp
1 /*
2  * Entity.cpp
3  *
4  *  Created on: Apr 9, 2012
5  *      Author: holy
6  */
7
8 #include "Entity.h"
9
10 #include <limits>
11
12 #include <iostream>
13 using namespace std;
14
15 using geometry::Shape;
16 using std::numeric_limits;
17
18
19 namespace game {
20
21 Entity::Entity(Shape *shape)
22 : bounds(shape)
23 , position()
24 , velocity()
25 , nextVelocity()
26 , maxVelocitySquared(numeric_limits<Scalar>::infinity())
27 , dynamic(true)
28 , updateVelocity(false) {
29
30 }
31
32 Entity::Entity(const Vector &position, Shape *shape)
33 : bounds(shape)
34 , position(position)
35 , velocity()
36 , nextVelocity()
37 , maxVelocitySquared(numeric_limits<Scalar>::infinity())
38 , dynamic(true)
39 , updateVelocity(false) {
40
41 }
42
43 Entity::~Entity(void) {
44
45 }
46
47
48 void Entity::Move(const Vector &delta) {
49         position += delta;
50         bounds->SetPosition(position);
51 }
52
53 void Entity::SetPosition(const Vector &p) {
54         position = p;
55         bounds->SetPosition(position);
56 }
57
58
59 void Entity::SetVelocity(const Vector &v) {
60         if (updateVelocity) {
61                 nextVelocity = v;
62         } else {
63                 velocity = v;
64         }
65 }
66
67
68 Entity::Vector Entity::Center(void) const {
69         return bounds->Center();
70 }
71
72
73 void Entity::Accelerate(Scalar factor) {
74         if (updateVelocity) {
75                 Vector scaled(nextVelocity * factor);
76                 float speedSquared(scaled.LengthSquared());
77                 if (speedSquared <= maxVelocitySquared) {
78                         nextVelocity = scaled;
79                 }
80         } else {
81                 Vector scaled(velocity * factor);
82                 float speedSquared(scaled.LengthSquared());
83                 if (speedSquared <= maxVelocitySquared) {
84                         velocity = scaled;
85                 }
86         }
87 }
88
89 void Entity::SetMaxVelocity(Scalar max) {
90         maxVelocitySquared = max * max;
91 }
92
93
94 void Entity::Update(float deltaT) {
95         if (velocity.X() == Limits::quiet_NaN() || velocity.X() == -Limits::quiet_NaN()) {
96                 cout << "NaN! " << velocity << endl;
97         }
98         position += velocity * deltaT;
99         bounds->SetPosition(position);
100 }
101
102 void Entity::Revert(float deltaT) {
103         position -= velocity * deltaT;
104         bounds->SetPosition(position);
105 }
106
107 void Entity::Collide(Entity &other, const Vector &normal) {
108         nextVelocity = velocity;
109         nextVelocity.Reflect(normal);
110         updateVelocity = true;
111 }
112
113 void Entity::PostCollide(void) {
114         if (updateVelocity) {
115                 velocity = nextVelocity;
116                 updateVelocity = false;
117         }
118 }
119
120 }