1 #include "controlewii.h"
2 #include "usereventtype.h"
3 #include "weaponitem.h"
6 ControleWii::ControleWii(Player
&p
) : Controle(p
) {
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
]) {
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
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
49 if (buttonsWii
& 4) { //R atira
55 void ControleWii::handleEvent(SDL_Event
&e
) {
56 if (jogador
.dead
) return;
57 std::list
<WeaponItem
*>::iterator it
;
61 switch (e
.user
.code
) {
63 cwiid_mesg_type
* type
= (cwiid_mesg_type
*)e
.user
.data1
;
64 union cwiid_mesg
*mesg
= (union cwiid_mesg
*)e
.user
.data2
;
70 for (int j
= 0; j
< CWIID_IR_SRC_COUNT
; j
++) {
71 if (cyclesSinceSeen
[j
] != -1)
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) {
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
);
94 case CWIID_MESG_BTN
: {
95 buttonsWii
= mesg
->btn_mesg
.buttons
;
96 if (mesg
->btn_mesg
.buttons
& 4) { //R atira
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());
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;
131 case SDL_MOUSEMOTION
: {
133 jogador
.setAim(e
.motion
.x
+game
->camera
.x
,e
.motion
.y
+game
->camera
.y
);
136 case SDL_MOUSEBUTTONDOWN
:
140 switch (e
.key
.keysym
.sym
) {
145 switch (e
.key
.keysym
.sym
) {
147 jogador
.onGround
= false;
148 jogador
.bypass
= true;
151 if (jogador
.onGround
) {
152 jogador
.addSpeed( 0, -8);
153 jogador
.onGround
= false;
157 for (it
= map
->items
.begin(); it
!= map
->items
.end(); ++it
)
158 if (game
->collisionManager
->checkCollision(&jogador
, (Thing
*) *it
)) {
159 jogador
.equip((*it
)->getWeapon());
164 map
->items
.erase(it
);
172 #define toggle_bit(bf,b) \
178 void err(cwiid_wiimote_t
*wiimote
, const char *s
, va_list ap
) {
179 if (wiimote
) printf("%d:", cwiid_get_id(wiimote
));
185 void cwiid_callback(cwiid_wiimote_t
*wiimote
, int mesg_count
,
186 union cwiid_mesg mesg
[], struct timespec
*timestamp
) {
190 for (i
=0; i
< mesg_count
; i
++) {
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
));
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() {
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");
235 if (cwiid_set_mesg_callback(wiimote
, cwiid_callback
)) {
236 fprintf(stderr
, "Unable to set message callback\n");
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");