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 Uint8
*keystates
= SDL_GetKeyState( NULL
);
20 if (keystates
[SDLK_DOWN
]) {
21 jogador
.bypass
= true;
23 if (jogador
.onGround
&& keystates
[SDLK_RCTRL
]) {
26 if (keystates
[SDLK_LEFT
]) {
27 jogador
.addSpeed(-3, 0);
29 if (keystates
[SDLK_RIGHT
]) {
30 jogador
.addSpeed(3, 0);
32 if (keystates
[SDLK_RIGHT
]) {
33 jogador
.addSpeed(3, 0);
35 if (!keystates
[SDLK_RIGHT
] && !keystates
[SDLK_LEFT
])
36 jogador
.addSpeed(closerToZero(-jogador
.getSpeedX(),sign(jogador
.getSpeedX())*-0.5),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 std::list
<WeaponItem
*>::iterator it
;
60 switch (e
.user
.code
) {
62 cwiid_mesg_type
* type
= (cwiid_mesg_type
*)e
.user
.data1
;
63 union cwiid_mesg
*mesg
= (union cwiid_mesg
*)e
.user
.data2
;
69 for (int j
= 0; j
< CWIID_IR_SRC_COUNT
; j
++) {
70 if (cyclesSinceSeen
[j
] != -1)
72 if (mesg
->ir_mesg
.src
[j
].valid
) {
73 cyclesSinceSeen
[j
] = 0;
74 lastSeenAt
[j
].x
= mesg
->ir_mesg
.src
[j
].pos
[CWIID_X
];
75 lastSeenAt
[j
].y
= mesg
->ir_mesg
.src
[j
].pos
[CWIID_Y
];
78 for (int i
= 0; i
< CWIID_IR_SRC_COUNT
; i
++)
79 if (cyclesSinceSeen
[i
] != -1) {
87 x
= jogador
.game
->config
->screen
["width"] - (x
/ 1024.0) * jogador
.game
->config
->screen
["width"];
88 y
= (y
/ 738.0) * jogador
.game
->config
->screen
["height"];
89 jogador
.setAim(x
+game
->camera
.x
,y
+game
->camera
.y
);
93 case CWIID_MESG_BTN
: {
94 buttonsWii
= mesg
->btn_mesg
.buttons
;
95 if (mesg
->btn_mesg
.buttons
& 4) { //R atira
100 case CWIID_MESG_NUNCHUK
: {
101 int newX
= mesg
->nunchuk_mesg
.stick
[CWIID_X
] - 120;
102 int newY
= mesg
->nunchuk_mesg
.stick
[CWIID_Y
] - 131;
104 if (stickX
!= 0xffff) {
105 jogador
.setSpeed(4.0*newX
/128,jogador
.getSpeedY());
109 buttonsNunchuck
= mesg
->nunchuk_mesg
.buttons
;
111 if (jogador
.onGround
&&
112 ((mesg
->nunchuk_mesg
.buttons
& 2) ||
113 newY
> 60)) { //pulo com botao C ou para cima
114 jogador
.addSpeed( 0, -8);
115 jogador
.onGround
= false;
118 if (newY
< -60) {// || (mesg->nunchuk_mesg.buttons & 1)) {//descer com botao Z ou para baixo
119 jogador
.bypass
= true;
130 case SDL_MOUSEMOTION
: {
132 jogador
.setAim(e
.motion
.x
+game
->camera
.x
,e
.motion
.y
+game
->camera
.y
);
135 case SDL_MOUSEBUTTONDOWN
:
139 switch (e
.key
.keysym
.sym
) {
144 switch (e
.key
.keysym
.sym
) {
146 jogador
.onGround
= false;
147 jogador
.bypass
= true;
150 if (jogador
.onGround
) {
151 jogador
.addSpeed( 0, -8);
152 jogador
.onGround
= false;
156 for (it
= map
->items
.begin(); it
!= map
->items
.end(); ++it
)
157 if (game
->collisionManager
->checkCollision(&jogador
, (Thing
*) *it
)) {
158 jogador
.equip((*it
)->getWeapon());
163 map
->items
.erase(it
);
171 #define toggle_bit(bf,b) \
177 void err(cwiid_wiimote_t
*wiimote
, const char *s
, va_list ap
) {
178 if (wiimote
) printf("%d:", cwiid_get_id(wiimote
));
184 void cwiid_callback(cwiid_wiimote_t
*wiimote
, int mesg_count
,
185 union cwiid_mesg mesg
[], struct timespec
*timestamp
) {
189 for (i
=0; i
< mesg_count
; i
++) {
191 SDL_UserEvent userevent
;
192 userevent
.type
= SDL_USEREVENT
;
193 event
.type
= SDL_USEREVENT
;
194 userevent
.code
= WIIMOTEEVENT
;
195 cwiid_mesg_type
*type
= (cwiid_mesg_type
*)malloc(sizeof(cwiid_mesg_type
));
196 *type
= mesg
[i
].type
;
197 userevent
.data1
= (void*)type
;
198 union cwiid_mesg
*m
= (union cwiid_mesg
*)malloc(sizeof(union cwiid_mesg
));
201 event
.user
= userevent
;
202 SDL_PushEvent(&event
);
206 void set_led_state(cwiid_wiimote_t
*wiimote
, unsigned char led_state
) {
207 if (cwiid_set_led(wiimote
, led_state
)) {
208 fprintf(stderr
, "Error setting LEDs \n");
212 void set_rpt_mode(cwiid_wiimote_t
*wiimote
, unsigned char rpt_mode
) {
213 if (cwiid_set_rpt_mode(wiimote
, rpt_mode
)) {
214 fprintf(stderr
, "Error setting report mode\n");
218 bool ControleWii::ControleWii::initializeWiimote() {
226 bdaddr
= *BDADDR_ANY
;
228 /* Connect to the wiimote */
229 printf("Put Wiimote in discoverable mode now (press 1+2)...\n");
230 if (!(wiimote
= cwiid_open(&bdaddr
, 0))) {
231 fprintf(stderr
, "Unable to connect to wiimote\n");
234 if (cwiid_set_mesg_callback(wiimote
, cwiid_callback
)) {
235 fprintf(stderr
, "Unable to set message callback\n");
239 toggle_bit(rpt_mode
, CWIID_RPT_BTN
); //abilita status report de botoes
241 toggle_bit(rpt_mode
, CWIID_RPT_NUNCHUK
); //abilita status report de nunchuck
243 toggle_bit(led_state
, CWIID_LED4_ON
); //liga quarto LED para indentificar ligaƧao com o NOSSO jogo e nao o wii
244 set_led_state(wiimote
, led_state
);
246 toggle_bit(rpt_mode
, CWIID_RPT_IR
); //abilita recebimento do infravermelho
247 set_rpt_mode(wiimote
, rpt_mode
);
249 if (cwiid_enable(wiimote
, CWIID_FLAG_MESG_IFC
)) {
250 fprintf(stderr
, "Error enabling messages\n");