-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItem.cpp
190 lines (137 loc) · 4.39 KB
/
Item.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "Item.h"
#include "SpriteUtil.h"
#include <map>
#include "MathUtil.h"
namespace dxco {
Item::Item(cocos2d::CCSprite* sprite, std::map<int, Animation*>& animations) {
this->sprite = sprite;
this->state = 0;
this->animations = animations;
this->xInitial = sprite->getPositionX();
this->yInitial = sprite->getPositionY();
this->defaultTexture = sprite->getTexture();
}
void Item::update(float dt) {
std::map<int, Animation*>::const_iterator itr = this->animations.find(this->getState());
if (itr != this->animations.end()) {
Animation* animation = itr->second;
animation->update(this->sprite, dt);
} else {
this->sprite->setTexture(this->defaultTexture);
}
}
void Item::updatePosition(float speed, float dt) {
float offset = speed * dt;
SpriteUtil::move(this->sprite, -offset, 0);
}
cocos2d::CCSprite* Item::getSprite() {
return this->sprite;
}
void Item::restartPosition() {
this->sprite->setPosition(ccp(this->xInitial, this->yInitial));
}
void Item::move(float deltaX, float deltaY) {
SpriteUtil::move(this->sprite, deltaX, deltaY);
}
void Item::moveTo(float x, float y) {
/*this is weird*/
SpriteUtil::moveTo(this->sprite, x, y);
}
cocos2d::CCPoint Item::getLocation() {
return this->sprite->getPosition();
}
int Item::getState() {
return this->state;
}
float Item::getTopPosition() {
return this->getLocation().y + this->getHeight() / 2;
}
float Item::getBottomPosition() {
return this->getLocation().y - this->getHeight() / 2;
}
float Item::getLeftPosition() {
return this->getLocation().x - this->getWidth() / 2;
}
float Item::getRightPosition() {
return this->getLocation().x + this->getWidth() / 2;
}
float Item::getX() {
return this->getLocation().x + (SpriteUtil::getWidth(this->sprite) / 2);
}
float Item::getY() {
return this->getLocation().y - (SpriteUtil::getHeight(this->sprite) / 2);
}
float Item::getHeight() {
return SpriteUtil::getHeight(this->sprite);
}
float Item::getWidth() {
return SpriteUtil::getWidth(this->sprite);
}
void Item::goTo(cocos2d::CCPoint point, float distance) {
float angle = MathUtil::angle(point, this->getLocation());
this->move(-cos(angle) * distance, -sin(angle) * distance);
}
bool Item::collides(Item* item) {
cocos2d::CCRect thisRect = cocos2d::CCRect(this->getBottomPosition(), this->getLeftPosition(),
this->getHeight(), this->getWidth());
cocos2d::CCRect itemRect = cocos2d::CCRect(item->getBottomPosition(), item->getLeftPosition(),
item->getHeight(), item->getWidth());
return thisRect.intersectsRect(itemRect);
}
bool Item::canAdvance(cocos2d::CCPoint target, float distance, std::vector<Item*> &items) {
bool result = true;
cocos2d::CCPoint position = this->getLocation();
float angle = MathUtil::angle(target, position);
float newX = position.x - cos(angle) * distance;
float newY = position.y - sin(angle) * distance;
cocos2d::CCPoint finalPosition = ccp(newX, newY);
float distanceTarget = MathUtil::distance(position, target);
float ratio = this->getColitionRatio();
for (int i = 0; i < items.size(); i++) {
Item* item = items[i];
if (item->isActive()) {
float itemDistance = MathUtil::distance(position, item->getLocation());
if (itemDistance < (ratio + item->getColitionRatio())) {
float newDistance = MathUtil::distance(finalPosition, item->getLocation());
/** me fijo si me estoy acercando o alejando, en el caso de estar alejandome dejo que se mueva*/
if (newDistance < itemDistance) {
result = false;
break;
}
}
}
}
return result;
}
bool Item::isActive() {
return this->isVisible();
}
bool Item::isVisible() {
return this->sprite->isVisible();
}
float Item::getColitionRatio() {
return this->getWidth() / 4;
}
//FIXME doesnt' work
bool Item::isOutOfScreen() {
if (this->getRightPosition() < 0) {
return true;
}
if (this->getTopPosition() < 0) {
return true;
}
cocos2d::CCSize visibleSize = cocos2d::CCDirector::sharedDirector()->getVisibleSize();
if (this->getLeftPosition() > visibleSize.width) {
return true;
}
if (this->getBottomPosition() > visibleSize.height) {
return true;
}
return false;
}
bool Item::inVisionRange(Item* item, float visionRange, float angleRange, float distance, float thisRotation) {
cocos2d::CCPoint toItem = item->getLocation() - this->getLocation();
float toItemAngle = MathUtil::angle(toItem) * -57.2957795;
return (distance < visionRange && abs(abs(thisRotation - toItemAngle)) < angleRange);
}
} /* namespace dxco */