-
Notifications
You must be signed in to change notification settings - Fork 0
/
Joystick.cpp
55 lines (40 loc) · 1.37 KB
/
Joystick.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
#include "Joystick.h"
#include "SpriteUtil.h"
#include "MathUtil.h"
namespace dxco {
Joystick::Joystick(cocos2d::CCSprite* sprite, float radio) {
this->sprite = sprite;
this->center = sprite->getPosition();
this->radio = radio;
}
void Joystick::onMoved(cocos2d::CCPoint location, float angle, float intensity) {
this->sprite->setPosition(getSpriteLocation(location));
}
void Joystick::onTouchedBegan(cocos2d::CCPoint location, float angle, float intensity) {
this->sprite->setPosition(getSpriteLocation(location));
}
cocos2d::CCPoint Joystick::getSpriteLocation(cocos2d::CCPoint &touchedLocation) {
cocos2d::CCPoint result = touchedLocation;
float distance = MathUtil::distance(touchedLocation, this->center);
if (distance > this->radio) {
float angle = MathUtil::angle(touchedLocation, center);
float x = this->center.x - cos(angle) * this->radio;
float y = this->center.y - sin(angle) * this->radio;
result = ccp(x, y);
}
return result;
}
void Joystick::onTouchedEnded(cocos2d::CCPoint location, float angle, float intensity) {
this->sprite->setPosition(this->center);
}
cocos2d::CCPoint Joystick::getCenter() {
return this->center;
}
float Joystick::getRadio() {
return this->radio;
}
void Joystick::setCenter(cocos2d::CCPoint location) {
SpriteUtil::moveTo(this->sprite, location.x, location.y);
this->center = this->sprite->getPosition();
}
} /* namespace dxco */