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 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
]) {
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
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
50 if (buttonsWii
& 4) { //R atira
56 void ControleWii::handleEvent(SDL_Event
&e
) {
57 if (jogador
.dead
) return;
58 std::list
<WeaponItem
*>::iterator it
;
62 switch (e
.user
.code
) {
64 cwiid_mesg_type
* type
= (cwiid_mesg_type
*)e
.user
.data1
;
65 union cwiid_mesg
*mesg
= (union cwiid_mesg
*)e
.user
.data2
;
71 for (int j
= 0; j
< CWIID_IR_SRC_COUNT
; j
++) {
72 if (cyclesSinceSeen
[j
] != -1)
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) {
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
);
95 case CWIID_MESG_BTN
: {
96 buttonsWii
= mesg
->btn_mesg
.buttons
;
97 if (mesg
->btn_mesg
.buttons
& 4) { //R atira
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());
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;
132 case SDL_MOUSEMOTION
: {
134 jogador
.setAim(e
.motion
.x
+game
->camera
.x
,e
.motion
.y
+game
->camera
.y
);
137 case SDL_MOUSEBUTTONDOWN
:
141 switch (e
.key
.keysym
.sym
) {
146 switch (e
.key
.keysym
.sym
) {
148 jogador
.onGround
= false;
149 jogador
.bypass
= true;
152 if (jogador
.onGround
) {
153 jogador
.addSpeed( 0, -8);
154 jogador
.onGround
= false;
158 for (it
= map
->items
.begin(); it
!= map
->items
.end(); ++it
)
159 if (game
->collisionManager
->checkCollision(&jogador
, (Thing
*) *it
)) {
160 jogador
.equip((*it
)->getWeapon());
165 map
->items
.erase(it
);
173 #define toggle_bit(bf,b) \
179 void err(cwiid_wiimote_t
*wiimote
, const char *s
, va_list ap
) {
180 if (wiimote
) printf("%d:", cwiid_get_id(wiimote
));
186 void cwiid_callback(cwiid_wiimote_t
*wiimote
, int mesg_count
,
187 union cwiid_mesg mesg
[], struct timespec
*timestamp
) {
191 for (i
=0; i
< mesg_count
; i
++) {
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
));
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() {
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");
236 if (cwiid_set_mesg_callback(wiimote
, cwiid_callback
)) {
237 fprintf(stderr
, "Unable to set message callback\n");
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");