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 jogador
.addSpeed(closerToZero(-jogador
.getSpeedX(),sign(jogador
.getSpeedX())*-0.5),0);
65 if (jogador
.dead
) return;
66 std::list
<WeaponItem
*>::iterator it
;
70 switch (e
.user
.code
) {
72 cwiid_mesg_type
* type
= (cwiid_mesg_type
*)e
.user
.data1
;
73 union cwiid_mesg
*mesg
= (union cwiid_mesg
*)e
.user
.data2
;
79 for (int j
= 0; j
< CWIID_IR_SRC_COUNT
; j
++) {
80 if (cyclesSinceSeen
[j
] != -1)
82 if (mesg
->ir_mesg
.src
[j
].valid
) {
83 cyclesSinceSeen
[j
] = 0;
84 lastSeenAt
[j
].x
= mesg
->ir_mesg
.src
[j
].pos
[CWIID_X
];
85 lastSeenAt
[j
].y
= mesg
->ir_mesg
.src
[j
].pos
[CWIID_Y
];
88 for (int i
= 0; i
< CWIID_IR_SRC_COUNT
; i
++)
89 if (cyclesSinceSeen
[i
] != -1) {
97 x
= jogador
.game
->config
->integer
["width"] - (x
/ 1024.0) * jogador
.game
->config
->integer
["width"];
98 y
= (y
/ 738.0) * jogador
.game
->config
->integer
["height"];
99 jogador
.setAim(x
+game
->camera
.x
,y
+game
->camera
.y
);
103 case CWIID_MESG_BTN
: {
104 buttonsWii
= mesg
->btn_mesg
.buttons
;
105 if (mesg
->btn_mesg
.buttons
& 4) { //R atira
110 case CWIID_MESG_NUNCHUK
: {
111 int newX
= mesg
->nunchuk_mesg
.stick
[CWIID_X
] - 120;
112 int newY
= mesg
->nunchuk_mesg
.stick
[CWIID_Y
] - 131;
114 if (stickX
!= 0xffff) {
115 jogador
.addSpeed(8.0*newX
/128.0,0);
119 buttonsNunchuck
= mesg
->nunchuk_mesg
.buttons
;
120 if (jogador
.onGround
&&
121 ((mesg
->nunchuk_mesg
.buttons
& 2) ||
122 newY
> 50)) { //pulo com botao C ou para cima
123 jogador
.addSpeed( 0, -8);
124 jogador
.onGround
= false;
127 if (newY
< -60) {// || (mesg->nunchuk_mesg.buttons & 1)) {//descer com botao Z ou para baixo
128 jogador
.bypass
= true;
139 case SDL_MOUSEMOTION
: {
141 jogador
.setAim(e
.motion
.x
+game
->camera
.x
,e
.motion
.y
+game
->camera
.y
);
144 case SDL_MOUSEBUTTONDOWN
:
148 switch (e
.key
.keysym
.sym
) {
153 switch (e
.key
.keysym
.sym
) {
155 jogador
.onGround
= false;
156 jogador
.bypass
= true;
159 if (jogador
.onGround
) {
160 jogador
.addSpeed( 0, -8);
161 jogador
.onGround
= false;
165 for (it
= map
->items
.begin(); it
!= map
->items
.end(); ++it
)
166 if (game
->collisionManager
->checkCollision(&jogador
, (Thing
*) *it
)) {
167 jogador
.equip((*it
)->getWeapon());
172 map
->items
.erase(it
);
180 #define toggle_bit(bf,b) \
186 void err(cwiid_wiimote_t
*wiimote
, const char *s
, va_list ap
) {
187 if (wiimote
) printf("%d:", cwiid_get_id(wiimote
));
193 void cwiid_callback(cwiid_wiimote_t
*wiimote
, int mesg_count
,
194 union cwiid_mesg mesg
[], struct timespec
*timestamp
) {
198 for (i
=0; i
< mesg_count
; i
++) {
200 SDL_UserEvent userevent
;
201 userevent
.type
= SDL_USEREVENT
;
202 event
.type
= SDL_USEREVENT
;
203 userevent
.code
= WIIMOTEEVENT
;
204 cwiid_mesg_type
*type
= (cwiid_mesg_type
*)malloc(sizeof(cwiid_mesg_type
));
205 *type
= mesg
[i
].type
;
206 userevent
.data1
= (void*)type
;
207 union cwiid_mesg
*m
= (union cwiid_mesg
*)malloc(sizeof(union cwiid_mesg
));
210 event
.user
= userevent
;
211 SDL_PushEvent(&event
);
215 void set_led_state(cwiid_wiimote_t
*wiimote
, unsigned char led_state
) {
216 if (cwiid_set_led(wiimote
, led_state
)) {
217 fprintf(stderr
, "Error setting LEDs \n");
221 void set_rpt_mode(cwiid_wiimote_t
*wiimote
, unsigned char rpt_mode
) {
222 if (cwiid_set_rpt_mode(wiimote
, rpt_mode
)) {
223 fprintf(stderr
, "Error setting report mode\n");
227 bool ControleWii::ControleWii::initializeWiimote() {
235 bdaddr
= *BDADDR_ANY
;
237 /* Connect to the wiimote */
238 printf("Put Wiimote in discoverable mode now (press 1+2)...\n");
239 if (!(wiimote
= cwiid_open(&bdaddr
, 0))) {
240 fprintf(stderr
, "Unable to connect to wiimote\n");
243 if (cwiid_set_mesg_callback(wiimote
, cwiid_callback
)) {
244 fprintf(stderr
, "Unable to set message callback\n");
248 toggle_bit(rpt_mode
, CWIID_RPT_BTN
); //abilita status report de botoes
250 toggle_bit(rpt_mode
, CWIID_RPT_NUNCHUK
); //abilita status report de nunchuck
252 toggle_bit(led_state
, CWIID_LED4_ON
); //liga quarto LED para indentificar ligaƧao com o NOSSO jogo e nao o wii
253 set_led_state(wiimote
, led_state
);
255 toggle_bit(rpt_mode
, CWIID_RPT_IR
); //abilita recebimento do infravermelho
256 set_rpt_mode(wiimote
, rpt_mode
);
258 if (cwiid_enable(wiimote
, CWIID_FLAG_MESG_IFC
)) {
259 fprintf(stderr
, "Error enabling messages\n");