Ajuste leve na velocidade permitindo movimentos mais sutis
[Projeto-PCG.git] / controlewii.cpp
blobde4b6158eaa5c7453b4139d13a86613cc433baab
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 if (jogador.dead) return;
65 std::list<WeaponItem*>::iterator it;
66 bool pegou = false;
67 switch( e.type ) {
68 case SDL_USEREVENT:
69 switch (e.user.code) {
70 case WIIMOTEEVENT:
71 cwiid_mesg_type* type = (cwiid_mesg_type*)e.user.data1;
72 union cwiid_mesg *mesg = (union cwiid_mesg*)e.user.data2;
73 switch (*type) {
74 case CWIID_MESG_IR: {
75 int sources = 0;
76 double x = 0.0;
77 double y = 0.0;
78 for (int j = 0; j < CWIID_IR_SRC_COUNT; j++) {
79 if (cyclesSinceSeen[j] != -1)
80 cyclesSinceSeen[j]++;
81 if (mesg->ir_mesg.src[j].valid) {
82 cyclesSinceSeen[j] = 0;
83 lastSeenAt[j].x = mesg->ir_mesg.src[j].pos[CWIID_X];
84 lastSeenAt[j].y = mesg->ir_mesg.src[j].pos[CWIID_Y];
87 for (int i = 0; i < CWIID_IR_SRC_COUNT; i++)
88 if (cyclesSinceSeen[i] != -1) {
89 x += lastSeenAt[i].x;
90 y += lastSeenAt[i].y;
91 sources++;
93 if (sources > 0) {
94 x = x / sources;
95 y = y / sources;
96 x = jogador.game->config->integer["width"] - (x / 1024.0) * jogador.game->config->integer["width"];
97 y = (y / 738.0) * jogador.game->config->integer["height"];
98 jogador.setAim(x+game->camera.x,y+game->camera.y);
101 break;
102 case CWIID_MESG_BTN: {
103 buttonsWii = mesg->btn_mesg.buttons;
104 if (mesg->btn_mesg.buttons & 4) { //R atira
105 jogador.fire();
108 break;
109 case CWIID_MESG_NUNCHUK: {
110 int newX = mesg->nunchuk_mesg.stick[CWIID_X] - 120;
111 int newY = mesg->nunchuk_mesg.stick[CWIID_Y] - 131;
113 if (stickX != 0xffff) {
114 jogador.addSpeed(3.0*newX/128.0,0);
116 stickX = newX;
117 stickY = newY;
118 buttonsNunchuck = mesg->nunchuk_mesg.buttons;
119 if (jogador.onGround &&
120 ((mesg->nunchuk_mesg.buttons & 2) ||
121 newY > 50)) { //pulo com botao C ou para cima
122 jogador.addSpeed( 0, -8);
123 jogador.onGround = false;
126 if (newY < -60) {// || (mesg->nunchuk_mesg.buttons & 1)) {//descer com botao Z ou para baixo
127 jogador.bypass = true;
130 break;
133 free(e.user.data1);
134 free(e.user.data2);
135 break;
137 break;
138 case SDL_MOUSEMOTION: {
139 if (!connected)
140 jogador.setAim(e.motion.x+game->camera.x,e.motion.y+game->camera.y);
142 break;
143 case SDL_MOUSEBUTTONDOWN:
144 jogador.fire();
145 break;
146 case SDL_KEYUP:
147 switch (e.key.keysym.sym) {
148 default: break;
150 break;
151 case SDL_KEYDOWN:
152 switch (e.key.keysym.sym) {
153 case SDLK_DOWN:
154 jogador.onGround = false;
155 jogador.bypass = true;
156 break;
157 case SDLK_UP:
158 if (jogador.onGround) {
159 jogador.addSpeed( 0, -8);
160 jogador.onGround = false;
162 break;
163 case SDLK_RETURN:
164 for (it = map->items.begin(); it != map->items.end(); ++it)
165 if (game->collisionManager->checkCollision(&jogador, (Thing*) *it)) {
166 jogador.equip((*it)->getWeapon());
167 pegou = true;
168 break;
170 if (pegou)
171 map->items.erase(it);
172 break;
173 default: break;
175 break;
179 #define toggle_bit(bf,b) \
180 (bf) = ((bf) & b) \
181 ? ((bf) & ~(b)) \
182 : ((bf) | (b))
184 cwiid_err_t err;
185 void err(cwiid_wiimote_t *wiimote, const char *s, va_list ap) {
186 if (wiimote) printf("%d:", cwiid_get_id(wiimote));
187 else printf("-1:");
188 vprintf(s, ap);
189 printf("\n");
192 void cwiid_callback(cwiid_wiimote_t *wiimote, int mesg_count,
193 union cwiid_mesg mesg[], struct timespec *timestamp) {
194 int i, j;
195 int valid_source;
197 for (i=0; i < mesg_count; i++) {
198 SDL_Event event;
199 SDL_UserEvent userevent;
200 userevent.type = SDL_USEREVENT;
201 event.type = SDL_USEREVENT;
202 userevent.code = WIIMOTEEVENT;
203 cwiid_mesg_type *type = (cwiid_mesg_type*)malloc(sizeof(cwiid_mesg_type));
204 *type = mesg[i].type;
205 userevent.data1 = (void*)type;
206 union cwiid_mesg *m = (union cwiid_mesg *)malloc(sizeof(union cwiid_mesg));
207 *m = mesg[i];
208 userevent.data2 = m;
209 event.user = userevent;
210 SDL_PushEvent(&event);
214 void set_led_state(cwiid_wiimote_t *wiimote, unsigned char led_state) {
215 if (cwiid_set_led(wiimote, led_state)) {
216 fprintf(stderr, "Error setting LEDs \n");
220 void set_rpt_mode(cwiid_wiimote_t *wiimote, unsigned char rpt_mode) {
221 if (cwiid_set_rpt_mode(wiimote, rpt_mode)) {
222 fprintf(stderr, "Error setting report mode\n");
226 bool ControleWii::ControleWii::initializeWiimote() {
227 mesg = 0;
228 led_state = 0;
229 rpt_mode = 0;
230 rumble = 0;
232 cwiid_set_err(err);
234 bdaddr = *BDADDR_ANY;
236 /* Connect to the wiimote */
237 printf("Put Wiimote in discoverable mode now (press 1+2)...\n");
238 if (!(wiimote = cwiid_open(&bdaddr, 0))) {
239 fprintf(stderr, "Unable to connect to wiimote\n");
240 return false;
242 if (cwiid_set_mesg_callback(wiimote, cwiid_callback)) {
243 fprintf(stderr, "Unable to set message callback\n");
244 return false;
247 toggle_bit(rpt_mode, CWIID_RPT_BTN); //abilita status report de botoes
249 toggle_bit(rpt_mode, CWIID_RPT_NUNCHUK); //abilita status report de nunchuck
251 toggle_bit(led_state, CWIID_LED4_ON); //liga quarto LED para indentificar ligaƧao com o NOSSO jogo e nao o wii
252 set_led_state(wiimote, led_state);
254 toggle_bit(rpt_mode, CWIID_RPT_IR); //abilita recebimento do infravermelho
255 set_rpt_mode(wiimote, rpt_mode);
257 if (cwiid_enable(wiimote, CWIID_FLAG_MESG_IFC)) {
258 fprintf(stderr, "Error enabling messages\n");
259 return false;
261 connected = true;
262 return true;