Consertado "efeito metralhadora" que aparece num F7-spam
[Projeto-PCG.git] / controlewii.cpp
blob5b20761fdc4aa761d8d2f596f28d3f754669c79c
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 Uint8 *keystates = SDL_GetKeyState( NULL );
20 if (keystates[SDLK_DOWN]) {
21 jogador.bypass = true;
23 if (jogador.onGround && keystates[SDLK_RCTRL]) {
24 jogador.crawl = true;
26 if (keystates[SDLK_LEFT]) {
27 jogador.addSpeed(-3, 0);
29 if (keystates[SDLK_RIGHT]) {
30 jogador.addSpeed(3, 0);
32 if (keystates[SDLK_RIGHT]) {
33 jogador.addSpeed(3, 0);
35 if (!keystates[SDLK_RIGHT] && !keystates[SDLK_LEFT])
36 jogador.addSpeed(closerToZero(-jogador.getSpeedX(),sign(jogador.getSpeedX())*-0.5),0);
37 if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(1)) { //botao esquerdo do mouse pressionado
38 jogador.fire();
40 if (jogador.onGround &&
41 buttonsNunchuck & 2) { //pulo com botao C ou para cima
42 jogador.addSpeed( 0, -8);
43 jogador.onGround = false;
45 if (jogador.onGround && buttonsNunchuck & 1) {//deitar com z
46 jogador.crawl = true;
49 if (buttonsWii & 4) { //R atira
50 jogador.fire();
55 void ControleWii::handleEvent(SDL_Event &e) {
56 std::list<WeaponItem*>::iterator it;
57 bool pegou = false;
58 switch( e.type ) {
59 case SDL_USEREVENT:
60 switch (e.user.code) {
61 case WIIMOTEEVENT:
62 cwiid_mesg_type* type = (cwiid_mesg_type*)e.user.data1;
63 union cwiid_mesg *mesg = (union cwiid_mesg*)e.user.data2;
64 switch (*type) {
65 case CWIID_MESG_IR: {
66 int sources = 0;
67 double x = 0.0;
68 double y = 0.0;
69 for (int j = 0; j < CWIID_IR_SRC_COUNT; j++) {
70 if (cyclesSinceSeen[j] != -1)
71 cyclesSinceSeen[j]++;
72 if (mesg->ir_mesg.src[j].valid) {
73 cyclesSinceSeen[j] = 0;
74 lastSeenAt[j].x = mesg->ir_mesg.src[j].pos[CWIID_X];
75 lastSeenAt[j].y = mesg->ir_mesg.src[j].pos[CWIID_Y];
78 for (int i = 0; i < CWIID_IR_SRC_COUNT; i++)
79 if (cyclesSinceSeen[i] != -1) {
80 x += lastSeenAt[i].x;
81 y += lastSeenAt[i].y;
82 sources++;
84 if (sources > 0) {
85 x = x / sources;
86 y = y / sources;
87 x = jogador.game->config->screen["width"] - (x / 1024.0) * jogador.game->config->screen["width"];
88 y = (y / 738.0) * jogador.game->config->screen["height"];
89 jogador.setAim(x+game->camera.x,y+game->camera.y);
92 break;
93 case CWIID_MESG_BTN: {
94 buttonsWii = mesg->btn_mesg.buttons;
95 if (mesg->btn_mesg.buttons & 4) { //R atira
96 jogador.fire();
99 break;
100 case CWIID_MESG_NUNCHUK: {
101 int newX = mesg->nunchuk_mesg.stick[CWIID_X] - 120;
102 int newY = mesg->nunchuk_mesg.stick[CWIID_Y] - 131;
104 if (stickX != 0xffff) {
105 jogador.setSpeed(4.0*newX/128,jogador.getSpeedY());
107 stickX = newX;
108 stickY = newY;
109 buttonsNunchuck = mesg->nunchuk_mesg.buttons;
111 if (jogador.onGround &&
112 ((mesg->nunchuk_mesg.buttons & 2) ||
113 newY > 60)) { //pulo com botao C ou para cima
114 jogador.addSpeed( 0, -8);
115 jogador.onGround = false;
118 if (newY < -60) {// || (mesg->nunchuk_mesg.buttons & 1)) {//descer com botao Z ou para baixo
119 jogador.bypass = true;
122 break;
125 free(e.user.data1);
126 free(e.user.data2);
127 break;
129 break;
130 case SDL_MOUSEMOTION: {
131 if (!connected)
132 jogador.setAim(e.motion.x+game->camera.x,e.motion.y+game->camera.y);
134 break;
135 case SDL_MOUSEBUTTONDOWN:
136 jogador.fire();
137 break;
138 case SDL_KEYUP:
139 switch (e.key.keysym.sym) {
140 default: break;
142 break;
143 case SDL_KEYDOWN:
144 switch (e.key.keysym.sym) {
145 case SDLK_DOWN:
146 jogador.onGround = false;
147 jogador.bypass = true;
148 break;
149 case SDLK_UP:
150 if (jogador.onGround) {
151 jogador.addSpeed( 0, -8);
152 jogador.onGround = false;
154 break;
155 case SDLK_RETURN:
156 for (it = map->items.begin(); it != map->items.end(); ++it)
157 if (game->collisionManager->checkCollision(&jogador, (Thing*) *it)) {
158 jogador.equip((*it)->getWeapon());
159 pegou = true;
160 break;
162 if (pegou)
163 map->items.erase(it);
164 break;
165 default: break;
167 break;
171 #define toggle_bit(bf,b) \
172 (bf) = ((bf) & b) \
173 ? ((bf) & ~(b)) \
174 : ((bf) | (b))
176 cwiid_err_t err;
177 void err(cwiid_wiimote_t *wiimote, const char *s, va_list ap) {
178 if (wiimote) printf("%d:", cwiid_get_id(wiimote));
179 else printf("-1:");
180 vprintf(s, ap);
181 printf("\n");
184 void cwiid_callback(cwiid_wiimote_t *wiimote, int mesg_count,
185 union cwiid_mesg mesg[], struct timespec *timestamp) {
186 int i, j;
187 int valid_source;
189 for (i=0; i < mesg_count; i++) {
190 SDL_Event event;
191 SDL_UserEvent userevent;
192 userevent.type = SDL_USEREVENT;
193 event.type = SDL_USEREVENT;
194 userevent.code = WIIMOTEEVENT;
195 cwiid_mesg_type *type = (cwiid_mesg_type*)malloc(sizeof(cwiid_mesg_type));
196 *type = mesg[i].type;
197 userevent.data1 = (void*)type;
198 union cwiid_mesg *m = (union cwiid_mesg *)malloc(sizeof(union cwiid_mesg));
199 *m = mesg[i];
200 userevent.data2 = m;
201 event.user = userevent;
202 SDL_PushEvent(&event);
206 void set_led_state(cwiid_wiimote_t *wiimote, unsigned char led_state) {
207 if (cwiid_set_led(wiimote, led_state)) {
208 fprintf(stderr, "Error setting LEDs \n");
212 void set_rpt_mode(cwiid_wiimote_t *wiimote, unsigned char rpt_mode) {
213 if (cwiid_set_rpt_mode(wiimote, rpt_mode)) {
214 fprintf(stderr, "Error setting report mode\n");
218 bool ControleWii::ControleWii::initializeWiimote() {
219 mesg = 0;
220 led_state = 0;
221 rpt_mode = 0;
222 rumble = 0;
224 cwiid_set_err(err);
226 bdaddr = *BDADDR_ANY;
228 /* Connect to the wiimote */
229 printf("Put Wiimote in discoverable mode now (press 1+2)...\n");
230 if (!(wiimote = cwiid_open(&bdaddr, 0))) {
231 fprintf(stderr, "Unable to connect to wiimote\n");
232 return false;
234 if (cwiid_set_mesg_callback(wiimote, cwiid_callback)) {
235 fprintf(stderr, "Unable to set message callback\n");
236 return false;
239 toggle_bit(rpt_mode, CWIID_RPT_BTN); //abilita status report de botoes
241 toggle_bit(rpt_mode, CWIID_RPT_NUNCHUK); //abilita status report de nunchuck
243 toggle_bit(led_state, CWIID_LED4_ON); //liga quarto LED para indentificar ligaƧao com o NOSSO jogo e nao o wii
244 set_led_state(wiimote, led_state);
246 toggle_bit(rpt_mode, CWIID_RPT_IR); //abilita recebimento do infravermelho
247 set_rpt_mode(wiimote, rpt_mode);
249 if (cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC)) {
250 fprintf(stderr, "Error enabling messages\n");
251 return false;
253 connected = true;
254 return true;