A pega armas no controle wii, corrigido bug de velocidade controle wii
[Projeto-PCG.git] / controlewii.cpp
blobcd4e2180d857bcac8b61cdf47ba68db092dda830
1 #include "controlewii.h"
2 #include "usereventtype.h"
3 #include "weaponitem.h"
4 #include <cmath>
6 ControleWii::ControleWii(Player &p) : Controle(p) {
7 connected = false;
8 initializeWiimote();
9 stickX = 0xffff;
10 stickY = 0xffff;
11 buttonsNunchuck = 0;
12 buttonsWii = 0;
13 for (int i = 0; i < CWIID_IR_SRC_COUNT; i++)
14 cyclesSinceSeen[i] = -1;
17 void ControleWii::handleOther() {
18 jogador.crawl = false;
19 if (jogador.dead) return;
20 Uint8 *keystates = SDL_GetKeyState( NULL );
21 if (keystates[SDLK_DOWN]) {
22 jogador.bypass = true;
24 if (jogador.onGround && keystates[SDLK_RCTRL]) {
25 jogador.crawl = true;
27 if (keystates[SDLK_LEFT]) {
28 jogador.addSpeed(-3, 0);
30 if (keystates[SDLK_RIGHT]) {
31 jogador.addSpeed(3, 0);
33 if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(1)) { //botao esquerdo do mouse pressionado
34 jogador.fire();
36 if (jogador.onGround &&
37 buttonsNunchuck & 2) { //pulo com botao C ou para cima
38 jogador.addSpeed( 0, -8);
39 jogador.onGround = false;
41 if (jogador.onGround && buttonsNunchuck & 1) {//deitar com z
42 jogador.crawl = true;
45 if (buttonsWii & 4) { //R atira
46 jogador.fire();
48 bool pegou = false;
49 std::list<WeaponItem*>::iterator it;
50 if (buttonsWii & 8) { //R atira
51 for (it = map->items.begin(); it != map->items.end(); ++it)
52 if (game->collisionManager->checkCollision(&jogador, (Thing*) *it)) {
53 jogador.equip((*it)->getWeapon());
54 pegou = true;
55 break;
57 if (pegou)
58 map->items.erase(it);
63 void ControleWii::handleEvent(SDL_Event &e) {
64 jogador.addSpeed(closerToZero(-jogador.getSpeedX(),sign(jogador.getSpeedX())*-0.5),0);
65 if (jogador.dead) return;
66 std::list<WeaponItem*>::iterator it;
67 bool pegou = false;
68 switch( e.type ) {
69 case SDL_USEREVENT:
70 switch (e.user.code) {
71 case WIIMOTEEVENT:
72 cwiid_mesg_type* type = (cwiid_mesg_type*)e.user.data1;
73 union cwiid_mesg *mesg = (union cwiid_mesg*)e.user.data2;
74 switch (*type) {
75 case CWIID_MESG_IR: {
76 int sources = 0;
77 double x = 0.0;
78 double y = 0.0;
79 for (int j = 0; j < CWIID_IR_SRC_COUNT; j++) {
80 if (cyclesSinceSeen[j] != -1)
81 cyclesSinceSeen[j]++;
82 if (mesg->ir_mesg.src[j].valid) {
83 cyclesSinceSeen[j] = 0;
84 lastSeenAt[j].x = mesg->ir_mesg.src[j].pos[CWIID_X];
85 lastSeenAt[j].y = mesg->ir_mesg.src[j].pos[CWIID_Y];
88 for (int i = 0; i < CWIID_IR_SRC_COUNT; i++)
89 if (cyclesSinceSeen[i] != -1) {
90 x += lastSeenAt[i].x;
91 y += lastSeenAt[i].y;
92 sources++;
94 if (sources > 0) {
95 x = x / sources;
96 y = y / sources;
97 x = jogador.game->config->integer["width"] - (x / 1024.0) * jogador.game->config->integer["width"];
98 y = (y / 738.0) * jogador.game->config->integer["height"];
99 jogador.setAim(x+game->camera.x,y+game->camera.y);
102 break;
103 case CWIID_MESG_BTN: {
104 buttonsWii = mesg->btn_mesg.buttons;
105 if (mesg->btn_mesg.buttons & 4) { //R atira
106 jogador.fire();
109 break;
110 case CWIID_MESG_NUNCHUK: {
111 int newX = mesg->nunchuk_mesg.stick[CWIID_X] - 120;
112 int newY = mesg->nunchuk_mesg.stick[CWIID_Y] - 131;
114 if (stickX != 0xffff) {
115 jogador.addSpeed(8.0*newX/128.0,0);
117 stickX = newX;
118 stickY = newY;
119 buttonsNunchuck = mesg->nunchuk_mesg.buttons;
120 if (jogador.onGround &&
121 ((mesg->nunchuk_mesg.buttons & 2) ||
122 newY > 50)) { //pulo com botao C ou para cima
123 jogador.addSpeed( 0, -8);
124 jogador.onGround = false;
127 if (newY < -60) {// || (mesg->nunchuk_mesg.buttons & 1)) {//descer com botao Z ou para baixo
128 jogador.bypass = true;
131 break;
134 free(e.user.data1);
135 free(e.user.data2);
136 break;
138 break;
139 case SDL_MOUSEMOTION: {
140 if (!connected)
141 jogador.setAim(e.motion.x+game->camera.x,e.motion.y+game->camera.y);
143 break;
144 case SDL_MOUSEBUTTONDOWN:
145 jogador.fire();
146 break;
147 case SDL_KEYUP:
148 switch (e.key.keysym.sym) {
149 default: break;
151 break;
152 case SDL_KEYDOWN:
153 switch (e.key.keysym.sym) {
154 case SDLK_DOWN:
155 jogador.onGround = false;
156 jogador.bypass = true;
157 break;
158 case SDLK_UP:
159 if (jogador.onGround) {
160 jogador.addSpeed( 0, -8);
161 jogador.onGround = false;
163 break;
164 case SDLK_RETURN:
165 for (it = map->items.begin(); it != map->items.end(); ++it)
166 if (game->collisionManager->checkCollision(&jogador, (Thing*) *it)) {
167 jogador.equip((*it)->getWeapon());
168 pegou = true;
169 break;
171 if (pegou)
172 map->items.erase(it);
173 break;
174 default: break;
176 break;
180 #define toggle_bit(bf,b) \
181 (bf) = ((bf) & b) \
182 ? ((bf) & ~(b)) \
183 : ((bf) | (b))
185 cwiid_err_t err;
186 void err(cwiid_wiimote_t *wiimote, const char *s, va_list ap) {
187 if (wiimote) printf("%d:", cwiid_get_id(wiimote));
188 else printf("-1:");
189 vprintf(s, ap);
190 printf("\n");
193 void cwiid_callback(cwiid_wiimote_t *wiimote, int mesg_count,
194 union cwiid_mesg mesg[], struct timespec *timestamp) {
195 int i, j;
196 int valid_source;
198 for (i=0; i < mesg_count; i++) {
199 SDL_Event event;
200 SDL_UserEvent userevent;
201 userevent.type = SDL_USEREVENT;
202 event.type = SDL_USEREVENT;
203 userevent.code = WIIMOTEEVENT;
204 cwiid_mesg_type *type = (cwiid_mesg_type*)malloc(sizeof(cwiid_mesg_type));
205 *type = mesg[i].type;
206 userevent.data1 = (void*)type;
207 union cwiid_mesg *m = (union cwiid_mesg *)malloc(sizeof(union cwiid_mesg));
208 *m = mesg[i];
209 userevent.data2 = m;
210 event.user = userevent;
211 SDL_PushEvent(&event);
215 void set_led_state(cwiid_wiimote_t *wiimote, unsigned char led_state) {
216 if (cwiid_set_led(wiimote, led_state)) {
217 fprintf(stderr, "Error setting LEDs \n");
221 void set_rpt_mode(cwiid_wiimote_t *wiimote, unsigned char rpt_mode) {
222 if (cwiid_set_rpt_mode(wiimote, rpt_mode)) {
223 fprintf(stderr, "Error setting report mode\n");
227 bool ControleWii::ControleWii::initializeWiimote() {
228 mesg = 0;
229 led_state = 0;
230 rpt_mode = 0;
231 rumble = 0;
233 cwiid_set_err(err);
235 bdaddr = *BDADDR_ANY;
237 /* Connect to the wiimote */
238 printf("Put Wiimote in discoverable mode now (press 1+2)...\n");
239 if (!(wiimote = cwiid_open(&bdaddr, 0))) {
240 fprintf(stderr, "Unable to connect to wiimote\n");
241 return false;
243 if (cwiid_set_mesg_callback(wiimote, cwiid_callback)) {
244 fprintf(stderr, "Unable to set message callback\n");
245 return false;
248 toggle_bit(rpt_mode, CWIID_RPT_BTN); //abilita status report de botoes
250 toggle_bit(rpt_mode, CWIID_RPT_NUNCHUK); //abilita status report de nunchuck
252 toggle_bit(led_state, CWIID_LED4_ON); //liga quarto LED para indentificar ligaƧao com o NOSSO jogo e nao o wii
253 set_led_state(wiimote, led_state);
255 toggle_bit(rpt_mode, CWIID_RPT_IR); //abilita recebimento do infravermelho
256 set_rpt_mode(wiimote, rpt_mode);
258 if (cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC)) {
259 fprintf(stderr, "Error enabling messages\n");
260 return false;
262 connected = true;
263 return true;