diff options
| author | Dusan Durech <dusan.d@Centrum.sk> | 2009-06-24 17:05:12 +0200 |
|---|---|---|
| committer | Dusan Durech <dusan.d@Centrum.sk> | 2009-06-24 17:05:12 +0200 |
| commit | 0f02f6efd455329fdcdd4737801b3c1e6cc536d3 (patch) | |
| tree | d856c1ce299e734cb455c2246908318a39ee4875 | |
| parent | ba961b3df79e19ab9313c43475aa30ddac31c112 (diff) | |
better code for control tux
| -rw-r--r-- | src/client/control.c | 96 | ||||
| -rw-r--r-- | src/client/control.h | 28 | ||||
| -rw-r--r-- | src/screen/world.c | 269 |
3 files changed, 197 insertions, 196 deletions
diff --git a/src/client/control.c b/src/client/control.c new file mode 100644 index 0000000..5d99c66 --- /dev/null +++ b/src/client/control.c @@ -0,0 +1,96 @@ + +#include <SDL.h> +#include <limits.h> +#include <assert.h> + +#include "main.h" +#include "control.h" + +control_t* control_new(SDLKey arg_up, SDLKey arg_right, SDLKey arg_left, SDLKey arg_down, + SDLKey arg_shot, SDLKey arg_switch) +{ + control_t *tmp; + + tmp = malloc(sizeof(control_t)); + memset(tmp, 0, sizeof(control_t)); + tmp->key[CONTROL_UP] = arg_up; + tmp->key[CONTROL_RIGHT] = arg_right; + tmp->key[CONTROL_LEFT] = arg_left; + tmp->key[CONTROL_DOWN] = arg_down; + tmp->key[CONTROL_SHOT] = arg_shot; + tmp->key[CONTROL_SWITCH] = arg_switch; + + return tmp; +} + +int control_get_key_route(control_t *my_control) +{ + Uint8 *mapa; + SDLKey ret_key; + int z; + int i; + + assert(my_control != NULL); + + ret_key = CONTROL_NONE; + + mapa = SDL_GetKeyState(NULL); + + for (i = 0; i<CONTROL_KEY_COUNT_ROUTE; i++) + { + if (mapa[my_control->key[i]] == SDL_PRESSED) + { + my_control->count[i]++; + } + else + { + my_control->count[i] = 0; + } + } + + z = INT_MAX; + + for (i = 0; i<CONTROL_KEY_COUNT_ROUTE; i++) + { + if ( my_control->count[i] > 0 && + my_control->count[i] < z && + mapa[my_control->key[i]] == SDL_PRESSED ) + { + z = my_control->count[i]; + ret_key = i; + } + } + + return ret_key; +} + +int control_get_key_action(control_t *my_control) +{ + Uint8 *mapa; + SDLKey ret_key; + + assert(my_control != NULL); + + ret_key = CONTROL_NONE; + + mapa = SDL_GetKeyState(NULL); + + if (mapa[my_control->key[CONTROL_SHOT]] == SDL_PRESSED) + { + return CONTROL_SHOT; + } + + if (mapa[my_control->key[CONTROL_SWITCH]] == SDL_PRESSED) + { + return CONTROL_SWITCH; + } + + return CONTROL_NONE; +} + +void control_destroy(control_t *my_control) +{ + assert(my_control != NULL); + + free(my_control); +} diff --git a/src/client/control.h b/src/client/control.h new file mode 100644 index 0000000..f36c9b5 --- /dev/null +++ b/src/client/control.h @@ -0,0 +1,28 @@ +#ifndef CONTROL_H + +#define CONTROL_H + +#define CONTROL_NONE -1 +#define CONTROL_UP 0 +#define CONTROL_RIGHT 1 +#define CONTROL_LEFT 2 +#define CONTROL_DOWN 3 +#define CONTROL_SHOT 4 +#define CONTROL_SWITCH 5 + +#define CONTROL_KEY_COUNT_ROUTE 4 +#define CONTROL_KEY_COUNT 6 + +typedef struct control_struct +{ + SDLKey key[CONTROL_KEY_COUNT]; + int count[CONTROL_KEY_COUNT]; +} control_t; + +extern control_t* control_new(SDLKey arg_up, SDLKey arg_right, SDLKey arg_left, SDLKey arg_down, + SDLKey arg_shot, SDLKey arg_switch); +extern int control_get_key_route(control_t *my_control); +extern int control_get_key_action(control_t *my_control); +extern void control_destroy(control_t *my_control); + +#endif diff --git a/src/screen/world.c b/src/screen/world.c index c3ccd8c..f156ae4 100644 --- a/src/screen/world.c +++ b/src/screen/world.c @@ -43,6 +43,7 @@ #include "settingKeys.h" #include "choiceArena.h" #include "table.h" +#include "control.h" static arena_t *arena; @@ -52,6 +53,9 @@ static bool_t isEndWorld; static tux_t *tuxWithControlRightKeyboard; static tux_t *tuxWithControlLeftKeyboard; +static control_t *control_tux_right; +static control_t *control_tux_left; + bool_t world_is_inicialized() { return isScreenWorldInit; @@ -130,6 +134,9 @@ void prepareArena() tuxWithControlRightKeyboard = NULL; tuxWithControlLeftKeyboard = NULL; + control_tux_right = NULL; + control_tux_left = NULL; + if (game_type_load_session() != NULL) { load_arena(game_type_load_session()); return; @@ -142,6 +149,14 @@ void prepareArena() public_server_get_settingCountRound(&arena->max_countRound); tux = tux_new(); + + control_tux_right = control_new(key_table_get_key(KEY_TUX_RIGHT_MOVE_UP), + key_table_get_key(KEY_TUX_RIGHT_MOVE_RIGHT), + key_table_get_key(KEY_TUX_RIGHT_MOVE_LEFT), + key_table_get_key(KEY_TUX_RIGHT_MOVE_DOWN), + key_table_get_key(KEY_TUX_RIGHT_SWITCH_WEAPON), + key_table_get_key(KEY_TUX_RIGHT_SHOOT)); + tux->control = TUX_CONTROL_KEYBOARD_RIGHT; tuxWithControlRightKeyboard = tux; public_server_get_setting_name_right(name); @@ -149,6 +164,14 @@ void prepareArena() space_add(arena->spaceTux, tux); tux = tux_new(); + + control_tux_left = control_new(key_table_get_key(KEY_TUX_LEFT_MOVE_UP), + key_table_get_key(KEY_TUX_LEFT_MOVE_RIGHT), + key_table_get_key(KEY_TUX_LEFT_MOVE_LEFT), + key_table_get_key(KEY_TUX_LEFT_MOVE_DOWN), + key_table_get_key(KEY_TUX_LEFT_SWITCH_WEAPON), + key_table_get_key(KEY_TUX_LEFT_SHOOT)); + tux->control = TUX_CONTROL_KEYBOARD_LEFT; tuxWithControlLeftKeyboard = tux; public_server_get_settingNameLeft(name); @@ -172,6 +195,14 @@ void prepareArena() public_server_get_settingCountRound(&arena->max_countRound); tux = tux_new(); + + control_tux_right = control_new(key_table_get_key(KEY_TUX_RIGHT_MOVE_UP), + key_table_get_key(KEY_TUX_RIGHT_MOVE_RIGHT), + key_table_get_key(KEY_TUX_RIGHT_MOVE_LEFT), + key_table_get_key(KEY_TUX_RIGHT_MOVE_DOWN), + key_table_get_key(KEY_TUX_RIGHT_SWITCH_WEAPON), + key_table_get_key(KEY_TUX_RIGHT_SHOOT)); + tux->control = TUX_CONTROL_KEYBOARD_RIGHT; tuxWithControlRightKeyboard = tux; public_server_get_setting_name_right(name); @@ -212,200 +243,38 @@ static void netAction(tux_t *tux, int action) } } -static void control_keyboard_right(tux_t *tux) +static void control_keyboard(tux_t *tux, control_t *control) { - static int lastKey = 0; - int countKey; - Uint8 *mapa; - mapa = SDL_GetKeyState(NULL); - - assert(tux != NULL); - assert(mapa != NULL); - - countKey = 0; - - if (mapa[(SDLKey) key_table_get_key(KEY_TUX_RIGHT_MOVE_UP)] == SDL_PRESSED) { - countKey++; - } - - if (mapa[(SDLKey) key_table_get_key(KEY_TUX_RIGHT_MOVE_RIGHT)] == SDL_PRESSED) { - countKey++; -} - - if (mapa[(SDLKey) key_table_get_key(KEY_TUX_RIGHT_MOVE_LEFT)] == SDL_PRESSED) { - countKey++; - } - - if (mapa[(SDLKey) key_table_get_key(KEY_TUX_RIGHT_MOVE_DOWN)] == SDL_PRESSED) { - countKey++; - } - - if (mapa[(SDLKey) key_table_get_key(KEY_TUX_RIGHT_SWITCH_WEAPON)] == SDL_PRESSED && - tux->isCanSwitchGun == TRUE) { - netAction(tux, TUX_SWITCH_GUN); - tux_action(tux, TUX_SWITCH_GUN); - } - - if (mapa[(SDLKey) key_table_get_key(KEY_TUX_RIGHT_SHOOT)] == SDL_PRESSED && - tux->isCanShot == TRUE) { - netAction(tux, TUX_SHOT); - tux_action(tux, TUX_SHOT); - } - - if (mapa[(SDLKey) key_table_get_key(KEY_TUX_RIGHT_MOVE_UP)] == SDL_PRESSED) { - if (countKey > 1 && lastKey == key_table_get_key(KEY_TUX_RIGHT_MOVE_UP)) { - goto tuUp; - } - - if (countKey == 1) { - lastKey = key_table_get_key(KEY_TUX_RIGHT_MOVE_UP); - } - - netAction(tux, TUX_UP); - tux_action(tux, TUX_UP); - - tuUp:; - } - - if (mapa[(SDLKey) key_table_get_key(KEY_TUX_RIGHT_MOVE_RIGHT)] == SDL_PRESSED) { - if (countKey > 1 && lastKey == key_table_get_key(KEY_TUX_RIGHT_MOVE_RIGHT)) { - goto tuRight; - } - - if (countKey == 1) { - lastKey = key_table_get_key(KEY_TUX_RIGHT_MOVE_RIGHT); - } - - netAction(tux, TUX_RIGHT); - tux_action(tux, TUX_RIGHT); - - tuRight:; - } - - if (mapa[(SDLKey) key_table_get_key(KEY_TUX_RIGHT_MOVE_LEFT)] == SDL_PRESSED) { - if (countKey > 1 && lastKey == key_table_get_key(KEY_TUX_RIGHT_MOVE_LEFT)) { - goto tuLeft; - } - - if (countKey == 1) { - lastKey = key_table_get_key(KEY_TUX_RIGHT_MOVE_LEFT); - } - - netAction(tux, TUX_LEFT); - tux_action(tux, TUX_LEFT); - - tuLeft:; - } - - if (mapa[(SDLKey) key_table_get_key(KEY_TUX_RIGHT_MOVE_DOWN)] == SDL_PRESSED) { - if (countKey > 1 && lastKey == key_table_get_key(KEY_TUX_RIGHT_MOVE_DOWN)) { - goto tuDown; - } - - if (countKey == 1) { - lastKey = key_table_get_key(KEY_TUX_RIGHT_MOVE_DOWN); - } - - netAction(tux, TUX_DOWN); - tux_action(tux, TUX_DOWN); - - tuDown:; - } -} - -static void control_keyboard_left(tux_t *tux) -{ - static int lastKey = 0; - int countKey; - Uint8 *mapa; - mapa = SDL_GetKeyState(NULL); - - assert(tux != NULL); - assert(mapa != NULL); - - countKey = 0; - - if (mapa[(SDLKey) key_table_get_key(KEY_TUX_LEFT_MOVE_UP)] == SDL_PRESSED) { - countKey++; - } - - if (mapa[(SDLKey) key_table_get_key(KEY_TUX_LEFT_MOVE_RIGHT)] == SDL_PRESSED) { - countKey++; - } - - if (mapa[(SDLKey) key_table_get_key(KEY_TUX_LEFT_MOVE_LEFT)] == SDL_PRESSED) { - countKey++; - } - - if (mapa[(SDLKey) key_table_get_key(KEY_TUX_LEFT_MOVE_DOWN)] == SDL_PRESSED) { - countKey++; - } - - if (mapa[(SDLKey) key_table_get_key(KEY_TUX_LEFT_SWITCH_WEAPON)] == SDL_PRESSED) { - if (tux->isCanSwitchGun == TRUE) { + switch (control_get_key_route(control)) + { + case CONTROL_UP : + netAction(tux, TUX_UP); + tux_action(tux, TUX_UP); + break; + case CONTROL_RIGHT : + netAction(tux, TUX_RIGHT); + tux_action(tux, TUX_RIGHT); + break; + case CONTROL_LEFT : + netAction(tux, TUX_LEFT); + tux_action(tux, TUX_LEFT); + break; + case CONTROL_DOWN : + netAction(tux, TUX_DOWN); + tux_action(tux, TUX_DOWN); + break; + } + + switch (control_get_key_action(control)) + { + case CONTROL_SHOT : + netAction(tux, TUX_SHOT); + tux_action(tux, TUX_SHOT); + break; + case CONTROL_SWITCH : + netAction(tux, TUX_SWITCH_GUN); tux_action(tux, TUX_SWITCH_GUN); - } - } - - if (mapa[(SDLKey) key_table_get_key(KEY_TUX_LEFT_SHOOT)] == SDL_PRESSED && - tux->isCanShot == TRUE) { - tux_action(tux, TUX_SHOT); - } - - if (mapa[(SDLKey) key_table_get_key(KEY_TUX_LEFT_MOVE_UP)] == SDL_PRESSED) { - if (countKey > 1 && lastKey == key_table_get_key(KEY_TUX_LEFT_MOVE_UP)) { - goto tuUp; - } - - if (countKey == 1) { - lastKey = key_table_get_key(KEY_TUX_LEFT_MOVE_UP); - } - - tux_action(tux, TUX_UP); - - tuUp:; - } - - if (mapa[(SDLKey) key_table_get_key(KEY_TUX_LEFT_MOVE_RIGHT)] == SDL_PRESSED) { - if (countKey > 1 && lastKey == key_table_get_key(KEY_TUX_LEFT_MOVE_RIGHT)) { - goto tuRight; - } - - if (countKey == 1) { - lastKey = key_table_get_key(KEY_TUX_LEFT_MOVE_RIGHT); - } - - tux_action(tux, TUX_RIGHT); - - tuRight:; - } - - if (mapa[(SDLKey) key_table_get_key(KEY_TUX_LEFT_MOVE_LEFT)] == SDL_PRESSED) { - if (countKey > 1 && lastKey == key_table_get_key(KEY_TUX_LEFT_MOVE_LEFT)) { - goto tuLeft; - } - - if (countKey == 1) { - lastKey = key_table_get_key(KEY_TUX_LEFT_MOVE_LEFT); - } - - tux_action(tux, TUX_LEFT); - - tuLeft:; - } - - if (mapa[(SDLKey) key_table_get_key(KEY_TUX_LEFT_MOVE_DOWN)] == SDL_PRESSED) { - if (countKey > 1 && lastKey == key_table_get_key(KEY_TUX_LEFT_MOVE_DOWN)) { - goto tuDown; - } - - if (countKey == 1) { - lastKey = key_table_get_key(KEY_TUX_LEFT_MOVE_DOWN); - } - - tux_action(tux, TUX_DOWN); - - tuDown:; + break; } } @@ -456,15 +325,15 @@ void world_tux_control(tux_t *p) assert(!_("[Error] Controls not defined")); break; - case TUX_CONTROL_KEYBOARD_LEFT: + case TUX_CONTROL_KEYBOARD_RIGHT: if (chat_is_active() == FALSE) { - control_keyboard_left(p); + control_keyboard(tuxWithControlRightKeyboard, control_tux_right); } break; - case TUX_CONTROL_KEYBOARD_RIGHT: + case TUX_CONTROL_KEYBOARD_LEFT: if (chat_is_active() == FALSE) { - control_keyboard_right(p); + control_keyboard(tuxWithControlLeftKeyboard, control_tux_left); } break; } @@ -587,6 +456,14 @@ void stoptWorld() term_quit(); save_dialog_quit(); + if (control_tux_right != NULL) { + control_destroy(control_tux_right); + } + + if (control_tux_left != NULL) { + control_destroy(control_tux_left); + } + #ifndef NO_SOUND music_stop(); #endif |