-
Notifications
You must be signed in to change notification settings - Fork 2
/
ActivateButton.cpp
170 lines (136 loc) · 3.36 KB
/
ActivateButton.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
#include "ActivateButton.h"
#include "Player.h"
ActivateButton::ActivateButton(float x, float y, int width, int height, char *textureSource)
: Object(x, y, width, height, textureSource, ACTIVATE_BUTTON)
{
mActivationRect = getShape()->getRect();
mActivationRect.left -= 20;
mActivationRect.right += 20;
mActivationRect.top -= 20;
mActivationRect.bottom += 20;
setResizeable(false);
mGate = NULL;
mLevel = NULL;
mGateId = 99999;
mPressedTexture = gGraphics->loadObjectTexture((char*)ACTIVATEBUTTON_PRESSED_SOURCE.c_str());
}
ActivateButton::~ActivateButton()
{
// dtor
}
void ActivateButton::update(float dt)
{
static bool deleted;
if(mGate != NULL)
{
deleted = false;
if(mPlayer->getActivateKey())
{
// inside activation rect?
RECT playerRect = mPlayer->getShape()->getRect();
if(playerRect.right > mActivationRect.left && playerRect.left < mActivationRect.right && playerRect.bottom > mActivationRect.top
&& playerRect.top < mActivationRect.bottom) {
mGate->activate();
mPlayer->setActivateKey(false);
}
}
}
else {
if(!deleted)
mGateId = 9999;
}
}
void ActivateButton::draw(void)
{
if(mGate != NULL) {
if(!mGate->getOpen())
Object::draw();
else
gGraphics->drawShape(*getShape(), mPressedTexture);
}
else
Object::draw();
//gGraphics->BlitRect(mActivationRect);
}
void ActivateButton::saveToFile(std::ofstream *fout)
{
*fout << getType() << " " << getID() << " " << getX() << " " << getY() << " " << getWidth() << " " << getHeight() << " ";
if(mGate != NULL)
*fout << mGate->getID() << " ";
else
*fout << 99999 << " ";
*fout << getTextureSource() << endl;
}
void ActivateButton::move(float dx, float dy)
{
Object::move(dx, dy);
mActivationRect.left += dx;
mActivationRect.right += dx;
mActivationRect.top += dy;
mActivationRect.bottom += dy;
}
void ActivateButton::editorMove(float dx, float dy)
{
Object::editorMove(dx, dy);
mActivationRect.left += dx;
mActivationRect.right += dx;
mActivationRect.top += dy;
mActivationRect.bottom += dy;
}
void ActivateButton::scale(direction side, int dwidth, int dheight)
{
}
void ActivateButton::onPlayerCollision(Player *player, MTV mtv, float dt)
{
}
std::vector<Property> ActivateButton::getProperties(void)
{
std::vector<Property> properties = Object::getProperties();
Property tmp;
char buffer[16];
tmp.name = "gate id";
if(mGate != NULL)
sprintf(buffer, "%i", mGate->getID());
else
sprintf(buffer, "%i", 99999);
tmp.value = buffer;
properties.push_back(tmp);
return properties;
}
void ActivateButton::loadProperties(std::vector<Property> propertyList)
{
Object::loadProperties(propertyList);
int tmp;
tmp = atoi(propertyList[2].value.c_str()); // gate id
if(mGate == NULL)
mLevel->connectGate(this, tmp);
else if(tmp != mGate->getID()) {
mLevel->connectGate(this, tmp);
}
}
void ActivateButton::setPlayer(Player *player)
{
mPlayer = player;
}
void ActivateButton::setLevel(Level *level)
{
mLevel = level;
}
bool ActivateButton::insideActivateArea(void)
{
if(mPlayer->getX() > mActivationRect.left && mPlayer->getX() < mActivationRect.right && mPlayer->getY() > mActivationRect.top
&& mPlayer->getY() < mActivationRect.bottom)
return true;
else
return false;
}
void ActivateButton::connectGate(Gate *gate)
{
mGate = gate;
if(mGate != NULL)
mGate->setActivateButton(this);
}
void ActivateButton::setGateId(int id)
{
mGateId = id;
}