Player agora morre
[Projeto-PCG.git] / controlewii.cpp
blob391a8b7800960f20eaf0b0bc00880629a0df47cf
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 if (jogador.dead) return;
19 jogador.crawl = false;
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 (keystates[SDLK_RIGHT]) {
34 jogador.addSpeed(3, 0);
36 if (!keystates[SDLK_RIGHT] && !keystates[SDLK_LEFT])
37 jogador.addSpeed(closerToZero(-jogador.getSpeedX(),sign(jogador.getSpeedX())*-0.5),0);
38 if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(1)) { //botao esquerdo do mouse pressionado
39 jogador.fire();
41 if (jogador.onGround &&
42 buttonsNunchuck & 2) { //pulo com botao C ou para cima
43 jogador.addSpeed( 0, -8);
44 jogador.onGround = false;
46 if (jogador.onGround && buttonsNunchuck & 1) {//deitar com z
47 jogador.crawl = true;
50 if (buttonsWii & 4) { //R atira
51 jogador.fire();
56 void ControleWii::handleEvent(SDL_Event &e) {
57 if (jogador.dead) return;
58 std::list<WeaponItem*>::iterator it;
59 bool pegou = false;
60 switch( e.type ) {
61 case SDL_USEREVENT:
62 switch (e.user.code) {
63 case WIIMOTEEVENT:
64 cwiid_mesg_type* type = (cwiid_mesg_type*)e.user.data1;
65 union cwiid_mesg *mesg = (union cwiid_mesg*)e.user.data2;
66 switch (*type) {
67 case CWIID_MESG_IR: {
68 int sources = 0;
69 double x = 0.0;
70 double y = 0.0;
71 for (int j = 0; j < CWIID_IR_SRC_COUNT; j++) {
72 if (cyclesSinceSeen[j] != -1)
73 cyclesSinceSeen[j]++;
74 if (mesg->ir_mesg.src[j].valid) {
75 cyclesSinceSeen[j] = 0;
76 lastSeenAt[j].x = mesg->ir_mesg.src[j].pos[CWIID_X];
77 lastSeenAt[j].y = mesg->ir_mesg.src[j].pos[CWIID_Y];
80 for (int i = 0; i < CWIID_IR_SRC_COUNT; i++)
81 if (cyclesSinceSeen[i] != -1) {
82 x += lastSeenAt[i].x;
83 y += lastSeenAt[i].y;
84 sources++;
86 if (sources > 0) {
87 x = x / sources;
88 y = y / sources;
89 x = jogador.game->config->screen["width"] - (x / 1024.0) * jogador.game->config->screen["width"];
90 y = (y / 738.0) * jogador.game->config->screen["height"];
91 jogador.setAim(x+game->camera.x,y+game->camera.y);
94 break;
95 case CWIID_MESG_BTN: {
96 buttonsWii = mesg->btn_mesg.buttons;
97 if (mesg->btn_mesg.buttons & 4) { //R atira
98 jogador.fire();
101 break;
102 case CWIID_MESG_NUNCHUK: {
103 int newX = mesg->nunchuk_mesg.stick[CWIID_X] - 120;
104 int newY = mesg->nunchuk_mesg.stick[CWIID_Y] - 131;
106 if (stickX != 0xffff) {
107 jogador.setSpeed(4.0*newX/128,jogador.getSpeedY());
109 stickX = newX;
110 stickY = newY;
111 buttonsNunchuck = mesg->nunchuk_mesg.buttons;
113 if (jogador.onGround &&
114 ((mesg->nunchuk_mesg.buttons & 2) ||
115 newY > 60)) { //pulo com botao C ou para cima
116 jogador.addSpeed( 0, -8);
117 jogador.onGround = false;
120 if (newY < -60) {// || (mesg->nunchuk_mesg.buttons & 1)) {//descer com botao Z ou para baixo
121 jogador.bypass = true;
124 break;
127 free(e.user.data1);
128 free(e.user.data2);
129 break;
131 break;
132 case SDL_MOUSEMOTION: {
133 if (!connected)
134 jogador.setAim(e.motion.x+game->camera.x,e.motion.y+game->camera.y);
136 break;
137 case SDL_MOUSEBUTTONDOWN:
138 jogador.fire();
139 break;
140 case SDL_KEYUP:
141 switch (e.key.keysym.sym) {
142 default: break;
144 break;
145 case SDL_KEYDOWN:
146 switch (e.key.keysym.sym) {
147 case SDLK_DOWN:
148 jogador.onGround = false;
149 jogador.bypass = true;
150 break;
151 case SDLK_UP:
152 if (jogador.onGround) {
153 jogador.addSpeed( 0, -8);
154 jogador.onGround = false;
156 break;
157 case SDLK_RETURN:
158 for (it = map->items.begin(); it != map->items.end(); ++it)
159 if (game->collisionManager->checkCollision(&jogador, (Thing*) *it)) {
160 jogador.equip((*it)->getWeapon());
161 pegou = true;
162 break;
164 if (pegou)
165 map->items.erase(it);
166 break;
167 default: break;
169 break;
173 #define toggle_bit(bf,b) \
174 (bf) = ((bf) & b) \
175 ? ((bf) & ~(b)) \
176 : ((bf) | (b))
178 cwiid_err_t err;
179 void err(cwiid_wiimote_t *wiimote, const char *s, va_list ap) {
180 if (wiimote) printf("%d:", cwiid_get_id(wiimote));
181 else printf("-1:");
182 vprintf(s, ap);
183 printf("\n");
186 void cwiid_callback(cwiid_wiimote_t *wiimote, int mesg_count,
187 union cwiid_mesg mesg[], struct timespec *timestamp) {
188 int i, j;
189 int valid_source;
191 for (i=0; i < mesg_count; i++) {
192 SDL_Event event;
193 SDL_UserEvent userevent;
194 userevent.type = SDL_USEREVENT;
195 event.type = SDL_USEREVENT;
196 userevent.code = WIIMOTEEVENT;
197 cwiid_mesg_type *type = (cwiid_mesg_type*)malloc(sizeof(cwiid_mesg_type));
198 *type = mesg[i].type;
199 userevent.data1 = (void*)type;
200 union cwiid_mesg *m = (union cwiid_mesg *)malloc(sizeof(union cwiid_mesg));
201 *m = mesg[i];
202 userevent.data2 = m;
203 event.user = userevent;
204 SDL_PushEvent(&event);
208 void set_led_state(cwiid_wiimote_t *wiimote, unsigned char led_state) {
209 if (cwiid_set_led(wiimote, led_state)) {
210 fprintf(stderr, "Error setting LEDs \n");
214 void set_rpt_mode(cwiid_wiimote_t *wiimote, unsigned char rpt_mode) {
215 if (cwiid_set_rpt_mode(wiimote, rpt_mode)) {
216 fprintf(stderr, "Error setting report mode\n");
220 bool ControleWii::ControleWii::initializeWiimote() {
221 mesg = 0;
222 led_state = 0;
223 rpt_mode = 0;
224 rumble = 0;
226 cwiid_set_err(err);
228 bdaddr = *BDADDR_ANY;
230 /* Connect to the wiimote */
231 printf("Put Wiimote in discoverable mode now (press 1+2)...\n");
232 if (!(wiimote = cwiid_open(&bdaddr, 0))) {
233 fprintf(stderr, "Unable to connect to wiimote\n");
234 return false;
236 if (cwiid_set_mesg_callback(wiimote, cwiid_callback)) {
237 fprintf(stderr, "Unable to set message callback\n");
238 return false;
241 toggle_bit(rpt_mode, CWIID_RPT_BTN); //abilita status report de botoes
243 toggle_bit(rpt_mode, CWIID_RPT_NUNCHUK); //abilita status report de nunchuck
245 toggle_bit(led_state, CWIID_LED4_ON); //liga quarto LED para indentificar ligaƧao com o NOSSO jogo e nao o wii
246 set_led_state(wiimote, led_state);
248 toggle_bit(rpt_mode, CWIID_RPT_IR); //abilita recebimento do infravermelho
249 set_rpt_mode(wiimote, rpt_mode);
251 if (cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC)) {
252 fprintf(stderr, "Error enabling messages\n");
253 return false;
255 connected = true;
256 return true;