-
Notifications
You must be signed in to change notification settings - Fork 0
/
Container.cpp
72 lines (54 loc) · 1.62 KB
/
Container.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
#include "Container.h"
#include "Item.h"
namespace dxco {
Container::Container(float x, float y, float width, float height) {
this->setContentSize(cocos2d::CCSize(width, height));
this->width = width;
this->height = height;
this->move(x, y);
}
float Container::getWidth() {
return this->width;
}
float Container::getHeight() {
return this->height;
}
void Container::move(float x, float y) {
float xFinal = this->getPositionX() + x;
float yFinal = this->getPositionY() + y;
this->moveToAbsolute(xFinal, yFinal);
}
void Container::moveTo(float x, float y) {
cocos2d::CCPoint origin =
cocos2d::CCDirector::sharedDirector()->getVisibleOrigin();
float xFinal = x;
float yFinal = y + this->getHeight() / 2;
this->moveToAbsolute(xFinal + origin.x, yFinal + origin.y);
}
void Container::moveToAbsolute(float x, float y) {
this->setPosition(ccp(x, y));
}
bool Container::inside(Item *item) {
return (item->getLeftPosition() > 0 &&
item->getRightPosition() < this->width &&
item->getBottomPosition() > 0 &&
item->getTopPosition() < this->height);
}
void Container::putInside(Item *item) {
cocos2d::CCPoint location = item->getLocation();
float itemWidth = item->getWidth() / 4;
float itemHeight = item->getHeight() / 4;
if (location.x - itemWidth < 0){
item->move(-location.x + itemWidth, 0);
}
if (location.x + itemWidth > this->width) {
item->move(-(location.x - this->width) -itemWidth, 0);
}
if (location.y - itemHeight < 0) {
item->move(0, -location.y + itemHeight);
}
if (location.y + itemHeight > this->height) {
item->move(0, -(location.y - this->height) -itemHeight);
}
}
} /* namespace dxco */