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
.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
]) {
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
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
45 if (buttonsWii
& 4) { //R atira
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());
63 void ControleWii::handleEvent(SDL_Event
&e
) {
64 if (jogador
.dead
) return;
65 std::list
<WeaponItem
*>::iterator it
;
69 switch (e
.user
.code
) {
71 cwiid_mesg_type
* type
= (cwiid_mesg_type
*)e
.user
.data1
;
72 union cwiid_mesg
*mesg
= (union cwiid_mesg
*)e
.user
.data2
;
78 for (int j
= 0; j
< CWIID_IR_SRC_COUNT
; j
++) {
79 if (cyclesSinceSeen
[j
] != -1)
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) {
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
);
102 case CWIID_MESG_BTN
: {
103 buttonsWii
= mesg
->btn_mesg
.buttons
;
104 if (mesg
->btn_mesg
.buttons
& 4) { //R atira
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);
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;
138 case SDL_MOUSEMOTION
: {
140 jogador
.setAim(e
.motion
.x
+game
->camera
.x
,e
.motion
.y
+game
->camera
.y
);
143 case SDL_MOUSEBUTTONDOWN
:
147 switch (e
.key
.keysym
.sym
) {
152 switch (e
.key
.keysym
.sym
) {
154 jogador
.onGround
= false;
155 jogador
.bypass
= true;
158 if (jogador
.onGround
) {
159 jogador
.addSpeed( 0, -8);
160 jogador
.onGround
= false;
164 for (it
= map
->items
.begin(); it
!= map
->items
.end(); ++it
)
165 if (game
->collisionManager
->checkCollision(&jogador
, (Thing
*) *it
)) {
166 jogador
.equip((*it
)->getWeapon());
171 map
->items
.erase(it
);
179 #define toggle_bit(bf,b) \
185 void err(cwiid_wiimote_t
*wiimote
, const char *s
, va_list ap
) {
186 if (wiimote
) printf("%d:", cwiid_get_id(wiimote
));
192 void cwiid_callback(cwiid_wiimote_t
*wiimote
, int mesg_count
,
193 union cwiid_mesg mesg
[], struct timespec
*timestamp
) {
197 for (i
=0; i
< mesg_count
; i
++) {
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
));
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() {
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");
242 if (cwiid_set_mesg_callback(wiimote
, cwiid_callback
)) {
243 fprintf(stderr
, "Unable to set message callback\n");
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");