Barra de vida pro jogador
[Projeto-PCG.git] / controlewii.cpp
blobb4a0d97434f58dd67077891cc9a22e43864b4f87
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.addSpeed(closerToZero(-jogador.getSpeedX(),sign(jogador.getSpeedX())*-0.5),0);
19 if (jogador.dead) return;
20 jogador.crawl = false;
21 Uint8 *keystates = SDL_GetKeyState( NULL );
22 if (keystates[SDLK_DOWN]) {
23 jogador.bypass = true;
25 if (jogador.onGround && keystates[SDLK_RCTRL]) {
26 jogador.crawl = true;
28 if (keystates[SDLK_LEFT]) {
29 jogador.addSpeed(-3, 0);
31 if (keystates[SDLK_RIGHT]) {
32 jogador.addSpeed(3, 0);
34 if (keystates[SDLK_RIGHT]) {
35 jogador.addSpeed(3, 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 if (jogador.dead) return;
57 std::list<WeaponItem*>::iterator it;
58 bool pegou = false;
59 switch( e.type ) {
60 case SDL_USEREVENT:
61 switch (e.user.code) {
62 case WIIMOTEEVENT:
63 cwiid_mesg_type* type = (cwiid_mesg_type*)e.user.data1;
64 union cwiid_mesg *mesg = (union cwiid_mesg*)e.user.data2;
65 switch (*type) {
66 case CWIID_MESG_IR: {
67 int sources = 0;
68 double x = 0.0;
69 double y = 0.0;
70 for (int j = 0; j < CWIID_IR_SRC_COUNT; j++) {
71 if (cyclesSinceSeen[j] != -1)
72 cyclesSinceSeen[j]++;
73 if (mesg->ir_mesg.src[j].valid) {
74 cyclesSinceSeen[j] = 0;
75 lastSeenAt[j].x = mesg->ir_mesg.src[j].pos[CWIID_X];
76 lastSeenAt[j].y = mesg->ir_mesg.src[j].pos[CWIID_Y];
79 for (int i = 0; i < CWIID_IR_SRC_COUNT; i++)
80 if (cyclesSinceSeen[i] != -1) {
81 x += lastSeenAt[i].x;
82 y += lastSeenAt[i].y;
83 sources++;
85 if (sources > 0) {
86 x = x / sources;
87 y = y / sources;
88 x = jogador.game->config->screen["width"] - (x / 1024.0) * jogador.game->config->screen["width"];
89 y = (y / 738.0) * jogador.game->config->screen["height"];
90 jogador.setAim(x+game->camera.x,y+game->camera.y);
93 break;
94 case CWIID_MESG_BTN: {
95 buttonsWii = mesg->btn_mesg.buttons;
96 if (mesg->btn_mesg.buttons & 4) { //R atira
97 jogador.fire();
100 break;
101 case CWIID_MESG_NUNCHUK: {
102 int newX = mesg->nunchuk_mesg.stick[CWIID_X] - 120;
103 int newY = mesg->nunchuk_mesg.stick[CWIID_Y] - 131;
105 if (stickX != 0xffff) {
106 jogador.setSpeed(4.0*newX/128,jogador.getSpeedY());
108 stickX = newX;
109 stickY = newY;
110 buttonsNunchuck = mesg->nunchuk_mesg.buttons;
112 if (jogador.onGround &&
113 ((mesg->nunchuk_mesg.buttons & 2) ||
114 newY > 60)) { //pulo com botao C ou para cima
115 jogador.addSpeed( 0, -8);
116 jogador.onGround = false;
119 if (newY < -60) {// || (mesg->nunchuk_mesg.buttons & 1)) {//descer com botao Z ou para baixo
120 jogador.bypass = true;
123 break;
126 free(e.user.data1);
127 free(e.user.data2);
128 break;
130 break;
131 case SDL_MOUSEMOTION: {
132 if (!connected)
133 jogador.setAim(e.motion.x+game->camera.x,e.motion.y+game->camera.y);
135 break;
136 case SDL_MOUSEBUTTONDOWN:
137 jogador.fire();
138 break;
139 case SDL_KEYUP:
140 switch (e.key.keysym.sym) {
141 default: break;
143 break;
144 case SDL_KEYDOWN:
145 switch (e.key.keysym.sym) {
146 case SDLK_DOWN:
147 jogador.onGround = false;
148 jogador.bypass = true;
149 break;
150 case SDLK_UP:
151 if (jogador.onGround) {
152 jogador.addSpeed( 0, -8);
153 jogador.onGround = false;
155 break;
156 case SDLK_RETURN:
157 for (it = map->items.begin(); it != map->items.end(); ++it)
158 if (game->collisionManager->checkCollision(&jogador, (Thing*) *it)) {
159 jogador.equip((*it)->getWeapon());
160 pegou = true;
161 break;
163 if (pegou)
164 map->items.erase(it);
165 break;
166 default: break;
168 break;
172 #define toggle_bit(bf,b) \
173 (bf) = ((bf) & b) \
174 ? ((bf) & ~(b)) \
175 : ((bf) | (b))
177 cwiid_err_t err;
178 void err(cwiid_wiimote_t *wiimote, const char *s, va_list ap) {
179 if (wiimote) printf("%d:", cwiid_get_id(wiimote));
180 else printf("-1:");
181 vprintf(s, ap);
182 printf("\n");
185 void cwiid_callback(cwiid_wiimote_t *wiimote, int mesg_count,
186 union cwiid_mesg mesg[], struct timespec *timestamp) {
187 int i, j;
188 int valid_source;
190 for (i=0; i < mesg_count; i++) {
191 SDL_Event event;
192 SDL_UserEvent userevent;
193 userevent.type = SDL_USEREVENT;
194 event.type = SDL_USEREVENT;
195 userevent.code = WIIMOTEEVENT;
196 cwiid_mesg_type *type = (cwiid_mesg_type*)malloc(sizeof(cwiid_mesg_type));
197 *type = mesg[i].type;
198 userevent.data1 = (void*)type;
199 union cwiid_mesg *m = (union cwiid_mesg *)malloc(sizeof(union cwiid_mesg));
200 *m = mesg[i];
201 userevent.data2 = m;
202 event.user = userevent;
203 SDL_PushEvent(&event);
207 void set_led_state(cwiid_wiimote_t *wiimote, unsigned char led_state) {
208 if (cwiid_set_led(wiimote, led_state)) {
209 fprintf(stderr, "Error setting LEDs \n");
213 void set_rpt_mode(cwiid_wiimote_t *wiimote, unsigned char rpt_mode) {
214 if (cwiid_set_rpt_mode(wiimote, rpt_mode)) {
215 fprintf(stderr, "Error setting report mode\n");
219 bool ControleWii::ControleWii::initializeWiimote() {
220 mesg = 0;
221 led_state = 0;
222 rpt_mode = 0;
223 rumble = 0;
225 cwiid_set_err(err);
227 bdaddr = *BDADDR_ANY;
229 /* Connect to the wiimote */
230 printf("Put Wiimote in discoverable mode now (press 1+2)...\n");
231 if (!(wiimote = cwiid_open(&bdaddr, 0))) {
232 fprintf(stderr, "Unable to connect to wiimote\n");
233 return false;
235 if (cwiid_set_mesg_callback(wiimote, cwiid_callback)) {
236 fprintf(stderr, "Unable to set message callback\n");
237 return false;
240 toggle_bit(rpt_mode, CWIID_RPT_BTN); //abilita status report de botoes
242 toggle_bit(rpt_mode, CWIID_RPT_NUNCHUK); //abilita status report de nunchuck
244 toggle_bit(led_state, CWIID_LED4_ON); //liga quarto LED para indentificar ligaƧao com o NOSSO jogo e nao o wii
245 set_led_state(wiimote, led_state);
247 toggle_bit(rpt_mode, CWIID_RPT_IR); //abilita recebimento do infravermelho
248 set_rpt_mode(wiimote, rpt_mode);
250 if (cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC)) {
251 fprintf(stderr, "Error enabling messages\n");
252 return false;
254 connected = true;
255 return true;